granitemoehybrid¶
granitemoehybrid
¶
GraniteMoE-Hybrid (GraniteMoeHybridForCausalLM, IBM Granite 4.0 and 4.0-H).
Llama's containers over one block class that holds either a Mamba-2 mixer or
attention, by config.layers_block_type, and then a feed-forward::
model.layers[i] GraniteMoeHybridDecoderLayer
.input_layernorm
.mamba GraniteMoeHybridMambaLayer, an SSD mixer (or .self_attn)
.post_attention_layernorm
.block_sparse_moe GraniteMoeHybridMoE (only when num_local_experts > 0)
.shared_mlp GraniteMoeHybridMLP (on every block)
h = x + mixer(input_layernorm(x)) * residual_multiplier
out = h + (block_sparse_moe(n) + shared_mlp(n)) * residual_multiplier with n = post_attention_layernorm(h)
(shared_mlp(n) alone on a dense checkpoint, Granite-4.0-H-Micro's.) mamba
is linear_attn (nnterp.StateSpace) and shared_mlp, the one feed-forward
every block has, is mlp; each block has self_attn or linear_attn,
never both. As on Granite (granite.py) the block adds each sublayer's output
times residual_multiplier: both mixers' attention_output are the module's
output times it, computed copies divided on assignment and carried back by a
transform. No module returns the feed-forward's sum, so mlp_output is the
block's binding of it (hidden_states_4 with experts, hidden_states_5
without) times the multiplier, and the family's Layer sets sourced for
it; the mixer and the shared expert, whose modules keep no config, read the
multiplier (and the Mlp whether routed experts run) off their parent block. A write lands on the sum, replacing both experts' contribution.
mlp.output is the shared expert's output alone and
layers[i].block_sparse_moe.output the routed experts'. The mixture's values
live on mlp (a Moe), which reads block_sparse_moe's router and
experts through its parent: shared_expert_output is mlp.output
and routed_output + shared_expert_output == mlp_output / residual_multiplier. mlp.intermediate_size
and the root's intermediate_size are the shared expert's width,
shared_intermediate_size; the routed experts are intermediate_size wide.
The attention runs the shared eager forward with attention_multiplier as its
scale and no rotary under position_embedding_type nope (Granite-4.0-H);
embedding_multiplier and logits_scaling are as on Granite.
RENAME
module-attribute
¶
RENAME = {'model.embed_tokens': 'embed_tokens', 'model.layers': 'layers', 'model.norm': 'norm', 'mamba': 'linear_attn', 'shared_mlp': 'mlp', 'activation': 'act_fn', 'output_linear': 'down_proj'}
ENVOYS
module-attribute
¶
ENVOYS = {GraniteMoeHybridDecoderLayer: Layer, GraniteMoeHybridAttention: Attention, GraniteMoeHybridMambaLayer: StateSpace, GraniteMoeHybridMLP: Mlp}
Attention
¶
Bases: Attention
GraniteMoE-Hybrid's attention: Granite's, the block adds its output times residual_multiplier.
Mlp
¶
Bases: Moe
GraniteMoE-Hybrid's shared expert, standing for the block's feed-forward: the block adds it plus the routed experts, times residual_multiplier.
It hosts the block's mixture of experts (block_sparse_moe, whose
router and experts it reads off its parent block), and is itself
the shared expert.
residual_multiplier
class-attribute
instance-attribute
¶
residual_multiplier = property(residual_multiplier)
has_experts
property
¶
Whether routed experts run beside this shared expert: the block's has_experts.
intermediate_size
property
¶
Width of the shared expert's hidden layer: the module's hidden_size (shared_intermediate_size).
intermediate_size
¶
intermediate_size(model: StandardizedTransformer) -> int
The MLP width is the shared expert's, shared_intermediate_size (mlp is the shared expert); intermediate_size is the routed experts'.