Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Conversation

Satrat
Copy link

@Satrat Satrat commented Jul 13, 2023

Int4 QAT was working out of the box, but the onnx export step was failing because onnx only supports 8-bit integer quantization. The fix for this was to trick onnx into thinking we were exporting 8-bit quantized weights by manually adjusting the range of the FakeQuantize modules. The weights themselves will still be between [-8, 7], and the original range boundaries are still accessible via the FakeQuantize.quant_min and FakeQuantize.quant_max attributes (they are deprecated by PyTorch, replaced by FakeQuantize.activation_post_process.quant_[min\max])

Testing

Run tests/sparseml/pytorch/test_torch_to_onnx_exporter/test_export_4bit_model for example usage, or just run TorchToONNX.export() on any Int4 QAT model:

model = ...
sample_batch = ...
exporter = TorchToONNX(sample_batch)
exporter.export(model, "model.onnx")

Old Behavior

Export fails with error:

"name": "SymbolicValueError",
"message": "For (quant_min, quant_max), ONNX allows only (0, 127), (0, 255) and (-128, 127). Got (-8, 7)

New Behavior

Export completes without errors

@Satrat Satrat changed the title Support Int4 ONNX Export [WIP] Support Int4 ONNX Export Jul 13, 2023
@Satrat Satrat changed the title [WIP] Support Int4 ONNX Export Support Int4 ONNX Export Jul 13, 2023
@Satrat Satrat requested a review from bfineran July 17, 2023 18:57
Copy link
Member

@rahul-tuli rahul-tuli left a comment

Choose a reason for hiding this comment

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

Nice diff! LGTM pending comments

@Satrat Satrat requested a review from rahul-tuli July 19, 2023 14:33
@Satrat Satrat merged commit ab9a168 into main Jul 19, 2023
@Satrat Satrat deleted the int4-onnx-export branch July 19, 2023 19:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants