-
Notifications
You must be signed in to change notification settings - Fork 294
Add support for Int4GroupwisePreshuffleTensor for fbgemm #2421
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
Merged
Merged
Changes from all commits
Commits
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
105 changes: 105 additions & 0 deletions
105
test/quantization/quantize_/test_int4_groupwise_preshuffle.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,105 @@ | ||
# 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 ( | ||
FbgemmConfig, | ||
quantize_, | ||
) | ||
from torchao.quantization.utils import compute_error | ||
from torchao.utils import ( | ||
TORCH_VERSION_AT_LEAST_2_8, | ||
_is_fbgemm_genai_gpu_available, | ||
is_sm_at_least_90, | ||
) | ||
|
||
|
||
@unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_8, "Need pytorch 2.8+") | ||
@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") | ||
@unittest.skipIf(not is_sm_at_least_90(), "Nedd sm90+") | ||
@unittest.skipIf( | ||
not _is_fbgemm_genai_gpu_available(), "Requires fbgemm-gpu-genai >= 1.2.0" | ||
) | ||
class TestInt4GroupwisePreshuffleTensor(TestCase): | ||
def setUp(self): | ||
self.config = FbgemmConfig( | ||
input_dtype=torch.bfloat16, | ||
weight_dtype=torch.int4, | ||
output_dtype=torch.bfloat16, | ||
block_size=[1, 128], | ||
preshuffle=True, | ||
) | ||
self.bmm_config = FbgemmConfig( | ||
input_dtype=torch.bfloat16, | ||
weight_dtype=torch.int4, | ||
output_dtype=torch.bfloat16, | ||
block_size=[1, 1, 128], | ||
preshuffle=True, | ||
) | ||
self.GPU_DEVICES = ["cuda"] if torch.cuda.is_available() else [] | ||
|
||
def test_linear(self): | ||
dtype = torch.bfloat16 | ||
device = "cuda" | ||
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) | ||
self.assertTrue(compute_error(original, quantized) > 20) | ||
|
||
def test_bmm(self): | ||
class M(torch.nn.Module): | ||
def __init__(self, weight): | ||
super().__init__() | ||
self.weight = weight | ||
|
||
def forward(self, x): | ||
return torch.bmm(x, self.weight) | ||
|
||
dtype = torch.bfloat16 | ||
device = "cuda" | ||
input = torch.randn(10, 32, 128, dtype=dtype, device=device) | ||
weight = torch.randn(10, 128, 256, dtype=dtype, device=device) | ||
m = M(weight).eval() | ||
original = m(input) | ||
m.weight = torch.nn.Parameter(m.weight.transpose(1, 2).contiguous()) | ||
quantize_(m, self.bmm_config, filter_fn=lambda x, fqn: True) | ||
quantized = m(input) | ||
self.assertTrue(compute_error(original, quantized) > 18) | ||
|
||
def test_to_device(self): | ||
for device in self.GPU_DEVICES: | ||
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | ||
quantize_(linear, self.config) | ||
linear.to(device) | ||
|
||
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | ||
quantize_(linear, self.config) | ||
linear.to(device=device) | ||
|
||
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | ||
quantize_(linear, self.config) | ||
linear.to(device) | ||
|
||
def test_module_path(self): | ||
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | ||
quantize_(linear, self.config) | ||
self.assertEqual( | ||
str(type(linear.weight)), | ||
"<class 'torchao.quantization.Int4GroupwisePreshuffleTensor'>", | ||
) | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,5 @@ | |
"to_fbgemm_fp8", | ||
"FbgemmFp8Tensor", | ||
"Int8DynamicActInt4WeightCPULayout", | ||
"Int4GroupwisePreshuffleTensor", | ||
] |
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
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,9 @@ | ||
from .int4_groupwise_preshuffle_tensor import ( | ||
Int4GroupwisePreshuffleTensor, | ||
) | ||
|
||
Int4GroupwisePreshuffleTensor.__module__ = "torchao.quantization" | ||
|
||
__all__ = [ | ||
"Int4GroupwisePreshuffleTensor", | ||
] |
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.
can we confirm (by actually testing it) that we can change the directory location later without breaking BC?
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.
I have a test that verifies the loaded weight have module path
torchao.quantization.Int4GroupwisePreshuffleTensor
, this (type(tensor)
)is used in the load code path: https://github.com/pytorch/pytorch/blob/d4b8857e51a089b7e0e722689398c5c3ada274c9/torch/_tensor.py#L262 which gives us good confidence that it would work as long as we do thisbut I can do a e2e test a bit later by uploading the file in huggingface hub and change the path locally to verify as well
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.
added in #2437