Skip to content

Fix MaskFormerSwin attention mask dtype to follow hidden states - #49032

Open
kaixuanliu wants to merge 4 commits into
huggingface:mainfrom
kaixuanliu:maskformer_swin
Open

kaixuanliu wants to merge 4 commits into
huggingface:mainfrom
kaixuanliu:maskformer_swin

Conversation

@kaixuanliu

@kaixuanliu kaixuanliu commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

MaskFormerSwinLayer.get_attn_mask hardcodes the shifted-window attention mask to float32 via .float(), ignoring the model dtype. When the model runs in bfloat16/float16, adding this fp32 mask promotes attention_scores to fp32, so the subsequent torch.matmul(attention_probs, value_layer) mixes fp32 with a bf16 value_layer.

This silently works in eager on some backends, but fails under torch.compile, where the fake-tensor meta kernel strictly validates dtypes:

torch._dynamo.exc.TorchRuntimeError: RuntimeError when making fake tensor call
  Dynamo failed to run FX node with fake tensors: call_function torch.matmul(
    FakeTensor(size=(289, 4, 144, 144)),                        # fp32
    FakeTensor(size=(289, 4, 144, 32), dtype=torch.bfloat16))   # bf16
  got RuntimeError('expected scalar type torch.float32 but found torch.bfloat16')

@molbap @guarin pls help review, thx!

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>

@guarin guarin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the pr! Seems we already have the same dtype conversion for the other models based on swin.

The only caveat I see is that we now have a different argument order because of BC:

    def get_attn_mask(self, input_resolution, device=None, dtype=None):

the others have dtype before device:

    def get_attn_mask(self, height: int, width: int, dtype: torch.dtype, device: torch.device) -> torch.Tensor | None:

cc @vasqu

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@molbap

molbap commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

agreed that arg order is best kept as it was before!

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>
@guarin

guarin commented Sep 23, 2026

Copy link
Copy Markdown
Member

agreed that arg order is best kept as it was before!

The arg order is already kept as before so all is good. The only issue is that the order is different from other models but this is required because of backwards compatibility.

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit but otherwise LGTM

w_region = (w_idx >= width - self.window_size).long() + (w_idx >= width - self.shift_size).long()
img_mask = (h_region[None, :, None, None] * 3 + w_region[None, None, :, None]).float()
img_mask = (h_region[None, :, None, None] * 3 + w_region[None, None, :, None]).to(
dtype=dtype if dtype is not None else torch.float32

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not default the dtype arg to float instead then? then we avoid the ternary no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well good advice. Looks better now.

@vasqu

vasqu commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Re the order: rather keep BC than breaking the order. its a bit awkward but not worth to break, especiallly considering that the model is quite old

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: maskformer

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 35942075177:2
Result: success | Jobs: 2 | Tests: 4 | Failures: 0 | Duration: 27s

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants