docstr: Test if the model can be saved and loaded using transformers and get the same weights.
  We override the test of the super class to check if the weights are the same.
docstr_tok: ["Test","if","the","model","can","be","saved","and","loaded","using","transformers","and","get","the","same","weights",".","We","override","the","test","of","the","super","class","to","check","if","the","weights","are","the","same","."]
code: def test_from_save_transformers(self):
      """
      Test if the model can be saved and loaded using transformers and get the same weights.
      We override the test of the super class to check if the weights are the same.
      """
      for model_name in self.all_model_names:
          transformers_model = self.trl_model_class.transformers_parent_class.from_pretrained(
              model_name
          )
  
          trl_model = self.trl_model_class.from_pretrained(model_name)
  
          with tempfile.TemporaryDirectory() as tmp_dir:
              trl_model.save_pretrained(tmp_dir)
              transformers_model_from_save = self.trl_model_class.transformers_parent_class.from_pretrained(
                  tmp_dir
              )
  
          # Check if the weights are the same
          for key in transformers_model.state_dict():
              self.assertTrue(
                  torch.allclose(
                      transformers_model_from_save.state_dict()[key],
                      transformers_model.state_dict()[key],
                  )
              )
  
          # Check if the trl model has the same keys as the transformers model
          # except the v_head
          for key in trl_model.state_dict():
              if "v_head" not in key:
                  self.assertTrue(
                      key in transformers_model.state_dict()
                  )
                  # check if the weights are the same
                  self.assertTrue(
                      torch.allclose(
                          trl_model.state_dict()[key],
                          transformers_model.state_dict()[key],
                      )
                  )
  
          # check if they have the same modules
          self.assertTrue(
              set(transformers_model_from_save.state_dict().keys())
              == set(transformers_model.state_dict().keys())
          )
  
imports: import gc
  import tempfile
  import unittest
  import torch
  from transformers import AutoModel, AutoModelForCausalLM, AutoModelForSeq2SeqLM
  from trl import AutoModelForCausalLMWithValueHead, AutoModelForSeq2SeqLMWithValueHead, create_reference_model
label_desc: Use image node_id 8 for calling the VHeadModelTester obj's underlying member method code with example usage: obj.test_from_save_transformers() without return types