Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
08e9095
Summary:
namgyu-youn Sep 21, 2025
db23cf3
rename for clearly: Int8PlainInt8Tensor -> Int8Tensor
namgyu-youn Sep 22, 2025
b861dbc
add flags for static/dynamic quant
namgyu-youn Sep 23, 2025
9383550
update static/dynamic quantization workflows
namgyu-youn Sep 24, 2025
2c84ba4
add kernel preference unit test
namgyu-youn Sep 24, 2025
8ddddd3
add kernel preference unit test
namgyu-youn Sep 24, 2025
bd6f58a
Merge remote-tracking branch 'upstream/main' into int8-quant
namgyu-youn Sep 24, 2025
b5cb3c8
fix missing attribute
namgyu-youn Sep 24, 2025
9a51cae
remove kernel preference args
namgyu-youn Sep 28, 2025
c53dad0
link new API with old API using version 2
namgyu-youn Sep 28, 2025
d300b02
add granularity, block size support
namgyu-youn Sep 30, 2025
c43a3ec
Merge branch 'main' into int8-quant
namgyu-youn Oct 4, 2025
590e0b7
add transpose, index selector workflows
namgyu-youn Oct 4, 2025
b3d4f3e
remove external zero point
namgyu-youn Oct 4, 2025
df79aa8
update int8 quantization API
namgyu-youn Oct 7, 2025
910906b
Merge remote-tracking branch 'upstream/main' into int8-quant
namgyu-youn Oct 7, 2025
c61b36e
add static quantization support
namgyu-youn Oct 14, 2025
0a45f90
sync with main branch
namgyu-youn Oct 16, 2025
1251187
split dispatch decorator
namgyu-youn Oct 16, 2025
844d99d
update int8-quant api
namgyu-youn Oct 17, 2025
a844678
update type-hint to prevent depenedency issue
namgyu-youn Oct 17, 2025
2c0389a
fix ci error
namgyu-youn Oct 20, 2025
bafeb43
revert unrelated changes
namgyu-youn Oct 23, 2025
7006cae
fix rebase
namgyu-youn Oct 23, 2025
49a7a89
update int8 quant api
namgyu-youn Oct 23, 2025
062f3cc
update int8
namgyu-youn Oct 24, 2025
680cec9
build setup for unit test, enable per-row/per-tensor granuarity
namgyu-youn Oct 24, 2025
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
4 changes: 3 additions & 1 deletion docs/source/quantization_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ First we want to lay out the torchao stack::

Quantization Algorithms/Flows: weight only/dynamic/static quantization, hqq, awq, gptq etc.
---------------------------------------------------------------------------------------------
Quantized Tensors (derived dtypes): Int4Tensor, Int4PreshuffledTensor, Float8Tensor
Quantized Tensors (derived dtypes): Int4Tensor, Int4PreshuffledTensor, Int8Tensor, Float8Tensor
---------------------------------------------------------------------------------------------
Quantization Primitive Ops/Efficient Kernels: matmul, quantize, dequantize
---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -88,6 +88,8 @@ So in general we structure Tensor subclasses by dervied dtpype and packing forma
- scaled int4
- preshuffled (special format to optimize for loading)
- float8 act + int4 weight dynamic quantization and int4 weight only quantization
* - Int8Tensor
- plain

.. note::
We don't have granularity specific tensor subclasses, i.e. no Float8RowwiseTensor or Float8BlockwiseTensor, all granularities are implemented in the same Tensor, we typically use a general `block_size` attribute to distinguish between different granularities, and each Tensor is allowed to support only a subset of all possible granularity options.
Expand Down
128 changes: 128 additions & 0 deletions test/quantization/quantize_/workflows/int8/test_int8_tensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD 3-Clause license found in the
# LICENSE file in the root directory of this source tree.

import unittest

import torch
from torch.testing._internal import common_utils
from torch.testing._internal.common_utils import run_tests
from torch._inductor.utils import run_and_get_code

from torchao.quantization.quantize_.common import KernelPreference
from torchao.quantization.quantize_.workflows.int8.int8_tensor import (
Int8Tensor,
QuantizeTensorToInt8Kwargs,
)
from torchao.quantization.utils import compute_error
from torchao.testing.utils import TorchAOIntegrationTestCase


@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available")
class TestInt8Tensor(TorchAOIntegrationTestCase):
def setUp(self):
super().setUp()
torch.manual_seed(42)
self.weight_fp = torch.randn(4, 3, dtype=torch.float32)
self.input_fp = torch.randn(2, 3, dtype=torch.float32)
self.bias = torch.randn(4)
self.block_size = [4, 3]

def test_creation_and_attributes(self):
"""Test tensor creation, dtypes, and ranges"""
tensor = Int8Tensor.from_hp(self.weight_fp, self.block_size)

self.assertEqual(tensor.shape, (4, 3))
self.assertEqual(tensor.qdata.dtype, torch.int8)
self.assertTrue(
torch.all(tensor.qdata >= -128) and torch.all(tensor.qdata <= 127)
)

@common_utils.parametrize(
"kernel_preference",
[KernelPreference.AUTO, KernelPreference.TORCH, KernelPreference.FBGEMM],
)
def test_kernel_preference(self, kernel_preference):
"""Test Int8Tensor with different kernels"""
tensor = Int8Tensor.from_hp(
self.weight_fp, self.block_size, kernel_preference=kernel_preference
)

self.assertEqual(tensor.kernel_preference, kernel_preference)

def test_linear_operations(self):
"""Test fp+int8 and int8+int8 linear ops with quantization error check"""
weight_q8 = Int8Tensor.from_hp(self.weight_fp, self.block_size)
input_q8 = Int8Tensor.from_hp(self.input_fp, self.block_size)

reference = torch.nn.functional.linear(self.input_fp, self.weight_fp, self.bias)
result_fp = torch.nn.functional.linear(self.input_fp, weight_q8, self.bias)
result_q8 = torch.nn.functional.linear(input_q8, weight_q8, self.bias)

self.assertEqual(result_fp.shape, reference.shape)
self.assertEqual(result_q8.shape, reference.shape)
self.assertTrue(compute_error(result_fp, reference) > 10)
self.assertTrue(compute_error(result_q8, reference) > 10)

def test_dynamic_quantization(self):
weight_q8_dynamic = Int8Tensor.from_hp(
self.weight_fp,
self.block_size,
act_quant_kwargs=QuantizeTensorToInt8Kwargs(),
)

reference = torch.nn.functional.linear(self.input_fp, self.weight_fp, self.bias)
result_dynamic = torch.nn.functional.linear(
self.input_fp, weight_q8_dynamic, self.bias
)

self.assertEqual(result_dynamic.shape, reference.shape)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: probably add a test for compute_error comparing floating point weight and int8+int8 weight as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't we already comparing 1) bflot16 vs. int8-quant, 2) float16 vs. int8-quant by dtype parameterization?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah this test should be removed


@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available")
def test_expected_kernel_operations(self):
"""Test Int8Tensor with FBGEMM kernels"""

# Setup model with Int8Tensor
weight_q8 = Int8Tensor.from_hp(
self.weight_fp,
self.block_size,
kernel_preference=KernelPreference.FBGEMM
)

def model(x):
return torch.nn.functional.linear(x, weight_q8, self.bias)

compiled_model = torch.compile(model)

output, code = run_and_get_code(compiled_model, self.input_fp)

self.assertEqual(output.shape, (2, 4))
self.assertTrue(len(code) > 0, "Should generate some compiled code")

# Test dequantization kernel
dequant_output = torch.ops.aten.dequantize.self(weight_q8)
self.assertEqual(dequant_output.shape, self.weight_fp.shape)

def test_error_handling_and_dequant(self):
"""Test input validation and dequantization accuracy"""
# Test 1D tensor validation
with self.assertRaises((AssertionError, ValueError, RuntimeError)):
Int8Tensor.from_hp(torch.randn(5), [1])

# Test wrong block_size validation
with self.assertRaises((AssertionError, ValueError, RuntimeError)):
Int8Tensor.from_hp(self.weight_fp, [1])

# Test dequantization with exact values
test_data = torch.tensor([[1.0, -1.0]], dtype=torch.float32)
tensor = Int8Tensor.from_hp(test_data, [1, 1])

dequantized = torch.ops.aten.dequantize.self(tensor)
self.assertEqual(dequantized.shape, test_data.shape)
self.assertLess(torch.abs(dequantized - test_data).max().item(), 0.1)


if __name__ == "__main__":
run_tests()
2 changes: 2 additions & 0 deletions torchao/quantization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
Int4PreshuffledTensor,
Int4Tensor,
Int4TilePackedTo4dTensor,
Int8Tensor,
IntxOpaqueTensor,
IntxUnpackedToInt8Tensor,
)
Expand Down Expand Up @@ -169,6 +170,7 @@
"IntxOpaqueTensor",
"IntxUnpackedToInt8Tensor",
"Int4TilePackedTo4dTensor",
"Int8Tensor",
"Float8Tensor",
"Int4OpaqueTensor",
# smooth quant - subject to change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def _choose_quant_func_and_quantize_tensor(
"""
from torchao.quantization.quantize_.workflows import (
Float8Tensor,
Int8Tensor,
QuantizeTensorToFloat8Kwargs,
QuantizeTensorToInt8Kwargs,
)

if isinstance(quant_kwargs, QuantizeTensorToFloat8Kwargs):
Expand All @@ -52,5 +54,11 @@ def _choose_quant_func_and_quantize_tensor(
quant_kwargs.hp_value_ub,
quant_kwargs.kernel_preference,
)
elif isinstance(quant_kwargs, QuantizeTensorToInt8Kwargs):
return Int8Tensor.from_hp(
tensor,
quant_kwargs.block_size or [1, tensor.shape[-1]],
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why not make block_size mandatory?

Copy link
Contributor

Choose a reason for hiding this comment

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

this one is still not resolved yet

kernel_preference=quant_kwargs.kernel_preference,
)

raise NotImplementedError(f"Quant kwargs not supported: {quant_kwargs}")
6 changes: 6 additions & 0 deletions torchao/quantization/quantize_/workflows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
Int4Tensor,
)
from .int4.int4_tile_packed_to_4d_tensor import Int4TilePackedTo4dTensor
from .int8.int8_tensor import (
Int8Tensor,
QuantizeTensorToInt8Kwargs,
)
from .intx.intx_opaque_tensor import (
IntxOpaqueTensor,
)
Expand All @@ -36,6 +40,8 @@
"Int4MarlinSparseTensor",
"Int4PlainInt32Tensor",
"Int4TilePackedTo4dTensor",
"Int8Tensor",
"QuantizeTensorToInt8Kwargs",
"Float8Tensor",
"QuantizeTensorToFloat8Kwargs",
"Int4OpaqueTensor",
Expand Down
Loading