Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 14, 2025

This PR implements the missing quantize_per_channel and dequantize_per_channel operations in the torchlib quantized_decomposed module.

Changes

Added two new functions to onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py:

quantized_decomposed_quantize_per_channel

  • Implements per-channel quantization using ONNX QuantizeLinear with per-axis support
  • Takes tensor inputs for scales and zero_points (one value per channel)
  • Supports axis parameter to specify the quantization dimension
  • Uses ONNX opset23 for per-axis quantization capabilities

quantized_decomposed_dequantize_per_channel

  • Implements per-channel dequantization using ONNX DequantizeLinear with per-axis support
  • Takes tensor inputs for scales and optional zero_points
  • zero_points parameter is Optional[TensorType] matching PyTorch reference
  • Supports both default output type and explicit output_dtype parameter

Implementation Details

Both functions:

  • Follow the existing code patterns using @torch_op decorator with trace_only=True
  • Have function signatures that match the PyTorch reference implementations from torch.ao.quantization.fx._decomposed
  • Use ONNX opset23 to leverage the axis and output_dtype parameters for per-axis quantization
  • Are properly registered and accessible as TracedOnnxFunctions

The implementation leverages ONNX's native per-axis quantization support rather than implementing the tensor manipulation logic from the PyTorch reference, making it more efficient and aligned with ONNX best practices.

Testing

Validated that:

  • Functions import correctly and have proper signatures
  • Function schemas are correctly generated with appropriate parameter types
  • No existing functionality is broken (per-tensor functions still work)
  • Functions can be used to build ONNX models

Fixes #2389.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot AI changed the title [WIP] [torchlib] Implement quantize_per_channel and dequantize_per_channel [torchlib] Implement quantize_per_channel and dequantize_per_channel Jun 14, 2025
Copilot AI requested a review from justinchuby June 14, 2025 15:50
Comment on lines +81 to +82
quant_min: int,
quant_max: int,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these unused? Why is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These parameters are unused because the ONNX QuantizeLinear/DequantizeLinear operators don't require explicit quant_min/quant_max parameters - they determine the quantization range from the data type and quantization parameters. The parameters are kept in the function signature for API compatibility with PyTorch's reference implementation, following the same pattern as the existing per-tensor functions above.

@justinchuby justinchuby marked this pull request as ready for review June 14, 2025 16:10
@codecov
Copy link

codecov bot commented Jun 14, 2025

Codecov Report

❌ Patch coverage is 54.54545% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.09%. Comparing base (43b1b74) to head (973e372).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...unction_libs/torch_lib/ops/quantized_decomposed.py 54.54% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2390      +/-   ##
==========================================
- Coverage   70.09%   70.09%   -0.01%     
==========================================
  Files         228      228              
  Lines       27393    27404      +11     
  Branches     2781     2782       +1     
==========================================
+ Hits        19202    19208       +6     
- Misses       7234     7239       +5     
  Partials      957      957              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@justinchuby
Copy link
Collaborator

Please format the code using lintrunner. You can do

pip install --upgrade lintrunner lintrunner-adapters
lintrunner init

then

lintrunner f --all-files

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 18, 2025

Please format the code using lintrunner. You can do

...

Done! Code has been formatted using lintrunner. The formatting fixed import sorting, whitespace issues, and line wrapping. Commit: 82c8f9e

Copilot AI requested a review from justinchuby June 18, 2025 00:28
@justinchuby justinchuby requested a review from xadupre December 30, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

[torchlib] Implement quantize_per_channel and dequantize_per_channel

2 participants