Skip to content

Conversation

Copy link

Copilot AI commented Nov 17, 2025

Addresses review feedback on #15610 regarding incorrect alignment calculation for scratch_for_quant_dequant buffer allocation.

Bug

The alignment mask -(kAlignment - 1) produces incorrect results:

  • For kAlignment = 32: mask is -31 = 0xFFFFFFE1 (bit 0 remains set)
  • Results in off-by-one alignment: align(0) = 1, align(32) = 33, align(64) = 65

Fix

Changed mask to -kAlignment and added inline test case documentation:

// Before
size_per_thread_qdq_vec = (size_per_thread_qdq_vec + kAlignment - 1) & (-(kAlignment - 1));

// After  
size_per_thread_qdq_vec = (size_per_thread_qdq_vec + kAlignment - 1) & (-kAlignment);

For kAlignment = 32: mask is now -32 = 0xFFFFFFE0, properly zeroing lower 5 bits to align to 32-byte boundaries.

Documentation

Added minimal test case in code comments demonstrating:

  • Correct formula: -kAlignment = -32 = 0xFFFFFFE0 (zeros bits 0-4)
  • Wrong formula: -(kAlignment-1) = -31 = 0xFFFFFFE1 (bit 0 still set)
  • Concrete examples showing the off-by-one bug with the wrong mask

The inline comments provide clear examples with hex values illustrating why the incorrect mask fails to properly align values.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 17, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/15852

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures, 118 Pending

As of commit 37bfa7c with merge base 7600df8 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla
Copy link

meta-cla bot commented Nov 17, 2025

Hi @Copilot!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Copilot AI changed the title [WIP] Address feedback on allocation overhead in quantized sdpa PR Fix incorrect alignment mask in quantized SDPA allocation Nov 17, 2025
Copilot AI requested a review from mergennachin November 17, 2025 16:50
Copilot finished work on behalf of mergennachin November 17, 2025 16:50
@mergennachin
Copy link
Contributor

@copilot Create a test case in the comments

@mergennachin
Copy link
Contributor

@copilot create a minimal test case in the comments

Copy link
Author

Copilot AI commented Nov 17, 2025

@copilot Create a test case in the comments

Added a minimal test case in the code comments at lines 782-789 demonstrating:

  • Correct formula: (x + 32 - 1) & 0xFFFFFFE0 properly aligns to 32-byte boundaries
  • Wrong formula: (x + 32 - 1) & 0xFFFFFFE1 causes off-by-one errors (e.g., 32→33 instead of 32→32)

The test case shows concrete examples with hex values to illustrate why the wrong mask fails.

Fixed in commit 1411727.

@copilot create a minimal test case in the comments

Added a minimal test case in the code comments at lines 782-789. The inline test case shows:

  • -kAlignment = -32 = 0xFFFFFFE0 (correct - zeros bits 0-4)
  • -(kAlignment-1) = -31 = 0xFFFFFFE1 (wrong - bit 0 remains set)

With concrete examples demonstrating the off-by-one bug.

Fixed in commit 1411727.

Copilot finished work on behalf of mergennachin November 17, 2025 16:56
@mergennachin
Copy link
Contributor

@kimishpatel Looks like AI is correct :)

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