class_docstr: Model wrapper around HuggingFace AutoModel models.
  
  Args:
      path: The path to a HuggingFace model.
      model_max_length: The maximum sequence length of the model.
      tokenizer_path: The path to the tokenizer.
      tokenizer_kwargs: Keyword arguments for the tokenizer.
      peft_path: The name or path to the HuggingFace's PEFT model.
      model_kwargs: Keyword arguments for the model.
      prompt_template: The model's prompt template.
      batch_size: Batch size for inference.
      logger: Logger for the model.
      shard_config: Shard config for tensor parallel.
class_docstr_tok: ["Model","wrapper","around","HuggingFace","AutoModel","models",".","Args",":","path",":","The","path","to","a","HuggingFace","model",".","model_max_length",":","The","maximum","sequence","length","of","the","model",".","tokenizer_path",":","The","path","to","the","tokenizer",".","tokenizer_kwargs",":","Keyword","arguments","for","the","tokenizer",".","peft_path",":","The","name","or","path","to","the","HuggingFace","'s","PEFT","model",".","model_kwargs",":","Keyword","arguments","for","the","model",".","prompt_template",":","The","model","'s","prompt","template",".","batch_size",":","Batch","size","for","inference",".","logger",":","Logger","for","the","model",".","shard_config",":","Shard","config","for","tensor","parallel","."]
docstr: Calculate loss only on target tokens.
  Hugging Face generate() function can't return per sample loss.
  It will only return the mean of the loss in a batch.
  In torch.nn.CrossEntropyLoss(), reduction should be specified as "none" to get per sample loss.
  
  Args:
      input_ids_list: A batch of input token ids.
      labels: A batch of labels.
  
  Returns:
      A list of loss.
docstr_tok: ["Calculate","loss","only","on","target","tokens",".","Hugging","Face","generate","(",")","function","ca","n't","return","per","sample","loss",".","It","will","only","return","the","mean","of","the","loss","in","a","batch",".","In","torch.nn.CrossEntropyLoss","(",")",",","reduction","should","be","specified","as","``","none","''","to","get","per","sample","loss",".","Args",":","input_ids_list",":","A","batch","of","input","token","ids",".","labels",":","A","batch","of","labels",".","Returns",":","A","list","of","loss","."]
code: def _calculate_loss(
      self,
      input_ids_list: List[torch.LongTensor],
      labels: List[torch.LongTensor],
  ) -> Tuple[List]:
      """
      Calculate loss only on target tokens.
      Hugging Face generate() function can't return per sample loss.
      It will only return the mean of the loss in a batch.
      In torch.nn.CrossEntropyLoss(), reduction should be specified as "none" to get per sample loss.
  
      Args:
          input_ids_list: A batch of input token ids.
          labels: A batch of labels.
  
      Returns:
          A list of loss.
  
      """
      input_ids = torch.nn.utils.rnn.pad_sequence(
          input_ids_list,
          batch_first=True,
          padding_value=self.tokenizer.pad_token_id,
      ).to(torch.cuda.current_device())
      labels = torch.nn.utils.rnn.pad_sequence(
          labels, batch_first=True, padding_value=IGNORE_INDEX
      ).to(torch.cuda.current_device())
      attention_mask = input_ids.ne(self.tokenizer.pad_token_id).to(
          torch.cuda.current_device()
      )
  
      outputs = self.model(input_ids, attention_mask=attention_mask)[0]
  
      shift_logits = outputs[..., :-1, :].contiguous()
      shift_labels = labels[..., 1:].contiguous()
  
      loss_fct = torch.nn.CrossEntropyLoss(
          reduction="none", ignore_index=IGNORE_INDEX
      )
      loss = loss_fct(
          shift_logits.view(-1, shift_logits.size(-1)),
          shift_labels.view(-1),
      ).view(shift_labels.size())
  
      lens = (labels[..., 1:] != IGNORE_INDEX).sum(-1).cpu().numpy()
  
      loss_sum = loss.sum(-1).to(torch.float32).cpu().detach().numpy()
      return loss_sum.tolist(), lens.tolist()
  
imports: import copy
  import math
  from typing import Any, Dict, List, Optional, Tuple
  import numpy
  import torch
  from colossal_eval.utils import Conversation, get_batch_prompt, is_rank_0
  from peft import PeftModel
  from tqdm import tqdm
  from transformers import AutoConfig, AutoModel, AutoModelForCausalLM, AutoTokenizer
  from colossalai.logging import DistributedLogger
  from colossalai.shardformer import ShardConfig, ShardFormer
  from .base import BaseModel
label_desc: Use image node_id 5 for calling the HuggingFaceModel obj's underlying member method code with example usage: obj._calculate_loss(input_ids_list, labels) and returns: loss_sum,  lens