code: def _get_dataset_embeddings_intermediate_quantities_influence_function(
      batch_embeddings_fn: Callable,
      inputs_dataset: DataLoader,
      aggregate: bool,
  ):
      """
      given `batch_embeddings_fn`, which produces the embeddings for a given batch,
      returns either the embeddings for an entire dataset (if `aggregate` is false),
      or the sum of the embeddings for an entire dataset (if `aggregate` is true).
      """
      # if aggregate is false, we concatenate the embeddings for all batches
      if not aggregate:
          return torch.cat(
              [batch_embeddings_fn(batch) for batch in inputs_dataset],
              dim=0,
          )
      else:
          # if aggregate is True, we return the sum of all embeddings for all
          # batches. we do this by summing over each batch, and then summing over all
          # batches.
          inputs_dataset_iter = iter(inputs_dataset)
  
          batch = next(inputs_dataset_iter)
          total_embedding = torch.sum(batch_embeddings_fn(batch), dim=0)
  
          for batch in inputs_dataset_iter:
              total_embedding += torch.sum(
                  batch_embeddings_fn(batch), dim=0
              )
  
          # we unsqueeze because regardless of aggregate, the returned tensor should
          # be 2D.
          return total_embedding.unsqueeze(0)
  
imports: import functools
  from abc import abstractmethod
  from operator import add
  from typing import Any, Callable, List, Optional, Tuple, Union
  import torch
  import torch.nn
  from captum._utils.gradient import _extract_parameters_from_layers
  from captum.influence._core.influence import DataInfluence
  from captum.influence._utils.common import _check_loss_fn, _compute_batch_loss_influence_function_base, _compute_jacobian_sample_wise_grads_per_batch, _dataset_fn, _flatten_params, _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, _params_to_names, _progress_bar_constructor, _self_influence_helper_intermediate_quantities_influence_function, _set_active_parameters, _top_eigen, _unflatten_params_factory, 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
label_desc: Use image node_id 3 for calling a global function with example usage: _get_dataset_embeddings_intermediate_quantities_influence_function(batch_embeddings_fn, inputs_dataset, aggregate) and returns: torch, total_embedding