|
13 | 13 | # limitations under the License. |
14 | 14 | """Testing suite for the PyTorch NemotronH model.""" |
15 | 15 |
|
| 16 | +import copy |
16 | 17 | import tempfile |
17 | 18 | import unittest |
18 | 19 |
|
|
46 | 47 |
|
47 | 48 | from transformers import DynamicCache, NemotronHForCausalLM, NemotronHModel, StaticCache |
48 | 49 | from transformers.cache_utils import DummyLayer |
| 50 | + from transformers.models.nemotron_h.modeling_nemotron_h import NemotronHMamba2Mixer |
49 | 51 |
|
50 | 52 |
|
51 | 53 | class NemotronHModelTester(ModelTesterConfigMixin): |
@@ -513,6 +515,48 @@ def test_model(self): |
513 | 515 | config_and_inputs = self.model_tester.prepare_config_and_inputs() |
514 | 516 | self.model_tester.create_and_check_model(*config_and_inputs) |
515 | 517 |
|
| 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 | + |
516 | 560 | def test_for_causal_lm(self): |
517 | 561 | config_and_inputs = self.model_tester.prepare_config_and_inputs() |
518 | 562 | self.model_tester.create_and_check_for_causal_lm(*config_and_inputs) |
|
0 commit comments