From bf5a3dcfd31df0a2e6a60e498516e57b906fdaeb Mon Sep 17 00:00:00 2001 From: "Axel.Cffrd.Dnty" Date: Fri, 28 Aug 2026 12:09:05 +0200 Subject: [PATCH 1/2] Preserve node output dtype when lifting scalars to attrs in Vulkan quantizer --- .../quantizer/vulkan_quantizer_utils.py | 6 +- .../test/quantizer/test_vulkan_quantizer.py | 61 +++++++++++++++++++ pytest.ini | 1 + 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 backends/vulkan/test/quantizer/test_vulkan_quantizer.py diff --git a/backends/vulkan/quantizer/vulkan_quantizer_utils.py b/backends/vulkan/quantizer/vulkan_quantizer_utils.py index c0b6ab39e84..a8e914b8ec0 100644 --- a/backends/vulkan/quantizer/vulkan_quantizer_utils.py +++ b/backends/vulkan/quantizer/vulkan_quantizer_utils.py @@ -207,15 +207,15 @@ def _convert_scalars_to_attrs(model: torch.fx.GraphModule) -> torch.fx.GraphModu prefix = "_tensor_constant_" get_new_attr_name = get_new_attr_name_with_prefix(prefix) tensor_constant_name = get_new_attr_name(model) - float_tensor = torch.tensor(float(args[i])) - model.register_buffer(tensor_constant_name, float_tensor) + scalar_tensor = torch.tensor(args[i], dtype=n.meta["val"].dtype) + model.register_buffer(tensor_constant_name, scalar_tensor) fake_mode = n.meta["val"].fake_mode with model.graph.inserting_before(n): get_attr_node = model.graph.create_node( "get_attr", tensor_constant_name, (), {} ) get_attr_node.meta["val"] = fake_mode.from_tensor( - float_tensor, static_shapes=True + scalar_tensor, static_shapes=True ) new_args.append(get_attr_node) n.args = tuple(new_args) diff --git a/backends/vulkan/test/quantizer/test_vulkan_quantizer.py b/backends/vulkan/test/quantizer/test_vulkan_quantizer.py new file mode 100644 index 00000000000..3f568e55d3b --- /dev/null +++ b/backends/vulkan/test/quantizer/test_vulkan_quantizer.py @@ -0,0 +1,61 @@ +# 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. + +import unittest + +import torch +from executorch.backends.vulkan.quantizer.vulkan_quantizer import ( + get_symmetric_quantization_config, + VulkanQuantizer, +) +from torch.export import export +from torchao.quantization.pt2e.quantize_pt2e import prepare_pt2e + + +class TestVulkanQuantizer(unittest.TestCase): + def test_int64_scalar_add_used_as_index(self): + """Scalars lifted to attrs must keep the op's output dtype; an int64 + add chain used as an index must not be promoted to float32.""" + + class M(torch.nn.Module): + def forward(self, x): + return x[:, torch.arange(4) + 0] + + quantizer = VulkanQuantizer() + quantization_config = get_symmetric_quantization_config() + quantizer.set_global(quantization_config) + example_inputs = (torch.randn(1, 4, 5),) + m = export(M(), example_inputs, strict=True).module() + m = quantizer.transform_for_annotation(m) + lifted_constants = [ + m.get_buffer(n.target) + for n in m.graph.nodes + if n.op == "get_attr" and n.target.startswith("_tensor_constant_") + ] + self.assertEqual(len(lifted_constants), 1) + self.assertEqual(lifted_constants[0].dtype, torch.int64) + m = prepare_pt2e(m, quantizer) + m(*example_inputs) + + def test_int64_scalar_lifted_without_set_global(self): + """Passing through VulkanQuantizer without a config still lifts scalars + and must preserve dtype.""" + + class M(torch.nn.Module): + def forward(self, x): + return x[:, torch.arange(4) + 0] + + example_inputs = (torch.randn(1, 4, 5),) + m = export(M(), example_inputs, strict=True).module() + m = VulkanQuantizer().transform_for_annotation(m) + lifted_constants = [ + m.get_buffer(n.target) + for n in m.graph.nodes + if n.op == "get_attr" and n.target.startswith("_tensor_constant_") + ] + self.assertEqual(len(lifted_constants), 1) + self.assertEqual(lifted_constants[0].dtype, torch.int64) + m(*example_inputs) diff --git a/pytest.ini b/pytest.ini index 576ed2b78ce..09225b72a34 100644 --- a/pytest.ini +++ b/pytest.ini @@ -85,6 +85,7 @@ testpaths = backends/test/harness/tests backends/test/suite/tests backends/transforms + backends/vulkan/test/quantizer/test_vulkan_quantizer.py backends/xnnpack/test # codegen From 67a2a2cd423e239c95c36a4ed9e3cacb13c66452 Mon Sep 17 00:00:00 2001 From: AxelNoun Date: Fri, 11 Sep 2026 22:19:02 +0200 Subject: [PATCH 2/2] Register Vulkan quantizer test in Buck instead of pytest.ini Per review feedback, drop backends/vulkan/test/quantizer/test_vulkan_quantizer.py from pytest.ini testpaths and add a test_vulkan_quantizer python_unittest target for it in backends/vulkan/test/targets.bzl. Authored with AI assistance (Claude Code). --- backends/vulkan/test/targets.bzl | 12 ++++++++++++ pytest.ini | 1 - 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backends/vulkan/test/targets.bzl b/backends/vulkan/test/targets.bzl index fe13759243b..77e74f9c8fc 100644 --- a/backends/vulkan/test/targets.bzl +++ b/backends/vulkan/test/targets.bzl @@ -89,6 +89,18 @@ def define_common_targets(is_fbcode = False): ], ) + python_unittest( + name = "test_vulkan_quantizer", + srcs = [ + "quantizer/test_vulkan_quantizer.py", + ], + deps = [ + "//caffe2:torch", + "//executorch/backends/vulkan/quantizer:vulkan_quantizer", + "//pytorch/ao:torchao", # @manual + ], + ) + runtime.python_library( name = "tester", srcs = ["tester.py"], diff --git a/pytest.ini b/pytest.ini index 09225b72a34..576ed2b78ce 100644 --- a/pytest.ini +++ b/pytest.ini @@ -85,7 +85,6 @@ testpaths = backends/test/harness/tests backends/test/suite/tests backends/transforms - backends/vulkan/test/quantizer/test_vulkan_quantizer.py backends/xnnpack/test # codegen