class_docstr: Megatron encoder-decoder base class
class_docstr_tok: ["Megatron","encoder-decoder","base","class"]
docstr:     Autoregressively invokes language model in the inference mode
  Args:
      request: Dictionary with the following fields
          * prompt: a string which text the model should complete.
          * tokens_to_generate: how many tokens to generate while doing prompt completion.
  Returns:
      response: A python dictionary with the following fields
          * prompt: original text of the prompt
          * tokenized_prompt: list of (str) tokens from prompt
          * completion: a python dictionary with the following subfields:
              * tokens: a list of triples (token, token_id, log_prob) comprising completion
              * text: completion text (as a single string)
docstr_tok: ["Autoregressively","invokes","language","model","in","the","inference","mode","Args",":","request",":","Dictionary","with","the","following","fields","*","prompt",":","a","string","which","text","the","model","should","complete",".","*","tokens_to_generate",":","how","many","tokens","to","generate","while","doing","prompt","completion",".","Returns",":","response",":","A","python","dictionary","with","the","following","fields","*","prompt",":","original","text","of","the","prompt","*","tokenized_prompt",":","list","of","(","str",")","tokens","from","prompt","*","completion",":","a","python","dictionary","with","the","following","subfields",":","*","tokens",":","a","list","of","triples","(","token",",","token_id",",","log_prob",")","comprising","completion","*","text",":","completion","text","(","as","a","single","string",")"]
code: def complete(self, request: Dict):
      """
          Autoregressively invokes language model in the inference mode
      Args:
          request: Dictionary with the following fields
              * prompt: a string which text the model should complete.
              * tokens_to_generate: how many tokens to generate while doing prompt completion.
      Returns:
          response: A python dictionary with the following fields
              * prompt: original text of the prompt
              * tokenized_prompt: list of (str) tokens from prompt
              * completion: a python dictionary with the following subfields:
                  * tokens: a list of triples (token, token_id, log_prob) comprising completion
                  * text: completion text (as a single string)
  
      """
      app_state = AppState()
  
      # The complete method only works with global batch = micro batch size = data parallel size = 1.
      _reconfigure_microbatch_calculator(
          rank=app_state.global_rank,
          rampup_batch_size=None,
          global_batch_size=1,
          micro_batch_size=1,
          data_parallel_size=1,
      )
      app_state = AppState()
  
      response = {}
      self.freeze()
      # naive greedy slow loop
      # TODO: add option for BeamSearchDecoder
  
      response["prompt"] = request["prompt"][0]
      response["completion"] = {}
      bos_id = request["bos_id"]
      tokens_enc = request["masked_sample"]
  
      response["masked_input"] = " ".join(
          self.tokenizer.ids_to_tokens(
              tokens_enc[0].cpu().numpy().tolist()
          )
      )
      enc_mask = tokens_enc != self.tokenizer.pad_id
  
      predicted_tokens_ids, log_probs = self.decode(
          tokens_enc,
          enc_mask,
          int(request["tokens_to_generate"]),
          bos_id=bos_id,
      )
      predicted_tokens_ids = (
          predicted_tokens_ids.cpu().numpy()[0].tolist()
      )
      log_probs = log_probs.cpu().numpy()[0].tolist()
      if self.tokenizer.eos_id in predicted_tokens_ids:
          idx = predicted_tokens_ids.index(self.tokenizer.eos_id)
          predicted_tokens_ids = predicted_tokens_ids[:idx]
      else:
          predicted_tokens_ids = [
              id
              for id in predicted_tokens_ids
              if id != self.tokenizer.pad_id
          ]
      if self.tokenizer.eos_id in predicted_tokens_ids:
          idx = predicted_tokens_ids.index(self.tokenizer.eos_id)
          predicted_tokens_ids = predicted_tokens_ids[:idx]
      # Legacy sentencepiece detokenization still preserves special tokens which messes up exact string match.
      if hasattr(self.tokenizer, "special_token_to_id"):
          predicted_tokens_ids = [
              id
              for id in predicted_tokens_ids
              if id not in self.tokenizer.special_token_to_id.values()
          ]
  
      predicted_tokens_dec = self.tokenizer.ids_to_tokens(
          predicted_tokens_ids
      )
      response["completion"]["text"] = self.tokenizer.tokens_to_text(
          predicted_tokens_dec
      )
      response["completion"]["tokens"] = list(
          zip(predicted_tokens_ids, predicted_tokens_dec, log_probs)
      )
      self.unfreeze()
      return response
  
imports: import copy
  import functools
  import inspect
  from typing import Any, Dict, List, Optional
  import torch
  from omegaconf import OmegaConf, open_dict
  from omegaconf.dictconfig import DictConfig
  from pytorch_lightning.accelerators import CPUAccelerator
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.collections.nlp.data.language_modeling.megatron.data_samplers import MegatronPretrainingRandomSampler, MegatronPretrainingSampler
  from nemo.collections.nlp.models.language_modeling.megatron_base_model import MegatronBaseModel
  from nemo.collections.nlp.modules.common.megatron.build_model import build_model
  from nemo.collections.nlp.modules.common.megatron.module import Float16Module
  from nemo.collections.nlp.modules.common.megatron.token_level_encoder_decoder import MegatronTokenLevelEncoderDecoderModule
  from nemo.collections.nlp.modules.common.megatron.utils import ApexGuardDefaults, average_losses_across_data_parallel_group, get_params_for_weight_decay_optimization
  from nemo.collections.nlp.modules.common.text_generation_utils import compute_beam_search_len_penalty, get_sampling_token_fn
  from nemo.collections.nlp.parts.utils_funcs import get_last_rank
  from nemo.utils import AppState, logging
label_desc: Use image node_id 40 for calling the MegatronLMEncoderDecoderModel obj's underlying member method code with example usage: obj.complete(request) and returns: response