Skip to content

Commit 2f6f691

Browse files
:red-circle: fix(nemotron_h): honor configured Mamba time-step limits (#48989)
fix(nemotron_h): honor configured Mamba time-step limits Signed-off-by: Yuhe Zhang <yuhez@nvidia.com> Co-authored-by: Arthur <arthur.zucker@gmail.com>
1 parent 4b401cf commit 2f6f691

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/transformers/models/nemotron_h/modeling_nemotron_h.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,7 @@ def __init__(self, config: NemotronHConfig, layer_idx: int | None = None, initia
411411
self.n_groups = config.n_groups
412412
self.head_dim = config.mamba_head_dim
413413
self.chunk_size = config.chunk_size
414-
# No upper limit
415-
self.time_step_limit = (config.time_step_min, float("inf"))
414+
self.time_step_limit = config.time_step_limit
416415

417416
self.conv_dim = self.intermediate_size + 2 * self.n_groups * self.ssm_state_size
418417

‎src/transformers/models/nemotron_h/modular_nemotron_h.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(self, config: NemotronHConfig, layer_idx: int | None = None, initia
6363
self.n_groups = config.n_groups
6464
self.head_dim = config.mamba_head_dim
6565
self.num_heads = config.mamba_num_heads
66+
self.time_step_limit = config.time_step_limit
6667

6768
self.conv1d = nn.Conv1d(
6869
in_channels=self.conv_dim,

‎tests/models/nemotron_h/test_modeling_nemotron_h.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Testing suite for the PyTorch NemotronH model."""
1515

16+
import copy
1617
import tempfile
1718
import unittest
1819

@@ -46,6 +47,7 @@
4647

4748
from transformers import DynamicCache, NemotronHForCausalLM, NemotronHModel, StaticCache
4849
from transformers.cache_utils import DummyLayer
50+
from transformers.models.nemotron_h.modeling_nemotron_h import NemotronHMamba2Mixer
4951

5052

5153
class NemotronHModelTester(ModelTesterConfigMixin):
@@ -513,6 +515,48 @@ def test_model(self):
513515
config_and_inputs = self.model_tester.prepare_config_and_inputs()
514516
self.model_tester.create_and_check_model(*config_and_inputs)
515517

518+
def test_mamba_time_step_min_only_affects_initialization(self):
519+
torch.manual_seed(0)
520+
config = self.model_tester.get_config()
521+
mixer = NemotronHMamba2Mixer(config, layer_idx=0).eval()
522+
with torch.no_grad():
523+
mixer.dt_bias.fill_(-12.0)
524+
mixer.D.zero_()
525+
526+
other_config = copy.deepcopy(config)
527+
other_config.time_step_min = 0.1
528+
other_mixer = NemotronHMamba2Mixer(other_config, layer_idx=0).eval()
529+
other_mixer.load_state_dict(mixer.state_dict())
530+
531+
hidden_states = torch.randn(2, 7, config.hidden_size, requires_grad=True)
532+
output = mixer(hidden_states)
533+
other_output = other_mixer(hidden_states)
534+
torch.testing.assert_close(output, other_output)
535+
gradient = torch.autograd.grad(output.square().sum(), hidden_states)[0]
536+
other_gradient = torch.autograd.grad(other_output.square().sum(), hidden_states)[0]
537+
torch.testing.assert_close(gradient, other_gradient)
538+
539+
def test_mamba_time_step_limit_bounds(self):
540+
torch.manual_seed(0)
541+
config = self.model_tester.get_config()
542+
config.time_step_limit = (0.01, 0.02)
543+
mixer = NemotronHMamba2Mixer(config, layer_idx=0).eval()
544+
with torch.no_grad():
545+
mixer.in_proj.weight[-config.mamba_num_heads :].zero_()
546+
mixer.D.zero_()
547+
reference_config = copy.deepcopy(config)
548+
reference_config.time_step_limit = (0.0, float("inf"))
549+
reference = NemotronHMamba2Mixer(reference_config, layer_idx=0).eval()
550+
reference.load_state_dict(mixer.state_dict())
551+
hidden_states = torch.randn(2, 7, config.hidden_size)
552+
553+
for dt_bias, expected_dt in [(-12.0, 0.01), (12.0, 0.02)]:
554+
with self.subTest(dt_bias=dt_bias), torch.no_grad():
555+
mixer.dt_bias.fill_(dt_bias)
556+
dt = torch.tensor(expected_dt)
557+
reference.dt_bias.copy_((dt + torch.log(-torch.expm1(-dt))).expand_as(reference.dt_bias))
558+
torch.testing.assert_close(mixer(hidden_states), reference(hidden_states))
559+
516560
def test_for_causal_lm(self):
517561
config_and_inputs = self.model_tester.prepare_config_and_inputs()
518562
self.model_tester.create_and_check_for_causal_lm(*config_and_inputs)

0 commit comments

Comments
 (0)