Class: NanoGPT::Web::TrainingState

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

Overview

Thread-safe shared state for training status

Constant Summary collapse

STATUSES =
%w[idle running stopped completed].freeze

Instance Method Summary collapse

Constructor Details

#initializeTrainingState

Returns a new instance of TrainingState.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nano_gpt/web/training_state.rb', line 11

def initialize
  @mutex = Mutex.new
  @state = {
    status: "idle",
    run_id: nil,
    current_iter: 0,
    current_loss: nil,
    best_val_loss: nil,
    max_iters: 0,
    dataset: nil
  }
  @stop_requested = false
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/nano_gpt/web/training_state.rb', line 31

def [](key)
  @mutex.synchronize { @state[key] }
end

#request_stop!Object



35
36
37
# File 'lib/nano_gpt/web/training_state.rb', line 35

def request_stop!
  @mutex.synchronize { @stop_requested = true }
end

#reset_stop!Object



43
44
45
# File 'lib/nano_gpt/web/training_state.rb', line 43

def reset_stop!
  @mutex.synchronize { @stop_requested = false }
end

#stop_requested?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/nano_gpt/web/training_state.rb', line 39

def stop_requested?
  @mutex.synchronize { @stop_requested }
end

#to_hObject



51
52
53
# File 'lib/nano_gpt/web/training_state.rb', line 51

def to_h
  @mutex.synchronize { @state.dup }
end

#to_jsonObject



47
48
49
# File 'lib/nano_gpt/web/training_state.rb', line 47

def to_json(*)
  @mutex.synchronize { JSON.generate(@state) }
end

#update(**attrs) ⇒ Object



25
26
27
28
29
# File 'lib/nano_gpt/web/training_state.rb', line 25

def update(**attrs)
  @mutex.synchronize do
    attrs.each { |k, v| @state[k] = v if @state.key?(k) }
  end
end