-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intx weight only linear quantizer for mps (#1192)
Summary: Pull Request resolved: #1192 Differential Revision: D65079774
- Loading branch information
1 parent
4f1fc4c
commit ed83de7
Showing
3 changed files
with
280 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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,104 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from typing import Optional | ||
import copy | ||
import os | ||
import sys | ||
|
||
import torch | ||
import torchao_mps_ops | ||
import unittest | ||
|
||
torchao_root: Optional[str] = os.getenv("TORCHAO_ROOT") | ||
assert torchao_root is not None, "TORCHAO_ROOT is not set" | ||
|
||
sys.path.insert(0, torchao_root) | ||
from torchao.experimental.quant_api import IntxWeightOnlyLinearQuantizer | ||
|
||
|
||
def parameterized(test_cases): | ||
def decorator(func): | ||
def wrapper(self): | ||
for case in test_cases: | ||
with self.subTest(case=case): | ||
func(self, *case) | ||
return wrapper | ||
return decorator | ||
|
||
|
||
class TestIntxWeightOnlyLinearQuantizer(unittest.TestCase): | ||
cases = [(nbit,) for nbit in range(1, 8)] | ||
|
||
def _model_setup(self): | ||
k0 = 512 | ||
k1 = 256 | ||
k2 = 128 | ||
k3 = 1024 | ||
layers = [ | ||
torch.nn.Linear(k0, k1, bias=False), | ||
torch.nn.Linear(k1, k2, bias=False), | ||
torch.nn.Linear(k2, k3, bias=False), | ||
] | ||
model = torch.nn.Sequential(*layers) | ||
return model | ||
|
||
def _quantize_model(self, model, precision, nbit, group_size): | ||
quantizer = IntxWeightOnlyLinearQuantizer( | ||
device="mps", | ||
precision=precision, | ||
bitwidth=nbit, | ||
groupsize=group_size, | ||
) | ||
quantized_model = copy.deepcopy(model) | ||
quantized_model = quantizer.quantize(quantized_model) | ||
return quantized_model | ||
|
||
@parameterized(cases) | ||
def test_export(self, nbit): | ||
model = self._model_setup() | ||
group_size = 32 | ||
m = 3 | ||
k0 = 512 | ||
activations = torch.randn(m, k0, dtype=torch.float32, device="mps") | ||
|
||
quantized_model = self._quantize_model(model, torch.float32, nbit, group_size) | ||
exported = torch.export.export(quantized_model, (activations,)) | ||
|
||
for node in exported.graph.nodes: | ||
if node.op == "call_function": | ||
self.assertTrue( | ||
str(node.target) | ||
== f"torchao._linear_fp_act_{nbit}bit_weight.default" | ||
) | ||
|
||
@parameterized(cases) | ||
def test_2d_output_device_and_shape(self, nbit): | ||
model = self._model_setup() | ||
group_size = 32 | ||
m = 3 | ||
activations = torch.randn(m, 512, dtype=torch.float32, device="mps") | ||
|
||
quantized_model = self._quantize_model(model, torch.float32, nbit, group_size) | ||
result = quantized_model(activations) | ||
self.assertTrue(result.is_mps) | ||
self.assertTrue(result.shape == (m, 1024)) | ||
|
||
@parameterized(cases) | ||
def test_3d_output_device_and_shape(self, nbit): | ||
model = self._model_setup() | ||
group_size = 32 | ||
leading_shape = (3, 5) | ||
activations = torch.randn(*leading_shape, 512, dtype=torch.float32, device="mps") | ||
|
||
quantized_model = self._quantize_model(model, torch.float32, nbit, group_size) | ||
result = quantized_model(activations) | ||
self.assertTrue(result.is_mps) | ||
self.assertTrue(result.shape == (*leading_shape, 1024)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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