code: def test_reinit_modules_with_layer_indices(self):
      # Comparing all weights of the model is rather complicated, so arbitrarily compare the
      # weights of attention module.
      preinit_weights = torch.cat(
          [
              layer.attention.output.dense.weight
              for layer in cached_transformers.get(
                  "bert-base-cased", True
              ).encoder.layer
          ]
      )
  
      # Test the case when reinit_modules is a valid int.
      postinit_weights = torch.cat(
          [
              layer.attention.output.dense.weight
              for layer in cached_transformers.get(
                  "bert-base-cased", True, reinit_modules=2
              ).encoder.layer
          ]
      )
      assert torch.equal(postinit_weights[:10], preinit_weights[:10])
      assert not torch.equal(
          postinit_weights[10:], preinit_weights[10:]
      )
  
      # Test the case when reinit_modules is a valid list of integers.
      postinit_weights = torch.cat(
          [
              layer.attention.output.dense.weight
              for layer in cached_transformers.get(
                  "bert-base-cased", True, reinit_modules=(10, 11)
              ).encoder.layer
          ]
      )
      assert torch.equal(postinit_weights[:10], preinit_weights[:10])
      assert not torch.equal(
          postinit_weights[10:], preinit_weights[10:]
      )
  
      # Should raise a ValueError because reinit_modules contains at least one index that is
      # greater than the models maximum number of layers
      with pytest.raises(ValueError):
          _ = cached_transformers.get(
              "bert-base-cased", True, reinit_modules=1000
          )
      with pytest.raises(ValueError):
          _ = cached_transformers.get(
              "bert-base-cased", True, reinit_modules=(1, 1000)
          )
      # The argument cannot mix layer indices and regex strings.
      with pytest.raises(ValueError):
          _ = cached_transformers.get(
              "bert-base-cased", True, reinit_modules=(1, "attentions")
          )
      # This model has a non-standard structure, so if a layer index or list of layer indexes
      # is provided, we raise a ConfigurationError.
      with pytest.raises(ConfigurationError):
          _ = cached_transformers.get(
              "sshleifer/tiny-gpt2", True, reinit_modules=1
          )
      with pytest.raises(ConfigurationError):
          _ = cached_transformers.get(
              "sshleifer/tiny-gpt2", True, reinit_modules=(1, 2)
          )
  
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 7 for calling the TestCachedTransformers obj's underlying member method code with example usage: obj.test_reinit_modules_with_layer_indices() without return types