class_docstr: This is a computationally-efficient implementation that computes the type of
  "infinitesimal" influence scored defined in the paper "Understanding Black-box
  Predictions via Influence Functions" by Koh et al
  (https://arxiv.org/pdf/1703.04730.pdf). This implementation does *not* follow
  the approach in that paper, however. Instead, it follows an implementation that is
  several orders of magnitudes faster, described in the paper "Scaling Up Influence
  Functions" by Schioppa et al (https://arxiv.org/pdf/2112.03052.pdf).
  
  This implementation computes a low-rank approximation of the inverse Hessian, i.e.
  a tall and skinny (with width k) matrix :math`R` such that
  :math`H^{-1} \approx RR'`, where k is small. In particular, let :math`V` be the
  matrix of width k whose columns contain the top-k eigenvectors of :math`H`, and let
  :math`S` be the k by k matrix whose diagonals contain the corresponding eigenvalues.
  This implementation lets :math`R=VS^{-0.5}`. Thus, the core computational step is
  computing the top-k eigenvalues / eigenvectors.
  
  This approximation is useful for several reasons:
  - It avoids numerical issues associated with inverting small eigenvalues
  - Since the influence score is given by
    :math`\nabla_\theta L(x)' H^{-1} \nabla_\theta L(z)`, which is approximated by
    :math`(\nabla_\theta L(x)' R) (\nabla_\theta L(z)' R)`, we can compute an
    "influence embedding" for a given example :math`x`, :math`\nabla_\theta L(x)' R`,
    such that the influence score of one example on another is approximately the
    dot-product of their respective embeddings.
  - Even for large models, we can store `R` in memory, provided k is small. This
    means influence embeddings (and thus influence scores) can be efficiently
    computed by doing a backwards pass to compute :math`\nabla_\theta L(x)` and then
    multiplying by :math`R'`. This is orders of magnitude faster than the previous
    LISSA approach of Koh et al, which to compute the influence score involving a
    given example, need to compute Hessian-vector products involving on the order
    of 10^4 examples.
  
  The key novelty of the approach by Schioppa et al is that it uses the Arnoldi
  iteration to find the top-k eigenvalues / eigenvectors of the Hessian without
  explicitly forming the Hessian. In more detail, the approach first runs the
  Arnoldi iteration, which only requires the ability to compute Hessian-vector
  products, to find a Krylov subspace of moderate dimension, i.e. 200. It then finds
  the top-k eigevalues / eigenvectors of the restriction of the Hessian to the
  subspace, where k is small, i.e. 50. Finally, it expresses the eigenvectors in
  the original basis. This approach for finding the top-k eigenvalues / eigenvectors
  is justified by the property of the Arnoldi iteration, that the Krylov subspace
  it returns tends to contain the top eigenvectors.
  
  This implementation require some computation time `__init__`, where it
  runs the Arnoldi iteration to calculate `R`. This computation is linear in
  `arnoldi_dim` as well as the size of `hessian_dataset`. After that initial
  overhead, calculation of influence scores is quick, only requiring a backwards pass
  and multiplication, per example.
  
  Unlike `NaiveInfluenceFunction`, this implementation does not flatten any
  parameters, as the 2D Hessian is never formed, and Pytorch's Hessian-vector
  implementation (`torch.autograd.functional.hvp`) allows the input and output
  vector to be a tuple of tensors. Avoiding flattening / unflattening parameters
  brings scalability gains.
docstr: This is the key method of this class, and can be run in 2 different modes,
  where the mode that is run depends on the arguments passed to this method:
  
  - influence score mode: This mode is used if `k` is None. This mode computes
    the influence score of every example in training dataset `train_dataset`
    on every example in the test dataset represented by `inputs`.
  - k-most influential mode: This mode is used if `k` is not None, and an int.
    This mode computes the proponents or opponents of every example in the
    test dataset represented by `inputs`. In particular, for each test example in
    the test dataset, this mode computes its proponents (resp. opponents),
    which are the indices in the training dataset `train_dataset` of the
    training examples with the `k` highest (resp. lowest) influence scores on the
    test example. Proponents are computed if `proponents` is True. Otherwise,
    opponents are computed. For each test example, this method also returns the
    actual influence score of each proponent (resp. opponent) on the test
    example.
  
  Args:
  
      inputs (tuple): `inputs` is the test batch and is a tuple of
              any, where the last element is assumed to be the labels for the
              batch. That is, `model(*batch[0:-1])` produces the output for
              `model`, and `batch[-1]` are the labels, if any. This is the same
              assumption made for each batch yielded by training dataset
              `train_dataset` - please see its documentation in `__init__` for
              more details on the assumed structure of a batch.
      k (int, optional): If not provided or `None`, the influence score mode will
              be run. Otherwise, the k-most influential mode will be run,
              and `k` is the number of proponents / opponents to return per
              example in the test dataset.
              Default: None
      proponents (bool, optional): Whether seeking proponents (`proponents=True`)
              or opponents (`proponents=False`), if running in k-most influential
              mode.
              Default: True
      show_progress (bool, optional): For all modes, computation of results
              requires "training dataset computations": computations for each
              batch in the training dataset `train_dataset`, which may
              take a long time. If `show_progress` is true, the progress of
              "training dataset computations" will be displayed. In particular,
              the number of batches for which computations have been performed
              will be displayed. It will try to use tqdm if available for
              advanced features (e.g. time estimation). Otherwise, it will
              fallback to a simple output of progress.
              Default: False
  
  Returns:
      The return value of this method depends on which mode is run.
  
      - influence score mode: if this mode is run (`k` is None), returns a 2D
        tensor `influence_scores` of shape `(input_size, train_dataset_size)`,
        where `input_size` is the number of examples in the test dataset, and
        `train_dataset_size` is the number of examples in training dataset
        `train_dataset`. In other words, `influence_scores[i][j]` is the
        influence score of the `j`-th example in `train_dataset` on the `i`-th
        example in the test dataset.
      - k-most influential mode: if this mode is run (`k` is an int), returns
        a namedtuple `(indices, influence_scores)`. `indices` is a 2D tensor of
        shape `(input_size, k)`, where `input_size` is the number of examples in
        the test dataset. If computing proponents (resp. opponents),
        `indices[i][j]` is the index in training dataset `train_dataset` of the
        example with the `j`-th highest (resp. lowest) influence score (out of
        the examples in `train_dataset`) on the `i`-th example in the test
        dataset. `influence_scores` contains the corresponding influence scores.
        In particular, `influence_scores[i][j]` is the influence score of example
        `indices[i][j]` in `train_dataset` on example `i` in the test dataset
        represented by `inputs`.
docstr_tok: ["This","is","the","key","method","of","this","class",",","and","can","be","run","in","2","different","modes",",","where","the","mode","that","is","run","depends","on","the","arguments","passed","to","this","method",":","-","influence","score","mode",":","This","mode","is","used","if","`","k","`","is","None",".","This","mode","computes","the","influence","score","of","every","example","in","training","dataset","`","train_dataset","`","on","every","example","in","the","test","dataset","represented","by","`","inputs","`",".","-","k-most","influential","mode",":","This","mode","is","used","if","`","k","`","is","not","None",",","and","an","int",".","This","mode","computes","the","proponents","or","opponents","of","every","example","in","the","test","dataset","represented","by","`","inputs","`",".","In","particular",",","for","each","test","example","in","the","test","dataset",",","this","mode","computes","its","proponents","(","resp",".","opponents",")",",","which","are","the","indices","in","the","training","dataset","`","train_dataset","`","of","the","training","examples","with","the","`","k","`","highest","(","resp",".","lowest",")","influence","scores","on","the","test","example",".","Proponents","are","computed","if","`","proponents","`","is","True",".","Otherwise",",","opponents","are","computed",".","For","each","test","example",",","this","method","also","returns","the","actual","influence","score","of","each","proponent","(","resp",".","opponent",")","on","the","test","example",".","Args",":","inputs","(","tuple",")",":","`","inputs","`","is","the","test","batch","and","is","a","tuple","of","any",",","where","the","last","element","is","assumed","to","be","the","labels","for","the","batch",".","That","is",",","`","model","(","*","batch","[","0",":","-1","]",")","`","produces","the","output","for","`","model","`",",","and","`","batch","[","-1","]","`","are","the","labels",",","if","any",".","This","is","the","same","assumption","made","for","each","batch","yielded","by","training","dataset","`","train_dataset","`","-","please","see","its","documentation","in","`","__init__","`","for","more","details","on","the","assumed","structure","of","a","batch",".","k","(","int",",","optional",")",":","If","not","provided","or","`","None","`",",","the","influence","score","mode","will","be","run",".","Otherwise",",","the","k-most","influential","mode","will","be","run",",","and","`","k","`","is","the","number","of","proponents","\/","opponents","to","return","per","example","in","the","test","dataset",".","Default",":","None","proponents","(","bool",",","optional",")",":","Whether","seeking","proponents","(","`","proponents=True","`",")","or","opponents","(","`","proponents=False","`",")",",","if","running","in","k-most","influential","mode",".","Default",":","True","show_progress","(","bool",",","optional",")",":","For","all","modes",",","computation","of","results","requires","``","training","dataset","computations","''",":","computations","for","each","batch","in","the","training","dataset","`","train_dataset","`",",","which","may","take","a","long","time",".","If","`","show_progress","`","is","true",",","the","progress","of","``","training","dataset","computations","''","will","be","displayed",".","In","particular",",","the","number","of","batches","for","which","computations","have","been","performed","will","be","displayed",".","It","will","try","to","use","tqdm","if","available","for","advanced","features","(","e.g",".","time","estimation",")",".","Otherwise",",","it","will","fallback","to","a","simple","output","of","progress",".","Default",":","False","Returns",":","The","return","value","of","this","method","depends","on","which","mode","is","run",".","-","influence","score","mode",":","if","this","mode","is","run","(","`","k","`","is","None",")",",","returns","a","2D","tensor","`","influence_scores","`","of","shape","`","(","input_size",",","train_dataset_size",")","`",",","where","`","input_size","`","is","the","number","of","examples","in","the","test","dataset",",","and","`","train_dataset_size","`","is","the","number","of","examples","in","training","dataset","`","train_dataset","`",".","In","other","words",",","`","influence_scores","[","i","]","[","j","]","`","is","the","influence","score","of","the","`","j","`","-th","example","in","`","train_dataset","`","on","the","`","i","`","-th","example","in","the","test","dataset",".","-","k-most","influential","mode",":","if","this","mode","is","run","(","`","k","`","is","an","int",")",",","returns","a","namedtuple","`","(","indices",",","influence_scores",")","`",".","`","indices","`","is","a","2D","tensor","of","shape","`","(","input_size",",","k",")","`",",","where","`","input_size","`","is","the","number","of","examples","in","the","test","dataset",".","If","computing","proponents","(","resp",".","opponents",")",",","`","indices","[","i","]","[","j","]","`","is","the","index","in","training","dataset","`","train_dataset","`","of","the","example","with","the","`","j","`","-th","highest","(","resp",".","lowest",")","influence","score","(","out","of","the","examples","in","`","train_dataset","`",")","on","the","`","i","`","-th","example","in","the","test","dataset",".","`","influence_scores","`","contains","the","corresponding","influence","scores",".","In","particular",",","`","influence_scores","[","i","]","[","j","]","`","is","the","influence","score","of","example","`","indices","[","i","]","[","j","]","`","in","`","train_dataset","`","on","example","`","i","`","in","the","test","dataset","represented","by","`","inputs","`","."]
code: def influence(  # type: ignore[override]
      self,
      inputs: Tuple,
      k: Optional[int] = None,
      proponents: bool = True,
      show_progress: bool = False,
  ) -> Union[Tensor, KMostInfluentialResults]:
      """
      This is the key method of this class, and can be run in 2 different modes,
      where the mode that is run depends on the arguments passed to this method:
  
      - influence score mode: This mode is used if `k` is None. This mode computes
        the influence score of every example in training dataset `train_dataset`
        on every example in the test dataset represented by `inputs`.
      - k-most influential mode: This mode is used if `k` is not None, and an int.
        This mode computes the proponents or opponents of every example in the
        test dataset represented by `inputs`. In particular, for each test example in
        the test dataset, this mode computes its proponents (resp. opponents),
        which are the indices in the training dataset `train_dataset` of the
        training examples with the `k` highest (resp. lowest) influence scores on the
        test example. Proponents are computed if `proponents` is True. Otherwise,
        opponents are computed. For each test example, this method also returns the
        actual influence score of each proponent (resp. opponent) on the test
        example.
  
      Args:
  
          inputs (tuple): `inputs` is the test batch and is a tuple of
                  any, where the last element is assumed to be the labels for the
                  batch. That is, `model(*batch[0:-1])` produces the output for
                  `model`, and `batch[-1]` are the labels, if any. This is the same
                  assumption made for each batch yielded by training dataset
                  `train_dataset` - please see its documentation in `__init__` for
                  more details on the assumed structure of a batch.
          k (int, optional): If not provided or `None`, the influence score mode will
                  be run. Otherwise, the k-most influential mode will be run,
                  and `k` is the number of proponents / opponents to return per
                  example in the test dataset.
                  Default: None
          proponents (bool, optional): Whether seeking proponents (`proponents=True`)
                  or opponents (`proponents=False`), if running in k-most influential
                  mode.
                  Default: True
          show_progress (bool, optional): For all modes, computation of results
                  requires "training dataset computations": computations for each
                  batch in the training dataset `train_dataset`, which may
                  take a long time. If `show_progress` is true, the progress of
                  "training dataset computations" will be displayed. In particular,
                  the number of batches for which computations have been performed
                  will be displayed. It will try to use tqdm if available for
                  advanced features (e.g. time estimation). Otherwise, it will
                  fallback to a simple output of progress.
                  Default: False
  
      Returns:
          The return value of this method depends on which mode is run.
  
          - influence score mode: if this mode is run (`k` is None), returns a 2D
            tensor `influence_scores` of shape `(input_size, train_dataset_size)`,
            where `input_size` is the number of examples in the test dataset, and
            `train_dataset_size` is the number of examples in training dataset
            `train_dataset`. In other words, `influence_scores[i][j]` is the
            influence score of the `j`-th example in `train_dataset` on the `i`-th
            example in the test dataset.
          - k-most influential mode: if this mode is run (`k` is an int), returns
            a namedtuple `(indices, influence_scores)`. `indices` is a 2D tensor of
            shape `(input_size, k)`, where `input_size` is the number of examples in
            the test dataset. If computing proponents (resp. opponents),
            `indices[i][j]` is the index in training dataset `train_dataset` of the
            example with the `j`-th highest (resp. lowest) influence score (out of
            the examples in `train_dataset`) on the `i`-th example in the test
            dataset. `influence_scores` contains the corresponding influence scores.
            In particular, `influence_scores[i][j]` is the influence score of example
            `indices[i][j]` in `train_dataset` on example `i` in the test dataset
            represented by `inputs`.
      """
      return _influence_route_to_helpers(
          self,
          inputs,
          k,
          proponents,
          show_progress=show_progress,
      )
  
imports: import functools
  from typing import Any, Callable, List, Optional, Tuple, Union
  import torch
  from captum._utils.gradient import _extract_parameters_from_layers
  from captum.influence._utils.common import _compute_batch_loss_influence_function_base, _compute_jacobian_sample_wise_grads_per_batch, _dataset_fn, _format_inputs_dataset, _functional_call, _get_k_most_influential_helper, _influence_batch_intermediate_quantities_influence_function, _influence_helper_intermediate_quantities_influence_function, _influence_route_to_helpers, _load_flexible_state_dict, _parameter_add, _parameter_dot, _parameter_linear_combination, _parameter_multiply, _parameter_to, _params_to_names, _progress_bar_constructor, _self_influence_helper_intermediate_quantities_influence_function, _top_eigen, KMostInfluentialResults
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader, Dataset
  from tqdm import tqdm
  from .influence_function import _get_dataset_embeddings_intermediate_quantities_influence_function, InfluenceFunctionBase, IntermediateQuantitiesInfluenceFunction
label_desc: Use image node_id 4 for calling the ArnoldiInfluenceFunction obj's underlying member method code with example usage: obj.influence(inputs, k, proponents, show_progress) and returns: _influence_route_to_helpers