Skip to content

Add torchao mps lowbit ops to llama runner #7037

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 2 commits into from
Dec 13, 2024
Merged
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
7 changes: 7 additions & 0 deletions examples/models/llama/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ if(EXECUTORCH_BUILD_TORCHAO)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../third-party/ao/torchao/experimental ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/ao/torchao/experimental)
target_link_options_shared_lib(torchao_ops_executorch)
list(APPEND link_libraries torchao_ops_executorch)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/../../../third-party/ao/torchao/experimental/ops/mps
${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/ao/torchao/experimental/ops/mps)
target_link_options_shared_lib(torchao_ops_mps_executorch)
list(APPEND link_libraries torchao_ops_mps_executorch)
endif()
endif()

set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack)
Expand Down
2 changes: 1 addition & 1 deletion examples/models/llama/export_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ def get_quantizer_and_quant_params(args):

def _qmode_type(value):
choices = ["int8", "8da4w", "8da4w-gptq", "vulkan_4w"]
patterns = [r"torchao:8da(\d+)w"]
patterns = [r"torchao:8da(\d+)w", r"torchao:fpa(\d+)w"]

if value in choices:
return value
Expand Down
33 changes: 28 additions & 5 deletions examples/models/llama/source_transformation/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,35 @@ def quantize( # noqa C901
if qmode == "int8":
# Add quantization mode options here: group size, bit width, etc.
return WeightOnlyInt8QuantHandler(model).quantized_model()
elif qmode.startswith("torchao:"):
elif qmode.startswith("torchao:fpa"):
pattern = r"torchao:fpa(\d+)w"
matches = re.findall(pattern, qmode)
assert len(matches) == 1, f"Expected 1 match for pattern but got {len(matches)}"
bitwidth = int(matches[0][0])
_load_torchao_aten_lib(libname="libtorchao_ops_mps_aten")
from torchao.experimental.quant_api import UIntxWeightOnlyLinearQuantizer

with torch.no_grad():
model = (
UIntxWeightOnlyLinearQuantizer(
device="mps",
precision=torch.float32,
groupsize=group_size,
bitwidth=bitwidth,
)
.quantize(model)
.to("cpu")
)

if verbose:
print("quantized model:", model)
Comment on lines +95 to +96
Copy link
Contributor

Choose a reason for hiding this comment

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

Please using logging

return model
elif qmode.startswith("torchao:8da"):
pattern = r"torchao:8da(\d+)w"
matches = re.findall(pattern, qmode)
assert len(matches) == 1, f"Expected 1 match for pattern but got {len(matches)}"
bitwidth = int(matches[0][0])
_load_torchao_ops_aten()
_load_torchao_aten_lib(libname="libtorchao_ops_aten")
from torchao.experimental.quant_api import Int8DynActIntxWeightLinearQuantizer

with torch.no_grad():
Expand Down Expand Up @@ -729,7 +752,7 @@ def get_quant_embedding_transform(args):
bitwidth, group_size = args.embedding_quantize.split(":")[1].split(",")
group_size = int(group_size)
bitwidth = int(bitwidth)
_load_torchao_ops_aten()
_load_torchao_aten_lib(libname="libtorchao_ops_aten")
from torchao.experimental.quant_api import IntxWeightEmbeddingQuantizer

def _torchao_embedding_quantizer(model):
Expand Down Expand Up @@ -785,15 +808,15 @@ def get_quant_weight_transform(args, dtype_override, verbose):
)


def _load_torchao_ops_aten():
def _load_torchao_aten_lib(libname):
import glob
import os

libs = glob.glob(
os.path.abspath(
os.path.join(
os.environ.get("CMAKE_INSTALL_PREFIX", ""),
"lib/libtorchao_ops_aten.*",
f"lib/{libname}.*",
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion third-party/ao
Submodule ao updated 354 files
Loading