mojolearn.TransformerBlockInference#
- class mojolearn.TransformerBlockInference(weights, *, n_heads, n_kv_heads=None, head_dim=None, window=0, rope_theta=10000.0, rope_scaling=None, rope_dim=None, max_positions=8192, qkv_bias=False, o_bias=False, norm='rmsnorm', norm_eps=1e-06, norm_bias=False, mlp='swiglu', mlp_bias=False, qk_norm=False, attn_softcap=None, numeric_profile=None)[source]#
Bases:
TransformerBlockForward-only
TransformerBlockon the CPU, from weights trained on a GPU.Takes the same constructor arguments as
TransformerBlock(the nine named weights,n_heads,n_kv_heads,head_dim,window) and applies the same weight checks. Supportsforward(x, lengths=None)from a zero state,allocate_state(B, max_tokens),forward(x, state)andstep(x, state); decoding one token at a time with a carried state gives the same bits as one forward pass over the whole sequence. A block has no output head, so it returns hidden states, not logits. Runs on the host binding shipped with mojolearn, loaded on first use.Examples
>>> blk = mojolearn.TransformerBlockInference(weights, n_heads=4) >>> y = blk.forward(x)
- __init__(weights, *, n_heads, n_kv_heads=None, head_dim=None, window=0, rope_theta=10000.0, rope_scaling=None, rope_dim=None, max_positions=8192, qkv_bias=False, o_bias=False, norm='rmsnorm', norm_eps=1e-06, norm_bias=False, mlp='swiglu', mlp_bias=False, qk_norm=False, attn_softcap=None, numeric_profile=None)#
Methods
allocate_state(batch_size, max_tokens)A zero
TransformerStateforbatch_sizesequences of up tomax_tokenspositions each.backward(x, grad_output)Not available: inference is forward only.
decode_session(state)Open a
TransformerDecodeSessionon this block andstate.forward(x[, state, lengths])Run the block on
(B, L, d_model)float32 input and return the output, same shape.get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
set_params(**params)Set constructor parameters.
step(x, state)Decode one token per sequence, continuing the cache in
state.vendor_used()The GPU backend this estimator runs on.