code: def _sequence_loss_fn(
      flow_preds: List[Tensor],
      flow_gt: Tensor,
      valid_flow_mask: Optional[Tensor],
      gamma: Tensor,
      max_flow: int = 256,
      exclude_large: bool = False,
      weights: Optional[Tensor] = None,
  ):
      """Loss function defined over sequence of flow predictions"""
      torch._assert(
          gamma < 1,
          "sequence_loss: `gamma` must be lower than 1, but got {}".format(
              gamma
          ),
      )
  
      if exclude_large:
          # exclude invalid pixels and extremely large diplacements
          flow_norm = torch.sum(flow_gt**2, dim=1).sqrt()
          if valid_flow_mask is not None:
              valid_flow_mask = valid_flow_mask & (flow_norm < max_flow)
          else:
              valid_flow_mask = flow_norm < max_flow
  
      if valid_flow_mask is not None:
          valid_flow_mask = valid_flow_mask.unsqueeze(1)
      flow_preds = torch.stack(
          flow_preds
      )  # shape = (num_flow_updates, batch_size, 2, H, W)
  
      abs_diff = (flow_preds - flow_gt).abs()
      if valid_flow_mask is not None:
          abs_diff = abs_diff * valid_flow_mask.unsqueeze(0)
  
      abs_diff = abs_diff.mean(axis=(1, 2, 3, 4))
      num_predictions = flow_preds.shape[0]
  
      # allocating on CPU and moving to device during run-time can force
      # an unwanted GPU synchronization that produces a large overhead
      if weights is None or len(weights) != num_predictions:
          weights = gamma ** torch.arange(
              num_predictions - 1,
              -1,
              -1,
              device=flow_preds.device,
              dtype=flow_preds.dtype,
          )
  
      flow_loss = (abs_diff * weights).sum()
      return flow_loss, weights
  
imports: from typing import List, Optional
  import torch
  from torch import nn, Tensor
  from torch.nn import functional
  from torchvision.prototype.models.depth.stereo.raft_stereo import grid_sample, make_coords_grid
label_desc: Use image node_id 2 for calling a global function with example usage: _sequence_loss_fn(flow_preds, flow_gt, valid_flow_mask, gamma, max_flow, exclude_large, weights) and returns: flow_loss,  weights