Skip to content

[Model] Fix hunyuan-vl shape mismatch#31403

Closed
Potabk wants to merge 1 commit intovllm-project:mainfrom
Potabk:hunyuan
Closed

[Model] Fix hunyuan-vl shape mismatch#31403
Potabk wants to merge 1 commit intovllm-project:mainfrom
Potabk:hunyuan

Conversation

@Potabk
Copy link
Copy Markdown
Contributor

@Potabk Potabk commented Dec 27, 2025

Purpose

After mmencoderattn, we got the out shaple like :[B, S, N, D], We should merge the multi-head before entering the O matrix.

Test Plan

Test Result


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

Signed-off-by: wangli <wangli858794774@gmail.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a shape mismatch in HunYuanVisionAttention by correctly reshaping the attention output before the final projection. The fix appears to be correct given the context of a 4D tensor output from the attention layer. I have one suggestion to make the implementation more maintainable and robust.

Comment on lines +250 to +255
out = out.view(
x.size(0),
-1,
self.num_attention_heads_per_partition
* self.hidden_size_per_attention_head,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

For better maintainability and readability, you can simplify the calculation of the last dimension in the view operation. Instead of re-calculating the partitioned hidden size from the number of heads and head size, you can directly use the shape of the weight from the subsequent projection layer o_proj. This makes the code more robust to future changes as it directly references the expected input dimension of the next layer.

        out = out.view(
            x.size(0),
            -1,
            self.o_proj.weight.shape[1],
        )

Comment on lines +250 to +255
out = out.view(
x.size(0),
-1,
self.num_attention_heads_per_partition
* self.hidden_size_per_attention_head,
)
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.

After mmencoderattn, we got the out shaple like :[B, S, N, D], We should merge the multi-head before entering the O matrix.

Not really, we will automatically reshape output to make sure it align with q, k, v input:

is_reshaped = query.dim() != 4
query, key, value = self.maybe_reshape_qkv_to_4d(
query, key, value, bsz, q_len, kv_len
)
output = vit_flash_attn_wrapper(
q=query,
k=key,
v=value,
cu_seqlens=cu_seqlens,
max_seqlen=max_seqlen,
batch_size=bsz,
is_rocm_aiter=(self.attn_backend == AttentionBackendEnum.ROCM_AITER_FA),
fa_version=self._fa_version,
)
if is_reshaped:
output = output.reshape(bsz, q_len, -1)

So I think we won't hit the shape mismatch issue here. 🤔

Copy link
Copy Markdown
Contributor Author

@Potabk Potabk Dec 27, 2025

Choose a reason for hiding this comment

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

So, it seems the issue for ascend, I'll have a further check

Copy link
Copy Markdown
Contributor Author

@Potabk Potabk Dec 27, 2025

Choose a reason for hiding this comment

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

Thx, Confirmed it is the issue for forward_oot vllm-project/vllm-ascend#5443

@Potabk Potabk closed this Dec 27, 2025
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.

2 participants