code: def _flatten_reduced_dim(x, dim):
      # Canonicalize reduced dim.
      reduced_dim = (
          tuple(range(x.dim()))
          if dim is None
          else tuple(d % x.dim() for d in dim)
      )
      nonreduced_dim = tuple(
          i for i in range(x.dim()) if i not in reduced_dim
      )
      # permute & flatten reduced dim.
      permutation = nonreduced_dim + reduced_dim
      return (
          x.permute(permutation).flatten(-len(reduced_dim), -1),
          reduced_dim,
      )
  
imports: import numbers
  import typing
  import torch
  import funsor.ops
label_desc: Use image node_id 1 for calling a global function with example usage: _flatten_reduced_dim(x, dim) and returns: x,  reduced_dim