Spaces:
Running on Zero
Running on Zero
File size: 23,326 Bytes
ccfee12 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | # MIT License
#
# Adapted from the official implementation of PRoPE
# "Cameras as Relative Positional Encoding" https://arxiv.org/pdf/2507.10496
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from functools import partial
from typing import Callable, Optional, Tuple, List
import torch
import torch.nn.functional as F
from einops import rearrange
class PropeDotProductAttention(torch.nn.Module):
"""PRoPE attention with precomputed RoPE coefficients."""
coeffs_x_0: torch.Tensor
coeffs_x_1: torch.Tensor
coeffs_y_0: torch.Tensor
coeffs_y_1: torch.Tensor
def __init__(
self,
head_dim: int,
patches_x: int,
patches_y: int,
image_width: int,
image_height: int,
freq_base: float = 10000.0, # qwen 10000
freq_scale: float = 1.0,
dim_arrange = [16, 56, 56], # (frame ,height, width) default for qwen.
depth = None,
):
super().__init__()
self.head_dim = head_dim
self.patches_x = patches_x
self.patches_y = patches_y
self.image_width = image_width
self.image_height = image_height
self.freq_base = freq_base
self.freq_scale = freq_scale
self.use_PRoPE = False
self.dim_arrange = dim_arrange
# fit Qwen scale-rope
pos_index_x = torch.arange(patches_x)
neg_index_x = torch.arange(patches_x).flip(0) * -1 - 1
index_x = torch.cat([neg_index_x[-(patches_x - patches_x // 2) :], pos_index_x[: patches_x // 2]], dim=0)
# print(index_x)
## Qwen rope apply order is frame, height, width!
# coeffs_x
coeffs_y: Tuple[torch.Tensor, torch.Tensor] = _rope_precompute_coeffs( #
# torch.tile(torch.arange(patches_x), (patches_y,)),
torch.tile(index_x, (patches_y,)),
freq_base=freq_base,
freq_scale=freq_scale,
# feat_dim=head_dim // 4,
feat_dim=dim_arrange[2],
)
# fit Qwen scale-rope
pos_index_y = torch.arange(patches_y)
neg_index_y = torch.arange(patches_y).flip(0) * -1 - 1
index_y = torch.cat([neg_index_y[-(patches_y - patches_y // 2) :], pos_index_y[: patches_y // 2]], dim=0)
# print(index_y)
## Qwen rope apply order is frame, height, width!
# coeffs_y
coeffs_x: Tuple[torch.Tensor, torch.Tensor] = _rope_precompute_coeffs(
# torch.repeat_interleave(torch.arange(patches_y), patches_x),
torch.repeat_interleave(index_y, patches_x),
freq_base=freq_base,
freq_scale=freq_scale,
# feat_dim=head_dim // 4,
feat_dim=dim_arrange[1],
)
# Do not save coeffs to checkpoint as `cameras` might change during testing.
self.register_buffer("coeffs_x_0", coeffs_x[0], persistent=False)
self.register_buffer("coeffs_x_1", coeffs_x[1], persistent=False)
self.register_buffer("coeffs_y_0", coeffs_y[0], persistent=False)
self.register_buffer("coeffs_y_1", coeffs_y[1], persistent=False)
# override load_state_dict to not load coeffs if they exist (for backward compatibility)
def load_state_dict(self, state_dict, strict=True):
# remove coeffs from state_dict
state_dict.pop("coeffs_x_0", None)
state_dict.pop("coeffs_x_1", None)
state_dict.pop("coeffs_y_0", None)
state_dict.pop("coeffs_y_1", None)
super().load_state_dict(state_dict, strict)
def forward(
self,
q: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
k: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
v: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
viewmats: torch.Tensor, # (batch, cameras, 4, 4)
Ks: Optional[torch.Tensor], # (batch, cameras, 3, 3)
**kwargs,
) -> torch.Tensor:
return prope_dot_product_attention(
q,
k,
v,
viewmats=viewmats,
Ks=Ks,
patches_x=self.patches_x,
patches_y=self.patches_y,
image_width=self.image_width,
image_height=self.image_height,
coeffs_x=(self.coeffs_x_0, self.coeffs_x_1),
coeffs_y=(self.coeffs_y_0, self.coeffs_y_1),
**kwargs,
)
def _precompute_and_cache_apply_fns(
self,
viewmats: torch.Tensor,
Ks: Optional[torch.Tensor],
depth = None,
):
(batch, cameras, _, _) = viewmats.shape
assert viewmats.shape == (batch, cameras, 4, 4)
assert Ks is None or Ks.shape == (batch, cameras, 3, 3)
self.cameras = cameras
self.use_PRoPE = True
self.apply_fn_q, self.apply_fn_kv, self.apply_fn_o = _prepare_apply_fns(
head_dim=self.head_dim,
viewmats=viewmats,
Ks=Ks,
patches_x=self.patches_x,
patches_y=self.patches_y,
image_width=self.image_width,
image_height=self.image_height,
coeffs_x=(self.coeffs_x_0, self.coeffs_x_1),
coeffs_y=(self.coeffs_y_0, self.coeffs_y_1),
dim_arrange=self.dim_arrange,
freq_base=self.freq_base,
freq_scale=self.freq_scale,
depth=depth
)
def _apply_to_q(self, q: torch.Tensor) -> torch.Tensor:
(batch, num_heads, seqlen, head_dim) = q.shape
# print("!!!", q.shape)
# print(self.cameras, self.patches_x, self.patches_y)
assert seqlen == self.cameras * self.patches_x * self.patches_y, f"seqlen:{seqlen}, {self.cameras}, {self.patches_x}, {self.patches_y}"
assert head_dim == self.head_dim
assert q.shape == (batch, num_heads, seqlen, head_dim)
assert self.apply_fn_q is not None
return self.apply_fn_q(q)
def _apply_to_kv(self, kv: torch.Tensor) -> torch.Tensor:
(batch, num_heads, seqlen, head_dim) = kv.shape
assert seqlen == self.cameras * self.patches_x * self.patches_y, f"seqlen:{seqlen}, {self.cameras}, {self.patches_x}, {self.patches_y}"
assert head_dim == self.head_dim
assert kv.shape == (batch, num_heads, seqlen, head_dim)
assert self.apply_fn_kv is not None
return self.apply_fn_kv(kv)
def _apply_to_o(self, o: torch.Tensor) -> torch.Tensor:
(batch, num_heads, seqlen, head_dim) = o.shape
assert seqlen == self.cameras * self.patches_x * self.patches_y
assert head_dim == self.head_dim
assert o.shape == (batch, num_heads, seqlen, head_dim)
assert self.apply_fn_o is not None
return self.apply_fn_o(o)
def prope_dot_product_attention(
q: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
k: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
v: torch.Tensor, # (batch, num_heads, seqlen, head_dim)
*,
viewmats: torch.Tensor, # (batch, cameras, 4, 4)
Ks: Optional[torch.Tensor], # (batch, cameras, 3, 3)
patches_x: int, # How many patches wide is each image?
patches_y: int, # How many patches tall is each image?
image_width: int, # Width of the image. Used to normalize intrinsics.
image_height: int, # Height of the image. Used to normalize intrinsics.
coeffs_x: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
coeffs_y: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
**kwargs,
) -> torch.Tensor:
"""Similar to torch.nn.functional.scaled_dot_product_attention, but applies PRoPE-style
positional encoding.
Currently, we assume that the sequence length is equal to:
cameras * patches_x * patches_y
And token ordering allows the `(seqlen,)` axis to be reshaped into
`(cameras, patches_x, patches_y)`.
"""
# We're going to assume self-attention: all inputs are the same shape.
(batch, num_heads, seqlen, head_dim) = q.shape
cameras = viewmats.shape[1]
assert q.shape == k.shape == v.shape
assert viewmats.shape == (batch, cameras, 4, 4)
assert Ks is None or Ks.shape == (batch, cameras, 3, 3)
assert seqlen == cameras * patches_x * patches_y
apply_fn_q, apply_fn_kv, apply_fn_o = _prepare_apply_fns(
head_dim=head_dim,
viewmats=viewmats,
Ks=Ks,
patches_x=patches_x,
patches_y=patches_y,
image_width=image_width,
image_height=image_height,
coeffs_x=coeffs_x,
coeffs_y=coeffs_y,
)
out = F.scaled_dot_product_attention(
query=apply_fn_q(q),
key=apply_fn_kv(k),
value=apply_fn_kv(v),
**kwargs,
)
out = apply_fn_o(out)
assert out.shape == (batch, num_heads, seqlen, head_dim)
return out
def _prepare_apply_fns(
head_dim: int, # Q/K/V will have this last dimension
viewmats: torch.Tensor, # (batch, cameras, 4, 4)
Ks: Optional[torch.Tensor], # (batch, cameras, 3, 3)
patches_x: int, # How many patches wide is each image?
patches_y: int, # How many patches tall is each image?
image_width: int, # Width of the image. Used to normalize intrinsics.
image_height: int, # Height of the image. Used to normalize intrinsics.
coeffs_x: Optional[torch.Tensor] = None,
coeffs_y: Optional[torch.Tensor] = None,
coeffs_z: Optional[torch.Tensor] = None,
dim_arrange = None,
freq_base = None,
freq_scale = None,
depth = None,
) -> Tuple[
Callable[[torch.Tensor], torch.Tensor],
Callable[[torch.Tensor], torch.Tensor],
Callable[[torch.Tensor], torch.Tensor],
]:
"""Prepare transforms for PRoPE-style positional encoding."""
device = viewmats.device
(batch, cameras, _, _) = viewmats.shape
viewmats = viewmats.to(torch.float32)
Ks = Ks.to(torch.float32)
# Normalize camera intrinsics.
if Ks is not None:
# Ks has been normalized in the dataset getitem !!
Ks_norm = Ks
# Compute the camera projection matrices we use in PRoPE.
# - K is an `image<-camera` transform.
# - viewmats is a `camera<-world` transform.
# - P = lift(K) @ viewmats is an `image<-world` transform.
P = torch.einsum("...ij,...jk->...ik", _lift_K(Ks_norm), viewmats)
P_T = P.transpose(-1, -2)
P_inv = torch.einsum(
"...ij,...jk->...ik",
_invert_SE3(viewmats),
_lift_K(_invert_K(Ks_norm)),
)
else:
# GTA formula. P is `camera<-world` transform.
P = viewmats
P_T = P.transpose(-1, -2)
P_inv = _invert_SE3(viewmats)
assert P.shape == P_inv.shape == (batch, cameras, 4, 4)
# Precompute cos/sin terms for RoPE. We use tiles/repeats for 'row-major'
# broadcasting.
assert coeffs_x is not None
if coeffs_x is None:
coeffs_x = _rope_precompute_coeffs(
torch.tile(torch.arange(patches_x, device=device), (patches_y * cameras,)),
freq_base=100.0,
freq_scale=1.0,
# feat_dim=head_dim // 4,
feat_dim=dim_arrange[1],
)
assert coeffs_y is not None
if coeffs_y is None:
coeffs_y = _rope_precompute_coeffs(
torch.tile(
torch.repeat_interleave(
torch.arange(patches_y, device=device), patches_x
),
(cameras,),
),
freq_base=100.0,
freq_scale=1.0,
# feat_dim=head_dim // 4,
feat_dim=dim_arrange[2],
)
if torch.isnan(P_inv).any():
print("!!P_inv has NaN!!!")
exit(0)
if torch.isnan(P_T).any():
print("!!P_T has NaN!!!")
exit(0)
if torch.isnan(coeffs_x[0]).any() or torch.isnan(coeffs_x[1]).any():
print("!!coeffs_x has NaN!!!")
exit(0)
if torch.isnan(coeffs_y[0]).any() or torch.isnan(coeffs_y[1]).any():
print("!!coeffs_y has NaN!!!")
exit(0)
# Block-diagonal transforms to the inputs and outputs of the attention operator.
assert head_dim % 4 == 0
transforms_q = [
(partial(_apply_tiled_projmat, matrix=P_T), dim_arrange[0]),
(partial(_rope_apply_coeffs, coeffs=coeffs_x), dim_arrange[1]),
(partial(_rope_apply_coeffs, coeffs=coeffs_y), dim_arrange[2]),
]
transforms_kv = [
(partial(_apply_tiled_projmat, matrix=P_inv), dim_arrange[0]),
(partial(_rope_apply_coeffs, coeffs=coeffs_x), dim_arrange[1]),
(partial(_rope_apply_coeffs, coeffs=coeffs_y), dim_arrange[2]),
]
transforms_o = [
(partial(_apply_tiled_projmat, matrix=P), dim_arrange[0]),
(partial(_rope_apply_coeffs, coeffs=coeffs_x, inverse=True), dim_arrange[1]),
(partial(_rope_apply_coeffs, coeffs=coeffs_y, inverse=True), dim_arrange[2]),
]
if len(dim_arrange) == 4:
index_z = rearrange(depth, 'b n h w -> b n (h w)') # (batch, frame, seq_len)
coeffs_z: Tuple[torch.Tensor, torch.Tensor] = _rope_precompute_coeffs_z(
index_z,
freq_base=freq_base,
freq_scale=freq_scale,
feat_dim=dim_arrange[3],
)
coeffs_z_0 = coeffs_z[0]
coeffs_z_1 = coeffs_z[1]
coeffs_z = (coeffs_z_0, coeffs_z_1)
transforms_q += [(partial(_rope_apply_coeffs_z, coeffs=coeffs_z), dim_arrange[3])]
transforms_kv += [(partial(_rope_apply_coeffs_z, coeffs=coeffs_z), dim_arrange[3])]
transforms_o += [(partial(_rope_apply_coeffs_z, coeffs=coeffs_z, inverse=True), dim_arrange[3])]
apply_fn_q = partial(_apply_block_diagonal, func_size_pairs=transforms_q)
apply_fn_kv = partial(_apply_block_diagonal, func_size_pairs=transforms_kv)
apply_fn_o = partial(_apply_block_diagonal, func_size_pairs=transforms_o)
return apply_fn_q, apply_fn_kv, apply_fn_o
def _apply_tiled_projmat(
feats: torch.Tensor, # (batch, num_heads, seqlen, feat_dim)
matrix: torch.Tensor, # (batch, cameras, D, D)
) -> torch.Tensor:
"""Apply projection matrix to features."""
# - seqlen => (cameras, patches_x * patches_y)
# - feat_dim => (feat_dim // 4, 4)
matrix = matrix.to(feats.dtype)
(batch, num_heads, seqlen, feat_dim) = feats.shape
cameras = matrix.shape[1]
assert seqlen > cameras and seqlen % cameras == 0
D = matrix.shape[-1]
assert matrix.shape == (batch, cameras, D, D)
assert feat_dim % D == 0
# print(matrix.device, feats.device)
return torch.einsum(
"bcij,bncpkj->bncpki",
matrix,
feats.reshape((batch, num_heads, cameras, -1, feat_dim // D, D)),
).reshape(feats.shape)
def _rope_apply_coeffs_z(
feats: torch.Tensor, # (batch, num_heads, seqlen_total, feat_dim)
coeffs: Tuple[torch.Tensor, torch.Tensor], # (batch, 1, frame, seqlen_img, num_freqs)
inverse: bool = False,
) -> torch.Tensor:
"""Apply RoPE coefficients to features. We adopt a 'split' ordering
convention. (in contrast to 'interleaved')"""
# print("Inject z rope!!")
#TODO change to interleaved same as Qwen?
cos, sin = coeffs
batch, num_heads, total_seq_len, feat_dim = feats.shape
_, __, frames, seq_len_per_img, num_freqs = cos.shape
cos = cos.to(feats.dtype)
sin = sin.to(feats.dtype)
# We allow (cos, sin) to be either with shape (1, 1, seqlen, feat_dim // 2),
# or (1, 1, seqlen_per_image, feat_dim // 2) and we repeat it to
# match the shape of feats.
feats = feats.reshape((batch, num_heads, frames, seq_len_per_img, feat_dim))
assert feats.shape[3] * frames == total_seq_len
# if cos.shape[2] != feats.shape[2]:
# n_repeats = feats.shape[2] // cos.shape[2]
# cos = cos.repeat(1, 1, n_repeats, 1)
# sin = sin.repeat(1, 1, n_repeats, 1)
assert len(cos.shape) == len(sin.shape) == len(feats.shape) == 5
assert cos.shape[-1] == sin.shape[-1] == feats.shape[-1] // 2
# cos (batch, 1, frame, seqlen_img, feat_dim)
x_in = feats[..., ::2] # even # (batch, num_heads, frames, seqlen_img, feat_dim)
y_in = feats[..., 1::2]
if inverse == False: # for qkv
x_out = cos * x_in - sin * y_in # broadcast on "num_heads"
y_out = sin * x_in + cos * y_in
else: # for out
x_out = cos * x_in + sin * y_in
y_out = -sin * x_in + cos * y_in
res = torch.stack((x_out, y_out), dim=-1).flatten(start_dim=-2)
res = rearrange(res, 'b n f s d -> b n (f s) d')
# print(res.shape)
return res
def _rope_precompute_coeffs_z(
positions: torch.Tensor, # (batch, frame, seq_len)
freq_base: float,
freq_scale: float,
feat_dim: int,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Precompute RoPE coefficients."""
assert len(positions.shape) == 3
assert feat_dim % 2 == 0
num_freqs = feat_dim // 2
freqs = freq_scale * (
freq_base
** (
-torch.arange(num_freqs, device=positions.device)[None, None, None, :]
/ num_freqs
)
)
# print(freqs.shape)
# print(positions[:128])
angles = positions[:, None, :, :, None] * freqs
# Shape should be: `(batch, num_heads, frame, seqlen, num_freqs)`; we're
# broadcasting across `num_heads`.
assert angles.shape == (positions.shape[0], 1, positions.shape[1], positions.shape[2], num_freqs)
return torch.cos(angles), torch.sin(angles)
def _rope_precompute_coeffs(
positions: torch.Tensor, # (seqlen,)
freq_base: float,
freq_scale: float,
feat_dim: int,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Precompute RoPE coefficients."""
assert len(positions.shape) == 1
assert feat_dim % 2 == 0
num_freqs = feat_dim // 2
freqs = freq_scale * (
freq_base
** (
-torch.arange(num_freqs, device=positions.device)[None, None, None, :]
/ num_freqs
)
)
# print(freqs.shape)
# print(positions[:128])
angles = positions[None, None, :, None] * freqs
# Shape should be: `(batch, num_heads, seqlen, num_freqs)`; we're
# broadcasting across `batch` and `num_heads`.
assert angles.shape == (1, 1, positions.shape[0], num_freqs)
return torch.cos(angles), torch.sin(angles)
if __name__ == '__main__':
patches_x = 64
patches_y = 64
freq_base = 1
freq_scale = 10000
head_dim = 128
pos_index = torch.arange(patches_x)
neg_index = torch.arange(patches_x).flip(0) * -1 - 1
index = torch.cat([neg_index[-(patches_x - patches_x // 2) :], pos_index[: patches_x // 2]], dim=0)
print(index)
print(torch.arange(patches_x))
coeffs_x: Tuple[torch.Tensor, torch.Tensor] = _rope_precompute_coeffs(
# torch.tile(torch.arange(patches_x), (patches_y,)),
torch.tile(index, (patches_y,)),
freq_base=freq_base,
freq_scale=freq_scale,
# feat_dim=head_dim // 4,
feat_dim=56,
)
coeffs_y: Tuple[torch.Tensor, torch.Tensor] = _rope_precompute_coeffs(
torch.repeat_interleave(torch.arange(patches_y), patches_x),
freq_base=freq_base,
freq_scale=freq_scale,
# feat_dim=head_dim // 4,
feat_dim=56,
)
def _rope_apply_coeffs(
feats: torch.Tensor, # (batch, num_heads, seqlen, feat_dim)
coeffs: Tuple[torch.Tensor, torch.Tensor],
inverse: bool = False,
) -> torch.Tensor:
"""Apply RoPE coefficients to features. We adopt a 'split' ordering
convention. (in contrast to 'interleaved')"""
#TODO change to interleaved same as Qwen?
cos, sin = coeffs
cos = cos.to(feats.dtype)
sin = sin.to(feats.dtype)
# We allow (cos, sin) to be either with shape (1, 1, seqlen, feat_dim // 2),
# or (1, 1, seqlen_per_image, feat_dim // 2) and we repeat it to
# match the shape of feats.
if cos.shape[2] != feats.shape[2]:
n_repeats = feats.shape[2] // cos.shape[2]
cos = cos.repeat(1, 1, n_repeats, 1)
sin = sin.repeat(1, 1, n_repeats, 1)
assert len(feats.shape) == len(cos.shape) == len(sin.shape) == 4
assert cos.shape[-1] == sin.shape[-1] == feats.shape[-1] // 2
x_in = feats[..., ::2] # even # (batch, num_heads, seqlen, feat_dim)
y_in = feats[..., 1::2]
if inverse == False: # for qkv
x_out = cos * x_in - sin * y_in
y_out = sin * x_in + cos * y_in
else: # for out
x_out = cos * x_in + sin * y_in
y_out = -sin * x_in + cos * y_in
res = torch.stack((x_out, y_out), dim=-1).flatten(start_dim=-2)
# print(res.shape)
return res
def _apply_block_diagonal(
feats: torch.Tensor, # (..., dim)
func_size_pairs: List[Tuple[Callable[[torch.Tensor], torch.Tensor], int]],
) -> torch.Tensor:
"""Apply a block-diagonal function to an input array.
Each function is specified as a tuple with form:
((Tensor) -> Tensor, int)
Where the integer is the size of the input to the function.
"""
funcs, block_sizes = zip(*func_size_pairs)
assert feats.shape[-1] == sum(block_sizes)
x_blocks = torch.split(feats, block_sizes, dim=-1)
out = torch.cat(
[f(x_block) for f, x_block in zip(funcs, x_blocks)],
dim=-1,
)
assert out.shape == feats.shape, "Input/output shapes should match."
return out
def _invert_SE3(transforms: torch.Tensor) -> torch.Tensor:
"""Invert a 4x4 SE(3) matrix."""
assert transforms.shape[-2:] == (4, 4)
Rinv = transforms[..., :3, :3].transpose(-1, -2)
out = torch.zeros_like(transforms)
out[..., :3, :3] = Rinv
out[..., :3, 3] = -torch.einsum("...ij,...j->...i", Rinv, transforms[..., :3, 3])
out[..., 3, 3] = 1.0
return out
def _lift_K(Ks: torch.Tensor) -> torch.Tensor:
"""Lift 3x3 matrices to homogeneous 4x4 matrices."""
assert Ks.shape[-2:] == (3, 3)
out = torch.zeros(Ks.shape[:-2] + (4, 4), device=Ks.device)
out[..., :3, :3] = Ks
out[..., 3, 3] = 1.0
return out
def _invert_K(Ks: torch.Tensor) -> torch.Tensor:
"""Invert 3x3 intrinsics matrices. Assumes no skew."""
assert Ks.shape[-2:] == (3, 3)
out = torch.zeros_like(Ks)
out[..., 0, 0] = 1.0 / Ks[..., 0, 0]
out[..., 1, 1] = 1.0 / Ks[..., 1, 1]
out[..., 0, 2] = -Ks[..., 0, 2] / Ks[..., 0, 0]
out[..., 1, 2] = -Ks[..., 1, 2] / Ks[..., 1, 1]
out[..., 2, 2] = 1.0
return out |