Skip to content

Commit

Permalink
fixing image_encoder to work with cuda_graphs
Browse files Browse the repository at this point in the history
Summary: the combination of tensors on multiple devices in get_rel_pos
was preventing cuda graphs from correctly optimizing things

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 0fea0e19e5bf0ee44a19669fe33e7e16002a55af
Pull Request resolved: #393
  • Loading branch information
HDCharles committed May 24, 2023
1 parent 6fdee8f commit d793ab4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions segment_anything/modeling/image_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ def get_rel_pos(q_size: int, k_size: int, rel_pos: torch.Tensor) -> torch.Tensor
rel_pos_resized = rel_pos

# Scale the coords with short length if shapes for q and k are different.
q_coords = torch.arange(q_size)[:, None] * max(k_size / q_size, 1.0)
k_coords = torch.arange(k_size)[None, :] * max(q_size / k_size, 1.0)
q_coords = (torch.arange(q_size).to(rel_pos.device)[:, None] * max(k_size / q_size, 1.0))
k_coords = (torch.arange(k_size).to(rel_pos.device)[None, :] * max(q_size / k_size, 1.0))
relative_coords = (q_coords - k_coords) + (k_size - 1) * max(q_size / k_size, 1.0)

return rel_pos_resized[relative_coords.long()]
Expand Down

0 comments on commit d793ab4

Please sign in to comment.