code: def infer_dependency_matrix(
      model: Callable, seq_shape: tuple, in_stride: int = 1
  ):
      """
      Randomizes parts of the input sequence several times in order to detect
      dependencies between input frames and output frames, aka whether a given
      output frame depends on a given input frame.
  
      This can prove useful to check whether a model behaves correctly in a
      streaming context and does not contain accidental dependencies to future
      frames that couldn't be known in a streaming scenario.
  
      Note that this can get very computationally expensive for very long
      sequences.
  
      Furthermore, this expects inference to be fully deterministic, else false
      dependencies may be found. This also means that the model must be in eval
      mode, to inhibit things like dropout layers.
  
      Arguments
      ---------
      model : Callable
          Can be a model or a function (potentially emulating streaming
          functionality). Does not require to be a trained model, random weights
          should usually suffice.
      seq_shape : tuple
          The function tries inferring by randomizing parts of the input sequence
          in order to detect unwanted dependencies.
          The shape is expected to look like `[batch_size, seq_len, num_feats]`,
          where `batch_size` may be `1`.
      in_stride : int
          Consider only N-th input, for when the input sequences are very long
          (e.g. raw audio) and the output is shorter (subsampled, filters, etc.)
  
      Returns
      -------
      dependencies : torch.BoolTensor
          Matrix representing whether an output is dependent on an input; index
          using `[in_frame_idx, out_frame_idx]`. `True` indicates a detected
          dependency.
      """
      # TODO: document arguments
  
      bs, seq_len, feat_len = seq_shape
  
      base_seq = torch.rand(seq_shape)
      with torch.no_grad():
          base_out = model(base_seq)
  
          if not model(base_seq).equal(base_out):
              raise ValueError(
                  "Expected deterministic model, but inferring twice on the same "
                  "data yielded different results. Make sure that you use "
                  "`eval()` mode so that it does not include randomness."
              )
      out_len, _out_feat_len = base_out.shape[1:]
  
      deps = torch.zeros(
          ((seq_len + (in_stride - 1)) // in_stride, out_len),
          dtype=torch.bool,
      )
  
      for in_frame_idx in range(0, seq_len, in_stride):
          test_seq = base_seq.clone()
          test_seq[:, in_frame_idx, :] = torch.rand(bs, feat_len)
  
          with torch.no_grad():
              test_out = model(test_seq)
  
          for out_frame_idx in range(out_len):
              if not torch.allclose(
                  test_out[:, out_frame_idx, :],
                  base_out[:, out_frame_idx, :],
              ):
                  deps[in_frame_idx // in_stride][out_frame_idx] = True
  
      return deps
  
imports: import math
  import torch
  from typing import Callable, List
label_desc: Use image node_id 3 for calling a global function with example usage: infer_dependency_matrix(model, seq_shape, in_stride) and returns: deps