Class: SamplerContext
- Inherits:
-
Object
- Object
- SamplerContext
- Defined in:
- lib/toy/train/sampler.rb
Instance Attribute Summary collapse
-
#generated_ids ⇒ Object
Returns the value of attribute generated_ids.
-
#prompt_ids ⇒ Object
Returns the value of attribute prompt_ids.
-
#rng_state ⇒ Object
Returns the value of attribute rng_state.
Instance Method Summary collapse
-
#initialize(prompt_ids, seed) ⇒ SamplerContext
constructor
A new instance of SamplerContext.
-
#next_u32 ⇒ Object
xorshift32 — Spinel-safe deterministic RNG.
-
#next_unit ⇒ Object
Float in [0, 1).
Constructor Details
#initialize(prompt_ids, seed) ⇒ SamplerContext
Returns a new instance of SamplerContext.
36 37 38 39 40 41 |
# File 'lib/toy/train/sampler.rb', line 36 def initialize(prompt_ids, seed) @generated_ids = [0] @generated_ids.pop @prompt_ids = prompt_ids @rng_state = seed end |
Instance Attribute Details
#generated_ids ⇒ Object
Returns the value of attribute generated_ids.
34 35 36 |
# File 'lib/toy/train/sampler.rb', line 34 def generated_ids @generated_ids end |
#prompt_ids ⇒ Object
Returns the value of attribute prompt_ids.
34 35 36 |
# File 'lib/toy/train/sampler.rb', line 34 def prompt_ids @prompt_ids end |
#rng_state ⇒ Object
Returns the value of attribute rng_state.
34 35 36 |
# File 'lib/toy/train/sampler.rb', line 34 def rng_state @rng_state end |
Instance Method Details
#next_u32 ⇒ Object
xorshift32 — Spinel-safe deterministic RNG. Don’t use Kernel#rand; we want reproducibility across Spinel builds.
45 46 47 48 49 50 51 52 |
# File 'lib/toy/train/sampler.rb', line 45 def next_u32 x = @rng_state x = x ^ ((x << 13) & 0xFFFFFFFF) x = x ^ (x >> 17) x = x ^ ((x << 5) & 0xFFFFFFFF) @rng_state = x & 0xFFFFFFFF @rng_state end |
#next_unit ⇒ Object
Float in [0, 1).
55 56 57 |
# File 'lib/toy/train/sampler.rb', line 55 def next_unit (next_u32.to_f) / 4294967296.0 end |