File size: 2,104 Bytes
9c890d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | # (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
"""Shared-parameter interpolation between first-frame and two-frame UCPE."""
from __future__ import annotations
import torch
from nets.ucpe_cd50_stable.ucpe import RelativeRayAttention as _FirstFrameAttention
from nets.ucpe_cd50_stable.ucpe_tubelet import (
RelativeRayAttention as _TwoFrameAttention,
)
from nets.ucpe_cd50_stable.ucpe_tubelet import (
build_lat_up_encoding,
build_relative_ray_transforms,
)
__all__ = [
"RelativeRayAttention",
"build_lat_up_encoding",
"build_relative_ray_transforms",
]
class RelativeRayAttention(_TwoFrameAttention):
transition_scale: float = 1.0
def forward(
self,
tokens: torch.Tensor,
ray_transforms: torch.Tensor,
*,
spatial_height: int,
spatial_width: int,
lat_up_encoding: torch.Tensor | None = None,
lat_up_valid: torch.Tensor | None = None,
) -> torch.Tensor:
scale = self.transition_scale
if not 0.0 <= scale <= 1.0:
raise ValueError("UCPE transition scale must be in [0, 1]")
self._validate_geometry(
tokens, ray_transforms, spatial_height, spatial_width, lat_up_encoding
)
first = None
if scale < 1.0:
first = _FirstFrameAttention.forward(
self,
tokens,
ray_transforms[:, :, 0],
spatial_height=spatial_height,
spatial_width=spatial_width,
lat_up_encoding=None
if lat_up_encoding is None
else lat_up_encoding[:, :, 0],
lat_up_valid=lat_up_valid,
)
if scale == 0.0:
return first
both = super().forward(
tokens,
ray_transforms,
spatial_height=spatial_height,
spatial_width=spatial_width,
lat_up_encoding=lat_up_encoding,
lat_up_valid=lat_up_valid,
)
return both if first is None else first + scale * (both - first)
|