class_docstr: A version of TracInCPFast which is optimized for "interactive" calls to
  `influence` for the purpose of calculating proponents / opponents, or
  influence scores. "Interactive" means there will be multiple calls to
  `influence`, with each call for a different batch of test examples, and
  subsequent calls rely on the results of previous calls. The implementation in
  this class has been optimized so that each call to `influence` is fast, so that
  it can be used for interactive analysis. This class should only be used for
  interactive use cases. It should not be used if `influence` will only be
  called once, because to enable fast calls to `influence`, time and memory
  intensive preprocessing is required in `__init__`. Furthermore, it should not
  be used to calculate self influence scores - `TracInCPFast` should be used
  instead for that purpose. To enable interactive analysis, this implementation
  computes and saves "embedding" vectors for all training examples in
  `train_dataset`. Crucially, the influence score of a training
  example on a test example is simply the dot-product of their corresponding
  vectors, and proponents / opponents can be found by first storing vectors for
  training examples in a nearest-neighbor data structure, and then finding the
  nearest-neighbors for a test example in terms of dot-product (see appendix F
  of the TracIn paper). This class should only be used if calls to `influence`
  to obtain proponents / opponents or influence scores will be made in an
  "interactive" manner, and there is sufficient memory to store vectors for the
  entire `train_dataset`. This is because in order to enable interactive
  analysis, this implementation incures overhead in `__init__` to setup the
  nearest-neighbors data structure, which is both time and memory intensive, as
  vectors corresponding to all training examples needed to be stored. To reduce
  memory usage, this implementation enables random projections of those vectors.
  Note that the influence scores computed with random projections are less
  accurate, though correct in expectation.
  
  In more detail regarding the "embedding" vectors - the influence of a training
  example on a test example, when only considering gradients in the last
  fully-connected layer, the sum of the contribution from each checkpoint. The
  contribution from a given checkpoint is
  :math`(x^T x')(\nabla_y f(y)^T \nabla_{y'} f(y'))`, using the notation in the
  description of `TracInCPFast`. As is, this is not a dot-product of 2 vectors.
  However, we can rewrite that contribution as
  :math`(x \nabla_y f(y)^T) \dot (x' f(y')^T)`. Both terms in this
  product are 2D matrices, as they are outer products, and the "product" is actually
  a dot-product, treating both matrices as vectors. Therefore, for a given
  checkpoint, its contribution to the "embedding" of an example is just the
  outer-product :math`(x \nabla_y f(y)^T)`, flattened. Furthemore, to reduce the
  dimension of this contribution, we can right-multiply and
  left-multiply the outer-product with two separate projection matrices. These
  transform :math`\nabla_y f(y)` and :math`x` to lower dimensional vectors. While
  the dimension of these two lower dimensional vectors do not necessarily need to
  be the same, in our implementation, we let them be the same, both equal to the
  square root of the desired projection dimension. Finally, the embedding of an
  example is the concatenation of the contributions from each checkpoint.
class_docstr_tok: ["A","version","of","TracInCPFast","which","is","optimized","for","``","interactive","''","calls","to","`","influence","`","for","the","purpose","of","calculating","proponents","\/","opponents",",","or","influence","scores",".","``","Interactive","''","means","there","will","be","multiple","calls","to","`","influence","`",",","with","each","call","for","a","different","batch","of","test","examples",",","and","subsequent","calls","rely","on","the","results","of","previous","calls",".","The","implementation","in","this","class","has","been","optimized","so","that","each","call","to","`","influence","`","is","fast",",","so","that","it","can","be","used","for","interactive","analysis",".","This","class","should","only","be","used","for","interactive","use","cases",".","It","should","not","be","used","if","`","influence","`","will","only","be","called","once",",","because","to","enable","fast","calls","to","`","influence","`",",","time","and","memory","intensive","preprocessing","is","required","in","`","__init__","`",".","Furthermore",",","it","should","not","be","used","to","calculate","self","influence","scores","-","`","TracInCPFast","`","should","be","used","instead","for","that","purpose",".","To","enable","interactive","analysis",",","this","implementation","computes","and","saves","``","embedding","''","vectors","for","all","training","examples","in","`","train_dataset","`",".","Crucially",",","the","influence","score","of","a","training","example","on","a","test","example","is","simply","the","dot-product","of","their","corresponding","vectors",",","and","proponents","\/","opponents","can","be","found","by","first","storing","vectors","for","training","examples","in","a","nearest-neighbor","data","structure",",","and","then","finding","the","nearest-neighbors","for","a","test","example","in","terms","of","dot-product","(","see","appendix","F","of","the","TracIn","paper",")",".","This","class","should","only","be","used","if","calls","to","`","influence","`","to","obtain","proponents","\/","opponents","or","influence","scores","will","be","made","in","an","''","interactive","''","manner",",","and","there","is","sufficient","memory","to","store","vectors","for","the","entire","`","train_dataset","`",".","This","is","because","in","order","to","enable","interactive","analysis",",","this","implementation","incures","overhead","in","`","__init__","`","to","setup","the","nearest-neighbors","data","structure",",","which","is","both","time","and","memory","intensive",",","as","vectors","corresponding","to","all","training","examples","needed","to","be","stored",".","To","reduce","memory","usage",",","this","implementation","enables","random","projections","of","those","vectors",".","Note","that","the","influence","scores","computed","with","random","projections","are","less","accurate",",","though","correct","in","expectation",".","In","more","detail","regarding","the","``","embedding","''","vectors","-","the","influence","of","a","training","example","on","a","test","example",",","when","only","considering","gradients","in","the","last","fully-connected","layer",",","the","sum","of","the","contribution","from","each","checkpoint",".","The","contribution","from","a","given","checkpoint","is",":","math","`","(","x^T","x","'",")","(","\\nabla_y","f","(","y",")","^T","\\nabla_","{","y","'","}","f","(","y","'",")",")","`",",","using","the","notation","in","the","description","of","`","TracInCPFast","`",".","As","is",",","this","is","not","a","dot-product","of","2","vectors",".","However",",","we","can","rewrite","that","contribution","as",":","math","`","(","x","\\nabla_y","f","(","y",")","^T",")","\\dot","(","x","'","f","(","y","'",")","^T",")","`",".","Both","terms","in","this","product","are","2D","matrices",",","as","they","are","outer","products",",","and","the","``","product","''","is","actually","a","dot-product",",","treating","both","matrices","as","vectors",".","Therefore",",","for","a","given","checkpoint",",","its","contribution","to","the","``","embedding","''","of","an","example","is","just","the","outer-product",":","math","`","(","x","\\nabla_y","f","(","y",")","^T",")","`",",","flattened",".","Furthemore",",","to","reduce","the","dimension","of","this","contribution",",","we","can","right-multiply","and","left-multiply","the","outer-product","with","two","separate","projection","matrices",".","These","transform",":","math","`","\\nabla_y","f","(","y",")","`","and",":","math","`","x","`","to","lower","dimensional","vectors",".","While","the","dimension","of","these","two","lower","dimensional","vectors","do","not","necessarily","need","to","be","the","same",",","in","our","implementation",",","we","let","them","be","the","same",",","both","equal","to","the","square","root","of","the","desired","projection","dimension",".","Finally",",","the","embedding","of","an","example","is","the","concatenation","of","the","contributions","from","each","checkpoint","."]
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 batch 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 batch represented by `inputs`. In particular, for each test example in
    the test batch, 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 batch.
              Default: None
      proponents (bool, optional): Whether seeking proponents (`proponents=True`)
              or opponents (`proponents=False`), if running in k-most influential
              mode.
              Default: True
  
  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 batch, 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 batch.
      - 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 batch. 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
        batch. `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 batch
        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","batch","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","batch","represented","by","`","inputs","`",".","In","particular",",","for","each","test","example","in","the","test","batch",",","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","batch",".","Default",":","None","proponents","(","bool",",","optional",")",":","Whether","seeking","proponents","(","`","proponents=True","`",")","or","opponents","(","`","proponents=False","`",")",",","if","running","in","k-most","influential","mode",".","Default",":","True","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","batch",",","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","batch",".","-","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","batch",".","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","batch",".","`","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","batch","represented","by","`","inputs","`","."]
code: def influence(  # type: ignore[override]
      self,
      inputs: Optional[Tuple[Any, ...]] = None,
      k: int = 5,
      proponents: bool = True,
  ) -> Union[Tensor, KMostInfluentialResults]:
      r"""
      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 batch 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 batch represented by `inputs`. In particular, for each test example in
        the test batch, 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 batch.
                  Default: None
          proponents (bool, optional): Whether seeking proponents (`proponents=True`)
                  or opponents (`proponents=False`), if running in k-most influential
                  mode.
                  Default: True
  
      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 batch, 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 batch.
          - 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 batch. 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
            batch. `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 batch
            represented by `inputs`.
      """
      assert inputs is not None, (
          "`inputs` argument is required."
          "`TracInCPFastRandProj` does not support computing self influence scores"
          "Even if it did, one would use the `self_influence` method."
      )
      return _influence_route_to_helpers(
          self,
          inputs,
          k,
          proponents,
      )
  
imports: import threading
  import warnings
  from collections import defaultdict
  from typing import Any, Callable, cast, Dict, Iterator, List, Optional, Tuple, Union
  import torch
  from captum._utils.common import _get_module_from_name, _sort_key_list
  from captum._utils.gradient import _gather_distributed_tensors
  from captum._utils.progress import NullProgress, progress
  from captum.influence._core.tracincp import _influence_route_to_helpers, KMostInfluentialResults, TracInCPBase
  from captum.influence._utils.common import _check_loss_fn, _format_inputs_dataset, _get_k_most_influential_helper, _jacobian_loss_wrt_inputs, _load_flexible_state_dict, _self_influence_by_batches_helper, _tensor_batch_dot
  from captum.influence._utils.nearest_neighbors import AnnoyNearestNeighbors, NearestNeighbors
  from captum.log import log_usage
  from torch import device, Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader, Dataset
label_desc: Use image node_id 5 for calling the TracInCPFastRandProj obj's underlying member method code with example usage: obj.influence(inputs, k, proponents) and returns: _influence_route_to_helpers