class_docstr: Base class for testing Profet tasks.
class_docstr_tok: ["Base","class","for","testing","Profet","tasks","."]
docstr: Tests for the seeding of the model.
  When creating two tasks with the same parameters & seed:
  - When creating both models from scratch
  - When creating the first model from scratch, and loading the same model for the second task
  The model for both tasks should:
      - Have identical weights
      - give identical outputs for the same input.
docstr_tok: ["Tests","for","the","seeding","of","the","model",".","When","creating","two","tasks","with","the","same","parameters","&","seed",":","-","When","creating","both","models","from","scratch","-","When","creating","the","first","model","from","scratch",",","and","loading","the","same","model","for","the","second","task","The","model","for","both","tasks","should",":","-","Have","identical","weights","-","give","identical","outputs","for","the","same","input","."]
code: def test_call_is_reproducible(
      self,
      profet_train_config: MetaModelConfig,
      profet_input_dir: Path,
      tmp_path: Path,
      use_same_model: bool,
      with_grad: bool,
  ):
      """Tests for the seeding of the model.
      When creating two tasks with the same parameters & seed:
      - When creating both models from scratch
      - When creating the first model from scratch, and loading the same model for the second task
      The model for both tasks should:
          - Have identical weights
          - give identical outputs for the same input.
      """
      # Create a new, empty training directory for this test.
      checkpoint_dir = tmp_path
  
      task_kwargs = dict(
          model_config=profet_train_config,
          input_dir=profet_input_dir,
          checkpoint_dir=checkpoint_dir,
          with_grad=with_grad,
      )
      task_a = self.Task(**task_kwargs)
  
      if use_same_model:
          # Use the same checkpoint directory, meaning that model for task #2 will be loaded from
          # the checkpoint created by task #1.
          task_b = self.Task(**task_kwargs)
      else:
          # NOTE: check if the training is seeded properly by using a different checkpoint
          # directory, forcing the task to create and train a new model using the same training
          # configuration, rather than load the same one as task_a.
          task_b_kwargs = task_kwargs.copy()
          task_b_kwargs["checkpoint_dir"] = checkpoint_dir.with_name(
              checkpoint_dir.name + "_2"
          )
          task_b = self.Task(**task_b_kwargs)
  
      # Check that, if the seed is the same, the weights are the same, regardless of if the model
      # is created from scratch or loaded from a checkpoint.
      task_a_params = dict(task_a.net.named_parameters())
      task_b_params = dict(task_b.net.named_parameters())
      assert task_a_params.keys() == task_b_params.keys()
      for param_name, task_a_param in task_a_params.items():
          task_b_param = task_b_params[param_name]
          assert torch.allclose(task_a_param, task_b_param), param_name
          if task_a_param.grad is not None:
              assert task_b_param.grad is not None
              assert torch.allclose(
                  task_a_param.grad, task_b_param.grad
              ), param_name
  
      trial_a = sample(task_a)
      trial_b = sample(task_b)
      assert trial_a.params == trial_b.params
  
      # NOTE: The forward pass samples from a distribution, therefore the values might be
      # different when calling the same task twice on the same input.
      # However, given two different task instances (with the same seed) they should give the same
      # exact sample.
      result_a = task_a(**trial_a.params)
      result_b = task_b(**trial_b.params)
      if not with_grad:
          # NOTE: Can't do this simple check with grads, bool of numpy arrays is ambiguous.
          assert result_a == result_b
  
      assert len(result_a) == 1 if not with_grad else 2
      assert result_a[0]["type"] == "objective"
      objective_a = result_a[0]["value"]
      objective_b = result_b[0]["value"]
      assert objective_a == objective_b
  
      if with_grad:
          assert result_a[1]["type"] == "gradient"
          grad_a = result_a[0]["value"]
          grad_b = result_b[0]["value"]
          assert np.allclose(grad_a, grad_b)
  
imports: from dataclasses import asdict, replace
  from logging import getLogger
  from pathlib import Path
  from typing import ClassVar, Type
  import numpy
  import pytest
  from _pytest.tmpdir import TempPathFactory
  from orion.algo.space import Space
  from typing import List, Union, overload
  from pytest_mock import MockerFixture
  from orion.benchmark.task.profet.profet_task import MetaModelConfig, ProfetTask
  from orion.core.worker.trial import Trial
  from .conftest import REAL_PROFET_DATA_DIR, is_nonempty_dir
label_desc: Use image node_id 9 for calling the ProfetTaskTests obj's underlying member method code with example usage: obj.test_call_is_reproducible(profet_train_config, profet_input_dir, tmp_path, use_same_model, with_grad) without return types