docstr: Mask and reduce loss based on each sample in batch
  Useful for ranking candidates with the same prompt in batch based on loss
docstr_tok: ["Mask","and","reduce","loss","based","on","each","sample","in","batch","Useful","for","ranking","candidates","with","the","same","prompt","in","batch","based","on","loss"]
code: def mask_and_reduce_loss_per_sample(
      self, loss_mask, unmasked_unreduced_loss
  ):
      """
      Mask and reduce loss based on each sample in batch
      Useful for ranking candidates with the same prompt in batch based on loss
      """
      losses = unmasked_unreduced_loss.float()
      loss_mask = loss_mask.view(-1).float()
      masked_loss = losses.view(-1) * loss_mask
      loss_per_sample = torch.mean(
          masked_loss.view(unmasked_unreduced_loss.size()), dim=-1
      )
      return loss_per_sample
  
imports: import collections
  import copy
  import os
  import random
  from typing import Dict, Optional, Union
  import numpy
  import torch
  from omegaconf import DictConfig
  from pytorch_lightning import Trainer
  from torch.utils.data import DataLoader
  from transformers import AutoModelWithLMHead
  from nemo.collections.nlp.data.dialogue import DialogueGPTClassificationDataset, DialogueSGDDataProcessor
  from nemo.collections.nlp.data.dialogue.data_processor.assistant_data_processor import DialogueAssistantDataProcessor
  from nemo.collections.nlp.data.dialogue.data_processor.design_data_processor import DialogueDesignDataProcessor
  from nemo.collections.nlp.metrics.classification_report import ClassificationReport
  from nemo.collections.nlp.metrics.dialogue_metrics import DialogueClassificationMetrics
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel
  from nemo.collections.nlp.models.language_modeling.megatron_gpt_prompt_learning_model import MegatronGPTPromptLearningModel
  from nemo.collections.nlp.models.nlp_model import NLPModel
  from nemo.collections.nlp.modules.common import VirtualPromptSource, VirtualPromptStyle
  from nemo.collections.nlp.modules.common.text_generation_utils import get_default_sampling_params, megatron_gpt_generate
  from nemo.collections.nlp.modules.common.transformer.text_generation import LengthParam
  from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector
  from nemo.core.classes.common import PretrainedModelInfo
  from nemo.utils import logging
label_desc: Use image node_id 14 for calling the DialogueGPTClassificationModel obj's underlying member method code with example usage: obj.mask_and_reduce_loss_per_sample(loss_mask, unmasked_unreduced_loss) and returns: loss_per_sample