code: def test_multi_head_self_attention_respects_masking(self):
      attention = MultiHeadSelfAttention(
          num_heads=3,
          input_dim=5,
          attention_dim=6,
          values_dim=9,
          attention_dropout_prob=0.0,
      )
      tensor = torch.randn(2, 12, 5)
      mask = torch.ones([2, 12])
      mask[0, 6:] = 0
      result = attention(tensor, mask)
      # Compute the same function without a mask, but with
      # only the unmasked elements - should be the same.
      result_without_mask = attention(tensor[:, :6, :])
      numpy.testing.assert_almost_equal(
          result[0, :6, :].detach().cpu().numpy(),
          result_without_mask[0, :, :].detach().cpu().numpy(),
      )
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/modules/seq2seq_encoders/multi_head_self_attention_test.py
imports: from __future__ import absolute_import
  import numpy
  import torch
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.modules.seq2seq_encoders import MultiHeadSelfAttention
  from allennlp.common.params import Params
label_desc: Use image node_id 3 for calling the MultiHeadSelfAttentionTest obj's underlying member method code with example usage: obj.test_multi_head_self_attention_respects_masking() without return types