Skip to content

mps + compile + quant: introduce mps_op_lib #1530

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion install/.pins/torchao-pin.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
711fa0809f06fc97febd0c3fe72563c3fe227e51
7513042f39515af4c643bc1f9399952ad7f4f904
24 changes: 19 additions & 5 deletions torchchat/utils/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
)
from torchao.experimental.quant_api import (
int8_dynamic_activation_intx_weight,
IntxWeightEmbeddingQuantizer,
EmbeddingQuantizer,
)
from torchao.quantization.granularity import (
PerGroup,
Expand Down Expand Up @@ -137,12 +137,12 @@ def quantize_model(
group_size = q_kwargs["groupsize"]
bit_width = q_kwargs["bitwidth"]
has_weight_zeros = q_kwargs["has_weight_zeros"]
granularity = PerRow() if group_size == -1 else PerGroup(group_size)
granularity = PerRow() if group_size == -1 else PerGroup(group_size)
weight_dtype = getattr(torch, f"int{bit_width}")

try:
quantize_(
model,
model,
int8_dynamic_activation_intx_weight(
weight_dtype=weight_dtype,
granularity=granularity,
Expand All @@ -154,7 +154,7 @@ def quantize_model(
print("Encountered error during quantization: {e}")
print("Trying with PlainLayout")
quantize_(
model,
model,
int8_dynamic_activation_intx_weight(
weight_dtype=weight_dtype,
granularity=granularity,
Expand Down Expand Up @@ -939,7 +939,7 @@ def quantized_model(self) -> nn.Module:
# class references
quantizer_class_dict = {
"embedding": EmbeddingOnlyQuantHandler,
"embedding:wx": IntxWeightEmbeddingQuantizer,
"embedding:wx": EmbeddingQuantizer,
"linear:int8": WeightOnlyInt8QuantHandler,
"precision": PrecisionHandler,
"executor": ExecutorHandler,
Expand Down Expand Up @@ -979,5 +979,19 @@ def quantized_model(self) -> nn.Module:
except Exception as e:
print("Unable to load torchao mps ops library.")

torchao_experimental_mps_op_lib_spec = importlib.util.spec_from_file_location(
"torchao_experimental_mps_op_lib",
f"{torchao_build_path}/src/ao/torchao/experimental/ops/mps/mps_op_lib.py",
)
torchao_experimental_mps_op_lib = importlib.util.module_from_spec(
torchao_experimental_mps_op_lib_spec
)
sys.modules["torchao_experimental_mps_op_lib"] = torchao_experimental_mps_op_lib
torchao_experimental_mps_op_lib_spec.loader.exec_module(
torchao_experimental_mps_op_lib
)
from torchao_experimental_mps_op_lib import *


except Exception as e:
print("Unable to import torchao experimental quant_api with error: ", e)
Loading