Class: NanoGPT::BaseConfig
- Inherits:
-
Object
- Object
- NanoGPT::BaseConfig
- Defined in:
- lib/nano_gpt/train_config.rb
Overview
Base configuration class with shared functionality Supports JSON config files with command-line overrides
Priority (highest to lowest):
1. Command-line arguments (--key=value)
2. JSON config file (--config=path.json)
3. Default values
Direct Known Subclasses
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
- .defaults ⇒ Object
-
.load(args) ⇒ Object
Load config from command-line args.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(values = {}) ⇒ BaseConfig
constructor
A new instance of BaseConfig.
-
#load_json(path) ⇒ Object
Load values from JSON file.
-
#save_json(path) ⇒ Object
Save current config to JSON file.
- #to_h ⇒ Object
Constructor Details
#initialize(values = {}) ⇒ BaseConfig
Returns a new instance of BaseConfig.
20 21 22 |
# File 'lib/nano_gpt/train_config.rb', line 20 def initialize(values = {}) @values = self.class.defaults.merge(values) end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
18 19 20 |
# File 'lib/nano_gpt/train_config.rb', line 18 def values @values end |
Class Method Details
.defaults ⇒ Object
14 15 16 |
# File 'lib/nano_gpt/train_config.rb', line 14 def self.defaults raise NotImplementedError, "Subclasses must define DEFAULTS" end |
.load(args) ⇒ Object
Load config from command-line args
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nano_gpt/train_config.rb', line 37 def self.load(args) config = new # First pass: find and load JSON config file args.each do |arg| if arg.start_with?("--config=") config.load_json(arg.split("=", 2).last) break end 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]) puts "Override: #{key} = #{config[key]}" end config end |
Instance Method Details
#[](key) ⇒ Object
24 25 26 |
# File 'lib/nano_gpt/train_config.rb', line 24 def [](key) @values[key.to_sym] end |
#[]=(key, value) ⇒ Object
28 29 30 |
# File 'lib/nano_gpt/train_config.rb', line 28 def []=(key, value) @values[key.to_sym] = value end |
#load_json(path) ⇒ Object
Load values from JSON file
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/nano_gpt/train_config.rb', line 69 def load_json(path) raise "Config file not found: #{path}" unless File.exist?(path) 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 |
#save_json(path) ⇒ Object
Save current config to JSON file
88 89 90 91 |
# File 'lib/nano_gpt/train_config.rb', line 88 def save_json(path) File.write(path, JSON.pretty_generate(@values)) puts "Saved config to #{path}" end |
#to_h ⇒ Object
32 33 34 |
# File 'lib/nano_gpt/train_config.rb', line 32 def to_h @values.dup end |