Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

Add more test cases to improve CUDA UT coverage (#2159)

Liang Lv03a44292f076
diff --git a/test/test_cpu/models/test_cosmos3.py b/test/unit/test_cpu/models/test_cosmos3.py
similarity index 100%
rename from test/test_cpu/models/test_cosmos3.py
rename to test/unit/test_cpu/models/test_cosmos3.py
diff --git a/test/unit/test_cuda/algorithms/test_block_runner.py b/test/unit/test_cuda/algorithms/test_block_runner.py
new file mode 100644
index 000000000..f96a3e1a9
--- /dev/null
+++ b/test/unit/test_cuda/algorithms/test_block_runner.py
@@ -0,0 +1,131 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+def _has_packed_weight(model):
+ """Return True if the reloaded model contains at least one packed quant layer."""
+ for module in model.modules():
+ if hasattr(module, "qweight") or hasattr(module, "packed_weight"):
+ return True
+ return False
+
+
+class TestBlockRunnerGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ def test_3bit_asym_sign_sgd_reload(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=3,
+ group_size=32,
+ sym=False,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+
+ @pytest.mark.timeout(180)
+ def test_group64_sign_sgd_reload(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=64,
+ sym=True,
+ iters=3,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+
+ @pytest.mark.timeout(180)
+ def test_4bit_sym_group128_reload(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+
+ @pytest.mark.timeout(180)
+ def test_2bit_asym_group32_reload(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=2,
+ group_size=32,
+ sym=False,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_sign_sgd_forward_after_reload(self, tiny_opt_model_path):
+ """Sign-SGD quantized model must forward-pass on GPU after reload."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=32,
+ sym=True,
+ iters=5,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
diff --git a/test/unit/test_cuda/algorithms/test_config_resolver.py b/test/unit/test_cuda/algorithms/test_config_resolver.py
new file mode 100644
index 000000000..0469f15d7
--- /dev/null
+++ b/test/unit/test_cuda/algorithms/test_config_resolver.py
@@ -0,0 +1,65 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestConfigResolverGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("scheme", ["W4A16", "W2A16", "W8A16", "MXFP4"])
+ def test_preset_scheme_export(self, tiny_opt_model_path, scheme):
+ autoround = AutoRound(tiny_opt_model_path, scheme=scheme, iters=0, disable_opt_rtn=True, nsamples=1, seqlen=16)
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert getattr(model.config.quantization_config, "quant_method", None) == "auto-round"
+
+ @pytest.mark.timeout(180)
+ def test_layer_config_override(self, tiny_opt_model_path):
+ """Per-layer override wins over the default scheme during config resolution."""
+ layer_config = {"model.decoder.layers.0.self_attn.q_proj": {"bits": 8}}
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ scheme="W4A16",
+ layer_config=layer_config,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+
+ def test_unknown_scheme_raises(self, tiny_opt_model_path):
+ """An unknown scheme name must be rejected by config resolution."""
+ from auto_round.schemes import preset_name_to_scheme
+
+ with pytest.raises(KeyError):
+ preset_name_to_scheme("NOT_A_REAL_SCHEME")
diff --git a/test/unit/test_cuda/algorithms/test_sign_sgd_pipeline.py b/test/unit/test_cuda/algorithms/test_sign_sgd_pipeline.py
new file mode 100644
index 000000000..a5a49712a
--- /dev/null
+++ b/test/unit/test_cuda/algorithms/test_sign_sgd_pipeline.py
@@ -0,0 +1,125 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestSignSgdPipeline:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ def test_sign_sgd_quantize_reload_forward(self, tiny_opt_model_path):
+ """Run a real sign-SGD quantization on GPU, reload and forward-pass it."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ qcfg = model.config.quantization_config
+ assert getattr(qcfg, "quant_method", None) == "auto-round"
+
+ # A real forward pass on CUDA proves the quantized graph is runnable.
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_w4a8_quantize_reload(self, tiny_opt_model_path):
+ """W4A8 with iters>0 exercises the activation-quantization hooks on GPU."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ act_bits=8,
+ group_size=128,
+ act_group_size=32,
+ sym=True,
+ iters=1,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+
+ # Run the act-quantized model on GPU to prove the W4A8 graph is runnable.
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_sign_sgd_asym(self, tiny_opt_model_path):
+ """Asymmetric sign-SGD quantization + export on GPU.
+
+ Reload of the AWQ-packed asymmetric export requires the optional
+ ``auto_awq`` package, so we verify the export itself completes.
+ """
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=False,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ compressed_model, quantized_model_path = autoround.quantize_and_save(
+ output_dir=self.save_dir, format="auto_round"
+ )
+ assert isinstance(compressed_model, torch.nn.Module)
+ assert os.path.isdir(quantized_model_path)
+
+ @pytest.mark.timeout(180)
+ def test_sign_sgd_bits2_group32(self, tiny_opt_model_path):
+ """2-bit, small-group sign-SGD exercises low-bit block optimization."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=2,
+ group_size=32,
+ sym=True,
+ iters=2,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
diff --git a/test/unit/test_cuda/auto_scheme/__init__.py b/test/unit/test_cuda/auto_scheme/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/unit/test_cuda/auto_scheme/test_auto_scheme.py b/test/unit/test_cuda/auto_scheme/test_auto_scheme.py
new file mode 100644
index 000000000..bbcedfec7
--- /dev/null
+++ b/test/unit/test_cuda/auto_scheme/test_auto_scheme.py
@@ -0,0 +1,82 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound, AutoScheme
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestAutoSchemeGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(240)
+ def test_auto_scheme_quantize_reload(self, tiny_qwen_model_path):
+ scheme = AutoScheme(avg_bits=5.0, options=("W4A16", "W8A16"))
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme=scheme,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+
+ @pytest.mark.timeout(240)
+ def test_auto_scheme_avg_bits_in_range(self, tiny_qwen_model_path):
+ """The auto-generated mixed scheme must land near the requested avg_bits."""
+ from auto_round.auto_scheme.utils import compute_avg_bits_for_model
+
+ target = 5.0
+ scheme = AutoScheme(avg_bits=target, options=("W4A16", "W8A16"))
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme=scheme,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ model, _ = autoround.quantize()
+ avg_bits, _ = compute_avg_bits_for_model(model, ignore_scale_zp_bits=True)
+ assert target - 0.3 < avg_bits <= target + 0.3
+
+ @pytest.mark.timeout(240)
+ def test_auto_scheme_iters1_reload(self, tiny_qwen_model_path):
+ """Auto scheme with sign-SGD (iters>0) + reload."""
+ scheme = AutoScheme(avg_bits=5.0, options=("W4A16", "W8A16"))
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme=scheme,
+ iters=1,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
diff --git a/test/unit/test_cuda/compressors/__init__.py b/test/unit/test_cuda/compressors/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/unit/test_cuda/compressors/test_layer_config.py b/test/unit/test_cuda/compressors/test_layer_config.py
new file mode 100644
index 000000000..2cfb748b2
--- /dev/null
+++ b/test/unit/test_cuda/compressors/test_layer_config.py
@@ -0,0 +1,136 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Real-GPU mixed-precision (layer_config) tests.
+
+Exercises ``auto_round.compressors`` and the per-layer config resolution path on
+the GPU by exporting a model with a mixed-precision ``layer_config`` and
+verifying the reloaded model runs on CUDA.
+"""
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestLayerConfigGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ def test_mixed_precision_quantize_reload(self, tiny_opt_model_path):
+ layer_config = {
+ "model.decoder.layers.0.self_attn.q_proj": {"bits": 8},
+ "model.decoder.layers.1.fc1": {"bits": 4, "group_size": 32},
+ }
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ layer_config=layer_config,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ enable_torch_compile=False,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_layer_config_string_scheme(self, tiny_opt_model_path):
+ """layer_config values given as preset scheme strings."""
+ layer_config = {
+ "model.decoder.layers.0.self_attn.q_proj": "W8A16",
+ "model.decoder.layers.1.fc1": "W4A16",
+ }
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ layer_config=layer_config,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ enable_torch_compile=False,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+
+ @pytest.mark.timeout(180)
+ def test_ignore_layers_not_quantized(self, tiny_opt_model_path):
+ """A layer listed in ignore_layers must not be quantized."""
+ ignored = "model.decoder.layers.0.self_attn.q_proj"
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ ignore_layers=ignored,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ enable_torch_compile=False,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ q_proj = model
+ for part in ignored.split("."):
+ q_proj = getattr(q_proj, part)
+ assert not hasattr(q_proj, "qweight"), "ignored layer was unexpectedly quantized"
+
+ @pytest.mark.timeout(180)
+ def test_layer_config_bits2_8(self, tiny_opt_model_path):
+ """Mixed 2-bit and 8-bit per-layer config."""
+ layer_config = {
+ "model.decoder.layers.0.self_attn.q_proj": {"bits": 8},
+ "model.decoder.layers.1.fc1": {"bits": 2, "group_size": 32},
+ }
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ layer_config=layer_config,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ enable_torch_compile=False,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
diff --git a/test/unit/test_cuda/data_type/__init__.py b/test/unit/test_cuda/data_type/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/unit/test_cuda/data_type/test_ste.py b/test/unit/test_cuda/data_type/test_ste.py
new file mode 100644
index 000000000..40c6dff86
--- /dev/null
+++ b/test/unit/test_cuda/data_type/test_ste.py
@@ -0,0 +1,116 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import pytest
+import torch
+
+from auto_round.data_type.utils import (
+ ceil_ste,
+ float8_e4m3fn_ste,
+ float8_e5m2_ste,
+ floor_ste,
+ reshape_pad_tensor_by_group_size,
+ revert_tensor_by_pad,
+ round_ste,
+)
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestSteGpu:
+ def test_round_floor_ceil_ste_grad_flow(self):
+ x = torch.randn(4, 8, device="cuda", requires_grad=True)
+ for fn in (round_ste, floor_ste, ceil_ste):
+ y = fn(x).sum()
+ y.backward()
+ assert x.grad is not None
+ assert torch.all(x.grad == 1.0)
+ x.grad = None
+
+ def test_float8_ste_on_cuda(self):
+ x = torch.randn(4, 8, device="cuda", requires_grad=True)
+ y = float8_e4m3fn_ste(x)
+ assert y.device.type == "cuda"
+ y.sum().backward()
+ assert x.grad is not None
+
+ def test_reshape_pad_revert_on_cuda(self):
+ t = torch.arange(0, 30, dtype=torch.float32, device="cuda").reshape(3, 10)
+ out, orig_shape, pad = reshape_pad_tensor_by_group_size(t, group_size=4)
+ assert pad == 2
+ assert out.device.type == "cuda"
+ reverted = revert_tensor_by_pad(out, orig_shape, pad)
+ assert reverted.shape == t.shape
+ assert torch.equal(reverted, t)
+
+ def test_float8_e5m2_ste_on_cuda(self):
+ x = torch.randn(4, 8, device="cuda", requires_grad=True)
+ y = float8_e5m2_ste(x)
+ assert y.device.type == "cuda"
+ y.sum().backward()
+ assert x.grad is not None
+
+ def test_reshape_pad_group_size_zero(self):
+ # group_size == 0 -> flat reshape to a single row, no padding
+ t = torch.randn(3, 4, 8, device="cuda")
+ out, orig_shape, pad = reshape_pad_tensor_by_group_size(t, group_size=0)
+ assert pad == 0
+ assert out.shape == (1, t.numel())
+ reverted = revert_tensor_by_pad(out, orig_shape, pad)
+ assert torch.equal(reverted, t)
+
+ def test_reshape_pad_group_size_minus_one(self):
+ # group_size == -1 -> returned unchanged (single row)
+ t = torch.randn(3, 8, device="cuda")
+ out, orig_shape, pad = reshape_pad_tensor_by_group_size(t, group_size=-1)
+ assert pad == 0
+ assert out is t
+
+ def test_reshape_pad_2d_group_size(self):
+ # 2D group_size tuple pads both M and N dimensions
+ t = torch.randn(5, 9, device="cuda")
+ out, orig_shape, pad = reshape_pad_tensor_by_group_size(t, group_size=(2, 4))
+ assert pad == (1, 3) # ceil(5/2)*2-5=1, ceil(9/4)*4-9=3
+ assert out.device.type == "cuda"
+ reverted = revert_tensor_by_pad(out, orig_shape, pad)
+ assert reverted.shape == t.shape
+ assert torch.equal(reverted, t)
+
+ def test_reshape_pad_3d_input_revert(self):
+ # 3D input is flattened to 2D before grouping, then restored
+ t = torch.randn(2, 3, 10, device="cuda")
+ out, orig_shape, pad = reshape_pad_tensor_by_group_size(t, group_size=4)
+ assert pad == 2
+ reverted = revert_tensor_by_pad(out, orig_shape, pad)
+ assert reverted.shape == t.shape
+ assert torch.equal(reverted, t)
+
+ def test_ste_through_linear_backward(self):
+ # STE is differentiable: gradient flows through round_ste inside a Linear
+ torch.manual_seed(0)
+ linear = torch.nn.Linear(8, 4, device="cuda")
+ x = torch.randn(2, 8, device="cuda", requires_grad=True)
+ out = linear(round_ste(x))
+ loss = (out.square() + out).sum()
+ loss.backward()
+ assert x.grad is not None
+ assert linear.weight.grad is not None
+
+ def test_round_ste_value_on_cuda(self):
+ x = torch.tensor([1.2, 2.7, -0.5], device="cuda", requires_grad=True)
+ y = round_ste(x)
+ # STE output equals the rounded value but keeps gradient flow
+ assert torch.equal(y, x.round())
+ y.sum().backward()
+ assert torch.equal(x.grad, torch.ones_like(x))
diff --git a/test/unit/test_cuda/export/test_autoround_int_export.py b/test/unit/test_cuda/export/test_autoround_int_export.py
new file mode 100644
index 000000000..8fe40e708
--- /dev/null
+++ b/test/unit/test_cuda/export/test_autoround_int_export.py
@@ -0,0 +1,107 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+def _has_packed_weight(model):
+ """Return True if the reloaded model contains at least one packed quant layer."""
+ for module in model.modules():
+ if hasattr(module, "qweight") or hasattr(module, "packed_weight"):
+ return True
+ return False
+
+
+class TestAutoroundIntExportGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("bits", [2, 4, 8])
+ @pytest.mark.parametrize("group_size", [32, 128])
+ def test_int_export_reload_forward(self, tiny_opt_model_path, bits, group_size):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=bits,
+ group_size=group_size,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("bits", [4, 8])
+ @pytest.mark.parametrize("group_size", [32, 128])
+ def test_asym_int_export_reload_forward(self, tiny_opt_model_path, bits, group_size):
+ """Asymmetric integer export path."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=bits,
+ group_size=group_size,
+ sym=False,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_int_export_group_size_64(self, tiny_opt_model_path):
+ """group_size=64 exercises the non-power-of-128 packing path."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ group_size=64,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ assert _has_packed_weight(model)
diff --git a/test/unit/test_cuda/export/test_fp8_schemes.py b/test/unit/test_cuda/export/test_fp8_schemes.py
new file mode 100644
index 000000000..abe0b2a3d
--- /dev/null
+++ b/test/unit/test_cuda/export/test_fp8_schemes.py
@@ -0,0 +1,88 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoConfig, AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestFp8SchemesGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ def _assert_fp8_weight(self, compressed_model):
+ found_fp8 = False
+ for module in compressed_model.modules():
+ w = getattr(module, "weight", None)
+ if isinstance(w, torch.nn.Parameter) and w.dtype in (torch.float8_e4m3fn, torch.float8_e5m2):
+ found_fp8 = True
+ break
+ assert found_fp8, "No fp8 weight found in the compressed model"
+
+ @pytest.mark.timeout(180)
+ def test_fp8_block_export(self, tiny_qwen_model_path):
+ autoround = AutoRound(tiny_qwen_model_path, scheme="FP8_BLOCK", iters=0, disable_opt_rtn=True, seqlen=2)
+ compressed_model, _ = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._assert_fp8_weight(compressed_model)
+
+ @pytest.mark.timeout(180)
+ def test_fp8_static_export(self, tiny_qwen_model_path):
+ autoround = AutoRound(tiny_qwen_model_path, scheme="FP8_STATIC", iters=0, disable_opt_rtn=True, seqlen=2)
+ compressed_model, quantized_model_path = autoround.quantize_and_save(
+ output_dir=self.save_dir, format="auto_round"
+ )
+ self._assert_fp8_weight(compressed_model)
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+
+ @pytest.mark.timeout(180)
+ def test_fp8_block_forward(self, tiny_qwen_model_path):
+ """Reloaded FP8_BLOCK model must run a forward pass on GPU."""
+ autoround = AutoRound(tiny_qwen_model_path, scheme="FP8_BLOCK", iters=0, disable_opt_rtn=True, seqlen=2)
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ config = AutoConfig.from_pretrained(quantized_model_path, trust_remote_code=True)
+ config.quantization_config = None
+ model = AutoModelForCausalLM.from_pretrained(
+ quantized_model_path,
+ config=config,
+ device_map="cuda:0",
+ trust_remote_code=True,
+ )
+ input_ids = torch.randint(0, 1000, (1, 8), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_fp8_plain_export(self, tiny_qwen_model_path):
+ """Weight-only FP8 (FPW8A16, per-channel scale) export."""
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme="FPW8A16",
+ iters=0,
+ disable_opt_rtn=True,
+ seqlen=2,
+ )
+ compressed_model, _ = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._assert_fp8_weight(compressed_model)
diff --git a/test/unit/test_cuda/export/test_mxfp_nvfp.py b/test/unit/test_cuda/export/test_mxfp_nvfp.py
new file mode 100644
index 000000000..892be0862
--- /dev/null
+++ b/test/unit/test_cuda/export/test_mxfp_nvfp.py
@@ -0,0 +1,79 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestMxfpNvfpExportGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("scheme", ["MXFP4", "NVFP4", "MXFP8", "MXFP4_RCEIL"])
+ def test_export_reload_forward(self, tiny_qwen_model_path, scheme):
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme=scheme,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("scheme", ["MXFP4", "NVFP4"])
+ def test_export_qlinear_modules(self, tiny_qwen_model_path, scheme):
+ """The reloaded model must contain the expected MXFP/NVFP quant-linear module."""
+ autoround = AutoRound(
+ tiny_qwen_model_path,
+ scheme=scheme,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ from auto_round.experimental import qmodules as ar_qmodules
+
+ mapping = {
+ "MXFP4": ar_qmodules.MXFP4QuantLinear,
+ "NVFP4": ar_qmodules.NVFP4QuantLinear,
+ }
+ found = False
+ for module in model.modules():
+ if isinstance(module, mapping[scheme]):
+ found = True
+ break
+ assert found, f"No {mapping[scheme].__name__} found in reloaded model"
diff --git a/test/unit/test_cuda/export/test_nvfp_autoround_export.py b/test/unit/test_cuda/export/test_nvfp_autoround_export.py
new file mode 100644
index 000000000..79d382e6a
--- /dev/null
+++ b/test/unit/test_cuda/export/test_nvfp_autoround_export.py
@@ -0,0 +1,48 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestNvfpAutoroundExport:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ def test_nvfp4_autoround_export(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ scheme="NVFP4",
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ # Reload the saved model and confirm the NVFP4 quantized weight
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ qcfg = model.config.quantization_config
+ assert getattr(qcfg, "quant_method", None) == "auto-round"
diff --git a/test/unit/test_cuda/models/test_special_model.py b/test/unit/test_cuda/models/test_special_model.py
new file mode 100644
index 000000000..23d6f0e85
--- /dev/null
+++ b/test/unit/test_cuda/models/test_special_model.py
@@ -0,0 +1,101 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+from auto_round.special_model_handler import get_predefined_ignore_layers
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestSpecialModelHandlerGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ def test_moe_gate_ignore_layers(self, tiny_qwen_moe_model_path):
+ model = AutoModelForCausalLM.from_pretrained(tiny_qwen_moe_model_path, trust_remote_code=True).to("cuda")
+ layers = get_predefined_ignore_layers(model)
+ assert any(".gate" in name for name in layers), f"No MoE gate ignore layers found: {layers}"
+
+ @pytest.mark.timeout(240)
+ def test_moe_quantize_reload(self, tiny_qwen_moe_model_path):
+ autoround = AutoRound(
+ tiny_qwen_moe_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=8,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ # Gate modules must remain unquantized (registered as ignore layers).
+ for name, module in model.named_modules():
+ if name.endswith(".gate"):
+ assert not hasattr(module, "qweight"), f"Gate layer {name} was unexpectedly quantized"
+
+ def test_non_special_model_ignore_layers_empty(self, tiny_opt_model_path):
+ """A plain OPT model is not special: no predefined ignore layers are returned."""
+ model = AutoModelForCausalLM.from_pretrained(tiny_opt_model_path, trust_remote_code=True).to("cuda")
+ layers = get_predefined_ignore_layers(model)
+ assert layers == []
+
+ @pytest.mark.timeout(240)
+ def test_moe_lm_head_not_quantized(self, tiny_qwen_moe_model_path):
+ """lm_head is not in the quant block list and must stay unquantized after export."""
+ autoround = AutoRound(
+ tiny_qwen_moe_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=1,
+ seqlen=8,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ lm_head = getattr(model, "lm_head", None)
+ if lm_head is not None:
+ assert not hasattr(lm_head, "qweight"), "lm_head was unexpectedly quantized"
+
+ @pytest.mark.timeout(240)
+ def test_moe_quantize_iters_reload(self, tiny_qwen_moe_model_path):
+ """MoE quantization with sign-SGD (iters>0) + reload."""
+ autoround = AutoRound(
+ tiny_qwen_moe_model_path,
+ bits=4,
+ group_size=128,
+ sym=True,
+ iters=1,
+ nsamples=1,
+ seqlen=8,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
diff --git a/test/unit/test_cuda/quantization/test_act_quantization.py b/test/unit/test_cuda/quantization/test_act_quantization.py
new file mode 100644
index 000000000..f100ae4c9
--- /dev/null
+++ b/test/unit/test_cuda/quantization/test_act_quantization.py
@@ -0,0 +1,134 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestActQuantizationGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ def _reload_and_forward(self, quantized_model_path):
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ def test_w4a8_act_dynamic(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ act_bits=8,
+ group_size=128,
+ act_group_size=32,
+ act_dynamic=True,
+ sym=True,
+ iters=1,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._reload_and_forward(quantized_model_path)
+
+ @pytest.mark.timeout(180)
+ def test_w8a8_act_dynamic(self, tiny_opt_model_path):
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=8,
+ act_bits=8,
+ group_size=128,
+ act_group_size=32,
+ act_dynamic=True,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._reload_and_forward(quantized_model_path)
+
+ @pytest.mark.timeout(180)
+ def test_w4a8_act_static(self, tiny_opt_model_path):
+ """Static (non-dynamic) activation quantization path."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ act_bits=8,
+ group_size=128,
+ act_group_size=128,
+ act_dynamic=False,
+ sym=True,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._reload_and_forward(quantized_model_path)
+
+ @pytest.mark.timeout(180)
+ def test_w4a8_act_group128(self, tiny_opt_model_path):
+ """Wider act_group_size exercises a different act-hook grouping path."""
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=4,
+ act_bits=8,
+ group_size=128,
+ act_group_size=128,
+ act_dynamic=True,
+ sym=True,
+ iters=1,
+ nsamples=2,
+ seqlen=16,
+ )
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+ self._reload_and_forward(quantized_model_path)
+
+ @pytest.mark.timeout(180)
+ def test_w8a8_asym(self, tiny_opt_model_path):
+ """Asymmetric W8A8 cannot be exported to the auto_round format.
+
+ ``auto_round`` supports W8A8 export only for the symmetric path; an
+ asymmetric W8A8 config is rejected during format resolution.
+ """
+ autoround = AutoRound(
+ tiny_opt_model_path,
+ bits=8,
+ act_bits=8,
+ group_size=128,
+ act_group_size=32,
+ act_dynamic=True,
+ sym=False,
+ iters=0,
+ disable_opt_rtn=True,
+ nsamples=2,
+ seqlen=16,
+ )
+ with pytest.raises(ValueError, match="does not support exporting"):
+ autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
diff --git a/test/unit/test_cuda/schemes/__init__.py b/test/unit/test_cuda/schemes/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/test/unit/test_cuda/schemes/test_scheme_presets.py b/test/unit/test_cuda/schemes/test_scheme_presets.py
new file mode 100644
index 000000000..f175acc24
--- /dev/null
+++ b/test/unit/test_cuda/schemes/test_scheme_presets.py
@@ -0,0 +1,59 @@
+# Copyright (c) 2026 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import shutil
+
+import pytest
+import torch
+from transformers import AutoModelForCausalLM
+
+from auto_round import AutoRound
+
+pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires CUDA")
+
+
+class TestSchemePresetsGpu:
+ @pytest.fixture(autouse=True)
+ def _save_dir(self, tmp_path):
+ self.save_dir = str(tmp_path / "saved")
+ yield
+ shutil.rmtree(self.save_dir, ignore_errors=True)
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize("scheme", ["W4A16", "W2A16", "W8A16", "W3A16", "INT8"])
+ def test_scheme_preset_quantize_forward(self, tiny_opt_model_path, scheme):
+ autoround = AutoRound(tiny_opt_model_path, scheme=scheme, iters=0, disable_opt_rtn=True, nsamples=1, seqlen=16)
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ assert isinstance(model, torch.nn.Module)
+ input_ids = torch.randint(0, 1000, (1, 16), device="cuda:0")
+ with torch.no_grad():
+ out = model(input_ids)
+ assert out.logits.shape[0] == 1
+
+ @pytest.mark.timeout(180)
+ @pytest.mark.parametrize(
+ ("bits", "expected"),
+ [(4, 4), (8, 8)],
+ )
+ def test_scheme_bits_exported(self, tiny_opt_model_path, bits, expected):
+ """The resolved scheme bits are persisted in the exported quantization config."""
+ autoround = AutoRound(tiny_opt_model_path, bits=bits, group_size=128, sym=True, iters=0, disable_opt_rtn=True)
+ _, quantized_model_path = autoround.quantize_and_save(output_dir=self.save_dir, format="auto_round")
+
+ model = AutoModelForCausalLM.from_pretrained(quantized_model_path, device_map="cuda:0", trust_remote_code=True)
+ qcfg = model.config.quantization_config
+ assert getattr(qcfg, "quant_method", None) == "auto-round"
+ assert getattr(qcfg, "bits", None) == expected