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: NOT IMPLEMENTED - no need to implement `TracInCPFastRandProj.self_influence`,
  as `TracInCPFast.self_influence` is sufficient - the latter does not benefit
  from random projections, since no quantities associated with a training
  example are stored (other than its self influence score)
  
  Computes self influence scores for a single batch or a Pytorch `DataLoader`
  that yields batches. Note that if `inputs` is a single batch, this
  will call `model` on that single batch, and if `inputs` yields
  batches, this will call `model` on each batch that is yielded. Therefore,
  please ensure that for both cases, the batch(es) that `model` is called
  with are not too large, so that there will not be an out-of-memory error.
  
  Args:
      inputs (tuple or DataLoader): Either a single tuple of any, or a
              `DataLoader`, where each batch yielded is a tuple of any. In
              either case, the tuple represents a single batch, 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 documentation for the
              `train_dataset` argument to `TracInCP.__init__` for
              more details on the assumed structure of a batch.
      show_progress (bool, optional): Computation of self influence scores can
              take a long time if `inputs` represents many examples. If
              `show_progress` is true, the progress of this computation will be
              displayed. In more detail, this computation will iterate over all
              checkpoints (provided as the `checkpoints` initialization argument)
              and all batches that `inputs` represents. Therefore, the
              total number of (checkpoint, batch) combinations that need to be
              iterated over is
              (# of checkpoints x # of batches that `inputs` represents).
              If `show_progress` is True, the total number of such combinations
              that have been iterated over is 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
      outer_loop_by_checkpoints (bool, optional): If performing an outer
              iteration over checkpoints; see method description for more
              details.
              Default: False
  
  Returns:
      self_influence_scores (Tensor): This is a 1D tensor containing the self
              influence scores of all examples in `inputs`, regardless of
              whether it represents a single batch or a `DataLoader` that yields
              batches.
docstr_tok: ["NOT","IMPLEMENTED","-","no","need","to","implement","`","TracInCPFastRandProj.self_influence","`",",","as","`","TracInCPFast.self_influence","`","is","sufficient","-","the","latter","does","not","benefit","from","random","projections",",","since","no","quantities","associated","with","a","training","example","are","stored","(","other","than","its","self","influence","score",")","Computes","self","influence","scores","for","a","single","batch","or","a","Pytorch","`","DataLoader","`","that","yields","batches",".","Note","that","if","`","inputs","`","is","a","single","batch",",","this","will","call","`","model","`","on","that","single","batch",",","and","if","`","inputs","`","yields","batches",",","this","will","call","`","model","`","on","each","batch","that","is","yielded",".","Therefore",",","please","ensure","that","for","both","cases",",","the","batch","(","es",")","that","`","model","`","is","called","with","are","not","too","large",",","so","that","there","will","not","be","an","out-of-memory","error",".","Args",":","inputs","(","tuple","or","DataLoader",")",":","Either","a","single","tuple","of","any",",","or","a","`","DataLoader","`",",","where","each","batch","yielded","is","a","tuple","of","any",".","In","either","case",",","the","tuple","represents","a","single","batch",",","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","documentation","for","the","`","train_dataset","`","argument","to","`","TracInCP.__init__","`","for","more","details","on","the","assumed","structure","of","a","batch",".","show_progress","(","bool",",","optional",")",":","Computation","of","self","influence","scores","can","take","a","long","time","if","`","inputs","`","represents","many","examples",".","If","`","show_progress","`","is","true",",","the","progress","of","this","computation","will","be","displayed",".","In","more","detail",",","this","computation","will","iterate","over","all","checkpoints","(","provided","as","the","`","checkpoints","`","initialization","argument",")","and","all","batches","that","`","inputs","`","represents",".","Therefore",",","the","total","number","of","(","checkpoint",",","batch",")","combinations","that","need","to","be","iterated","over","is","(","#","of","checkpoints","x","#","of","batches","that","`","inputs","`","represents",")",".","If","`","show_progress","`","is","True",",","the","total","number","of","such","combinations","that","have","been","iterated","over","is","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","outer_loop_by_checkpoints","(","bool",",","optional",")",":","If","performing","an","outer","iteration","over","checkpoints",";","see","method","description","for","more","details",".","Default",":","False","Returns",":","self_influence_scores","(","Tensor",")",":","This","is","a","1D","tensor","containing","the","self","influence","scores","of","all","examples","in","`","inputs","`",",","regardless","of","whether","it","represents","a","single","batch","or","a","`","DataLoader","`","that","yields","batches","."]
code: def self_influence(
      self,
      inputs: Optional[Union[Tuple[Any, ...], DataLoader]] = None,
      show_progress: bool = False,
      outer_loop_by_checkpoints: bool = False,
  ) -> Tensor:
      """
      NOT IMPLEMENTED - no need to implement `TracInCPFastRandProj.self_influence`,
      as `TracInCPFast.self_influence` is sufficient - the latter does not benefit
      from random projections, since no quantities associated with a training
      example are stored (other than its self influence score)
  
      Computes self influence scores for a single batch or a Pytorch `DataLoader`
      that yields batches. Note that if `inputs` is a single batch, this
      will call `model` on that single batch, and if `inputs` yields
      batches, this will call `model` on each batch that is yielded. Therefore,
      please ensure that for both cases, the batch(es) that `model` is called
      with are not too large, so that there will not be an out-of-memory error.
  
      Args:
          inputs (tuple or DataLoader): Either a single tuple of any, or a
                  `DataLoader`, where each batch yielded is a tuple of any. In
                  either case, the tuple represents a single batch, 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 documentation for the
                  `train_dataset` argument to `TracInCP.__init__` for
                  more details on the assumed structure of a batch.
          show_progress (bool, optional): Computation of self influence scores can
                  take a long time if `inputs` represents many examples. If
                  `show_progress` is true, the progress of this computation will be
                  displayed. In more detail, this computation will iterate over all
                  checkpoints (provided as the `checkpoints` initialization argument)
                  and all batches that `inputs` represents. Therefore, the
                  total number of (checkpoint, batch) combinations that need to be
                  iterated over is
                  (# of checkpoints x # of batches that `inputs` represents).
                  If `show_progress` is True, the total number of such combinations
                  that have been iterated over is 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
          outer_loop_by_checkpoints (bool, optional): If performing an outer
                  iteration over checkpoints; see method description for more
                  details.
                  Default: False
  
      Returns:
          self_influence_scores (Tensor): This is a 1D tensor containing the self
                  influence scores of all examples in `inputs`, regardless of
                  whether it represents a single batch or a `DataLoader` that yields
                  batches.
      """
      warnings.warn(
          (
              "WARNING: If calculating self influence scores, when only considering "
              "gradients with respect to the last fully-connected layer, "
              "`TracInCPFastRandProj` should not be used. Instead, please use "
              "`TracInCPFast`. This is because when calculating self influence "
              "scores, no quantities associated with a training example are stored "
              "so that memory-saving benefit of the random projections used by "
              "`TracInCPFastRandProj`needed. Further considering the fact that "
              "random projections results only in approximate self influence "
              "scores, there is no reason to use `TracInCPFastRandProj` when "
              "calculating self influence scores."
          )
      )
      raise NotImplementedError
  
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 4 for calling the TracInCPFastRandProj obj's underlying member method code with example usage: obj.self_influence(inputs, show_progress, outer_loop_by_checkpoints) without return types