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: Lightning calls this inside the training loop with the data from the training dataloader passed in as
  ``batch``.
  
  Args:
      batch: a dictionary with following
          items:
  
            - ``'input_ids'`` (:obj:`torch.Tensor`): an integer torch tensor of shape ``[Batch, Time]`` containing
              encoded source text
            - ``'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 a 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
            - ``'subtokens_mask'`` (:obj:`torch.Tensor`): not required for training and can be omitted
  
      batch_idx (:obj:`int`): an index of batch. Mandatory Lightning parameter
  
  Returns:
      :obj:`Dict[str, Union[torch.Tensor, float]]`: a dictionary with 2 items:
  
          - ``'loss'`` (:obj:`torch.Tensor`): torch tensor containing mean aggregated punctuation and
            capitalization loss
          - ``'lr'`` (:obj:`float`): a float containing learning rate
docstr_tok: ["Lightning","calls","this","inside","the","training","loop","with","the","data","from","the","training","dataloader","passed","in","as","``","batch","``",".","Args",":","batch",":","a","dictionary","with","following","items",":","-","``","'input_ids","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","an","integer","torch","tensor","of","shape","``","[","Batch",",","Time","]","``","containing","encoded","source","text","-","``","'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","a","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","-","``","'subtokens_mask","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","not","required","for","training","and","can","be","omitted","batch_idx","(",":","obj",":","`","int","`",")",":","an","index","of","batch",".","Mandatory","Lightning","parameter","Returns",":",":","obj",":","`","Dict","[","str",",","Union","[","torch.Tensor",",","float","]","]","`",":","a","dictionary","with","2","items",":","-","``","'loss","'","``","(",":","obj",":","`","torch.Tensor","`",")",":","torch","tensor","containing","mean","aggregated","punctuation","and","capitalization","loss","-","``","'lr","'","``","(",":","obj",":","`","float","`",")",":","a","float","containing","learning","rate"]
code: def training_step(
      self, batch: Dict[str, torch.Tensor], batch_idx: int
  ) -> Dict[str, Union[torch.Tensor, float]]:
      """
      Lightning calls this inside the training loop with the data from the training dataloader passed in as
      ``batch``.
  
      Args:
          batch: a dictionary with following
              items:
  
                - ``'input_ids'`` (:obj:`torch.Tensor`): an integer torch tensor of shape ``[Batch, Time]`` containing
                  encoded source text
                - ``'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 a 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
                - ``'subtokens_mask'`` (:obj:`torch.Tensor`): not required for training and can be omitted
  
          batch_idx (:obj:`int`): an index of batch. Mandatory Lightning parameter
  
      Returns:
          :obj:`Dict[str, Union[torch.Tensor, float]]`: a dictionary with 2 items:
  
              - ``'loss'`` (:obj:`torch.Tensor`): torch tensor containing mean aggregated punctuation and
                capitalization loss
              - ``'lr'`` (:obj:`float`): a float containing learning rate
      """
      loss, _, _ = self._make_step(batch)
      lr = self._optimizer.param_groups[0]["lr"]
      self.log("lr", lr, prog_bar=True)
      self.log("train_loss", loss)
      return {"loss": loss, "lr": lr}
  
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 5 for calling the PunctuationCapitalizationModel obj's underlying member method code with example usage: obj.training_step(batch, batch_idx) and returns: dict