code: def build_sincos2d_pos_embed(
      feat_shape: List[int],
      dim: int = 64,
      temperature: float = 10000.0,
      reverse_coord: bool = False,
      interleave_sin_cos: bool = False,
      dtype: torch.dtype = torch.float32,
      device: Optional[torch.device] = None,
  ) -> torch.Tensor:
      """
  
      Args:
          feat_shape:
          dim:
          temperature:
          reverse_coord: stack grid order W, H instead of H, W
          interleave_sin_cos: sin, cos, sin, cos stack instead of sin, sin, cos, cos
          dtype:
          device:
  
      Returns:
  
      """
      assert (
          dim % 4 == 0
      ), "Embed dimension must be divisible by 4 for sin-cos 2D position embedding"
      pos_dim = dim // 4
      bands = freq_bands(
          pos_dim,
          temperature=temperature,
          step=1,
          dtype=dtype,
          device=device,
      )
  
      if reverse_coord:
          feat_shape = feat_shape[::-1]  # stack W, H instead of H, W
      grid = (
          torch.stack(
              torch.meshgrid(
                  [
                      torch.arange(s, device=device, dtype=dtype)
                      for s in feat_shape
                  ]
              )
          )
          .flatten(1)
          .transpose(0, 1)
      )
      pos2 = grid.unsqueeze(-1) * bands.unsqueeze(0)
      # FIXME add support for unflattened spatial dim?
  
      stack_dim = (
          2 if interleave_sin_cos else 1
      )  # stack sin, cos, sin, cos  instead of sin sin cos cos
      pos_emb = torch.stack(
          [torch.sin(pos2), torch.cos(pos2)], dim=stack_dim
      ).flatten(1)
      return pos_emb
  
imports: import math
  from typing import List, Tuple, Optional, Union
  import torch
  from torch import nn
  from .trace_utils import _assert
label_desc: Use image node_id 3 for calling a global function with example usage: build_sincos2d_pos_embed(feat_shape, dim, temperature, reverse_coord, interleave_sin_cos, dtype, device) and returns: pos_emb