code: def test_from_pretrained_avoids_weights_download_if_override_weights(
      self,
  ):
      config = AutoConfig.from_pretrained(
          "epwalsh/bert-xsmall-dummy", cache_dir=self.TEST_DIR
      )
      # only download config because downloading pretrained weights in addition takes too long
      transformer = AutoModel.from_config(
          AutoConfig.from_pretrained(
              "epwalsh/bert-xsmall-dummy", cache_dir=self.TEST_DIR
          )
      )
      transformer = AutoModel.from_config(config)
  
      # clear cache directory
      self.clear_test_dir()
  
      save_weights_path = str(self.TEST_DIR / "bert_weights.pth")
      torch.save(transformer.state_dict(), save_weights_path)
  
      override_transformer = cached_transformers.get(
          "epwalsh/bert-xsmall-dummy",
          False,
          override_weights_file=save_weights_path,
          cache_dir=self.TEST_DIR,
      )
      # check that only three files were downloaded (filename.json, filename, filename.lock), for config.json
      # if more than three files were downloaded, then model weights were also (incorrectly) downloaded
      # NOTE: downloaded files are not explicitly detailed in Huggingface's public API,
      # so this assertion could fail in the future
      json_fnames = [
          fname
          for fname in os.listdir(str(self.TEST_DIR))
          if fname.endswith(".json")
      ]
      assert len(json_fnames) == 1
      json_data = json.load(open(str(self.TEST_DIR / json_fnames[0])))
      assert (
          json_data["url"]
          == "https://huggingface.co/epwalsh/bert-xsmall-dummy/resolve/main/config.json"
      )
      resource_id = os.path.splitext(json_fnames[0])[0]
      assert set(os.listdir(str(self.TEST_DIR))) == set(
          [
              json_fnames[0],
              resource_id,
              resource_id + ".lock",
              "bert_weights.pth",
          ]
      )
  
      # check that override weights were loaded correctly
      for p1, p2 in zip(
          transformer.parameters(), override_transformer.parameters()
      ):
          assert p1.data.ne(p2.data).sum() == 0
  
imports: import json
  import os
  import pytest
  import torch
  from allennlp.common import cached_transformers
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
  from transformers import AutoConfig, AutoModel
label_desc: Use image node_id 5 for calling the TestCachedTransformers obj's underlying member method code with example usage: obj.test_from_pretrained_avoids_weights_download_if_override_weights() without return types