Class: NanoGPT::SampleConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/nano_gpt/train_config.rb

Overview

Configuration for sampling/generation

Constant Summary collapse

DEFAULTS =

Defaults match bin/sample exactly

{
  out_dir: "out-shakespeare-char",
  dataset: "shakespeare_char",
  start: "\n",
  num_samples: 5,
  max_new_tokens: 500,
  temperature: 0.8,
  top_k: 200,
  seed: 1337,
  device: "auto"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ SampleConfig

Returns a new instance of SampleConfig.



177
178
179
# File 'lib/nano_gpt/train_config.rb', line 177

def initialize(values = {})
  @values = DEFAULTS.merge(values)
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



175
176
177
# File 'lib/nano_gpt/train_config.rb', line 175

def values
  @values
end

Class Method Details

.load(args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/nano_gpt/train_config.rb', line 193

def self.load(args)
  config = new

  # First pass: find and load JSON config file
  config_file = nil
  args.each do |arg|
    if arg.start_with?("--config=")
      config_file = arg.split("=", 2).last
      break
    end
  end

  if config_file
    config.load_json(config_file)
  end

  # Second pass: apply command-line overrides
  args.each do |arg|
    next unless arg.start_with?("--") && arg.include?("=")
    next if arg.start_with?("--config=")

    key, val = arg[2..].split("=", 2)
    key = key.to_sym

    unless config.values.key?(key)
      puts "Warning: Unknown config key: #{key}"
      next
    end

    config[key] = parse_value(val, config[key])
  end

  config
end

.parse_value(val, existing) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/nano_gpt/train_config.rb', line 250

def self.parse_value(val, existing)
  case existing
  when Integer then val.to_i
  when Float then val.to_f
  when TrueClass, FalseClass then val.downcase == "true"
  else val
  end
end

Instance Method Details

#[](key) ⇒ Object



181
182
183
# File 'lib/nano_gpt/train_config.rb', line 181

def [](key)
  @values[key.to_sym]
end

#[]=(key, value) ⇒ Object



185
186
187
# File 'lib/nano_gpt/train_config.rb', line 185

def []=(key, value)
  @values[key.to_sym] = value
end

#load_json(path) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/nano_gpt/train_config.rb', line 228

def load_json(path)
  unless File.exist?(path)
    raise "Config file not found: #{path}"
  end

  json = JSON.parse(File.read(path))
  puts "Loaded config from #{path}"

  json.each do |key, val|
    key = key.to_sym
    unless @values.key?(key)
      puts "Warning: Unknown config key in JSON: #{key}"
      next
    end
    @values[key] = val
  end

  self
end

#to_hObject



189
190
191
# File 'lib/nano_gpt/train_config.rb', line 189

def to_h
  @values.dup
end