|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import torch |
| 7 | +import torch.nn.functional as F |
| 8 | +from executorch.backends.arm.quantizer import ( |
| 9 | + get_vgf_snorm_quantization_config, |
| 10 | + VgfQuantizer, |
| 11 | +) |
| 12 | +from executorch.backends.arm.quantizer.quantization_config import ( |
| 13 | + _is_canonical_flow_offset_grid_sampler, |
| 14 | + VGFQuantizationConfig, |
| 15 | +) |
| 16 | +from executorch.backends.arm.vgf import VgfCompileSpec |
| 17 | +from torch.export import export |
| 18 | +from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e |
| 19 | +from torchao.quantization.pt2e.quantizer import QuantizationSpec |
| 20 | + |
| 21 | + |
| 22 | +class FlowOffsetSamplerChain(torch.nn.Module): |
| 23 | + def forward(self, image: torch.Tensor, flow: torch.Tensor) -> torch.Tensor: |
| 24 | + image = torch.relu(image) |
| 25 | + height, width = image.shape[-2:] |
| 26 | + horizontal = torch.linspace( |
| 27 | + -1.0, 1.0, width, device=image.device, dtype=image.dtype |
| 28 | + ).view(1, 1, 1, width) |
| 29 | + vertical = torch.linspace( |
| 30 | + -1.0, 1.0, height, device=image.device, dtype=image.dtype |
| 31 | + ).view(1, 1, height, 1) |
| 32 | + base_grid = torch.cat( |
| 33 | + ( |
| 34 | + horizontal.expand(1, -1, height, -1), |
| 35 | + vertical.expand(1, -1, -1, width), |
| 36 | + ), |
| 37 | + dim=1, |
| 38 | + ) |
| 39 | + grid = (base_grid + flow[:, :2]).permute(0, 2, 3, 1) |
| 40 | + sampled = F.grid_sample( |
| 41 | + image, |
| 42 | + grid, |
| 43 | + mode="bilinear", |
| 44 | + padding_mode="border", |
| 45 | + align_corners=True, |
| 46 | + ) |
| 47 | + return torch.relu(sampled) |
| 48 | + |
| 49 | + |
| 50 | +def test_vgf_snorm_quantization_avoids_requantization_boundaries(): |
| 51 | + inputs = ( |
| 52 | + torch.randn(1, 4, 8, 12), |
| 53 | + torch.randn(1, 4, 8, 12) * 0.01, |
| 54 | + ) |
| 55 | + exported = export(FlowOffsetSamplerChain(), inputs, strict=True).module() |
| 56 | + grid_sampler = next( |
| 57 | + node |
| 58 | + for node in exported.graph.nodes |
| 59 | + if node.target == torch.ops.aten.grid_sampler.default |
| 60 | + ) |
| 61 | + assert _is_canonical_flow_offset_grid_sampler(grid_sampler) |
| 62 | + |
| 63 | + config = get_vgf_snorm_quantization_config(is_per_channel=False) |
| 64 | + assert isinstance(config, VGFQuantizationConfig) |
| 65 | + for qspec in (config.input_activation, config.output_activation): |
| 66 | + assert isinstance(qspec, QuantizationSpec) |
| 67 | + assert (qspec.quant_min, qspec.quant_max) == (-127, 127) |
| 68 | + |
| 69 | + quantizer = VgfQuantizer( |
| 70 | + VgfCompileSpec()._set_preserve_io_quantization(True), |
| 71 | + use_composable_quantizer=True, |
| 72 | + ) |
| 73 | + quantizer.set_global(config) |
| 74 | + quantizer.set_io(config) |
| 75 | + prepared = prepare_pt2e(exported, quantizer) |
| 76 | + prepared(*inputs) |
| 77 | + converted = convert_pt2e(prepared) |
| 78 | + |
| 79 | + quantize = torch.ops.quantized_decomposed.quantize_per_tensor.default |
| 80 | + dequantize = torch.ops.quantized_decomposed.dequantize_per_tensor.default |
| 81 | + requantize_nodes = [ |
| 82 | + node |
| 83 | + for node in converted.graph.nodes |
| 84 | + if node.target == quantize |
| 85 | + and isinstance(node.args[0], torch.fx.Node) |
| 86 | + and node.args[0].target == dequantize |
| 87 | + ] |
| 88 | + assert not requantize_nodes |
0 commit comments