Skip to content
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

Make int8 dynamic quant in autoquant serializable #1484

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Changes from 2 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
31 changes: 7 additions & 24 deletions torchao/quantization/autoquant.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ 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 +440,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 +918,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 +962,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
Loading