Skip to content

Conversation

@AryanBagade
Copy link

@AryanBagade AryanBagade commented Dec 5, 2025

Changes keepdim default from False to True in _choose_qparams_affine to match _choose_scale_float8 behavior. This ensures scale/zero_point maintain the same rank as input tensor, making downstream handling more consistent.

Part 1 of fixing #3324

Changes

Core Changes (torchao/quantization/quant_primitives.py)

  • Changed keepdim: bool = Falsekeepdim: bool = True in both choose_qparams_affine (line 1220) and _choose_qparams_affine (line 1526)
  • Added reshape logic (lines 1600-1608) to match _choose_scale_float8 behavior
  • Saved original_input_size before reshaping to compute correct output shape
  • Added documentation explaining the alignment with _choose_scale_float8

Workflow Simplification (torchao/quantization/quantize_/workflows/intx/intx_unpacked_to_int8_tensor.py)

  • Removed manual reshape logic (lines 247-254) that is no longer needed

Test Updates(test/quantization/test_quant_primitives.py)

  • Updated 3 test cases to squeeze scale/zero_point before comparison with reference values
  • All 7 test_choose_qparams tests now pass

Changes keepdim default from False to True in _choose_qparams_affine to match
_choose_scale_float8 behavior. This ensures scale/zero_point maintain the same rank as input tensor, making downstream handling more consistent.

Fixes pytorch#3324
@pytorch-bot
Copy link

pytorch-bot bot commented Dec 5, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/3447

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 13 New Failures

As of commit bdf1210 with merge base aa21b80 (image):

NEW FAILURES - The following jobs have failed:

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

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 5, 2025
Comment on lines +1601 to +1608
# Reshape scale and zero_point to match expected output shape
# This aligns with _choose_scale_float8 behavior
if keepdim:
output_shape = [
original_input_size[i] // block_size[i] for i in range(len(block_size))
]
scale = scale.reshape(output_shape)
zero_point = zero_point.reshape(output_shape)
Copy link
Contributor

@jerryzh168 jerryzh168 Dec 5, 2025

Choose a reason for hiding this comment

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

is this needed? keepdim=True is already used in int8_tensor.py

Edit: oh OK I think it is needed because the reshapes we are doing before

input = input.view(shape_for_reduction)

Copy link
Author

Choose a reason for hiding this comment

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

Yes, this reshape is needed! int8_tensor.py passes scale/zero_point directly to quantize_affine, which internally reshapes the scale at line 461. So it doesn't need the output to be pre-reshaped. but the thing is, IntxUnpackedToInt8Tensor.init (lines131-136) asserts that scale.shape must exactly match tuple(n_blocks) before passing to quantize_affine:

  assert scale.shape == tuple(n_blocks), ( 
  f"Expected scale to have shape {n_blocks} (inferred from
  block_size={block_size}), but got {scale.shape}"
  )

Copy link
Author

Choose a reason for hiding this comment

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

basically without this reshape:

  • keepdim=True gives scale shape like (1, 5, 1) for block_size (10, 4) on input (10, 20)
  • But IntxUnpackedToInt8Tensor expects (1, 5)
  • The assertion would fail!

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah that's correct, I'll approve the CI to run all the tests to see, especially this one:

block_size = (3, 3, 2, 2)

Copy link
Contributor

Choose a reason for hiding this comment

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

also in a future PR we could remove some of the reshaping logic in quantize_affine/dequantize _affine as well:

shape_for_reduction, reduction_dims = _get_reduction_params(
block_size, input.size()
)
original_shape = input.shape
input = input.view(shape_for_reduction)
shape_after_reduction = shape_for_reduction
for i in reduction_dims:
shape_after_reduction[i] = 1
scale = scale.view(shape_after_reduction)

also eventually remove the block_size arg from these ops (bc-breaking)

Copy link
Author

Choose a reason for hiding this comment

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

sure, sounds like a plan, happy to contribute

@jerryzh168
Copy link
Contributor

Thanks, I think it's a good start, we can remove keepdim arg in next PR after this PR is merged

@AryanBagade
Copy link
Author

I see 25 integration tests failed due to backward compatibility issues with the keepdim=True default change

@jerryzh168
Copy link
Contributor

it's expected, I think maybe just don't change the default for now, but turn the keepdim to True in these tests one by one to make sure these tests are fixed, and alls the callsites are fixed before making the switch would be better

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants