class_docstr: A model for restoring punctuation and capitalization in text. The model is usually used together with ASR model
  because ASR models often return text without punctuation and capitalization.
  
  The model consists of a language model and two multilayer perceptrons (MLP) on top the language model. The first
  MLP serves for punctuation prediction and the second is for capitalization prediction. You can use only BERT-like
  HuggingFace language models (model ``forward`` method accepts ``input_ids``, ``token_types_ids``,
  ``attention_mask`` arguments). See more about model config options :ref:`here<model-config-label>`.
  
  Use method :meth:`~add_punctuation_capitalization` for model inference.
  
  For training and testing use dataset
  :class:`~nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset.BertPunctuationCapitalizationDataset`,
  for training on huge amounts of data which cannot be loaded into memory simultaneously use
  :class:`~nemo.collections.nlp.data.token_classification.punctuation_capitalization_tarred_dataset.BertPunctuationCapitalizationTarredDataset`.
  
  Args:
      cfg: a model configuration. It should follow dataclass
          :class:`~nemo.collections.nlp.models.token_classification.punctuation_capitalization_config.PunctuationCapitalizationModelConfig`
          See an example of full config in
          `nemo/examples/nlp/token_classification/conf/punctuation_capitalization_config.yaml
          <https://github.com/NVIDIA/NeMo/blob/main/examples/nlp/token_classification/conf/punctuation_capitalization_config.yaml>`_
      trainer: an instance of a PyTorch Lightning trainer
class_docstr_tok: ["A","model","for","restoring","punctuation","and","capitalization","in","text",".","The","model","is","usually","used","together","with","ASR","model","because","ASR","models","often","return","text","without","punctuation","and","capitalization",".","The","model","consists","of","a","language","model","and","two","multilayer","perceptrons","(","MLP",")","on","top","the","language","model",".","The","first","MLP","serves","for","punctuation","prediction","and","the","second","is","for","capitalization","prediction",".","You","can","use","only","BERT-like","HuggingFace","language","models","(","model","``","forward","``","method","accepts","``","input_ids","``",",","``","token_types_ids","``",",","``","attention_mask","``","arguments",")",".","See","more","about","model","config","options",":","ref",":","`","here","<","model-config-label",">","`",".","Use","method",":","meth",":","`","~add_punctuation_capitalization","`","for","model","inference",".","For","training","and","testing","use","dataset",":","class",":","`","~nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset.BertPunctuationCapitalizationDataset","`",",","for","training","on","huge","amounts","of","data","which","can","not","be","loaded","into","memory","simultaneously","use",":","class",":","`","~nemo.collections.nlp.data.token_classification.punctuation_capitalization_tarred_dataset.BertPunctuationCapitalizationTarredDataset","`",".","Args",":","cfg",":","a","model","configuration",".","It","should","follow","dataclass",":","class",":","`","~nemo.collections.nlp.models.token_classification.punctuation_capitalization_config.PunctuationCapitalizationModelConfig","`","See","an","example","of","full","config","in","`","nemo\/examples\/nlp\/token_classification\/conf\/punctuation_capitalization_config.yaml","<","https",":","\/\/github.com\/NVIDIA\/NeMo\/blob\/main\/examples\/nlp\/token_classification\/conf\/punctuation_capitalization_config.yaml",">","`","_","trainer",":","an","instance","of","a","PyTorch","Lightning","trainer"]
docstr: A method called by :meth:`validation_step` and :meth:`test_step`. Performs forward pass and updates metrics.
  
  Args:
      batch (:obj:`Dict[str, torch.Tensor]`): a dictionary with following items:
  
          - ``'input_ids'`` (:obj:`torch.Tensor`): an integer torch tensor of shape ``[Batch, Time]`` containing
            encoded source text.
          - ``'subtokens_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. An
            element of this item is ``True`` if corresponding token from ``'input_ids'`` element is the first
            token in some word.
          - ``'segment_ids'`` (:obj:`torch.Tensor`): a zeros integer torch tensor of shape ``[Batch, Time]``.
          - ``'input_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. Serves as
            attention mask. should be ``False`` on padding tokens and ``True`` on other tokens.
          - ``'loss_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. Which token
            to compute loss on. See more details in description of parameters ``ignore_start_end`` and
            ``ignore_extra_tokens`` of class
            :class:`~nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset.BertPunctuationCapitalizationDataset`.
          - ``'punct_labels'`` (:obj:`torch.Tensor`): a long torch tensor of shape ``[Batch, Time]``. Contains
            encoded punctuation labels.
          - ``'capit_labels'`` (:obj:`torch.Tensor`): a long torch tensor of shape ``[Batch, Time]``. Contains
            encoded capitalization labels.
      mode: either ``'validation'`` or ``'test'`` depending on caller method.
      dataloader_idx: NeMo parameter for multi dataset validation.
  
  Returns:
      :obj:`Dict[str, None]`: a dictionary containing items ``'loss'``, ``'punct_class_report'``,
      ``'capit_class_report'`` which values are ``None``. Values are ``None`` because metrics are computed using
      ``torchmetrics``.
docstr_tok: ["A","method","called","by",":","meth",":","`","validation_step","`","and",":","meth",":","`","test_step","`",".","Performs","forward","pass","and","updates","metrics",".","Args",":","batch","(",":","obj",":","`","Dict","[","str",",","torch.Tensor","]","`",")",":","a","dictionary","with","following","items",":","-","``","'input_ids","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","an","integer","torch","tensor","of","shape","``","[","Batch",",","Time","]","``","containing","encoded","source","text",".","-","``","'subtokens_mask","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","boolean","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","An","element","of","this","item","is","``","True","``","if","corresponding","token","from","``","'input_ids","'","``","element","is","the","first","token","in","some","word",".","-","``","'segment_ids","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","zeros","integer","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","-","``","'input_mask","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","boolean","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","Serves","as","attention","mask",".","should","be","``","False","``","on","padding","tokens","and","``","True","``","on","other","tokens",".","-","``","'loss_mask","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","boolean","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","Which","token","to","compute","loss","on",".","See","more","details","in","description","of","parameters","``","ignore_start_end","``","and","``","ignore_extra_tokens","``","of","class",":","class",":","`","~nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset.BertPunctuationCapitalizationDataset","`",".","-","``","'punct_labels","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","long","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","Contains","encoded","punctuation","labels",".","-","``","'capit_labels","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","a","long","torch","tensor","of","shape","``","[","Batch",",","Time","]","``",".","Contains","encoded","capitalization","labels",".","mode",":","either","``","'validation","'","``","or","``","'test","'","``","depending","on","caller","method",".","dataloader_idx",":","NeMo","parameter","for","multi","dataset","validation",".","Returns",":",":","obj",":","`","Dict","[","str",",","None","]","`",":","a","dictionary","containing","items","``","'loss","'","``",",","``","'punct_class_report","'","``",",","``","'capit_class_report","'","``","which","values","are","``","None","``",".","Values","are","``","None","``","because","metrics","are","computed","using","``","torchmetrics","``","."]
code: def eval_step(
      self,
      batch: Dict[str, torch.Tensor],
      mode: str,
      dataloader_idx: int,
  ) -> Dict[str, None]:
      """
      A method called by :meth:`validation_step` and :meth:`test_step`. Performs forward pass and updates metrics.
  
      Args:
          batch (:obj:`Dict[str, torch.Tensor]`): a dictionary with following items:
  
              - ``'input_ids'`` (:obj:`torch.Tensor`): an integer torch tensor of shape ``[Batch, Time]`` containing
                encoded source text.
              - ``'subtokens_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. An
                element of this item is ``True`` if corresponding token from ``'input_ids'`` element is the first
                token in some word.
              - ``'segment_ids'`` (:obj:`torch.Tensor`): a zeros integer torch tensor of shape ``[Batch, Time]``.
              - ``'input_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. Serves as
                attention mask. should be ``False`` on padding tokens and ``True`` on other tokens.
              - ``'loss_mask'`` (:obj:`torch.Tensor`): a boolean torch tensor of shape ``[Batch, Time]``. Which token
                to compute loss on. See more details in description of parameters ``ignore_start_end`` and
                ``ignore_extra_tokens`` of class
                :class:`~nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset.BertPunctuationCapitalizationDataset`.
              - ``'punct_labels'`` (:obj:`torch.Tensor`): a long torch tensor of shape ``[Batch, Time]``. Contains
                encoded punctuation labels.
              - ``'capit_labels'`` (:obj:`torch.Tensor`): a long torch tensor of shape ``[Batch, Time]``. Contains
                encoded capitalization labels.
          mode: either ``'validation'`` or ``'test'`` depending on caller method.
          dataloader_idx: NeMo parameter for multi dataset validation.
  
      Returns:
          :obj:`Dict[str, None]`: a dictionary containing items ``'loss'``, ``'punct_class_report'``,
          ``'capit_class_report'`` which values are ``None``. Values are ``None`` because metrics are computed using
          ``torchmetrics``.
      """
      loss, punct_logits, capit_logits = self._make_step(batch)
      subtokens_mask = batch["subtokens_mask"]
      punct_preds = torch.argmax(punct_logits, axis=-1)[subtokens_mask]
      punct_labels = batch["punct_labels"][subtokens_mask]
      capit_preds = torch.argmax(capit_logits, axis=-1)[subtokens_mask]
      capit_labels = batch["capit_labels"][subtokens_mask]
      self.metrics[mode]["loss"][dataloader_idx](
          loss=loss,
          num_measurements=batch["loss_mask"].sum().to(loss.device),
      )
      self.metrics[mode]["punct_class_report"][dataloader_idx](
          punct_preds, punct_labels
      )
      self.metrics[mode]["capit_class_report"][dataloader_idx](
          capit_preds, capit_labels
      )
      # torchmetrics are used for metrics computation
      return {
          "loss": None,
          "punct_class_report": None,
          "capit_class_report": None,
      }
  
imports: import copy
  import warnings
  from math import ceil
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from tqdm import tqdm
  from nemo.collections.common.losses import AggregatorLoss, CrossEntropyLoss
  from nemo.collections.common.metrics import GlobalAverageLossMetric
  from nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset import BertPunctuationCapitalizationDataset, PunctuationCapitalizationEvalDataConfig, PunctuationCapitalizationTrainDataConfig, load_label_ids, raise_not_equal_labels_error
  from nemo.collections.nlp.data.token_classification.punctuation_capitalization_infer_dataset import BertPunctuationCapitalizationInferDataset
  from nemo.collections.nlp.data.token_classification.punctuation_capitalization_tarred_dataset import BertPunctuationCapitalizationTarredDataset
  from nemo.collections.nlp.metrics.classification_report import ClassificationReport
  from nemo.collections.nlp.models.nlp_model import NLPModel
  from nemo.collections.nlp.models.token_classification.punctuation_capitalization_config import is_legacy_model_config, legacy_model_config_to_new_model_config
  from nemo.collections.nlp.modules.common import TokenClassifier
  from nemo.core.classes.common import PretrainedModelInfo, typecheck
  from nemo.core.classes.exportable import Exportable
  from nemo.core.neural_types import LogitsType, NeuralType
  from nemo.utils import logging
label_desc: Use image node_id 6 for calling the PunctuationCapitalizationModel obj's underlying member method code with example usage: obj.eval_step(batch, mode, dataloader_idx) and returns: dict