From c4c90588d5cc75a6eb538096f830ce3c01072e89 Mon Sep 17 00:00:00 2001 From: Stephen Jia Date: Thu, 19 Nov 2026 14:18:47 -0800 Subject: [PATCH] [ET-VK][ops] Fuse a trailing relu into q8ta_pixel_shuffle ## Ulterior Motive Quantized models that upsample with `PixelShuffle` followed by `ReLU` lower to `dequantize -> pixel_shuffle -> relu -> quantize`, four fp32 dispatches per upsample. This commit fuses that chain into the existing int8 `q8ta_pixel_shuffle` kernel. ## Rationale **What**: A new overload `et_vk.q8ta_pixel_shuffle.with_activation(..., str activation)`, which the quantized pixel shuffle pattern emits when an activation sits between `pixel_shuffle` and `quantize`. `relu` is the only supported activation. **Why**: The pattern only matched `dequantize -> pixel_shuffle -> quantize`. With a `relu` in between, nothing fused and the upsample ran in fp32. ## Details - Pattern (`quantized_pixel_shuffle.py`) - **Before**: `dequantize -> [clone] -> pixel_shuffle -> [clone] -> quantize` fuses into `q8ta_pixel_shuffle.default`. - **After**: an optional activation (`relu`, with optional clones after it) is also matched, and the match emits `q8ta_pixel_shuffle.with_activation` with the activation name. Supported activations are listed in one table, `_ACTIVATIONS`. - Runtime: both overloads register the same dispatch and shader, so no shader variant is added. - For a positive output scale, `quantize(relu(x)) == max(quantize(x), zp_out)`. The kernel therefore applies relu by raising the lower clamp bound of the requantized value from -128 to `zp_out`, passed as a new push constant `q_min`. - This is bit-exact with the unfused chain, because `q8ta_quantize` uses the same `round(x * inv_scale) + zp` arithmetic. - With an activation, the pure byte-shuffle (`passthrough`) path is disabled. - `add_q8ta_pixel_shuffle_node` takes an `ActivationType`, reusing the enum and parser from the q8ta conv2d ops. - Tests - `etvk_ops_bench`: the reference and throughput functions are registered for the new overload. `correctness.json` adds one case with 8 argument sets: 4 shapes (including odd sizes and batch 2), each with requantizing and with matching quantization params, across the 3 x 3 input/output layout sweep. `performance.json` adds one case with 3 upsample shapes. - `test_vulkan_passes.py`: `test_fuse_quantized_pixel_shuffle_relu` checks the fused op, its arguments, that no quant/dequant/relu nodes remain, and that the fused graph matches the eager module exactly. Authored with Claude Code. Differential Revision: [D133330257](https://our.internmc.facebook.com/intern/diff/D133330257/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D133330257/)! ghstack-source-id: 478906449 Pull-Request: https://github.com/pytorch/executorch/pull/25316 --- backends/vulkan/custom_ops_lib.py | 30 ++++++ backends/vulkan/op_registry.py | 7 +- .../patterns/quantized_pixel_shuffle.py | 46 ++++++---- .../graph/ops/glsl/q8ta_pixel_shuffle.glsl | 8 +- .../graph/ops/impl/Q8taPixelShuffle.cpp | 22 ++++- .../runtime/graph/ops/impl/Q8taPixelShuffle.h | 8 +- .../q8ta/Q8taPixelShuffleReference.cpp | 19 +++- .../q8ta/Q8taPixelShuffleThroughput.cpp | 4 + .../pixel_shuffle/q8ta/cases/correctness.json | 92 +++++++++++++++++++ .../pixel_shuffle/q8ta/cases/performance.json | 42 +++++++++ backends/vulkan/test/test_vulkan_passes.py | 53 +++++++++++ 11 files changed, 304 insertions(+), 27 deletions(-) diff --git a/backends/vulkan/custom_ops_lib.py b/backends/vulkan/custom_ops_lib.py index bc54ae6f66c..31e9872b060 100644 --- a/backends/vulkan/custom_ops_lib.py +++ b/backends/vulkan/custom_ops_lib.py @@ -1110,6 +1110,36 @@ def q8ta_pixel_shuffle_impl( lib.impl(name, q8ta_pixel_shuffle_impl, "CompositeExplicitAutograd") q8ta_pixel_shuffle_op = getattr(getattr(torch.ops, namespace), name) + +def q8ta_pixel_shuffle_with_activation_impl( + input: torch.Tensor, + input_scale: float, + input_zero_point: int, + output_inv_scale: float, + output_zero_point: int, + upscale_factor: int, + activation: str, +): + output_scale = 1.0 / output_inv_scale + dequant = torch.ops.quantized_decomposed.dequantize_per_tensor( + input, input_scale, input_zero_point, -128, 127, input.dtype + ) + out = torch.nn.functional.pixel_shuffle(dequant, upscale_factor) + if activation == "relu": + out = torch.nn.functional.relu(out) + else: + assert activation == "none", f"Unsupported activation: {activation}" + return torch.ops.quantized_decomposed.quantize_per_tensor( + out, output_scale, output_zero_point, -128, 127, torch.int8 + ) + + +name = "q8ta_pixel_shuffle.with_activation" +lib.define( + f"{name}(Tensor input, float input_scale, int input_zero_point, float output_inv_scale, int output_zero_point, int upscale_factor, str activation) -> Tensor" +) +lib.impl(name, q8ta_pixel_shuffle_with_activation_impl, "CompositeExplicitAutograd") + ######################## ## embedding_q4gsw ## ######################## diff --git a/backends/vulkan/op_registry.py b/backends/vulkan/op_registry.py index 3f9c550b779..5fd9c91b3c8 100644 --- a/backends/vulkan/op_registry.py +++ b/backends/vulkan/op_registry.py @@ -670,7 +670,12 @@ def register_q8ta_relu(): # ============================================================================= -@update_features(exir_ops.edge.et_vk.q8ta_pixel_shuffle.default) +@update_features( + [ + exir_ops.edge.et_vk.q8ta_pixel_shuffle.default, + exir_ops.edge.et_vk.q8ta_pixel_shuffle.with_activation, + ] +) def register_q8ta_pixel_shuffle(): # The fused kernel is restricted to the channels-packed family # (PACKED_INT8_4W4C, PACKED_INT8_4C1W, PACKED_INT8_CONV2D), all of which diff --git a/backends/vulkan/patterns/quantized_pixel_shuffle.py b/backends/vulkan/patterns/quantized_pixel_shuffle.py index 1a180863935..56302116e17 100644 --- a/backends/vulkan/patterns/quantized_pixel_shuffle.py +++ b/backends/vulkan/patterns/quantized_pixel_shuffle.py @@ -4,7 +4,7 @@ # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. -from typing import List, Optional, Set +from typing import Dict, List, Optional, Set import executorch.backends.vulkan.utils as utils @@ -30,6 +30,12 @@ exir_ops.edge.dim_order_ops._clone_dim_order.default, } +# Activations that can follow the pixel_shuffle, mapped to the activation name +# passed to q8ta_pixel_shuffle.with_activation. +_ACTIVATIONS: Dict[object, str] = { + exir_ops.edge.aten.relu.default: "relu", +} + def _is_noop_passthrough(node: torch.fx.Node) -> bool: return node.op == "call_function" and node.target in _NOOP_PASSTHROUGH_TARGETS @@ -61,6 +67,8 @@ class QuantizedPixelShuffleMatch(PatternMatch): [optional] clone / _clone_dim_order aten.pixel_shuffle.default (upscale_factor = r) [optional] clone / _clone_dim_order + [optional] activation (aten.relu.default) + [optional] clone / _clone_dim_order q8ta_quantize_per_tensor (fp32 -> int8) The anchor is the dequantize node since it is a unique entry point. @@ -88,8 +96,14 @@ def __init__(self, dequantize_node: torch.fx.Node) -> None: if pixel_shuffle_node.target != exir_ops.edge.aten.pixel_shuffle.default: return - # Walk forward to the quantize node (skipping any clones). + # Walk forward to the quantize node (skipping any clones), through an + # optional activation. quantize_node = _skip_passthrough_user(pixel_shuffle_node, self.all_nodes) + self.activation_node: Optional[torch.fx.Node] = None + if quantize_node is not None and quantize_node.target in _ACTIVATIONS: + self.activation_node = quantize_node + self.all_nodes.append(self.activation_node) + quantize_node = _skip_passthrough_user(self.activation_node, self.all_nodes) if quantize_node is None or not utils.is_quant_node(quantize_node): return @@ -139,8 +153,6 @@ def make_quantized_pixel_shuffle_custom_op( graph_module: torch.fx.GraphModule, match: QuantizedPixelShuffleMatch, ) -> None: - op_target = exir_ops.edge.et_vk.q8ta_pixel_shuffle.default - # The fused op takes the *inverse* of the output scale to match the # runtime kernel's expectation. output_scale = match.output_scales_node @@ -162,19 +174,21 @@ def make_quantized_pixel_shuffle_custom_op( "quantized_pixel_shuffle pattern only supports scalar output scales" ) + args = ( + match.input_int8_node, + match.input_scales_node, + match.input_zeros_node, + inv_output_scale, + match.output_zeros_node, + match.upscale_factor, + ) + op_target = exir_ops.edge.et_vk.q8ta_pixel_shuffle.default + if match.activation_node is not None: + op_target = exir_ops.edge.et_vk.q8ta_pixel_shuffle.with_activation + args = (*args, _ACTIVATIONS[match.activation_node.target]) + with graph_module.graph.inserting_before(match.output_node): - new_node = graph_module.graph.create_node( - "call_function", - op_target, - args=( - match.input_int8_node, - match.input_scales_node, - match.input_zeros_node, - inv_output_scale, - match.output_zeros_node, - match.upscale_factor, - ), - ) + new_node = graph_module.graph.create_node("call_function", op_target, args) new_node.meta["val"] = match.output_node.meta["val"] match.quantize_output_node.replace_all_uses_with(new_node) diff --git a/backends/vulkan/runtime/graph/ops/glsl/q8ta_pixel_shuffle.glsl b/backends/vulkan/runtime/graph/ops/glsl/q8ta_pixel_shuffle.glsl index 638799e1a3c..be603381e0b 100644 --- a/backends/vulkan/runtime/graph/ops/glsl/q8ta_pixel_shuffle.glsl +++ b/backends/vulkan/runtime/graph/ops/glsl/q8ta_pixel_shuffle.glsl @@ -35,14 +35,16 @@ layout(push_constant) uniform restrict Block { float inv_scale_out; int zp_in; int zp_out; + // Lower clamp bound of requantized values; raised to zp_out to apply relu. + int q_min; }; layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; ${layout_declare_spec_const(C, "int", "inp_layout", "CONTIG_LAYOUT_INT")} ${layout_declare_spec_const(C, "int", "outp_layout", "CONTIG_LAYOUT_INT")} -// Set by the host when input and output quantization params match, making the -// shader a pure byte shuffle. +// Set by the host when input and output quantization params match and no +// activation is applied, making the shader a pure byte shuffle. ${layout_declare_spec_const(C, "int", "passthrough", "0")} /* @@ -66,7 +68,7 @@ int requantize_word(const int packed) { const int q_in = (packed << (24 - 8 * i)) >> 24; const float dq = float(q_in - zp_in) * scale_in; const int q_out = - clamp(int(round(dq * inv_scale_out)) + zp_out, -128, 127); + clamp(int(round(dq * inv_scale_out)) + zp_out, q_min, 127); result |= (q_out & 0xFF) << (8 * i); } return result; diff --git a/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.cpp b/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.cpp index 2bf14fdf152..650afcbbbea 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.cpp @@ -6,6 +6,8 @@ * LICENSE file in the root directory of this source tree. */ +#include + #include #include @@ -13,6 +15,7 @@ #include +#include #include #include @@ -100,6 +103,7 @@ void add_q8ta_pixel_shuffle_node( const ValueRef output_inv_scale, const ValueRef output_zp, const ValueRef upscale_factor, + const ActivationType activation, const ValueRef packed_int8_output) { // dtype must be the int8x4 packed type VK_CHECK_COND(graph.dtype_of(packed_int8_input) == vkapi::kInt8x4); @@ -145,6 +149,12 @@ void add_q8ta_pixel_shuffle_node( float inv_scale_out = graph.extract_scalar(output_inv_scale); int32_t zp_in = graph.extract_scalar(input_zp); int32_t zp_out = graph.extract_scalar(output_zp); + // quantize(relu(x)) == max(quantize(x), zp_out), so relu only raises the + // lower clamp bound of the requantized value. + int32_t q_min = -128; + if (activation == ActivationType::kRelu) { + q_min = std::clamp(zp_out, -128, 127); + } // Detect the pure-byte-shuffle case: same scale & same zero-point. In that // case the shader can skip the requantize math entirely. @@ -152,7 +162,8 @@ void add_q8ta_pixel_shuffle_node( const float scale_diff = std::abs(scale_in - scale_out_actual); const float scale_thresh = 1e-7f * std::max(std::abs(scale_in), 1e-7f); int32_t passthrough = 0; - if (scale_diff <= scale_thresh && zp_in == zp_out) { + if (scale_diff <= scale_thresh && zp_in == zp_out && + activation == ActivationType::kNone) { passthrough = 1; } @@ -161,6 +172,7 @@ void add_q8ta_pixel_shuffle_node( PushConstantDataInfo(&inv_scale_out, sizeof(inv_scale_out)), PushConstantDataInfo(&zp_in, sizeof(zp_in)), PushConstantDataInfo(&zp_out, sizeof(zp_out)), + PushConstantDataInfo(&q_min, sizeof(q_min)), }; // UBOs @@ -209,6 +221,12 @@ void q8ta_pixel_shuffle( const ValueRef output_inv_scale = args.at(idx++); const ValueRef output_zp = args.at(idx++); const ValueRef upscale_factor = args.at(idx++); + // q8ta_pixel_shuffle.with_activation passes an activation before the output. + ActivationType activation = ActivationType::kNone; + if (args.size() == 8) { + activation = + activation_type_from_string(graph.extract_string(args.at(idx++))); + } const ValueRef packed_int8_output = args.at(idx); add_q8ta_pixel_shuffle_node( @@ -219,11 +237,13 @@ void q8ta_pixel_shuffle( output_inv_scale, output_zp, upscale_factor, + activation, packed_int8_output); } REGISTER_OPERATORS { VK_REGISTER_OP(et_vk.q8ta_pixel_shuffle.default, q8ta_pixel_shuffle); + VK_REGISTER_OP(et_vk.q8ta_pixel_shuffle.with_activation, q8ta_pixel_shuffle); } } // namespace vkcompute diff --git a/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.h b/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.h index 7522b71e042..992e58a667e 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.h +++ b/backends/vulkan/runtime/graph/ops/impl/Q8taPixelShuffle.h @@ -10,14 +10,17 @@ #include +#include + namespace vkcompute { // // Fused PixelShuffle operating on int8x4 packed tensors. // // Replaces the decomposed chain: -// q8ta_dequantize -> view -> permute -> view -> q8ta_quantize -// with a single byte-shuffle (and optional requantize when scales differ). +// q8ta_dequantize -> view -> permute -> view -> [relu] -> q8ta_quantize +// with a single byte-shuffle (and optional requantize when scales differ or an +// activation is applied). // void add_q8ta_pixel_shuffle_node( @@ -28,6 +31,7 @@ void add_q8ta_pixel_shuffle_node( const ValueRef output_inv_scale, const ValueRef output_zp, const ValueRef upscale_factor, + const ActivationType activation, const ValueRef packed_int8_output); } // namespace vkcompute diff --git a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleReference.cpp b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleReference.cpp index 3c74741ca83..f64499564e9 100644 --- a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleReference.cpp +++ b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleReference.cpp @@ -7,6 +7,8 @@ #include #include +#include + namespace vkcompute::shader_profiling::reference { static Tensor q8ta_pixel_shuffle_reference( const Json& spec, @@ -18,6 +20,9 @@ static Tensor q8ta_pixel_shuffle_reference( inverse_out_scale = q8ta::scale(spec, 3); const int64_t iz = q8ta::integer(spec, 2), oz = q8ta::integer(spec, 4), r = q8ta::integer(spec, 5); + // q8ta_pixel_shuffle.with_activation passes an activation before the output. + const bool relu = spec.at("args").size() == 8 && + spec.at("args").at(6).at("string") == "relu"; const float actual_out_scale = 1.0f / inverse_out_scale; const bool passthrough = std::abs(is - actual_out_scale) <= 1e-7f * std::max(std::abs(is), 1e-7f) && @@ -34,10 +39,14 @@ static Tensor q8ta_pixel_shuffle_reference( input.sizes[3] + x / r; const int64_t value = int64_t(input.values[source]); - out.values[i] = passthrough - ? double(value) - : double(q8ta::requantize( - q8ta::dequantize(value, is, iz), inverse_out_scale, oz)); + int64_t q = passthrough + ? value + : q8ta::requantize( + q8ta::dequantize(value, is, iz), inverse_out_scale, oz); + if (relu) { + q = std::max(q, oz); + } + out.values[i] = double(q); }); return out; } @@ -45,5 +54,7 @@ REGISTER_REFERENCE_FUNCTIONS { REGISTER_REFERENCE_FN(q8ta_pixel_shuffle, q8ta_pixel_shuffle_reference); REGISTER_OPERATOR_REFERENCE( et_vk.q8ta_pixel_shuffle.default, q8ta_pixel_shuffle); + REGISTER_OPERATOR_REFERENCE( + et_vk.q8ta_pixel_shuffle.with_activation, q8ta_pixel_shuffle); } } // namespace vkcompute::shader_profiling::reference diff --git a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleThroughput.cpp b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleThroughput.cpp index 4334d243439..e8ea69218eb 100644 --- a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleThroughput.cpp +++ b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/Q8taPixelShuffleThroughput.cpp @@ -25,6 +25,10 @@ REGISTER_THROUGHPUT_FUNCTIONS { et_vk.q8ta_pixel_shuffle.default, throughput::tensor_traffic); REGISTER_OPERATOR_COMPUTE_FLOPS( et_vk.q8ta_pixel_shuffle.default, pixel_shuffle_flops); + REGISTER_OPERATOR_MEMORY_TRAFFIC( + et_vk.q8ta_pixel_shuffle.with_activation, throughput::tensor_traffic); + REGISTER_OPERATOR_COMPUTE_FLOPS( + et_vk.q8ta_pixel_shuffle.with_activation, pixel_shuffle_flops); } } // namespace } // namespace vkcompute::shader_profiling diff --git a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/correctness.json b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/correctness.json index b2e4085a181..c25708702f2 100644 --- a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/correctness.json +++ b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/correctness.json @@ -250,5 +250,97 @@ {"tensor": {"name": "output", "sizes": [2, 8, 12, 10], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} ] ] + }, + { + "name": "q8ta/pixel_shuffle_relu/correctness", + "operator": "et_vk.q8ta_pixel_shuffle.with_activation", + "target_ms": 100, + "benchmark_runs": 1, + "warmup_runs": 1, + "max_dispatches": 200, + "validation_method": {"function": "is_close", "atol": 1.0076922944664253, "rtol": 0}, + "sweep_variables": {"v_INPUT_LAYOUT": ["4w4c", "4c", "4c1w"], "v_OUTPUT_LAYOUT": ["4w4c", "4c", "4c1w"]}, + "args_sweep": [ + [ + {"tensor": {"name": "input", "sizes": [1, 16, 4, 4], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 4, 8, 8], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 24, 8, 4], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 6, 16, 8], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 4, 5, 7], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 1, 10, 14], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [2, 32, 6, 5], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [2, 8, 12, 10], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 16, 4, 4], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.05}, + {"int": -10}, + {"float": 20.0}, + {"int": -10}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 4, 8, 8], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 24, 8, 4], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.05}, + {"int": -10}, + {"float": 20.0}, + {"int": -10}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 6, 16, 8], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 4, 5, 7], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.05}, + {"int": -10}, + {"float": 20.0}, + {"int": -10}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 1, 10, 14], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [2, 32, 6, 5], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.05}, + {"int": -10}, + {"float": 20.0}, + {"int": -10}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [2, 8, 12, 10], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ] + ] } ] diff --git a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/performance.json b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/performance.json index 7047694189d..e86d74b3f9f 100644 --- a/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/performance.json +++ b/backends/vulkan/test/ops/benchmark_v2/operators/pixel_shuffle/q8ta/cases/performance.json @@ -187,5 +187,47 @@ {"tensor": {"name": "output", "sizes": [1, 64, 512, 512], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} ] ] + }, + { + "name": "q8ta/pixel_shuffle_relu/performance", + "operator": "et_vk.q8ta_pixel_shuffle.with_activation", + "target_ms": 100, + "benchmark_runs": 1, + "warmup_runs": 1, + "max_dispatches": 200, + "validation_method": {"function": "is_close", "atol": 1.0076922944664253, "rtol": 0}, + "sweep_variables": {"v_INPUT_LAYOUT": ["4w4c", "4c", "4c1w"], "v_OUTPUT_LAYOUT": ["4w4c", "4c", "4c1w"]}, + "args_sweep": [ + [ + {"tensor": {"name": "input", "sizes": [1, 256, 32, 32], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 64, 64, 64], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 128, 64, 64], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 32, 128, 128], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ], + [ + {"tensor": {"name": "input", "sizes": [1, 64, 128, 128], "dtype": "int8x4", "storage": "buffer", "layout": "v_INPUT_LAYOUT", "data": {"rand_uniform_int": [-128, 127]}}}, + {"float": 0.00711200013756752}, + {"int": 0}, + {"float": 76.9230728149414}, + {"int": 5}, + {"int": 2}, + {"string": "relu"}, + {"tensor": {"name": "output", "sizes": [1, 16, 256, 256], "dtype": "int8x4", "storage": "buffer", "layout": "v_OUTPUT_LAYOUT", "data": {"fill": 0}}} + ] + ] } ] diff --git a/backends/vulkan/test/test_vulkan_passes.py b/backends/vulkan/test/test_vulkan_passes.py index 11e8872a8d2..36e06c89ceb 100644 --- a/backends/vulkan/test/test_vulkan_passes.py +++ b/backends/vulkan/test/test_vulkan_passes.py @@ -1847,6 +1847,59 @@ def forward(self, x): self.assertEqual(fused_node.args[4], 1) self.assertEqual(fused_node.args[5], 2) + def test_fuse_quantized_pixel_shuffle_relu(self): + """A relu between pixel_shuffle and quantize fuses into + et_vk.q8ta_pixel_shuffle.with_activation with activation "relu".""" + + class PixelShuffleReluModule(torch.nn.Module): + def forward(self, x): + x_dq = torch.ops.quantized_decomposed.dequantize_per_tensor( + x, 0.1, 3, -128, 127, torch.int8 + ) + y = torch.relu(torch.nn.functional.pixel_shuffle(x_dq, 2)) + return torch.ops.quantized_decomposed.quantize_per_tensor( + y, 0.05, -20, -128, 127, torch.int8 + ) + + model = PixelShuffleReluModule() + x = torch.randint(-128, 127, (1, 96, 16, 9), dtype=torch.int8) + expected = model(x) + program = torch.export.export(model, (x,), strict=True) + edge_program = to_edge( + program, + compile_config=EdgeCompileConfig( + _check_ir_validity=False, + preserve_ops=[torch.ops.aten.pixel_shuffle.default], + ), + ) + + ep = edge_program._edge_programs["forward"] + fuse_pass = FusePatternsPass() + fuse_pass._exported_program = ep + result = fuse_pass.call(ep.graph_module) + + self.assertTrue(result.modified) + + gm = ep.graph_module + self.assertEqual(op_node_count(gm, "q8ta_pixel_shuffle.with_activation"), 1) + self.assertEqual(op_node_count(gm, "q8ta_pixel_shuffle.default"), 0) + self.assertEqual(op_node_count(gm, "pixel_shuffle.default"), 0) + self.assertEqual(op_node_count(gm, "relu.default"), 0) + self.assertEqual(op_node_count(gm, "dequantize_per_tensor.default"), 0) + self.assertEqual(op_node_count(gm, "quantize_per_tensor.default"), 0) + + fused_node = next( + n + for n in gm.graph.nodes + if get_target_canonical_name(n) == "q8ta_pixel_shuffle.with_activation" + ) + # args = (input, input_scale, input_zp, inv_output_scale, output_zp, r, + # activation) + self.assertEqual(fused_node.args[3], 20.0) + self.assertEqual(fused_node.args[4], -20) + self.assertEqual(fused_node.args[6], "relu") + self.assertTrue(torch.equal(gm(x)[0], expected)) + def test_quantized_pixel_shuffle_pattern_rejects_non_match(self): """A `dq -> relu -> q` chain (no pixel_shuffle in between) must NOT be fused. The new matcher only triggers when a single