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:
      attr_method (Attribution): Instance of a supported perturbation attribution
              Supported methods include FeatureAblation, ShapleyValueSampling,
              ShapleyValues, Lime, and KernelShap. Lime and KernelShap do not
              support per-token attribution and will only return attribution
              for the full target sequence.
              class created with the llm model that follows huggingface style
              interface convention
      tokenizer (Tokenizer): tokenizer of the llm model used in the attr_method
      attr_target (str): attribute towards log probability or probability.
              Available values ["log_prob", "prob"]
              Default: "log_prob"
docstr_tok: ["Args",":","attr_method","(","Attribution",")",":","Instance","of","a","supported","perturbation","attribution","Supported","methods","include","FeatureAblation",",","ShapleyValueSampling",",","ShapleyValues",",","Lime",",","and","KernelShap",".","Lime","and","KernelShap","do","not","support","per-token","attribution","and","will","only","return","attribution","for","the","full","target","sequence",".","class","created","with","the","llm","model","that","follows","huggingface","style","interface","convention","tokenizer","(","Tokenizer",")",":","tokenizer","of","the","llm","model","used","in","the","attr_method","attr_target","(","str",")",":","attribute","towards","log","probability","or","probability",".","Available","values","[","``","log_prob","''",",","``","prob","''","]","Default",":","``","log_prob","''"]
code: def __init__(
      self,
      attr_method: Attribution,
      tokenizer,
      attr_target: str = "log_prob",  # TODO: support callable attr_target
  ):
      """
      Args:
          attr_method (Attribution): Instance of a supported perturbation attribution
                  Supported methods include FeatureAblation, ShapleyValueSampling,
                  ShapleyValues, Lime, and KernelShap. Lime and KernelShap do not
                  support per-token attribution and will only return attribution
                  for the full target sequence.
                  class created with the llm model that follows huggingface style
                  interface convention
          tokenizer (Tokenizer): tokenizer of the llm model used in the attr_method
          attr_target (str): attribute towards log probability or probability.
                  Available values ["log_prob", "prob"]
                  Default: "log_prob"
      """
  
      assert isinstance(
          attr_method, self.SUPPORTED_METHODS
      ), f"LLMAttribution does not support {type(attr_method)}"
  
      super().__init__(attr_method.forward_func)
  
      # shallow copy is enough to avoid modifying original instance
      self.attr_method = copy(attr_method)
      self.include_per_token_attr = isinstance(
          attr_method, self.SUPPORTED_PER_TOKEN_ATTR_METHODS
      )
  
      self.attr_method.forward_func = self._forward_func
  
      # alias, we really need a model and don't support wrapper functions
      # coz we need call model.forward, model.generate, etc.
      self.model = cast(nn.Module, self.forward_func)
  
      self.tokenizer = tokenizer
      self.device = (
          cast(torch.device, self.model.device)
          if hasattr(self.model, "device")
          else next(self.model.parameters()).device
      )
  
      assert attr_target in (
          "log_prob",
          "prob",
      ), "attr_target should be either 'log_prob' or 'prob'"
      self.attr_target = attr_target
  
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 1 to create a new LLMAttribution object from inherited base classes: Attribution with example: obj = LLMAttribution(attr_method, tokenizer, attr_target)