Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix Nemotron-H SSM state with padded batches
Signed-off-by: Kartikeya Dhavala <kartikeyadhavala2468@gmail.com>
  • Loading branch information
Knightkolla committed Nov 23, 2026
commit 38278c69a4cea3beebad94e609251ac91ffb3376
4 changes: 3 additions & 1 deletion mlx_lm/models/nemotron_h/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ def _ssm(
C = C.reshape(batch_size, seq_len, self.n_groups, self.ssm_state_size)
if cache:
state = cache[1]
lengths = cache.lengths
else:
state = None
state, lengths = None, None

y, state = ssm_update(
hidden_states,
Expand All @@ -193,6 +194,7 @@ def _ssm(
state,
self.time_step_limit,
mask,
lengths,
)
if cache:
cache[1] = state
Expand Down
53 changes: 53 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4086,6 +4086,59 @@ def test_granitemoehybrid_padded_batch(self):
err = mx.abs(got[uids[1]] - alone).max().item()
self.assertLess(err, 1e-5)

def test_nemotron_h_padded_batch(self):
from mlx_lm.generate import BatchGenerator, generate_step
from mlx_lm.models import nemotron_h

mx.random.seed(0)
args = nemotron_h.ModelArgs.from_dict(
{
"model_type": "nemotron_h",
"vocab_size": 128,
"hidden_size": 128,
"intermediate_size": 128,
"num_hidden_layers": 4,
"max_position_embeddings": 256,
"num_attention_heads": 8,
"num_key_value_heads": 4,
"attention_bias": False,
"mamba_num_heads": 8,
"mamba_head_dim": 64,
"mamba_proj_bias": False,
"ssm_state_size": 64,
"conv_kernel": 4,
"n_groups": 4,
"mlp_bias": False,
"layer_norm_epsilon": 1e-5,
"use_bias": False,
"use_conv_bias": True,
"hybrid_override_pattern": ["M", "*", "M", "*"],
}
)
model = nemotron_h.Model(args)
model.eval()
for layer in model.layers:
if layer.block_type == "M":
layer.mixer.A_log = mx.full(layer.mixer.A_log.shape, -4.0)
layer.mixer.conv1d.weight = layer.mixer.conv1d.weight * 30

prompts = [list(range(1, 13)), [7, 8, 9], [21, 22, 23, 24, 25]]
alone = [
next(generate_step(mx.array(p), model, max_tokens=1))[1] for p in prompts
]

gen = BatchGenerator(model, max_tokens=1)
uids = gen.insert(prompts)
got = {}
while len(got) < len(prompts):
for r in gen.next_generated():
got[r.uid] = r.logprobs
gen.close()

for uid, ref in zip(uids, alone):
err = mx.abs(got[uid] - ref).max().item()
self.assertLess(err, 1e-4)

def test_laguna_sanitize(self):
from mlx_lm.models import laguna

Expand Down