code: def test_t5_inference(layout, activation, ctx):
      with ctx:
          cfg = T5Model.get_cfg("google_t5_small")
          cfg.defrost()
          cfg.MODEL.layout = layout
          cfg.MODEL.activation = activation
          cfg.freeze()
  
          model = T5Model.from_cfg(cfg)
          model.initialize()
          model.hybridize()
  
          # while keeping T5Model implementation consistent with Huggingface's, this
          # temporary class would help the backbone fit into the provided nmt tests.
          class TempWithHead(HybridBlock):
              def __init__(self, model):
                  super().__init__()
                  self.model = model
                  self.layout = model.layout
                  self.src_vocab_size = model.vocab_size
                  self.tgt_vocab_size = model.vocab_size
                  # append a final output layer
                  self.output_layer = nn.Dense(
                      units=model.vocab_size,
                      in_units=model._d_model,
                      flatten=False,
                      use_bias=False,
                      dtype=model._dtype,
                  )
                  self.output_layer.weight = (
                      model.input_embedding_layer.weight
                  )
  
              def forward(self, *args, **kwargs):
                  return self.output_layer(self.model(*args, **kwargs))
  
          backbone = TempWithHead(model)
          backbone.hybridize()
          verify_nmt_model(backbone)
  
          inference_model = T5Inference(model)
          inference_model.hybridize()
          verify_nmt_inference(
              train_model=backbone, inference_model=inference_model
          )
  
file_path: gluon-nlp/tests/test_models_t5.py
imports: import pytest
  import tempfile
  import mxnet
  from mxnet import np, npx
  from mxnet.gluon import nn, HybridBlock
  from gluonnlp.models.t5 import T5Model, T5Inference, t5_cfg_reg, list_pretrained_t5, get_pretrained_t5
  from gluonnlp.utils.testing import verify_nmt_model, verify_nmt_inference
label_desc: Use image node_id 3 for calling a global function with example usage: test_t5_inference(layout, activation, ctx) and returns: self