r""" Creates 3D sinusoidal positional embeddings. Args: embed_dim (`int`): The embedding dimension of inputs. It must be divisible by 16. spatial_size (`int` or `Tuple[int, int]`): The spatial dimension of positional embeddings. If an integer is provi
(
embed_dim: int,
spatial_size: Union[int, Tuple[int, int]],
temporal_size: int,
spatial_interpolation_scale: float = 1.0,
temporal_interpolation_scale: float = 1.0,
device: Optional[torch.device] = None,
output_type: str = "np",
)
| 79 | |
| 80 | |
| 81 | def get_3d_sincos_pos_embed( |
| 82 | embed_dim: int, |
| 83 | spatial_size: Union[int, Tuple[int, int]], |
| 84 | temporal_size: int, |
| 85 | spatial_interpolation_scale: float = 1.0, |
| 86 | temporal_interpolation_scale: float = 1.0, |
| 87 | device: Optional[torch.device] = None, |
| 88 | output_type: str = "np", |
| 89 | ) -> torch.Tensor: |
| 90 | r""" |
| 91 | Creates 3D sinusoidal positional embeddings. |
| 92 | |
| 93 | Args: |
| 94 | embed_dim (`int`): |
| 95 | The embedding dimension of inputs. It must be divisible by 16. |
| 96 | spatial_size (`int` or `Tuple[int, int]`): |
| 97 | The spatial dimension of positional embeddings. If an integer is provided, the same size is applied to both |
| 98 | spatial dimensions (height and width). |
| 99 | temporal_size (`int`): |
| 100 | The temporal dimension of postional embeddings (number of frames). |
| 101 | spatial_interpolation_scale (`float`, defaults to 1.0): |
| 102 | Scale factor for spatial grid interpolation. |
| 103 | temporal_interpolation_scale (`float`, defaults to 1.0): |
| 104 | Scale factor for temporal grid interpolation. |
| 105 | |
| 106 | Returns: |
| 107 | `torch.Tensor`: |
| 108 | The 3D sinusoidal positional embeddings of shape `[temporal_size, spatial_size[0] * spatial_size[1], |
| 109 | embed_dim]`. |
| 110 | """ |
| 111 | if output_type == "np": |
| 112 | return _get_3d_sincos_pos_embed_np( |
| 113 | embed_dim=embed_dim, |
| 114 | spatial_size=spatial_size, |
| 115 | temporal_size=temporal_size, |
| 116 | spatial_interpolation_scale=spatial_interpolation_scale, |
| 117 | temporal_interpolation_scale=temporal_interpolation_scale, |
| 118 | ) |
| 119 | if embed_dim % 4 != 0: |
| 120 | raise ValueError("`embed_dim` must be divisible by 4") |
| 121 | if isinstance(spatial_size, int): |
| 122 | spatial_size = (spatial_size, spatial_size) |
| 123 | |
| 124 | embed_dim_spatial = 3 * embed_dim // 4 |
| 125 | embed_dim_temporal = embed_dim // 4 |
| 126 | |
| 127 | # 1. Spatial |
| 128 | grid_h = torch.arange(spatial_size[1], device=device, dtype=torch.float32) / spatial_interpolation_scale |
| 129 | grid_w = torch.arange(spatial_size[0], device=device, dtype=torch.float32) / spatial_interpolation_scale |
| 130 | grid = torch.meshgrid(grid_w, grid_h, indexing="xy") # here w goes first |
| 131 | grid = torch.stack(grid, dim=0) |
| 132 | |
| 133 | grid = grid.reshape([2, 1, spatial_size[1], spatial_size[0]]) |
| 134 | pos_embed_spatial = get_2d_sincos_pos_embed_from_grid(embed_dim_spatial, grid, output_type="pt") |
| 135 | |
| 136 | # 2. Temporal |
| 137 | grid_t = torch.arange(temporal_size, device=device, dtype=torch.float32) / temporal_interpolation_scale |
| 138 | pos_embed_temporal = get_1d_sincos_pos_embed_from_grid(embed_dim_temporal, grid_t, output_type="pt") |
no test coverage detected