code: def test_retrieval_encoder(self, model_parallel_config):
      init_method_std = 0.02
  
      batch = 2
      neighbors = 2
      # rotary pos emb dim
      dim = 128
      pad_id = 19999
      num_attention_heads = 8
      chunks = 32
      text_chunk_size = 64
      input_length = chunks * text_chunk_size
      vocab_size = 20000
  
      hidden = torch.randint(
          0, vocab_size, (batch, input_length)
      ).cuda()  # (seq, batch, dim)
      hidden_mask = (hidden != pad_id).cuda()
  
      hidden_emb = (
          torch.rand(batch, input_length, dim).cuda().half()
      )  # (batch, seq, dim)
      retrieved = torch.randint(
          0, vocab_size, (batch, chunks, neighbors, 2 * text_chunk_size)
      ).cuda()
      pad_id = vocab_size - 1
      context_mask = (retrieved != pad_id).cuda()
      retrieved_emb = (
          torch.rand(batch, chunks, neighbors, 2 * text_chunk_size, dim)
          .cuda()
          .half()
      )
  
      layer_type = [
          LayerType.encoder,
          LayerType.retrieval_encoder,
          LayerType.encoder,
          LayerType.retrieval_encoder,
      ]
      num_layers = len(layer_type)
  
      init_method = init_method_normal(init_method_std)
      scaled_init_method = scaled_init_method_normal(
          init_method_std, num_layers
      )
      encoder = (
          MegatronRetrievalTransformerEncoderModule(
              config=model_parallel_config,
              init_method=init_method,
              output_layer_init_method=scaled_init_method,
              hidden_size=dim,
              ffn_hidden_size=dim * 4,
              num_layers=num_layers,
              num_attention_heads=num_attention_heads,
              precision=16,
              chunk_size=text_chunk_size,
              layer_type=layer_type,
          )
          .cuda()
          .half()
      )
      out = encoder(
          retrieved_emb,
          context_mask,
          context_attn_mask=hidden_mask,
          encoder_output=hidden_emb,
      )
      assert out.shape == torch.Size(
          [batch, chunks, neighbors, 2 * text_chunk_size, dim]
      )
  
file_path: NeMo/tests/collections/nlp/test_retrieval_module.py
imports: import pytest
  import torch
  from einops import rearrange
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.nlp.modules.common.megatron.attention import ParallelChunkedCrossAttention
  from nemo.collections.nlp.modules.common.megatron.layer_type import LayerType
  from nemo.collections.nlp.modules.common.megatron.megatron_init import initialize_model_parallel_for_nemo
  from nemo.collections.nlp.modules.common.megatron.position_embedding import RotaryEmbedding
  from nemo.collections.nlp.modules.common.megatron.retrieval_token_level_encoder_decoder import MegatronRetrievalTokenLevelEncoderDecoderModule
  from nemo.collections.nlp.modules.common.megatron.retrieval_transformer import MegatronRetrievalTransformerDecoderModule, MegatronRetrievalTransformerEncoderModule
  from nemo.collections.nlp.modules.common.megatron.utils import build_attention_mask_3d, init_method_normal, scaled_init_method_normal
  from nemo.collections.nlp.parts.nlp_overrides import NLPDDPStrategy
label_desc: Use image node_id 3 for calling the TestRetrievalModule obj's underlying member method code with example usage: obj.test_retrieval_encoder(model_parallel_config) without return types