Skip to content

Make int8 dynamic quant in autoquant serializable #1484

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 4 commits into from
Jan 3, 2025
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
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ repos:
alias: ruff-isolated
args:
- --isolated
- select F821,F823,W191
- --select
- F821,F823,W191
37 changes: 13 additions & 24 deletions torchao/quantization/autoquant.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ def from_float(cls, weight):

# avoid circular dep
from torchao.dtypes import to_affine_quantized_intx
from torchao.quantization.quant_api import (
_int8_symm_per_token_reduced_range_quant,
)

# input settings
input_quant_func = _int8_symm_per_token_reduced_range_quant

# weight settings
mapping_type = MappingType.SYMMETRIC
Expand All @@ -436,32 +442,9 @@ def get_weight_block_size(x):
target_dtype = torch.int8
eps = torch.finfo(torch.float32).eps
zero_point_dtype = torch.int64

# input settings
def get_per_token_block_size(x):
block_size = list(x.shape)
for i in range(len(block_size) - 1):
block_size[i] = 1
return block_size

input_mapping_type = MappingType.SYMMETRIC
input_target_dtype = torch.int8
input_eps = 1e-5
input_quant_min = -127
input_quant_max = 127
_layout = cls.layout
input_quant_func = lambda x: to_affine_quantized_intx(
x,
input_mapping_type,
get_per_token_block_size(x),
input_target_dtype,
eps=input_eps,
quant_min=input_quant_min,
quant_max=input_quant_max,
scale_dtype=torch.float32 if x.dtype == torch.float16 else None,
)

block_size = get_weight_block_size(weight)

weight = to_affine_quantized_intx(
weight,
mapping_type,
Expand Down Expand Up @@ -937,6 +920,7 @@ def get_per_token_block_size(x):

input_target_dtype = torch.float8_e4m3fn
_layout = Float8Layout(mm_config=Float8MMConfig(use_fast_accum=True))
# TODO: make this serializable
input_quant_func = lambda x: _input_activation_quant_func_fp8(
x=x,
activation_granularity=cls.activation_granularity,
Expand Down Expand Up @@ -980,6 +964,7 @@ def get_weight_block_size(x):

input_target_dtype = torch.float8_e4m3fn
_layout = Float8Layout(mm_config=Float8MMConfig(use_fast_accum=True))
# TODO: make this serializable
input_quant_func = lambda x: _input_activation_quant_func_fp8(
x=x,
activation_granularity=cls.activation_granularity,
Expand Down Expand Up @@ -1287,3 +1272,7 @@ def finalize_autoquant():
model(*example_input)

return model


if TORCH_VERSION_AT_LEAST_2_5:
torch.serialization.add_safe_globals(ALL_AUTOQUANT_CLASS_LIST)
Loading