code: def test_encoder_decoder_module_inference(
      self, model_parallel_config
  ):
      # rotary pos emb dim
      batch = 2
      neighbors = 2
      dim = 128
      pad_id = 19999
      num_attention_heads = 8
      chunks = 32
      text_chunk_size = 64
      input_length = chunks * text_chunk_size
      vocab_size = 20000
      enc_num_layers = 4
      dec_num_layers = 6
      enc_cross_attention = [3]  # layer numbers for cross attention
      dec_cross_attention = [3, 5]  # layer numbers for cross attention
  
      all_tokens = torch.randint(
          0, vocab_size, (batch, input_length + 1)
      ).cuda()  # (seq, batch, dim)
      hidden = all_tokens[:, :-1]
      labels = all_tokens[:, 1:]
  
      hidden_mask = (hidden != pad_id).cuda()
      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()
  
      class FakeTokenizer:
          eos_id = vocab_size - 2
  
      tokenizer = FakeTokenizer()
  
      encoder_decoder = (
          MegatronRetrievalTokenLevelEncoderDecoderModule(
              config=model_parallel_config,
              vocab_size=vocab_size,
              hidden_size=dim,
              max_position_embeddings=input_length,
              num_attention_heads=num_attention_heads,
              ffn_hidden_size=dim * 4,
              precision=16,
              chunk_size=text_chunk_size,
              enc_num_layers=enc_num_layers,
              dec_num_layers=dec_num_layers,
              enc_cross_attention=enc_cross_attention,
              dec_cross_attention=dec_cross_attention,
              add_position_embedding=False,
              tokenizer=tokenizer,
              hidden_dropout=0.0,
              attention_dropout=0.0,
          )
          .cuda()
          .half()
      )
  
      out = encoder_decoder(
          hidden,
          hidden_mask,
          retrieved_ids=retrieved,
          retrieved_attn_mask=context_mask,
      )
  
      out_1 = encoder_decoder(
          hidden[:, :62],
          hidden_mask[:, :62],
          retrieved_attn_mask=None,
          retrieved_ids=None,
          set_inference_key_value_memory=True,
          inference_max_sequence_len=input_length,
          neighbors=neighbors,
      )
      assert (out[:, :62] - out_1[:, :62]).abs().max().item() < 1e-2
  
      out_1 = encoder_decoder(
          hidden[:, 62:63],
          hidden_mask[:, :63],
          retrieved_attn_mask=None,
          retrieved_ids=None,
          set_inference_key_value_memory=False,
          inference_max_sequence_len=input_length,
          neighbors=neighbors,
      )
      assert (out[:, 62] - out_1[:, 0]).abs().max().item() < 1e-2
  
      out_2 = encoder_decoder(
          hidden[:, 63:64],
          hidden_mask[:, :64],
          retrieved_ids=retrieved[:, :1],
          retrieved_attn_mask=context_mask[:, :1],
          set_inference_key_value_memory=False,
          inference_max_sequence_len=input_length,
          neighbors=neighbors,
      )
      assert (out[:, 63] - out_2[:, 0]).abs().max().item() < 1e-2
      for i in range(64, 127):
          out_2 = encoder_decoder(
              hidden[:, i : i + 1],
              hidden_mask[:, : i + 1],
              retrieved_ids=retrieved[:, :1],
              retrieved_attn_mask=context_mask[:, :1],
              set_inference_key_value_memory=False,
              inference_max_sequence_len=input_length,
              neighbors=neighbors,
          )
          assert (out[:, i] - out_2[:, 0]).abs().max().item() < 1e-2
      for i in range(127, 191):
          out_3 = encoder_decoder(
              hidden[:, i : i + 1],
              hidden_mask[:, : i + 1],
              retrieved_ids=retrieved[:, :2],
              retrieved_attn_mask=context_mask[:, :2],
              set_inference_key_value_memory=False,
              inference_max_sequence_len=input_length,
              neighbors=neighbors,
          )
          assert (out[:, i] - out_3[:, 0]).abs().max().item() < 1e-2
  
      out_1 = encoder_decoder(
          hidden[:, :130],
          hidden_mask[:, :130],
          retrieved_ids=retrieved[:, :2],
          retrieved_attn_mask=context_mask[:, :2],
          set_inference_key_value_memory=True,
          inference_max_sequence_len=input_length,
          neighbors=neighbors,
      )
      assert (out[:, :130] - out_1[:, :130]).abs().max().item() < 1e-2
      for i in range(130, 191):
          out_3 = encoder_decoder(
              hidden[:, i : i + 1],
              hidden_mask[:, : i + 1],
              retrieved_ids=retrieved[:, :2],
              retrieved_attn_mask=context_mask[:, :2],
              set_inference_key_value_memory=False,
              inference_max_sequence_len=input_length,
              neighbors=neighbors,
          )
          assert (out[:, i] - out_3[:, 0]).abs().max().item() < 1e-2
  
file_path: NeMo/tests/collections/nlp/test_retrieval_module_inference.py
imports: import pytest
  import torch
  import torch.nn.functional
  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 5 for calling the TestRetrievalModuleInference obj's underlying member method code with example usage: obj.test_encoder_decoder_module_inference(model_parallel_config) without return types