Class: NanoGPT::Web::MetricsStore
- Inherits:
-
Object
- Object
- NanoGPT::Web::MetricsStore
- Defined in:
- lib/nano_gpt/web/metrics_store.rb
Overview
SQLite-backed storage for training metrics Uses WAL mode for concurrent read/write access
Instance Attribute Summary collapse
-
#db_path ⇒ Object
readonly
Returns the value of attribute db_path.
Instance Method Summary collapse
- #checkpoints_for_run(run_id) ⇒ Object
- #close ⇒ Object
- #create_run(dataset:, config:, status: "running") ⇒ Object
- #get_run(run_id) ⇒ Object
-
#initialize(db_path = "nanogpt_metrics.db") ⇒ MetricsStore
constructor
A new instance of MetricsStore.
- #latest_run ⇒ Object
- #list_runs(limit: 50) ⇒ Object
- #metrics_for_run(run_id) ⇒ Object
- #record_checkpoint(run_id, path:, suffix:, iteration:, val_loss:) ⇒ Object
- #record_metrics(run_id, iteration, metrics_hash) ⇒ Object
- #update_run(run_id, **attrs) ⇒ Object
Constructor Details
#initialize(db_path = "nanogpt_metrics.db") ⇒ MetricsStore
Returns a new instance of MetricsStore.
13 14 15 16 17 18 19 20 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 13 def initialize(db_path = "nanogpt_metrics.db") @db_path = db_path @db = SQLite3::Database.new(db_path) @db.results_as_hash = true @db.execute("PRAGMA journal_mode=WAL") @db.execute("PRAGMA synchronous=NORMAL") create_tables end |
Instance Attribute Details
#db_path ⇒ Object (readonly)
Returns the value of attribute db_path.
11 12 13 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 11 def db_path @db_path end |
Instance Method Details
#checkpoints_for_run(run_id) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 77 def checkpoints_for_run(run_id) @db.execute( "SELECT * FROM checkpoints WHERE run_id = ? ORDER BY iteration DESC", [run_id] ) end |
#close ⇒ Object
84 85 86 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 84 def close @db.close end |
#create_run(dataset:, config:, status: "running") ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 22 def create_run(dataset:, config:, status: "running") @db.execute( "INSERT INTO training_runs (dataset, config_json, status, started_at) VALUES (?, ?, ?, ?)", [dataset, JSON.generate(config), status, Time.now.iso8601] ) @db.last_insert_row_id end |
#get_run(run_id) ⇒ Object
62 63 64 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 62 def get_run(run_id) @db.get_first_row("SELECT * FROM training_runs WHERE id = ?", [run_id]) end |
#latest_run ⇒ Object
58 59 60 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 58 def latest_run @db.get_first_row("SELECT * FROM training_runs ORDER BY id DESC LIMIT 1") end |
#list_runs(limit: 50) ⇒ Object
66 67 68 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 66 def list_runs(limit: 50) @db.execute("SELECT * FROM training_runs ORDER BY id DESC LIMIT ?", [limit]) end |
#metrics_for_run(run_id) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 51 def metrics_for_run(run_id) @db.execute( "SELECT iteration, metric_type, value FROM metrics WHERE run_id = ? ORDER BY iteration", [run_id] ) end |
#record_checkpoint(run_id, path:, suffix:, iteration:, val_loss:) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 70 def record_checkpoint(run_id, path:, suffix:, iteration:, val_loss:) @db.execute( "INSERT INTO checkpoints (run_id, path, suffix, iteration, val_loss, saved_at) VALUES (?, ?, ?, ?, ?, ?)", [run_id, path, suffix, iteration, val_loss == Float::INFINITY ? nil : val_loss, Time.now.iso8601] ) end |
#record_metrics(run_id, iteration, metrics_hash) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 41 def record_metrics(run_id, iteration, metrics_hash) recorded_at = Time.now.iso8601 metrics_hash.each do |metric_type, value| @db.execute( "INSERT INTO metrics (run_id, iteration, metric_type, value, recorded_at) VALUES (?, ?, ?, ?, ?)", [run_id, iteration, metric_type.to_s, value, recorded_at] ) end end |
#update_run(run_id, **attrs) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nano_gpt/web/metrics_store.rb', line 30 def update_run(run_id, **attrs) sets = [] values = [] attrs.each do |key, value| sets << "#{key} = ?" values << value end values << run_id @db.execute("UPDATE training_runs SET #{sets.join(', ')} WHERE id = ?", values) end |