class_docstr: In Appendix F, Page 14 of the TracIn paper, they show that the calculation
  of the influence score of between a test example x' and a training example x,
  can be computed much more quickly than naive back-propagation in the special
  case when considering only gradients in the last fully-connected layer. This class
  computes influence scores for that special case. Note that the computed
  influence scores are exactly the same as when naive back-propagation is used -
  there is no loss in accuracy.
  
  In more detail regarding the influence score computation: let :math`x`
  and :math`\nabla_y f(y)` be the input and output-gradient of the last
  fully-connected layer, respectively, for a training example. Similarly, let
  :math`x'` and :math`\nabla_{y'} f(y')` be the corresponding quantities for
  a test example. Then, the influence score of the training example on the test
  example is 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'))`.
class_docstr_tok: ["In","Appendix","F",",","Page","14","of","the","TracIn","paper",",","they","show","that","the","calculation","of","the","influence","score","of","between","a","test","example","x","'","and","a","training","example","x",",","can","be","computed","much","more","quickly","than","naive","back-propagation","in","the","special","case","when","considering","only","gradients","in","the","last","fully-connected","layer",".","This","class","computes","influence","scores","for","that","special","case",".","Note","that","the","computed","influence","scores","are","exactly","the","same","as","when","naive","back-propagation","is","used","-","there","is","no","loss","in","accuracy",".","In","more","detail","regarding","the","influence","score","computation",":","let",":","math","`","x","`","and",":","math","`","\\nabla_y","f","(","y",")","`","be","the","input","and","output-gradient","of","the","last","fully-connected","layer",",","respectively",",","for","a","training","example",".","Similarly",",","let",":","math","`","x","'","`","and",":","math","`","\\nabla_","{","y","'","}","f","(","y","'",")","`","be","the","corresponding","quantities","for","a","test","example",".","Then",",","the","influence","score","of","the","training","example","on","the","test","example","is","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","'",")",")","`","."]
docstr: Computes the influence of examples in training dataset `train_dataset`
  on the examples in the test batch represented by `inputs`.
  This implementation does not require knowing the number of training examples
  in advance. Instead, the number of training examples is inferred from the
  output of `_basic_computation_tracincp_fast`.
  
  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.
      show_progress (bool, optional): To compute the influence of examples in
              training dataset `train_dataset`, we compute the influence
              of each batch. If `show_progress` is true, the progress of this
              computation will be displayed. In particular, the number of batches
              for which influence has been computed 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:
      influence_scores (Tensor): Influence scores from the `TracInCPFast` method.
      Its shape is `(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`. For example:
      `influence_scores[i][j]` is the influence score for the j-th training
      example to the i-th example in the test batch.
docstr_tok: ["Computes","the","influence","of","examples","in","training","dataset","`","train_dataset","`","on","the","examples","in","the","test","batch","represented","by","`","inputs","`",".","This","implementation","does","not","require","knowing","the","number","of","training","examples","in","advance",".","Instead",",","the","number","of","training","examples","is","inferred","from","the","output","of","`","_basic_computation_tracincp_fast","`",".","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",".","show_progress","(","bool",",","optional",")",":","To","compute","the","influence","of","examples","in","training","dataset","`","train_dataset","`",",","we","compute","the","influence","of","each","batch",".","If","`","show_progress","`","is","true",",","the","progress","of","this","computation","will","be","displayed",".","In","particular",",","the","number","of","batches","for","which","influence","has","been","computed","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",":","influence_scores","(","Tensor",")",":","Influence","scores","from","the","`","TracInCPFast","`","method",".","Its","shape","is","`","(","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","`",".","For","example",":","`","influence_scores","[","i","]","[","j","]","`","is","the","influence","score","for","the","j-th","training","example","to","the","i-th","example","in","the","test","batch","."]
code: def _influence(  # type: ignore[override]
      self,
      inputs: Tuple[Any, ...],
      show_progress: bool = False,
  ) -> Tensor:
      r"""
      Computes the influence of examples in training dataset `train_dataset`
      on the examples in the test batch represented by `inputs`.
      This implementation does not require knowing the number of training examples
      in advance. Instead, the number of training examples is inferred from the
      output of `_basic_computation_tracincp_fast`.
  
      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.
          show_progress (bool, optional): To compute the influence of examples in
                  training dataset `train_dataset`, we compute the influence
                  of each batch. If `show_progress` is true, the progress of this
                  computation will be displayed. In particular, the number of batches
                  for which influence has been computed 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:
          influence_scores (Tensor): Influence scores from the `TracInCPFast` method.
          Its shape is `(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`. For example:
          `influence_scores[i][j]` is the influence score for the j-th training
          example to the i-th example in the test batch.
      """
  
      train_dataloader = self.train_dataloader
  
      if show_progress:
          train_dataloader = progress(
              train_dataloader,
              desc=(
                  f"Using {self.get_name()} to compute "
                  "influence for training batches"
              ),
              total=self.train_dataloader_len,
          )
  
      return torch.cat(
          [
              self._influence_batch_tracincp_fast(inputs, batch)
              for batch in train_dataloader
          ],
          dim=1,
      )
  
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 TracInCPFast obj's underlying member method code with example usage: obj._influence(inputs, show_progress) and returns: torch