class_docstr: Attribution class for large language models. It wraps a perturbation-based
  attribution algorthm to produce commonly interested attribution
  results for the use case of text generation.
  The wrapped instance will calculate attribution in the
  same way as configured in the original attribution algorthm, but it will provide a
  new "attribute" function which accepts text-based inputs
  and returns LLMAttributionResult
class_docstr_tok: ["Attribution","class","for","large","language","models",".","It","wraps","a","perturbation-based","attribution","algorthm","to","produce","commonly","interested","attribution","results","for","the","use","case","of","text","generation",".","The","wrapped","instance","will","calculate","attribution","in","the","same","way","as","configured","in","the","original","attribution","algorthm",",","but","it","will","provide","a","new","``","attribute","''","function","which","accepts","text-based","inputs","and","returns","LLMAttributionResult"]
docstr: Args:
      inp (InterpretableInput): input prompt for which attributions are computed
      target (str or Tensor, optional): target response with respect to
              which attributions are computed. If None, it uses the model
              to generate the target based on the input and gen_args.
              Default: None
      num_trials (int, optional): number of trials to run. Return is the average
              attribibutions over all the trials.
              Defaults: 1.
      gen_args (dict, optional): arguments for generating the target. Only used if
              target is not given. When None, the default arguments are used,
              {"max_length": 25, "do_sample": False}
              Defaults: None
      **kwargs (Any): any extra keyword arguments passed to the call of the
              underlying attribute function of the given attribution instance
  
  Returns:
  
      attr (LLMAttributionResult): Attribution result. token_attr will be None
              if attr method is Lime or KernelShap.
docstr_tok: ["Args",":","inp","(","InterpretableInput",")",":","input","prompt","for","which","attributions","are","computed","target","(","str","or","Tensor",",","optional",")",":","target","response","with","respect","to","which","attributions","are","computed",".","If","None",",","it","uses","the","model","to","generate","the","target","based","on","the","input","and","gen_args",".","Default",":","None","num_trials","(","int",",","optional",")",":","number","of","trials","to","run",".","Return","is","the","average","attribibutions","over","all","the","trials",".","Defaults",":","1.","gen_args","(","dict",",","optional",")",":","arguments","for","generating","the","target",".","Only","used","if","target","is","not","given",".","When","None",",","the","default","arguments","are","used",",","{","``","max_length","''",":","25",",","``","do_sample","''",":","False","}","Defaults",":","None","*","*","kwargs","(","Any",")",":","any","extra","keyword","arguments","passed","to","the","call","of","the","underlying","attribute","function","of","the","given","attribution","instance","Returns",":","attr","(","LLMAttributionResult",")",":","Attribution","result",".","token_attr","will","be","None","if","attr","method","is","Lime","or","KernelShap","."]
code: def attribute(
      self,
      inp: InterpretableInput,
      target: Union[str, torch.Tensor, None] = None,
      num_trials: int = 1,
      gen_args: Optional[Dict] = None,
      use_cached_outputs: bool = True,
      # internal callback hook can be used for logging
      _inspect_forward: Optional[Callable] = None,
      **kwargs,
  ) -> LLMAttributionResult:
      """
      Args:
          inp (InterpretableInput): input prompt for which attributions are computed
          target (str or Tensor, optional): target response with respect to
                  which attributions are computed. If None, it uses the model
                  to generate the target based on the input and gen_args.
                  Default: None
          num_trials (int, optional): number of trials to run. Return is the average
                  attribibutions over all the trials.
                  Defaults: 1.
          gen_args (dict, optional): arguments for generating the target. Only used if
                  target is not given. When None, the default arguments are used,
                  {"max_length": 25, "do_sample": False}
                  Defaults: None
          **kwargs (Any): any extra keyword arguments passed to the call of the
                  underlying attribute function of the given attribution instance
  
      Returns:
  
          attr (LLMAttributionResult): Attribution result. token_attr will be None
                  if attr method is Lime or KernelShap.
      """
  
      assert isinstance(
          inp, self.SUPPORTED_INPUTS
      ), f"LLMAttribution does not support input type {type(inp)}"
  
      if target is None:
          # generate when None
          assert hasattr(self.model, "generate") and callable(
              self.model.generate
          ), (
              "The model does not have recognizable generate function."
              "Target must be given for attribution"
          )
  
          if not gen_args:
              gen_args = DEFAULT_GEN_ARGS
  
          model_inp = self._format_model_input(inp.to_model_input())
          output_tokens = self.model.generate(model_inp, **gen_args)
          target_tokens = output_tokens[0][model_inp.size(1) :]
      else:
          assert (
              gen_args is None
          ), "gen_args must be None when target is given"
  
          if type(target) is str:
              # exclude sos
              target_tokens = self.tokenizer.encode(target)[1:]
              target_tokens = torch.tensor(target_tokens)
          elif type(target) is torch.Tensor:
              target_tokens = target
  
      attr = torch.zeros(
          [
              1 + len(target_tokens)
              if self.include_per_token_attr
              else 1,
              inp.n_itp_features,
          ],
          dtype=torch.float,
          device=self.device,
      )
  
      for _ in range(num_trials):
          attr_input = inp.to_tensor().to(self.device)
  
          cur_attr = self.attr_method.attribute(
              attr_input,
              additional_forward_args=(
                  inp,
                  target_tokens,
                  use_cached_outputs,
                  _inspect_forward,
              ),
              **kwargs,
          )
  
          # temp necessary due to FA & Shapley's different return shape of multi-task
          # FA will flatten output shape internally (n_output_token, n_itp_features)
          # Shapley will keep output shape (batch, n_output_token, n_input_features)
          cur_attr = cur_attr.reshape(attr.shape)
  
          attr += cur_attr
  
      attr = attr / num_trials
  
      attr = inp.format_attr(attr)
  
      return LLMAttributionResult(
          attr[0],
          attr[1:]
          if self.include_per_token_attr
          else None,  # shape(n_output_token, n_input_features)
          inp.values,
          self.tokenizer.convert_ids_to_tokens(target_tokens),
      )
  
imports: from copy import copy
  from typing import Callable, cast, Dict, List, Optional, Union
  import matplotlib.pyplot
  import numpy
  import torch
  from captum.attr._core.feature_ablation import FeatureAblation
  from captum.attr._core.kernel_shap import KernelShap
  from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients
  from captum.attr._core.lime import Lime
  from captum.attr._core.shapley_value import ShapleyValues, ShapleyValueSampling
  from captum.attr._utils.attribution import Attribution
  from captum.attr._utils.interpretable_input import InterpretableInput, TextTemplateInput, TextTokenInput
  from torch import nn, Tensor
label_desc: Use image node_id 4 for calling the LLMAttribution obj's underlying member method code with example usage: obj.attribute(inp, target, num_trials, gen_args, use_cached_outputs, _inspect_forward) and returns: LLMAttributionResult