docstr: Asserting if outputted attention matrix is similar to hard coded attention matrix
docstr_tok: ["Asserting","if","outputted","attention","matrix","is","similar","to","hard","coded","attention","matrix"]
code: def test_block_sparse_attention_probs(self):
      """
      Asserting if outputted attention matrix is similar to hard coded attention matrix
      """
  
      if not self.test_attention_probs:
          return
  
      model = BigBirdModel.from_pretrained(
          "google/bigbird-roberta-base",
          attention_type="block_sparse",
          num_random_blocks=3,
          block_size=16,
      )
      model.to(torch_device)
      model.eval()
      config = model.config
  
      input_ids = self._get_dummy_input_ids()
  
      hidden_states = model.embeddings(input_ids)
  
      batch_size, seqlen, _ = hidden_states.size()
      attn_mask = torch.ones(
          batch_size, seqlen, device=torch_device, dtype=torch.float
      )
      to_seq_length = from_seq_length = seqlen
      from_block_size = to_block_size = config.block_size
  
      (
          blocked_mask,
          band_mask,
          from_mask,
          to_mask,
      ) = model.create_masks_for_block_sparse_attn(
          attn_mask, config.block_size
      )
      from_blocked_mask = to_blocked_mask = blocked_mask
  
      for i in range(config.num_hidden_layers):
          pointer = model.encoder.layer[i].attention.self
  
          query_layer = pointer.transpose_for_scores(
              pointer.query(hidden_states)
          )
          key_layer = pointer.transpose_for_scores(
              pointer.key(hidden_states)
          )
          value_layer = pointer.transpose_for_scores(
              pointer.value(hidden_states)
          )
  
          (
              context_layer,
              attention_probs,
          ) = pointer.bigbird_block_sparse_attention(
              query_layer,
              key_layer,
              value_layer,
              band_mask,
              from_mask,
              to_mask,
              from_blocked_mask,
              to_blocked_mask,
              pointer.num_attention_heads,
              pointer.num_random_blocks,
              pointer.attention_head_size,
              from_block_size,
              to_block_size,
              batch_size,
              from_seq_length,
              to_seq_length,
              seed=pointer.seed,
              plan_from_length=None,
              plan_num_rand_blocks=None,
              output_attentions=True,
          )
  
          context_layer = context_layer.contiguous().view(
              batch_size, from_seq_length, -1
          )
          cl = torch.einsum(
              "bhqk,bhkd->bhqd", attention_probs, value_layer
          )
          cl = cl.view(context_layer.size())
  
          self.assertTrue(torch.allclose(context_layer, cl, atol=0.001))
  
imports: import unittest
  from transformers import BigBirdConfig, is_torch_available
  from transformers.models.auto import get_values
  from transformers.models.big_bird.tokenization_big_bird import BigBirdTokenizer
  from transformers.testing_utils import require_torch, slow, torch_device
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
  from ...test_pipeline_mixin import PipelineTesterMixin
label_desc: Use image node_id 4 for calling the BigBirdModelIntegrationTest obj's underlying member method code with example usage: obj.test_block_sparse_attention_probs() without return types