-
Notifications
You must be signed in to change notification settings - Fork 225
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
Lowbit #1070
Lowbit #1070
Changes from all commits
74396ff
95c66b8
3b7e17c
008dd90
1406a3c
334bc5b
b8ee9da
a71ec16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
85d03de43160328eaf350e7ec3877d3d7b57da50 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,6 +395,13 @@ def decode_n_tokens( | |
) | ||
input_pos += 1 | ||
break | ||
if _i == 1: | ||
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. Remove this before final landing. It was to do more accurate tokens/sec measurement during development, especially for torch.compile. |
||
t0 = time.time() | ||
if _i == num_new_tokens - 2: | ||
t1 = time.time() | ||
print(f"\nTime to generate {num_new_tokens-2} tokens: {t1-t0}") | ||
print(f"\nTokens/sec to generate {num_new_tokens-2} tokens: {(num_new_tokens-2) / (t1-t0)}") | ||
|
||
|
||
if not encountered_eos: | ||
eos_token = torch.tensor( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,9 +92,21 @@ def quantize_model( | |
|
||
try: | ||
# Easier to ask forgiveness than permission | ||
quant_handler = ao_quantizer_class_dict[quantizer]( | ||
groupsize=q_kwargs["groupsize"], device=device, precision=precision | ||
) | ||
if quantizer == "linear:a8wlow": | ||
quant_handler = ao_quantizer_class_dict[quantizer]( | ||
device=device, | ||
precision=precision, | ||
bitwidth=q_kwargs.get("bitwidth", 4), | ||
groupsize=q_kwargs.get("groupsize", 128), | ||
has_weight_zeros=q_kwargs.get("has_weight_zeros", False), | ||
squeeze_unsqueeze_dim0=True, | ||
Comment on lines
+97
to
+102
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. nit: We can probably curry Int8DynActLowbitWeightQuantizer to match the same args as the other AO quant classes 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. I modeled the args after Int8DynActInt4WeightQuantizer, and used "groupsize" instead of "group_size" to match the experience. But Int8DynActInt4WeightQuantizer doesn't have bitwidth and has_weight_zeros as concepts, but happy to rename those to whatever you think best. |
||
) | ||
else: | ||
quant_handler = ao_quantizer_class_dict[quantizer]( | ||
groupsize=q_kwargs["groupsize"], | ||
device=device, | ||
precision=precision, | ||
) | ||
except TypeError as e: | ||
if "unexpected keyword argument 'device'" in str(e): | ||
quant_handler = ao_quantizer_class_dict[quantizer]( | ||
|
@@ -581,3 +593,33 @@ def quantized_model(self) -> nn.Module: | |
"linear:int4": Int4WeightOnlyQuantizer, | ||
"linear:a8w4dq": Int8DynActInt4WeightQuantizer, | ||
} | ||
|
||
try: | ||
import importlib.util | ||
import sys | ||
import os | ||
torchao_build_path = f"{os.getcwd()}/torchao-build" | ||
|
||
# Load quantizer | ||
torchao_experimental_spec = importlib.util.spec_from_file_location( | ||
"torchao_experimental", | ||
f"{torchao_build_path}/src/ao/torchao/experimental/kernels/cpu/linear/examples/torch_custom_op/torch_custom_op.py", | ||
) | ||
torchao_experimental = importlib.util.module_from_spec(torchao_experimental_spec) | ||
sys.modules["torchao_experimental"] = torchao_experimental | ||
torchao_experimental_spec.loader.exec_module(torchao_experimental) | ||
from torchao_experimental import Int8DynActLowbitWeightQuantizer | ||
ao_quantizer_class_dict["linear:a8wlow"] = Int8DynActLowbitWeightQuantizer | ||
|
||
# Try loading custom op | ||
try: | ||
import glob | ||
libs = glob.glob(f"{torchao_build_path}/cmake-out/liblowbit_op_aten.*") | ||
libs = list(filter(lambda l: (l.endswith("so") or l.endswith("dylib")), libs)) | ||
torch.ops.load_library(libs[0]) | ||
except Exception as e: | ||
print("Failed to load custom ops : ", e) | ||
print("Slow fallback kernels will be used.") | ||
|
||
except Exception as e: | ||
print(f"Failed to use torchao_experimental kernels: {e}") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
if [ -z "${TORCHCHAT_ROOT}" ]; then | ||
# Get the absolute path of the current script | ||
SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" | ||
# Get the absolute path of the parent directory | ||
TORCHCHAT_ROOT="$(dirname "$SCRIPT_PATH")" | ||
fi | ||
|
||
if [ -z "${TORCHAO_BUILD_DIR}" ]; then | ||
TORCHAO_BUILD_DIR="torchao-build" | ||
fi | ||
|
||
source "$TORCHCHAT_ROOT/scripts/install_utils.sh" | ||
|
||
find_cmake_prefix_path | ||
clone_torchao | ||
install_torchao_custom_aten_ops |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,54 @@ install_executorch_libs() { | |
|
||
install_executorch_python_libs $1 | ||
} | ||
|
||
clone_torchao() { | ||
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. We have a pin for 0.5.0 that you might be able to piggy back off of. Can save you some effort 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. This is cloning torchao from source, but the pin looks like it is from pip? 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. We can decide on it when your diff lands Big thing here is that AO doesn't have Mac nightlies so I'm fine with us moving back to direct clones for mac |
||
echo "Cloning torchao to ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src" | ||
rm -rf ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src | ||
mkdir -p ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src | ||
pushd ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src | ||
echo $pwd | ||
|
||
cp -R /Users/scroy/fbsource/fbcode/pytorch/ao . | ||
# git clone https://github.com/pytorch/ao.git | ||
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. TODO: uncomment after D62394341 lands and commit hash is updated. |
||
# cd ao | ||
# git checkout $(cat ${TORCHCHAT_ROOT}/.pins/torchao-pin.txt) | ||
|
||
popd | ||
} | ||
|
||
install_torchao_custom_aten_ops() { | ||
echo "Installing custom torchao ops" | ||
pushd ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src/ao/torchao/experimental/kernels/cpu/linear/examples/torch_custom_op | ||
export TORCHAO_INCLUDE_DIRS=${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src/ao | ||
|
||
if [ "${CMAKE_OUT_DIR}" == "" ]; then | ||
CMAKE_OUT_DIR="${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/cmake-out" | ||
fi | ||
|
||
cmake -DTORCHAO_INCLUDE_DIRS=${TORCHAO_INCLUDE_DIRS} \ | ||
-DCMAKE_PREFIX_PATH=${MY_CMAKE_PREFIX_PATH} \ | ||
-DPLATFORM="ATEN" \ | ||
-S . \ | ||
-B ${CMAKE_OUT_DIR} -G Ninja | ||
cmake --build ${CMAKE_OUT_DIR} | ||
} | ||
|
||
install_torchao_custom_executorch_ops() { | ||
echo "Installing custom torchao ops" | ||
pushd ${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src/ao/torchao/experimental/kernels/cpu/linear/examples/torch_custom_op | ||
export TORCHAO_INCLUDE_DIRS=${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/src/ao | ||
|
||
if [ "${CMAKE_OUT_DIR}" == "" ]; then | ||
CMAKE_OUT_DIR="${TORCHCHAT_ROOT}/${TORCHAO_BUILD_DIR}/cmake-out" | ||
fi | ||
|
||
cmake -DTORCHAO_INCLUDE_DIRS=${TORCHAO_INCLUDE_DIRS} \ | ||
-DCMAKE_PREFIX_PATH=${MY_CMAKE_PREFIX_PATH} \ | ||
-DEXECUTORCH_INCLUDE_DIRS=${EXECUTORCH_INCLUDE_DIRS} \ | ||
-DEXECUTORCH_LIBRARIES=${EXECUTORCH_LIBRARIES} \ | ||
-DPLATFORM="EXECUTORCH" \ | ||
-S . \ | ||
-B ${CMAKE_OUT_DIR} -G Ninja | ||
cmake --build ${CMAKE_OUT_DIR} | ||
} |
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.
TODO: update to commit hash after D62394341 lands.