Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

update position encoding to make detr torch fx traceable #531

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion models/position_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def forward(self, tensor_list: NestedTensor):
y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale
x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale

dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
dim_t = torch_arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)

pos_x = x_embed[:, :, :, None] / dim_t
Expand Down Expand Up @@ -87,3 +87,8 @@ def build_position_encoding(args):
raise ValueError(f"not supported {args.position_embedding}")

return position_embedding


@torch.fx.wrap
def torch_arange(x: int, dtype: torch.dtype, device: torch.device) -> torch.Tensor:
return torch.arange(x, dtype=dtype, device=device)