class_docstr: `BetterTransformersTestMixin` to wrap necessary functions for testing `BetterTransformer`
  integration. This includes the following tests:
      - `test_logits`: This tests if the converted model produces the same logits
      than the original model.
      - `test_raise_on_save`: Test if the converion properly raises an error if someone tries to save the model using `save_pretrained`.
class_docstr_tok: ["`","BetterTransformersTestMixin","`","to","wrap","necessary","functions","for","testing","`","BetterTransformer","`","integration",".","This","includes","the","following","tests",":","-","`","test_logits","`",":","This","tests","if","the","converted","model","produces","the","same","logits","than","the","original","model",".","-","`","test_raise_on_save","`",":","Test","if","the","converion","properly","raises","an","error","if","someone","tries","to","save","the","model","using","`","save_pretrained","`","."]
docstr: This tests if the converted model produces the same logits
  as the original model.
docstr_tok: ["This","tests","if","the","converted","model","produces","the","same","logits","as","the","original","model","."]
code: def _test_logits(
      self, model_id: str, model_type: str, **preprocessor_kwargs
  ):
      r"""
      This tests if the converted model produces the same logits
      as the original model.
      """
      # The first row of the attention mask needs to be all ones -> check: https://github.com/pytorch/pytorch/blob/19171a21ee8a9cc1a811ac46d3abd975f0b6fc3b/test/test_nn.py#L5283
      inputs = self.prepare_inputs_for_class(
          model_id=model_id,
          model_type=model_type,
          **preprocessor_kwargs,
      )
  
      torch.manual_seed(0)
      hf_random_model = AutoModel.from_pretrained(model_id).eval()
      random_config = hf_random_model.config
  
      hf_random_model = hf_random_model.eval()
  
      torch.manual_seed(0)
      converted_model = BetterTransformer.transform(
          hf_random_model, keep_original_model=True
      )
  
      self.assertFalse(hf_random_model.training)
      self.assertFalse(converted_model.training)
      self.assertFalse(
          hasattr(hf_random_model, "use_bettertransformer"),
          f"The model {hf_random_model.__class__.__name__} has been converted to a `fast` model by mistake.",
      )
  
      with torch.no_grad():
          r"""
          Make sure the models are in eval mode! Make also sure that the original model
          has not been converted to a fast model. The check is done above.
          """
          torch.manual_seed(0)
          hf_hidden_states = hf_random_model(**inputs)[0]
  
          torch.manual_seed(0)
          bt_hidden_states = converted_model(**inputs)[0]
  
          if (
              "quick_gelu"
              in flatten_dict(random_config.to_dict()).values()
          ):
              # Since `quick_gelu` is a rather slightly modified version of `GeLU` we expect a discrepency.
              tol = 3e-1
          elif (
              "gelu_new"
              in flatten_dict(random_config.to_dict()).values()
          ):
              # Since `gelu_new` is a slightly modified version of `GeLU` we expect a small
              # discrepency.
              tol = 4e-2
          else:
              tol = 2e-3
  
          if hasattr(self, "compare_outputs"):
              self.compare_outputs(
                  model_type,
                  hf_hidden_states,
                  bt_hidden_states,
                  atol=tol,
                  model_name=hf_random_model.__class__.__name__,
              )
          elif "attention_mask" in inputs:
              for i, attention_mask in enumerate(
                  inputs["attention_mask"]
              ):
                  length = (
                      torch.argwhere(attention_mask != 0).max().item()
                  )
                  self.assert_equal(
                      tensor1=hf_hidden_states[i, : length + 1, :],
                      tensor2=bt_hidden_states[i, : length + 1, :],
                      atol=tol,
                      model_name=hf_random_model.__class__.__name__,
                  )
          else:
              self.assert_equal(
                  tensor1=hf_hidden_states[:, :3, :],
                  tensor2=bt_hidden_states[:, :3, :],
                  atol=tol,
                  model_name=hf_random_model.__class__.__name__,
              )
  
imports: import copy
  import inspect
  import tempfile
  import unittest
  import torch
  from transformers import AutoModel
  from optimum.bettertransformer import BetterTransformer
  from optimum.utils.testing_utils import flatten_dict, require_torch_gpu
label_desc: Use image node_id 4 for calling the BetterTransformersTestMixin obj's underlying member method code with example usage: obj._test_logits(model_id, model_type) without return types