class_docstr: BERT encoder with QA head training.
class_docstr_tok: ["BERT","encoder","with","QA","head","training","."]
docstr: Get prediction for unlabeled inference data
  
  Args:
      file: inference data
      batch_size: batch size to use during inference
      num_samples: number of samples to use of inference data. Default: -1 if all data should be used.
      output_nbest_file: optional output file for writing out nbest list
      output_prediction_file: optional output file for writing out predictions
      
  Returns:
      model predictions, model nbest list
docstr_tok: ["Get","prediction","for","unlabeled","inference","data","Args",":","file",":","inference","data","batch_size",":","batch","size","to","use","during","inference","num_samples",":","number","of","samples","to","use","of","inference","data",".","Default",":","-1","if","all","data","should","be","used",".","output_nbest_file",":","optional","output","file","for","writing","out","nbest","list","output_prediction_file",":","optional","output","file","for","writing","out","predictions","Returns",":","model","predictions",",","model","nbest","list"]
code: def inference(
      self,
      file: str,
      batch_size: int = 1,
      num_samples: int = -1,
      output_nbest_file: Optional[str] = None,
      output_prediction_file: Optional[str] = None,
  ):
      """
      Get prediction for unlabeled inference data
  
      Args:
          file: inference data
          batch_size: batch size to use during inference
          num_samples: number of samples to use of inference data. Default: -1 if all data should be used.
          output_nbest_file: optional output file for writing out nbest list
          output_prediction_file: optional output file for writing out predictions
  
      Returns:
          model predictions, model nbest list
      """
      # store predictions for all queries in a single list
      all_predictions = []
      all_nbest = []
      mode = self.training
      device = "cuda" if torch.cuda.is_available() else "cpu"
      try:
          # Switch model to evaluation mode
          self.eval()
          self.to(device)
          logging_level = logging.get_verbosity()
          logging.set_verbosity(logging.WARNING)
          dataloader_cfg = {
              "batch_size": batch_size,
              "file": file,
              "shuffle": False,
              "num_samples": num_samples,
              "num_workers": 2,
              "pin_memory": False,
              "drop_last": False,
          }
          dataloader_cfg = OmegaConf.create(dataloader_cfg)
          infer_datalayer = self._setup_dataloader_from_config(
              cfg=dataloader_cfg, mode=INFERENCE_MODE
          )
  
          all_logits = []
          all_unique_ids = []
          for i, batch in enumerate(infer_datalayer):
              (
                  input_ids,
                  token_type_ids,
                  attention_mask,
                  unique_ids,
              ) = batch
              logits = self.forward(
                  input_ids=input_ids.to(device),
                  token_type_ids=token_type_ids.to(device),
                  attention_mask=attention_mask.to(device),
              )
              all_logits.append(logits)
              all_unique_ids.append(unique_ids)
          logits = torch.cat(all_logits)
          unique_ids = tensor2list(torch.cat(all_unique_ids))
          s, e = logits.split(dim=-1, split_size=1)
          start_logits = tensor2list(s.squeeze(-1))
          end_logits = tensor2list(e.squeeze(-1))
          (
              all_predictions,
              all_nbest,
              scores_diff,
          ) = infer_datalayer.dataset.get_predictions(
              unique_ids=unique_ids,
              start_logits=start_logits,
              end_logits=end_logits,
              n_best_size=self._cfg.dataset.n_best_size,
              max_answer_length=self._cfg.dataset.max_answer_length,
              version_2_with_negative=self._cfg.dataset.version_2_with_negative,
              null_score_diff_threshold=self._cfg.dataset.null_score_diff_threshold,
              do_lower_case=self._cfg.dataset.do_lower_case,
          )
  
          with open(file, "r") as test_file_fp:
              test_data = json.load(test_file_fp)["data"]
              id_to_question_mapping = {}
              for title in test_data:
                  for par in title["paragraphs"]:
                      for question in par["qas"]:
                          id_to_question_mapping[
                              question["id"]
                          ] = question["question"]
  
          for question_id in all_predictions:
              all_predictions[question_id] = (
                  id_to_question_mapping[question_id],
                  all_predictions[question_id],
              )
  
          if output_nbest_file is not None:
              with open(output_nbest_file, "w") as writer:
                  writer.write(json.dumps(all_nbest, indent=4) + "\n")
          if output_prediction_file is not None:
              with open(output_prediction_file, "w") as writer:
                  writer.write(
                      json.dumps(all_predictions, indent=4) + "\n"
                  )
  
      finally:
          # set mode back to its original value
          self.train(mode=mode)
          logging.set_verbosity(logging_level)
  
      return all_predictions, all_nbest
  
imports: import json
  from typing import Optional
  import torch
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from torch.cuda.amp import autocast
  from nemo.collections.common.losses import SpanningLoss
  from nemo.collections.nlp.data import SquadDataset
  from nemo.collections.nlp.data.question_answering_squad.qa_squad_processing import EVALUATION_MODE, INFERENCE_MODE, TRAINING_MODE
  from nemo.collections.nlp.models.nlp_model import NLPModel
  from nemo.collections.nlp.modules.common import TokenClassifier
  from nemo.collections.nlp.parts.utils_funcs import tensor2list
  from nemo.core.classes.common import PretrainedModelInfo, typecheck
  from nemo.utils import logging
label_desc: Use image node_id 8 for calling the QAModel obj's underlying member method code with example usage: obj.inference(file, batch_size, num_samples, output_nbest_file, output_prediction_file) and returns: all_predictions,  all_nbest