code: def test_transformer_stack_from_params(params):
      torch.manual_seed(SEED)
      transformer_stack = TransformerStack.from_params(params)
  
      # Make sure we have the right number of modules.
      modules = dict(transformer_stack.named_modules())
      assert len(modules["layers"]) == PARAMS_DICT["num_hidden_layers"]
  
      hidden_states = torch.randn(2, 3, 6)
      attention_mask = torch.tensor([[0, 1, 0], [1, 1, 0]])
  
      # Make sure forward pass can run.
      torch.manual_seed(SEED)
      output = transformer_stack.forward(
          hidden_states, attention_mask=attention_mask
      )
  
      # Make sure we get the same results when instantiating from a single layer.
      torch.manual_seed(SEED)
      layer_params = copy.deepcopy(PARAMS_DICT)
      num_hidden_layers = layer_params.pop("num_hidden_layers")
      transformer_layer = TransformerLayer(**layer_params)  # type: ignore[arg-type]
      transformer_stack_from_layer = TransformerStack(
          num_hidden_layers, transformer_layer  # type: ignore[arg-type]
      )
  
      torch.manual_seed(SEED)
      from_layer_output = transformer_stack_from_layer.forward(
          hidden_states, attention_mask=attention_mask
      )
  
      assert torch.allclose(
          from_layer_output.final_hidden_states,
          output.final_hidden_states,
      )
  
      # Make sure forward pass raises with bad input.
      with pytest.raises(AssertionError):
          transformer_stack.forward(
              torch.randn(2, 3, 6),
              attention_mask=torch.randn(2, 3),
              encoder_hidden_states=torch.randn(2, 3, 6),
          )
  
file_path: allennlp/tests/modules/transformer/transformer_stack_test.py
imports: import copy
  import torch
  import pytest
  from allennlp.common import Params
  from allennlp.common import cached_transformers
  from allennlp.modules.transformer import TransformerStack, TransformerLayer
label_desc: Use image node_id 2 for calling a global function with example usage: test_transformer_stack_from_params(params) without return types