docstr: Tests that we can convert back and forth between the legacy cache format and DynamicCache
docstr_tok: ["Tests","that","we","can","convert","back","and","forth","between","the","legacy","cache","format","and","DynamicCache"]
code: def test_cache_equivalence(self):
      """Tests that we can convert back and forth between the legacy cache format and DynamicCache"""
      legacy_cache = ()
      new_cache = DynamicCache()
  
      # Creates a new cache with 10 layers in both formats
      for layer_idx in range(10):
          new_key = torch.rand((2, 4, 8, 16))
          new_value = torch.rand((2, 4, 8, 16))
          new_cache.update(new_key, new_value, layer_idx)
          legacy_cache += ((new_key, new_value),)
  
      # Sanity check 1: they must have the same shapes
      self.assertTrue(len(legacy_cache), len(new_cache))
      for layer_idx in range(10):
          self.assertTrue(
              len(legacy_cache[layer_idx]), len(legacy_cache[layer_idx])
          )
          for key_value_idx in range(2):
              self.assertTrue(
                  legacy_cache[layer_idx][key_value_idx].shape
                  == new_cache[layer_idx][key_value_idx].shape
              )
  
      # Sanity check 2: we can get the sequence length in multiple ways with DynamicCache, and they return the
      # expected value
      self.assertTrue(
          legacy_cache[0][0].shape[-2]
          == new_cache[0][0].shape[-2]
          == new_cache.get_seq_length()
          == 8
      )
  
      # Sanity check 3: they must be equal, and both support indexing
      for layer_idx in range(10):
          for key_value_idx in range(2):
              self.assertTrue(
                  torch.allclose(
                      new_cache[layer_idx][key_value_idx],
                      legacy_cache[layer_idx][key_value_idx],
                  )
              )
  
      # Test 1: We can convert from legacy to new with no changes
      from_legacy = DynamicCache.from_legacy_cache(legacy_cache)
      for layer_idx in range(10):
          for key_value_idx in range(2):
              self.assertTrue(
                  torch.allclose(
                      from_legacy[layer_idx][key_value_idx],
                      legacy_cache[layer_idx][key_value_idx],
                  )
              )
  
      # Test 2: We can convert from new to legacy with no changes
      to_legacy = new_cache.to_legacy_cache()
      for layer_idx in range(10):
          for key_value_idx in range(2):
              self.assertTrue(
                  torch.allclose(
                      to_legacy[layer_idx][key_value_idx],
                      new_cache[layer_idx][key_value_idx],
                  )
              )
  
file_path: transformers/tests/test_cache_utils.py
imports: import unittest
  from transformers import set_seed
  from transformers.testing_utils import is_torch_available, require_auto_gptq, require_torch, require_torch_gpu, slow
label_desc: Use image node_id 1 for calling the CacheTest obj's underlying member method code with example usage: obj.test_cache_equivalence() without return types