class_docstr: Attribution class for large language models. It wraps a gradient-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,
  with respect to the log probabilities of each
  generated token and the whole sequence. 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","gradient-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",",","with","respect","to","the","log","probabilities","of","each","generated","token","and","the","whole","sequence",".","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
      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
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","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"]
code: def attribute(
      self,
      inp: InterpretableInput,
      target: Union[str, torch.Tensor, None] = None,
      gen_args: Optional[Dict] = None,
      **kwargs,
  ):
      """
      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
          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
      """
  
      assert isinstance(
          inp, self.SUPPORTED_INPUTS
      ), f"LLMGradAttribution 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_inp = inp.to_tensor().to(self.device)
  
      attr_list = []
      for cur_target_idx, _ in enumerate(target_tokens):
          # attr in shape(batch_size, input+output_len, emb_dim)
          attr = self.attr_method.attribute(
              attr_inp,
              additional_forward_args=(
                  inp,
                  target_tokens,
                  cur_target_idx,
              ),
              **kwargs,
          )
          attr = cast(Tensor, attr)
  
          # will have the attr for previous output tokens
          # cut to shape(batch_size, inp_len, emb_dim)
          if cur_target_idx:
              attr = attr[:, :-cur_target_idx]
  
          # the author of IG uses sum
          # https://github.com/ankurtaly/Integrated-Gradients/blob/master/BertModel/bert_model_utils.py#L350
          attr = attr.sum(-1)
  
          attr_list.append(attr)
  
      # assume inp batch only has one instance
      # to shape(n_output_token, ...)
      attr = torch.cat(attr_list, dim=0)
  
      # grad attr method do not care the length of features in interpretable format
      # it attributes to all the elements of the output of the specified layer
      # so we need special handling for the inp type which don't care all the elements
      if isinstance(inp, TextTokenInput) and inp.itp_mask is not None:
          itp_mask = inp.itp_mask.to(self.device)
          itp_mask = itp_mask.expand_as(attr)
          attr = attr[itp_mask].view(attr.size(0), -1)
  
      # for all the gradient methods we support in this class
      # the seq attr is the sum of all the token attr if the attr_target is log_prob,
      # shape(n_input_features)
      seq_attr = attr.sum(0)
  
      return LLMAttributionResult(
          seq_attr,
          attr,  # 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 LLMGradientAttribution obj's underlying member method code with example usage: obj.attribute(inp, target, gen_args) and returns: LLMAttributionResult