Add squeeze/unsqueeze support to IntQuantTensor - #1622
Draft
mumallaeng wants to merge 1 commit into
Draft
mumallaeng wants to merge 1 commit into
mumallaeng wants to merge 1 commit into
Conversation
QuantTensor had no squeeze/unsqueeze, so torch.squeeze/torch.unsqueeze (and the .squeeze()/.unsqueeze() methods) either raised or silently dropped quantization metadata (scale/zero_point/bit_width) for IntQuantTensor. Add squeeze/unsqueeze to IntMixin following the same pattern already used by transpose/permute: per-channel metadata (same rank as the value tensor) is squeezed/unsqueezed alongside the value, per-tensor (lower-rank) metadata is left untouched. Register torch.squeeze/ torch.unsqueeze handlers in torch_handler.py so the free-function form dispatches correctly too. FloatQuantTensor is intentionally left out of scope, per the prior PR Xilinx#941 discussion - a follow-up can extend this once FloatQuantTensor's metadata shape handling is worked out separately. Signed-off-by: mumallaeng <mumallaeng@outlook.com>
Collaborator
|
Hey, thanks for opening this PR. Unless this is causing some issues for you, we might want to skip this change for now. We are planning major changes around QuantTensor #1579, and we might want to merge that before applying other changes to QuantTensor. |
Author
|
@Giuseppe5 Thanks for the heads-up, that makes sense. I'll convert this to a draft and hold it until #1579 lands, then re-check whether squeeze/unsqueeze is still needed on top of the tensor-subclass QuantTensor. |
mumallaeng
marked this pull request as draft
September 22, 2026 01:00
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Reason for this PR
QuantTensor had no squeeze/unsqueeze, so torch.squeeze/torch.unsqueeze (and the .squeeze()/.unsqueeze() methods) either raised or silently dropped quantization metadata (scale/zero_point/bit_width) for IntQuantTensor.
Closes #891.
Changes Made in this PR
Added squeeze/unsqueeze to IntMixin in base_quant_tensor.py, following the same pattern already used by transpose/permute: per-channel metadata (same rank as the value tensor) is squeezed/unsqueezed alongside the value, per-tensor (lower-rank) metadata is left untouched. Registered torch.squeeze/torch.unsqueeze handlers in torch_handler.py so the free-function form dispatches correctly too.
FloatQuantTensor is intentionally left out of scope, per the prior PR #941 discussion - a follow-up can extend this once FloatQuantTensor's metadata shape handling is worked out separately.
Testing Summary
Added test_quant_tensor_squeeze and test_quant_tensor_unsqueeze to tests/brevitas/quant_tensor/test_quant_tensor.py, covering both per-tensor and per-channel scale. Ran the full test_quant_tensor.py file locally (30/30 pass). Confirmed the new tests fail with AttributeError against the pre-change source and pass after. isort/yapf (pinned versions from .pre-commit-config.yaml) show no diff on the changed files.
Risk Highlight
Checklist
masterbranch.