mojolearn.SambaInference#
- class mojolearn.SambaInference(config, weights)[source]#
Bases:
objectForward-only
SambaStacklanguage model on the CPU.Maps
(B, L)integer token ids to(B, L, vocab)float32 logits with the stack’s forward (no dropout): embedding, each block in stack order, final RMSNorm and the tied or untied output head. Build it from aSambaStack.save_checkpointfile withfrom_checkpoint, or from aSambaConfigand a dict of weights keyed by the config’s registry names. Weights may be packed bymojolearn.lowbit; they are unpacked exactly to float32 and copied at construction.allocate_state,forward(ids, state)andstepcarry a decode state. There is no loss, backward or training:lossandtrain_stepraiseNotImplementedError.- Parameters:
config (SambaConfig) – Model configuration; anything else raises
TypeError.weights (dict) – float32 arrays keyed by registry name. Missing or unknown names, wrong shapes and non-finite values raise
ValueError.
- Variables:
config (SambaConfig) – The configuration.
weight_format (str) – Format the weights were given in: “float32”, or the packed
mojolearn.lowbitformat.
Examples
>>> import mojolearn >>> lm = mojolearn.SambaInference.from_checkpoint("samba.ckpt") >>> logits = lm.forward(ids) # ids: (B, L) integers >>> st = lm.allocate_state(batch_size=1, max_tokens=256) >>> logits = lm.forward(prompt, st) >>> next_logits = lm.step(next_ids, st) # (B, vocab)
Methods
allocate_state(batch_size, max_tokens)A zero decode state for
batch_sizesequences of up tomax_tokenspositions.forward(inputs[, state, lengths])Logits for token ids; also available as
logitsand by calling the model.from_checkpoint(path)Build from a
SambaStack.save_checkpointfile (the streamed archive or the older JSON format).logits(inputs[, state, lengths])Logits for token ids; also available as
logitsand by calling the model.loss(inputs, targets)Not available: inference only.
An independent copy of every weight, keyed by registry name.
step(inputs, state)Decode one token per row and update
statein place.train_step(inputs, targets)Not available: inference only.
- classmethod from_checkpoint(path)[source]#
Build from a
SambaStack.save_checkpointfile (the streamed archive or the older JSON format).The checkpoint’s integrity checks run, and its schema, profile and weight registry must match its config, otherwise
ValueError. Optimizer, schedule and random state are validated and dropped.- Parameters:
path (str or path-like) – Checkpoint file.
- Returns:
SambaInference
- forward(inputs, state=None, *, lengths=None)[source]#
Logits for token ids; also available as
logitsand by calling the model.With
state=Noneevery block starts from a zero state. Pass a state fromallocate_stateto carry it: each block’s state is read and updated in place, so a laterforwardorstepon that state continues the sequence.- Parameters:
inputs (array of int, shape (B, L)) – Token ids in [0, vocab).
state (SambaState or None, default None) – Decode state to read and update.
lengths (sequence of int or None, default None) – Ragged batch: row i is real at positions
[0, lengths[i]). Stateless forward only; with astateit raisesValueError.
- Returns:
mojolearn.Array of float32, shape (B, L, vocab)
- logits(inputs, state=None, *, lengths=None)#
Logits for token ids; also available as
logitsand by calling the model.With
state=Noneevery block starts from a zero state. Pass a state fromallocate_stateto carry it: each block’s state is read and updated in place, so a laterforwardorstepon that state continues the sequence.- Parameters:
inputs (array of int, shape (B, L)) – Token ids in [0, vocab).
state (SambaState or None, default None) – Decode state to read and update.
lengths (sequence of int or None, default None) – Ragged batch: row i is real at positions
[0, lengths[i]). Stateless forward only; with astateit raisesValueError.
- Returns:
mojolearn.Array of float32, shape (B, L, vocab)
- allocate_state(batch_size, max_tokens)[source]#
A zero decode state for
batch_sizesequences of up tomax_tokenspositions.Holds one block state per layer, in stack order, as
SambaStack.allocate_statedoes. Both arguments must be positive, otherwiseValueError.- Parameters:
batch_size (int) – Number of sequences.
max_tokens (int) – Maximum total positions per sequence.
- Returns:
SambaState
- step(inputs, state)[source]#
Decode one token per row and update
statein place.A step’s logits equal the logits a one-shot
forwardgives at that position.- Parameters:
inputs (array of int, shape (B,) or (B, 1)) – Token ids.
state (SambaState) – Required decode state from
allocate_state.
- Returns:
mojolearn.Array of float32, shape (B, vocab)