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: Test that the inverse converted model and hf model have the same logits
docstr_tok: ["Test","that","the","inverse","converted","model","and","hf","model","have","the","same","logits"]
code: def _test_invert_model_logits(
      self,
      model_id: str,
      model_type: str,
      keep_original_model=True,
      **preprocessor_kwargs,
  ):
      r"""
      Test that the inverse converted model and hf model have the same logits
      """
      inputs = self.prepare_inputs_for_class(
          model_id, model_type=model_type, **preprocessor_kwargs
      )
  
      hf_model = AutoModel.from_pretrained(model_id)
      hf_model = hf_model.eval()
  
      with torch.inference_mode():
          torch.manual_seed(42)
          output_hf = hf_model(**inputs)
  
          bt_model = BetterTransformer.transform(
              hf_model, keep_original_model=keep_original_model
          )
          bt_model = BetterTransformer.reverse(bt_model)
  
          torch.manual_seed(42)
          output_bt = bt_model(**inputs)
  
      for i in range(len(output_bt)):
          if isinstance(output_bt[i], torch.Tensor):
              self.assertTrue(
                  torch.allclose(output_bt[i], output_hf[i], atol=1e-4),
                  f" Maxdiff: {(output_bt[i] - output_hf[i]).abs().max()}",
              )
          elif isinstance(output_bt[i], tuple):
              flattened_output_bt = [
                  out
                  for j in range(len(output_bt[i]))
                  for out in output_bt[i][j]
              ]
              flattened_output_hf = [
                  out
                  for j in range(len(output_hf[i]))
                  for out in output_hf[i][j]
              ]
              for j in range(len(flattened_output_bt)):
                  if isinstance(flattened_output_bt[j], torch.Tensor):
                      self.assertTrue(
                          torch.allclose(
                              flattened_output_bt[j],
                              flattened_output_hf[j],
                              atol=1e-4,
                          ),
                          f" Maxdiff: {(flattened_output_bt[j] - flattened_output_hf[j]).abs().max()}",
                      )
                  elif isinstance(flattened_output_bt[j], tuple):
                      for k in range(len(flattened_output_bt[j])):
                          self.assertTrue(
                              torch.allclose(
                                  flattened_output_bt[j][k],
                                  flattened_output_hf[j][k],
                                  atol=1e-4,
                              ),
                              f" Maxdiff: {(flattened_output_bt[j][k] - flattened_output_hf[j][k]).abs().max()}",
                          )
  
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 9 for calling the BetterTransformersTestMixin obj's underlying member method code with example usage: obj._test_invert_model_logits(model_id, model_type, keep_original_model) without return types