class_docstr: Implements MASTER as described in paper: <https://arxiv.org/pdf/1910.02562.pdf>`_.
  Implementation based on the official Pytorch implementation: <https://github.com/wenwenyu/MASTER-pytorch>`_.
  
  Args:
  ----
      feature_extractor: the backbone serving as feature extractor
      vocab: vocabulary, (without EOS, SOS, PAD)
      d_model: d parameter for the transformer decoder
      dff: depth of the pointwise feed-forward layer
      num_heads: number of heads for the mutli-head attention module
      num_layers: number of decoder layers to stack
      max_length: maximum length of character sequence handled by the model
      dropout: dropout probability of the decoder
      input_shape: size of the image inputs
      exportable: onnx exportable returns only logits
      cfg: dictionary containing information about the model
class_docstr_tok: ["Implements","MASTER","as","described","in","paper",":","<","https",":","\/\/arxiv.org\/pdf\/1910.02562.pdf",">","`","_",".","Implementation","based","on","the","official","Pytorch","implementation",":","<","https",":","\/\/github.com\/wenwenyu\/MASTER-pytorch",">","`","_",".","Args",":","--","--","feature_extractor",":","the","backbone","serving","as","feature","extractor","vocab",":","vocabulary",",","(","without","EOS",",","SOS",",","PAD",")","d_model",":","d","parameter","for","the","transformer","decoder","dff",":","depth","of","the","pointwise","feed-forward","layer","num_heads",":","number","of","heads","for","the","mutli-head","attention","module","num_layers",":","number","of","decoder","layers","to","stack","max_length",":","maximum","length","of","character","sequence","handled","by","the","model","dropout",":","dropout","probability","of","the","decoder","input_shape",":","size","of","the","image","inputs","exportable",":","onnx","exportable","returns","only","logits","cfg",":","dictionary","containing","information","about","the","model"]
docstr: Compute categorical cross-entropy loss for the model.
  Sequences are masked after the EOS character.
  
  Args:
  ----
      gt: the encoded tensor with gt labels
      model_output: predicted logits of the model
      seq_len: lengths of each gt word inside the batch
  
  Returns:
  -------
      The loss of the model on the batch
docstr_tok: ["Compute","categorical","cross-entropy","loss","for","the","model",".","Sequences","are","masked","after","the","EOS","character",".","Args",":","--","--","gt",":","the","encoded","tensor","with","gt","labels","model_output",":","predicted","logits","of","the","model","seq_len",":","lengths","of","each","gt","word","inside","the","batch","Returns",":","--","--","--","-","The","loss","of","the","model","on","the","batch"]
code: def compute_loss(
      model_output: torch.Tensor,
      gt: torch.Tensor,
      seq_len: torch.Tensor,
  ) -> torch.Tensor:
      """Compute categorical cross-entropy loss for the model.
      Sequences are masked after the EOS character.
  
      Args:
      ----
          gt: the encoded tensor with gt labels
          model_output: predicted logits of the model
          seq_len: lengths of each gt word inside the batch
  
      Returns:
      -------
          The loss of the model on the batch
      """
      # Input length : number of timesteps
      input_len = model_output.shape[1]
      # Add one for additional <eos> token (sos disappear in shift!)
      seq_len = seq_len + 1  # type: ignore[assignment]
      # Compute loss: don't forget to shift gt! Otherwise the model learns to output the gt[t-1]!
      # The "masked" first gt char is <sos>. Delete last logit of the model output.
      cce = F.cross_entropy(
          model_output[:, :-1, :].permute(0, 2, 1),
          gt[:, 1:],
          reduction="none",
      )
      # Compute mask, remove 1 timestep here as well
      mask_2d = (
          torch.arange(input_len - 1, device=model_output.device)[
              None, :
          ]
          >= seq_len[:, None]
      )
      cce[mask_2d] = 0
  
      ce_loss = cce.sum(1) / seq_len.to(dtype=model_output.dtype)
      return ce_loss.mean()
  
imports: from copy import deepcopy
  from typing import Any, Callable, Dict, List, Optional, Tuple
  import torch
  from torch import nn
  from torch.nn import functional
  from torchvision.models._utils import IntermediateLayerGetter
  from doctr.datasets import VOCABS
  from doctr.models.classification import magc_resnet31
  from doctr.models.modules.transformer import Decoder, PositionalEncoding
  from ...utils.pytorch import _bf16_to_float32, load_pretrained_params
  from .base import _MASTER, _MASTERPostProcessor
label_desc: Use image node_id 3 for calling the MASTER obj's underlying member method code with example usage: obj.compute_loss(model_output, gt, seq_len) and returns: ce_loss