-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optim deformable detr #33600
Open
yonigozlan
wants to merge
4
commits into
huggingface:main
Choose a base branch
from
yonigozlan:optim-deformable-detr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−31
Open
Optim deformable detr #33600
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -523,8 +523,8 @@ def __init__(self, embedding_dim=64, temperature=10000, normalize=False, scale=N | |||||
def forward(self, pixel_values, pixel_mask): | ||||||
if pixel_mask is None: | ||||||
raise ValueError("No pixel mask provided") | ||||||
y_embed = pixel_mask.cumsum(1, dtype=torch.float32) | ||||||
x_embed = pixel_mask.cumsum(2, dtype=torch.float32) | ||||||
y_embed = pixel_mask.cumsum(1, dtype=torch.float16) | ||||||
x_embed = pixel_mask.cumsum(2, dtype=torch.float16) | ||||||
if self.normalize: | ||||||
eps = 1e-6 | ||||||
y_embed = (y_embed - 0.5) / (y_embed[:, -1:, :] + eps) * self.scale | ||||||
|
@@ -580,11 +580,14 @@ def build_position_encoding(config): | |||||
|
||||||
|
||||||
def multi_scale_deformable_attention( | ||||||
value: Tensor, value_spatial_shapes: Tensor, sampling_locations: Tensor, attention_weights: Tensor | ||||||
value: Tensor, | ||||||
value_spatial_shapes: Union[Tensor, List[Tuple]], | ||||||
sampling_locations: Tensor, | ||||||
attention_weights: Tensor, | ||||||
) -> Tensor: | ||||||
batch_size, _, num_heads, hidden_dim = value.shape | ||||||
_, num_queries, num_heads, num_levels, num_points, _ = sampling_locations.shape | ||||||
value_list = value.split([height.item() * width.item() for height, width in value_spatial_shapes], dim=1) | ||||||
value_list = value.split([height * width for height, width in value_spatial_shapes], dim=1) | ||||||
sampling_grids = 2 * sampling_locations - 1 | ||||||
sampling_value_list = [] | ||||||
for level_id, (height, width) in enumerate(value_spatial_shapes): | ||||||
|
@@ -695,6 +698,7 @@ def forward( | |||||
position_embeddings: Optional[torch.Tensor] = None, | ||||||
reference_points=None, | ||||||
spatial_shapes=None, | ||||||
spatial_shapes_list=None, | ||||||
level_start_index=None, | ||||||
output_attentions: bool = False, | ||||||
): | ||||||
|
@@ -704,7 +708,8 @@ def forward( | |||||
|
||||||
batch_size, num_queries, _ = hidden_states.shape | ||||||
batch_size, sequence_length, _ = encoder_hidden_states.shape | ||||||
if (spatial_shapes[:, 0] * spatial_shapes[:, 1]).sum() != sequence_length: | ||||||
total_elements = sum(shape[0] * shape[1] for shape in spatial_shapes_list) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case it holds here :)
Suggested change
|
||||||
if total_elements != sequence_length: | ||||||
raise ValueError( | ||||||
"Make sure to align the spatial shapes with the sequence length of the encoder hidden states" | ||||||
) | ||||||
|
@@ -739,9 +744,11 @@ def forward( | |||||
else: | ||||||
raise ValueError(f"Last dim of reference_points must be 2 or 4, but got {reference_points.shape[-1]}") | ||||||
|
||||||
if self.disable_custom_kernels: | ||||||
if self.disable_custom_kernels or MultiScaleDeformableAttention is None: | ||||||
# PyTorch implementation | ||||||
output = multi_scale_deformable_attention(value, spatial_shapes, sampling_locations, attention_weights) | ||||||
output = multi_scale_deformable_attention( | ||||||
value, spatial_shapes_list, sampling_locations, attention_weights | ||||||
) | ||||||
else: | ||||||
try: | ||||||
# custom kernel | ||||||
|
@@ -755,7 +762,9 @@ def forward( | |||||
) | ||||||
except Exception: | ||||||
# PyTorch implementation | ||||||
output = multi_scale_deformable_attention(value, spatial_shapes, sampling_locations, attention_weights) | ||||||
output = multi_scale_deformable_attention( | ||||||
value, spatial_shapes_list, sampling_locations, attention_weights | ||||||
) | ||||||
output = self.output_proj(output) | ||||||
|
||||||
return output, attention_weights | ||||||
|
@@ -900,6 +909,7 @@ def forward( | |||||
position_embeddings: torch.Tensor = None, | ||||||
reference_points=None, | ||||||
spatial_shapes=None, | ||||||
spatial_shapes_list=None, | ||||||
level_start_index=None, | ||||||
output_attentions: bool = False, | ||||||
): | ||||||
|
@@ -932,6 +942,7 @@ def forward( | |||||
position_embeddings=position_embeddings, | ||||||
reference_points=reference_points, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
output_attentions=output_attentions, | ||||||
) | ||||||
|
@@ -997,6 +1008,7 @@ def forward( | |||||
position_embeddings: Optional[torch.Tensor] = None, | ||||||
reference_points=None, | ||||||
spatial_shapes=None, | ||||||
spatial_shapes_list=None, | ||||||
level_start_index=None, | ||||||
encoder_hidden_states: Optional[torch.Tensor] = None, | ||||||
encoder_attention_mask: Optional[torch.Tensor] = None, | ||||||
|
@@ -1048,6 +1060,7 @@ def forward( | |||||
position_embeddings=position_embeddings, | ||||||
reference_points=reference_points, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
output_attentions=output_attentions, | ||||||
) | ||||||
|
@@ -1219,6 +1232,7 @@ def forward( | |||||
attention_mask=None, | ||||||
position_embeddings=None, | ||||||
spatial_shapes=None, | ||||||
spatial_shapes_list=None, | ||||||
level_start_index=None, | ||||||
valid_ratios=None, | ||||||
output_attentions=None, | ||||||
|
@@ -1260,7 +1274,8 @@ def forward( | |||||
hidden_states = inputs_embeds | ||||||
hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training) | ||||||
|
||||||
reference_points = self.get_reference_points(spatial_shapes, valid_ratios, device=inputs_embeds.device) | ||||||
spatial_shapes_tuple = tuple(spatial_shapes_list) | ||||||
reference_points = self.get_reference_points(spatial_shapes_tuple, valid_ratios, device=inputs_embeds.device) | ||||||
|
||||||
encoder_states = () if output_hidden_states else None | ||||||
all_attentions = () if output_attentions else None | ||||||
|
@@ -1275,6 +1290,7 @@ def forward( | |||||
position_embeddings, | ||||||
reference_points, | ||||||
spatial_shapes, | ||||||
spatial_shapes_list, | ||||||
level_start_index, | ||||||
output_attentions, | ||||||
) | ||||||
|
@@ -1285,6 +1301,7 @@ def forward( | |||||
position_embeddings=position_embeddings, | ||||||
reference_points=reference_points, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
output_attentions=output_attentions, | ||||||
) | ||||||
|
@@ -1341,6 +1358,7 @@ def forward( | |||||
position_embeddings=None, | ||||||
reference_points=None, | ||||||
spatial_shapes=None, | ||||||
spatial_shapes_list=None, | ||||||
level_start_index=None, | ||||||
valid_ratios=None, | ||||||
output_attentions=None, | ||||||
|
@@ -1416,6 +1434,7 @@ def forward( | |||||
position_embeddings, | ||||||
reference_points_input, | ||||||
spatial_shapes, | ||||||
spatial_shapes_list, | ||||||
level_start_index, | ||||||
encoder_hidden_states, | ||||||
encoder_attention_mask, | ||||||
|
@@ -1428,6 +1447,7 @@ def forward( | |||||
encoder_hidden_states=encoder_hidden_states, | ||||||
reference_points=reference_points_input, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
encoder_attention_mask=encoder_attention_mask, | ||||||
output_attentions=output_attentions, | ||||||
|
@@ -1735,11 +1755,11 @@ def forward( | |||||
source_flatten = [] | ||||||
mask_flatten = [] | ||||||
lvl_pos_embed_flatten = [] | ||||||
spatial_shapes = [] | ||||||
spatial_shapes_list = [] | ||||||
for level, (source, mask, pos_embed) in enumerate(zip(sources, masks, position_embeddings_list)): | ||||||
batch_size, num_channels, height, width = source.shape | ||||||
spatial_shape = (height, width) | ||||||
spatial_shapes.append(spatial_shape) | ||||||
spatial_shapes_list.append(spatial_shape) | ||||||
source = source.flatten(2).transpose(1, 2) | ||||||
mask = mask.flatten(1) | ||||||
pos_embed = pos_embed.flatten(2).transpose(1, 2) | ||||||
|
@@ -1750,7 +1770,7 @@ def forward( | |||||
source_flatten = torch.cat(source_flatten, 1) | ||||||
mask_flatten = torch.cat(mask_flatten, 1) | ||||||
lvl_pos_embed_flatten = torch.cat(lvl_pos_embed_flatten, 1) | ||||||
spatial_shapes = torch.as_tensor(spatial_shapes, dtype=torch.long, device=source_flatten.device) | ||||||
spatial_shapes = torch.as_tensor(spatial_shapes_list, dtype=torch.long, device=source_flatten.device) | ||||||
level_start_index = torch.cat((spatial_shapes.new_zeros((1,)), spatial_shapes.prod(1).cumsum(0)[:-1])) | ||||||
valid_ratios = torch.stack([self.get_valid_ratio(m, dtype=source_flatten.dtype) for m in masks], 1) | ||||||
|
||||||
|
@@ -1762,6 +1782,7 @@ def forward( | |||||
attention_mask=mask_flatten, | ||||||
position_embeddings=lvl_pos_embed_flatten, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
valid_ratios=valid_ratios, | ||||||
output_attentions=output_attentions, | ||||||
|
@@ -1819,6 +1840,7 @@ def forward( | |||||
encoder_attention_mask=mask_flatten, | ||||||
reference_points=reference_points, | ||||||
spatial_shapes=spatial_shapes, | ||||||
spatial_shapes_list=spatial_shapes_list, | ||||||
level_start_index=level_start_index, | ||||||
valid_ratios=valid_ratios, | ||||||
output_attentions=output_attentions, | ||||||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, why should we use float16 instead of float32? shouldn't it be
pixel_values.dtype
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops absolutely, thanks for catching that.