code: def gen_positional_indices(
      shape: torch.Size,
      dim: int,
      device: Optional[torch.device] = None,
  ) -> Iterator[torch.LongTensor]:
      ndim = len(shape)
      _dim = ndim + dim if dim < 0 else dim
      if _dim >= ndim or _dim < 0:
          raise ValueError(f"dim={dim} invalid for shape {shape}.")
  
      cumsize = shape[_dim + 1 :].numel()
      for i, s in enumerate(reversed(shape[: _dim + 1])):
          yield torch.arange(0, s * cumsize, cumsize, device=device)[
              (...,) + i * (None,)
          ]
          cumsize *= s
  
imports: from __future__ import annotations
  import math
  from functools import lru_cache
  from math import pi
  from numbers import Number
  from typing import Any, Callable, Iterable, Iterator, Optional, Tuple, Union
  import torch
  from botorch.utils.safe_math import logdiffexp
  from numpy.polynomial.legendre import leggauss
  from torch import BoolTensor, LongTensor, Tensor
label_desc: Use image node_id 4 for calling a global function with example usage: gen_positional_indices(shape, dim, device) without return types