code: def test_evaluator(evaluator_type: str):
      if evaluator_type == "lightning":
          model = SimpleLightningModel()
          evaluator = create_lighting_evaluator()
          evaluator._init_optimizer_helpers(model)
          evaluator.bind_model(model)
          tensor_hook = TensorHook(
              model.model.conv1.weight,
              "model.conv1.weight",
              tensor_hook_factory,
          )
          forward_hook = ForwardHook(
              model.model.conv1, "model.conv1", forward_hook_factory
          )
          backward_hook = BackwardHook(
              model.model.conv1, "model.conv1", backward_hook_factory
          )
      elif evaluator_type == "pytorch":
          model = SimpleTorchModel().to(device)
          evaluator = create_pytorch_evaluator(model)
          evaluator._init_optimizer_helpers(model)
          evaluator.bind_model(model)
          tensor_hook = TensorHook(
              model.conv1.weight, "conv1.weight", tensor_hook_factory
          )
          forward_hook = ForwardHook(
              model.conv1, "conv1", forward_hook_factory
          )
          backward_hook = BackwardHook(
              model.conv1, "conv1", backward_hook_factory
          )
      else:
          raise ValueError(f"wrong evaluator_type: {evaluator_type}")
  
      # test train with patch & hook
      reset_flags()
      evaluator.patch_loss(loss_patch)
      evaluator.patch_optimizer_step(
          [optimizer_before_step_patch], [optimizer_after_step_patch]
      )
      evaluator.register_hooks(
          [tensor_hook, forward_hook, backward_hook]
      )
  
      evaluator.train(max_steps=1)
      assert_flags()
      assert all(
          [
              len(hook.buffer) == 1
              for hook in [tensor_hook, forward_hook, backward_hook]
          ]
      )
  
      # test finetune with patch & hook
      reset_flags()
      evaluator.remove_all_hooks()
      evaluator.register_hooks(
          [tensor_hook, forward_hook, backward_hook]
      )
  
      evaluator.finetune()
      assert_flags()
      assert all(
          [
              len(hook.buffer) == 50
              for hook in [tensor_hook, forward_hook, backward_hook]
          ]
      )
  
file_path: nni/test/algo/compression/utils/test_evaluator.py
imports: from __future__ import annotations
  import pytest
  import torch
  from nni.compression.utils.evaluator import TensorHook, ForwardHook, BackwardHook
  from ..assets.device import device
  from ..assets.simple_mnist import SimpleLightningModel, SimpleTorchModel, create_lighting_evaluator, create_pytorch_evaluator
label_desc: Use image node_id 9 for calling a global function with example usage: test_evaluator(evaluator_type) without return types