code: def cumulative_average(
      layer_in: Tensor, layer_cache: tuple, mask=None, step=None
  ) -> Tensor:
      """
      Computes the cumulative average as described in
      :cite:`DBLP:journals/corr/abs-1805-00631` -- Equations (1) (5) (6)
  
      Args:
          layer_in (FloatTensor): sequence to average
              ``(batch_size, input_len, dimension)``
          layer_cache: tuple(bool, dict)
          if layer_cahe[0] is True use step otherwise mask
          mask: mask matrix used to compute the cumulative average
          step: current step of the dynamic decoding
  
      Returns:
          a tensor of the same shape and type as ``layer_in``.
      """
  
      if layer_cache[0]:
          average_attention = (
              layer_in + step * layer_cache[1]["prev_g"]
          ) / (step + 1)
          layer_cache[1]["prev_g"] = average_attention
          return average_attention
      else:
          return torch.matmul(mask.to(layer_in.dtype), layer_in)
  
file_path: OpenNMT-py/onmt/modules/average_attn.py
imports: import torch
  import torch.nn
  from torch import Tensor
  from typing import Optional
  from onmt.modules.position_ffn import PositionwiseFeedForward
  from onmt.modules.position_ffn import ActivationFunction
label_desc: Use image node_id 2 for calling a global function with example usage: cumulative_average(layer_in, layer_cache, mask, step) and returns: average_attention, torch