code: def calc_ppl(self, texts):
      with torch.no_grad():
          text = " ".join(texts)
          eval_loss = []
          input_ids = torch.tensor(
              self.ppl_tokenizer.encode(text, add_special_tokens=True)
          ).unsqueeze(0)
          # Strided perplexity calculation from huggingface.co/transformers/perplexity.html
          for i in range(0, input_ids.size(1), self.stride):
              begin_loc = max(i + self.stride - self.max_length, 0)
              end_loc = min(i + self.stride, input_ids.size(1))
              trg_len = end_loc - i
              input_ids_t = input_ids[:, begin_loc:end_loc].to(
                  textattack.shared.utils.device
              )
              target_ids = input_ids_t.clone()
              target_ids[:, :-trg_len] = -100
  
              outputs = self.ppl_model(input_ids_t, labels=target_ids)
              log_likelihood = outputs[0] * trg_len
  
              eval_loss.append(log_likelihood)
  
      return torch.exp(torch.stack(eval_loss).sum() / end_loc).item()
  
file_path: TextAttack/textattack/metrics/quality_metrics/perplexity.py
imports: import torch
  from textattack.attack_results import FailedAttackResult, SkippedAttackResult
  from textattack.metrics import Metric
  import textattack.shared.utils
label_desc: Use image node_id 3 for calling the Perplexity obj's underlying member method code with example usage: obj.calc_ppl(texts) and returns: torch