Repository: huggingface/transformers
File: tests/models/timesfm2_5/test_modeling_timesfm2_5.py (Python)

Code before:
            intermediate_size=self.intermediate_size,
            head_dim=self.head_dim,
            num_hidden_layers=self.num_hidden_layers,
            num_attention_heads=self.num_attention_heads,
            num_key_value_heads=self.num_attention_heads,
            rms_norm_eps=self.rms_norm_eps,
        )

    def get_pipeline_config(self):
        return self.get_config()

    def prepare_config_and_inputs(self):
        forecast_input = torch.stack(
            [
                torch.tensor(np.sin(np.linspace(0, 20, 100)), dtype=torch.float32, device=torch_device),
                torch.tensor(np.cos(np.linspace(0, 20, 100)), dtype=torch.float32, device=torch_device),
            ]
        )
        return self.get_config(), forecast_input

    def prepare_config_and_inputs_for_common(self):
        config, forecast_input = self.prepare_config_and_inputs()
        inputs_dict = {"past_values": forecast_input}
        return config, inputs_dict

    def create_and_check_loss_backward(self, config, forecast_input):
        model = TimesFm2_5ModelForPrediction(config)
        model.to(torch_device)
        model.train()

        future_values = torch.randn(forecast_input.shape[0], config.horizon_length, device=torch_device)
        outputs = model(past_values=forecast_input, future_values=future_values)
        outputs.loss.backward()

        # Not all trainable parameters are guaranteed to receive gradients for a single loss path.
        has_grad = any(p.grad is not None and p.grad.abs().sum() > 0 for p in model.parameters() if p.requires_grad)
        self.parent.assertTrue(has_grad, "No gradients were computed during backpropagation")


@require_torch
class TimesFm2_5ModelTest(ModelTesterMixin, unittest.TestCase):
    all_model_classes = (TimesFm2_5ModelForPrediction,) if is_torch_available() else ()
    test_resize_embeddings = False
    is_encoder_decoder = False
    test_inputs_embeds = False

    def setUp(self):
        self.model_tester = TimesFm2_5ModelTester(self)
        self.config_tester = ConfigTester(self, config_class=TimesFm2_5Config)

    def test_create_and_run_model(self):

Review comment (suggestion):
Sorry just noticing that we can implicitly check this when we set `is_training=True` instead in `TimesFm2_5ModelTester` init. Since it seems that not every param has gradients, you can also set `test_all_params_have_gradient=False`

Code after:
            hidden_size=self.hidden_size,
            intermediate_size=self.intermediate_size,
            head_dim=self.head_dim,
            num_hidden_layers=self.num_hidden_layers,
            num_attention_heads=self.num_attention_heads,
            num_key_value_heads=self.num_attention_heads,
            rms_norm_eps=self.rms_norm_eps,
        )

    def get_pipeline_config(self):
        return self.get_config()

    def prepare_config_and_inputs(self):
        forecast_input = torch.stack(
            [
                torch.tensor(np.sin(np.linspace(0, 20, 100)), dtype=torch.float32, device=torch_device),
                torch.tensor(np.cos(np.linspace(0, 20, 100)), dtype=torch.float32, device=torch_device),
            ]
        )
        return self.get_config(), forecast_input

    def prepare_config_and_inputs_for_common(self):
        config, forecast_input = self.prepare_config_and_inputs()
        inputs_dict = {"past_values": forecast_input}
        return config, inputs_dict


@require_torch
class TimesFm2_5ModelTest(ModelTesterMixin, unittest.TestCase):
    all_model_classes = (TimesFm2_5ModelForPrediction,) if is_torch_available() else ()
    test_resize_embeddings = False
    is_encoder_decoder = False
    test_inputs_embeds = False
    test_all_params_have_gradient = False

    def setUp(self):
        self.model_tester = TimesFm2_5ModelTester(self)
        self.config_tester = ConfigTester(self, config_class=TimesFm2_5Config)

    def test_create_and_run_model(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        model = TimesFm2_5ModelForPrediction(config)
        model.to(torch_device)
        model.eval()
        results = model(**inputs_dict)
        assert results.mean_predictions is not None

    @unittest.skip(reason="FA backend not yet supported because of forced masks")
    def test_sdpa_can_dispatch_on_flash(self):
        pass