class_docstr: Pretraining version with optional prediction head and variant for sparse prediction.
class_docstr_tok: ["Pretraining","version","with","optional","prediction","head","and","variant","for","sparse","prediction","."]
code: def _forward_sparse(
      self, outputs: torch.Tensor, labels: Optional[torch.Tensor] = None
  ):
      labels = labels.view(-1)
      mask_positions = labels.view(-1) != self.loss_fn.ignore_index
      num_masks_guaranteed = round(
          self.sparse_prediction * labels.shape[0]
      )
      # outputs = outputs[mask_positions]  # not allowed as dynamic shape op
      # labels = labels[mask_positions]
      # torch.masked_select(labels, mask_positions)  # not allowed as a dynamic shape operator
  
      # indices = torch.arange(mask_positions.shape[0], device=outputs.device)[mask_positions] # not allowed
      indices = torch.argsort(mask_positions.int())[
          -num_masks_guaranteed:
      ]  # ugh
  
      outputs = outputs[
          indices
      ]  # not allowed as dynamic shape op, but ok with indices
      labels = labels[indices]
      # alternative:
      # outputs = torch.take_along_dim(outputs, indices.view(-1, 1), 0)
      # labels = torch.take(labels, indices)
  
      outputs = self.decoder(self.prediction_head(outputs))
      masked_lm_loss = self.loss_fn(outputs, labels)
      return masked_lm_loss
  
imports: import torch
  from transformers import PretrainedConfig, PreTrainedModel
  from transformers import AutoConfig, AutoModel, AutoModelForMaskedLM, AutoModelForSequenceClassification, AutoModelForTokenClassification
  from typing import Optional
  from omegaconf import OmegaConf
  from .fff import FFF
  from .components import _get_norm_fn, _get_nonlin_fn, EmbeddingComponent, PoolingComponent, PredictionHeadComponent, GLU, get_extended_attention_mask, _init_module
  from .attention import get_attention_mechanism
label_desc: Use image node_id 5 for calling the ScriptableLMForPreTraining obj's underlying member method code with example usage: obj._forward_sparse(outputs, labels) and returns: masked_lm_loss