-
Notifications
You must be signed in to change notification settings - Fork 316
Add IntxUnpackedTensor #2732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metascroy
wants to merge
4
commits into
main
Choose a base branch
from
intx-awq-removal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add IntxUnpackedTensor #2732
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
test/quantization/quantize_/workflows/intx/test_intx_unpacked_tensor.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD 3-Clause license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
import torch | ||
from torch.testing._internal.common_utils import ( | ||
TestCase, | ||
run_tests, | ||
) | ||
|
||
from torchao.quantization import ( | ||
IntxWeightOnlyConfig, | ||
quantize_, | ||
) | ||
from torchao.quantization.granularity import PerGroup | ||
from torchao.quantization.utils import compute_error | ||
from torchao.utils import ( | ||
TORCH_VERSION_AT_LEAST_2_8, | ||
) | ||
|
||
|
||
@unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_8, "Need pytorch 2.8+") | ||
class TestIntxUnpackedTensor(TestCase): | ||
def setUp(self): | ||
self.config = IntxWeightOnlyConfig( | ||
weight_dtype=torch.int4, | ||
granularity=PerGroup(32), | ||
VERSION=2, | ||
) | ||
|
||
def test_linear(self): | ||
dtype = torch.bfloat16 | ||
device = "cpu" | ||
input = torch.randn(1, 128, dtype=dtype, device=device) | ||
linear = torch.nn.Linear(128, 256, dtype=dtype, device=device) | ||
original = linear(input) | ||
quantize_(linear, self.config) | ||
quantized = linear(input) | ||
error = compute_error(original, quantized) | ||
self.assertTrue(error > 20) | ||
|
||
def test_slice(self): | ||
dtype = torch.bfloat16 | ||
device = "cpu" | ||
dummy = torch.nn.Linear(256, 256, bias=False, dtype=dtype, device=device) | ||
|
||
dummy1 = torch.nn.Linear(256, 64, bias=False, dtype=dtype, device=device) | ||
dummy1.weight = torch.nn.Parameter( | ||
dummy.weight.narrow(0, 0, 64), requires_grad=False | ||
) | ||
|
||
dummy2 = torch.nn.Linear(128, 256, dtype=dtype, device=device) | ||
dummy2.weight = torch.nn.Parameter( | ||
dummy.weight.narrow(1, 0, 128), requires_grad=False | ||
) | ||
|
||
quantize_(dummy, self.config) | ||
weight1 = dummy.weight.narrow(0, 0, 64) | ||
weight2 = dummy.weight.narrow(1, 0, 128) | ||
|
||
self.assertEqual(weight1.int_data, dummy.weight.int_data.narrow(0, 0, 64)) | ||
self.assertEqual(weight1.scale, dummy.weight.scale.narrow(0, 0, 64)) | ||
|
||
self.assertEqual(weight2.int_data, dummy.weight.int_data.narrow(1, 0, 128)) | ||
self.assertEqual(weight2.scale, dummy.weight.scale.narrow(1, 0, 4)) | ||
|
||
# check for sliced weight, before and after float8 quantization | ||
# does not differ too much | ||
input = torch.randn(2, 256, dtype=dtype, device=device) | ||
res_ref = dummy1(input) | ||
dummy.weight = torch.nn.Parameter(weight1, requires_grad=False) | ||
res = dummy(input) | ||
assert compute_error(res, res_ref) > 20 | ||
|
||
input = torch.randn(2, 128, dtype=dtype, device=device) | ||
res_ref = dummy2(input) | ||
dummy.weight = torch.nn.Parameter(weight2, requires_grad=False) | ||
res = dummy(input) | ||
assert compute_error(res, res_ref) > 15 | ||
|
||
def test_slice_and_copy_(self): | ||
device = "cpu" | ||
l = torch.nn.Linear(1024, 1024).to(device).to(torch.bfloat16) | ||
l.weight = torch.nn.Parameter( | ||
torch.zeros(1024, 1024, dtype=torch.bfloat16, device=device) | ||
) | ||
quantize_(l, self.config) | ||
param = l.weight | ||
param_data = param.data | ||
param_data = param_data.narrow(0, 0, 512) | ||
assert param.data.int_data.data_ptr() == param_data.int_data.data_ptr() | ||
assert param.data.scale.data_ptr() == param_data.scale.data_ptr() | ||
assert param.data.zero_point.data_ptr() == param_data.zero_point.data_ptr() | ||
orig_value = param.data.int_data[0][0].item() | ||
|
||
# dummy_l has random input (shouldn't be 0) | ||
dummy_l = torch.nn.Linear(1024, 1024).to(device).to(torch.bfloat16) | ||
quantize_(dummy_l, self.config) | ||
quantized = dummy_l.weight | ||
quantized = quantized.narrow(0, 0, 512) | ||
|
||
param_data.copy_(quantized) | ||
|
||
# making sure param.data is updated | ||
assert param.data.int_data[0][0] != orig_value | ||
|
||
def test_to_dtype(self): | ||
activations_bf16 = torch.randn(1, 128, dtype=torch.bfloat16) | ||
activations_fp32 = torch.randn(1, 128, dtype=torch.float32) | ||
activations_fp16 = torch.randn(1, 128, dtype=torch.float16) | ||
|
||
linear = torch.nn.Linear(128, 256) | ||
quantize_(linear, self.config) | ||
|
||
linear.to(dtype=torch.float16) | ||
linear(activations_fp16) | ||
|
||
linear.to(dtype=torch.float32) | ||
linear(activations_fp32) | ||
|
||
linear.to(dtype=torch.bfloat16) | ||
linear(activations_bf16) | ||
|
||
def test_export(self): | ||
linear = torch.nn.Linear(128, 256) | ||
quantize_(linear, self.config) | ||
ep = torch.export.export(linear, (torch.randn(1, 128),)) | ||
assert "torch.ops.torchao.dequantize_affine.default" in ep.graph_module.code | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,8 @@ class PackingFormat(str, Enum): | |
preshuffled is referring to the preshuffled format used by fbgemm kernels | ||
""" | ||
PRESHUFFLED = "preshuffled" | ||
|
||
""" | ||
Unpacked means the subbyte quantized data is stored as int8 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this int only? we could be more specific and say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can make the format UNPACKED_TO_INT8 |
||
""" | ||
UNPACKED = "unpacked" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .intx_unpacked_tensor import IntxUnpackedTensor | ||
|
||
__all__ = [ | ||
"IntxUnpackedTensor", | ||
] |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we updated the name to
version