From 588c5cc8a25ce61fb8702d25bba34117b98a48c6 Mon Sep 17 00:00:00 2001 From: Oscar Andersson Date: Mon, 30 Dec 2024 10:24:41 +0100 Subject: [PATCH] Populate __init__.py for quantizer and Arm root Populate backends/arm/quantizer/__init__.py and backends/arm/__init__.py Signed-off-by: Oscar Andersson Change-Id: If8e4c89a9d0393b5cd7c8e6639fd67be39f77ca9 --- backends/arm/__init__.py | 10 +++++++ backends/arm/ethosu_backend.py | 4 +-- backends/arm/ethosu_partitioner.py | 3 +-- backends/arm/quantizer/__init__.py | 10 ++++++- backends/arm/quantizer/arm_quantizer.py | 3 +-- .../arm/quantizer/quantization_annotator.py | 3 +-- backends/arm/test/ops/test_expand.py | 2 +- backends/arm/test/ops/test_hardtanh.py | 2 +- backends/arm/test/ops/test_max_pool.py | 2 +- backends/arm/test/ops/test_permute.py | 2 +- backends/arm/test/ops/test_relu.py | 2 +- backends/arm/test/ops/test_repeat.py | 2 +- backends/arm/test/ops/test_sigmoid_16bit.py | 2 +- backends/arm/test/ops/test_sigmoid_32bit.py | 2 +- backends/arm/test/ops/test_var.py | 2 +- backends/arm/test/ops/test_where.py | 2 +- backends/arm/test/tester/arm_tester.py | 5 ++-- backends/arm/test/tester/test_pipeline.py | 2 +- backends/arm/tosa_partitioner.py | 2 +- backends/arm/tosa_quant_utils.py | 4 +-- examples/arm/aot_arm_compiler.py | 12 ++++----- examples/arm/ethos_u_minimal_example.ipynb | 26 +++++++++---------- 22 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 backends/arm/__init__.py diff --git a/backends/arm/__init__.py b/backends/arm/__init__.py new file mode 100644 index 00000000000..9e32cec2c59 --- /dev/null +++ b/backends/arm/__init__.py @@ -0,0 +1,10 @@ +# Copyright 2025 Arm Limited and/or its affiliates. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +from .arm_backend import ArmCompileSpecBuilder # noqa # usort: skip +from .tosa_backend import TOSABackend # noqa # usort: skip +from .tosa_partitioner import TOSAPartitioner # noqa # usort: skip +from .ethosu_backend import EthosUBackend # noqa # usort: skip +from .ethosu_partitioner import EthosUPartitioner # noqa # usort: skip diff --git a/backends/arm/ethosu_backend.py b/backends/arm/ethosu_backend.py index 402f3fed42b..0df2394bdb0 100644 --- a/backends/arm/ethosu_backend.py +++ b/backends/arm/ethosu_backend.py @@ -14,9 +14,9 @@ import logging from typing import final, List -from executorch.backends.arm.arm_vela import vela_compile +from executorch.backends.arm import TOSABackend -from executorch.backends.arm.tosa_backend import TOSABackend +from executorch.backends.arm.arm_vela import vela_compile from executorch.exir.backend.backend_details import BackendDetails, PreprocessResult from executorch.exir.backend.compile_spec_schema import CompileSpec from torch.export.exported_program import ExportedProgram diff --git a/backends/arm/ethosu_partitioner.py b/backends/arm/ethosu_partitioner.py index 27102592e15..17ab01ae29f 100644 --- a/backends/arm/ethosu_partitioner.py +++ b/backends/arm/ethosu_partitioner.py @@ -10,8 +10,7 @@ from executorch.backends.arm.arm_backend import ( is_ethosu, ) # usort: skip -from executorch.backends.arm.ethosu_backend import EthosUBackend -from executorch.backends.arm.tosa_partitioner import TOSAPartitioner +from executorch.backends.arm import EthosUBackend, TOSAPartitioner from executorch.exir.backend.compile_spec_schema import CompileSpec from executorch.exir.backend.partitioner import DelegationSpec from torch.fx.passes.operator_support import OperatorSupportBase diff --git a/backends/arm/quantizer/__init__.py b/backends/arm/quantizer/__init__.py index 9743c95a143..90b99fa223b 100644 --- a/backends/arm/quantizer/__init__.py +++ b/backends/arm/quantizer/__init__.py @@ -1,4 +1,12 @@ -# Copyright 2024 Arm Limited and/or its affiliates. +# Copyright 2024-2025 Arm Limited and/or its affiliates. # # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. + + +from .quantization_config import QuantizationConfig # noqa # usort: skip +from .arm_quantizer import ( # noqa + EthosUQuantizer, + get_symmetric_quantization_config, + TOSAQuantizer, +) diff --git a/backends/arm/quantizer/arm_quantizer.py b/backends/arm/quantizer/arm_quantizer.py index ee08f8e9eec..ffa854e7acf 100644 --- a/backends/arm/quantizer/arm_quantizer.py +++ b/backends/arm/quantizer/arm_quantizer.py @@ -19,7 +19,7 @@ import torch from executorch.backends.arm._passes import ArmPassManager -from executorch.backends.arm.quantizer import arm_quantizer_utils +from executorch.backends.arm.quantizer import arm_quantizer_utils, QuantizationConfig from executorch.backends.arm.quantizer.arm_quantizer_utils import ( # type: ignore[attr-defined] mark_node_as_annotated, ) @@ -27,7 +27,6 @@ annotate_graph, ) -from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig from executorch.backends.arm.tosa_specification import TosaSpecification from executorch.backends.arm.arm_backend import ( get_tosa_spec, diff --git a/backends/arm/quantizer/quantization_annotator.py b/backends/arm/quantizer/quantization_annotator.py index 5398101fd9a..da1fef67115 100644 --- a/backends/arm/quantizer/quantization_annotator.py +++ b/backends/arm/quantizer/quantization_annotator.py @@ -10,8 +10,7 @@ import torch import torch.fx -from executorch.backends.arm.quantizer import arm_quantizer_utils -from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig +from executorch.backends.arm.quantizer import arm_quantizer_utils, QuantizationConfig from executorch.backends.arm.tosa_utils import get_node_debug_info from torch.ao.quantization.quantizer import QuantizationSpecBase, SharedQuantizationSpec from torch.ao.quantization.quantizer.utils import ( diff --git a/backends/arm/test/ops/test_expand.py b/backends/arm/test/ops/test_expand.py index 9750f660003..3a7c5fc01f1 100644 --- a/backends/arm/test/ops/test_expand.py +++ b/backends/arm/test/ops/test_expand.py @@ -16,7 +16,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_hardtanh.py b/backends/arm/test/ops/test_hardtanh.py index 6742b398ef5..46b44078785 100644 --- a/backends/arm/test/ops/test_hardtanh.py +++ b/backends/arm/test/ops/test_hardtanh.py @@ -13,7 +13,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_max_pool.py b/backends/arm/test/ops/test_max_pool.py index 2f3426f2dda..657597f9058 100644 --- a/backends/arm/test/ops/test_max_pool.py +++ b/backends/arm/test/ops/test_max_pool.py @@ -12,7 +12,7 @@ import pytest import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_permute.py b/backends/arm/test/ops/test_permute.py index e71b6687865..50db1231b41 100644 --- a/backends/arm/test/ops/test_permute.py +++ b/backends/arm/test/ops/test_permute.py @@ -13,7 +13,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_relu.py b/backends/arm/test/ops/test_relu.py index 6977d30f2fa..3fc64c89be1 100644 --- a/backends/arm/test/ops/test_relu.py +++ b/backends/arm/test/ops/test_relu.py @@ -10,7 +10,7 @@ from typing import Tuple import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_repeat.py b/backends/arm/test/ops/test_repeat.py index 7ea4d15120a..da2770cfafe 100644 --- a/backends/arm/test/ops/test_repeat.py +++ b/backends/arm/test/ops/test_repeat.py @@ -13,7 +13,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_sigmoid_16bit.py b/backends/arm/test/ops/test_sigmoid_16bit.py index 240000e6973..3cd11699a0a 100644 --- a/backends/arm/test/ops/test_sigmoid_16bit.py +++ b/backends/arm/test/ops/test_sigmoid_16bit.py @@ -6,7 +6,7 @@ import pytest import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( get_symmetric_quantization_config, TOSAQuantizer, ) diff --git a/backends/arm/test/ops/test_sigmoid_32bit.py b/backends/arm/test/ops/test_sigmoid_32bit.py index 14808eedaf9..fbfc263a6d0 100644 --- a/backends/arm/test/ops/test_sigmoid_32bit.py +++ b/backends/arm/test/ops/test_sigmoid_32bit.py @@ -5,7 +5,7 @@ import pytest import torch -from executorch.backends.arm.quantizer.arm_quantizer import TOSAQuantizer +from executorch.backends.arm.quantizer import TOSAQuantizer from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig from executorch.backends.arm.test import common from executorch.backends.arm.test.tester.test_pipeline import ( diff --git a/backends/arm/test/ops/test_var.py b/backends/arm/test/ops/test_var.py index 6690c668f94..fb23f24307e 100644 --- a/backends/arm/test/ops/test_var.py +++ b/backends/arm/test/ops/test_var.py @@ -11,7 +11,7 @@ import unittest import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/ops/test_where.py b/backends/arm/test/ops/test_where.py index dd4f3326f8e..91d616232fa 100644 --- a/backends/arm/test/ops/test_where.py +++ b/backends/arm/test/ops/test_where.py @@ -9,7 +9,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/test/tester/arm_tester.py b/backends/arm/test/tester/arm_tester.py index 6346a53edef..fc64239150d 100644 --- a/backends/arm/test/tester/arm_tester.py +++ b/backends/arm/test/tester/arm_tester.py @@ -18,6 +18,7 @@ import torch.utils._pytree as pytree import tosa_tools.v0_80.serializer.tosa_serializer as ts # type: ignore[import-untyped] +from executorch.backends.arm import EthosUPartitioner, TOSAPartitioner from executorch.backends.arm._passes.arm_pass_manager import ArmPassManager from executorch.backends.arm.arm_backend import ( @@ -26,8 +27,7 @@ is_ethosu, is_tosa, ) -from executorch.backends.arm.ethosu_partitioner import EthosUPartitioner -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, @@ -47,7 +47,6 @@ print_error_diffs, ) from executorch.backends.arm.tosa_mapping import extract_tensor_meta -from executorch.backends.arm.tosa_partitioner import TOSAPartitioner from executorch.backends.xnnpack.test.tester import Tester from executorch.devtools.backend_debug import get_delegation_info diff --git a/backends/arm/test/tester/test_pipeline.py b/backends/arm/test/tester/test_pipeline.py index cad62c021f6..95d9898c19d 100644 --- a/backends/arm/test/tester/test_pipeline.py +++ b/backends/arm/test/tester/test_pipeline.py @@ -8,7 +8,7 @@ import torch -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, diff --git a/backends/arm/tosa_partitioner.py b/backends/arm/tosa_partitioner.py index 06b0555bc44..87283f8ec39 100644 --- a/backends/arm/tosa_partitioner.py +++ b/backends/arm/tosa_partitioner.py @@ -13,11 +13,11 @@ get_tosa_spec, is_tosa, ) # usort: skip +from executorch.backends.arm import TOSABackend from executorch.backends.arm._passes.arm_pass_utils import get_first_fake_tensor from executorch.backends.arm.operator_support.tosa_supported_operators import ( tosa_support_factory, ) -from executorch.backends.arm.tosa_backend import TOSABackend from executorch.exir.backend.compile_spec_schema import CompileSpec from executorch.exir.backend.partitioner import ( DelegationSpec, diff --git a/backends/arm/tosa_quant_utils.py b/backends/arm/tosa_quant_utils.py index 0cfa19eb453..afdbf78422e 100644 --- a/backends/arm/tosa_quant_utils.py +++ b/backends/arm/tosa_quant_utils.py @@ -10,8 +10,6 @@ import math from typing import cast, List, NamedTuple, Tuple -import executorch.backends.arm.tosa_mapping - import torch.fx import torch.fx.node @@ -234,7 +232,7 @@ def build_rescale( def build_rescale_to_int32( tosa_fb: ts.TosaSerializer, - input_arg: executorch.backends.arm.tosa_mapping.TosaArg, + input_arg: TosaArg, input_zp: int, rescale_scale: list[float], is_scale32: bool = True, diff --git a/examples/arm/aot_arm_compiler.py b/examples/arm/aot_arm_compiler.py index 446d1a4eca4..8e02c15b21c 100644 --- a/examples/arm/aot_arm_compiler.py +++ b/examples/arm/aot_arm_compiler.py @@ -17,19 +17,17 @@ import torch from examples.devtools.scripts.export_bundled_program import save_bundled_program -from executorch.backends.arm.arm_backend import ( +from executorch.backends.arm import ( ArmCompileSpecBuilder, - get_tosa_spec, - is_ethosu, - is_tosa, + EthosUPartitioner, + TOSAPartitioner, ) -from executorch.backends.arm.ethosu_partitioner import EthosUPartitioner -from executorch.backends.arm.quantizer.arm_quantizer import ( +from executorch.backends.arm.arm_backend import get_tosa_spec, is_ethosu, is_tosa +from executorch.backends.arm.quantizer import ( EthosUQuantizer, get_symmetric_quantization_config, TOSAQuantizer, ) -from executorch.backends.arm.tosa_partitioner import TOSAPartitioner from executorch.backends.arm.tosa_specification import TosaSpecification from executorch.backends.arm.util.arm_model_evaluator import ( diff --git a/examples/arm/ethos_u_minimal_example.ipynb b/examples/arm/ethos_u_minimal_example.ipynb index d73695e9d48..9571fbf0292 100644 --- a/examples/arm/ethos_u_minimal_example.ipynb +++ b/examples/arm/ethos_u_minimal_example.ipynb @@ -79,8 +79,8 @@ "metadata": {}, "outputs": [], "source": [ - "from executorch.backends.arm.arm_backend import ArmCompileSpecBuilder\n", - "from executorch.backends.arm.quantizer.arm_quantizer import (\n", + "from executorch.backends.arm import ArmCompileSpecBuilder\n", + "from executorch.backends.arm.quantizer import (\n", " EthosUQuantizer,\n", " get_symmetric_quantization_config,\n", ")\n", @@ -89,7 +89,7 @@ "target = \"ethos-u55-128\"\n", "\n", "# Create a compilation spec describing the target for configuring the quantizer\n", - "# Some args are used by the Arm Vela graph compiler later in the example. Refer to Arm Vela documentation for an \n", + "# Some args are used by the Arm Vela graph compiler later in the example. Refer to Arm Vela documentation for an\n", "# explanation of its flags: https://gitlab.arm.com/artificial-intelligence/ethos-u/ethos-u-vela/-/blob/main/OPTIONS.md\n", "spec_builder = ArmCompileSpecBuilder().ethosu_compile_spec(\n", " target,\n", @@ -100,12 +100,12 @@ "compile_spec = spec_builder.build()\n", "\n", "# Create and configure quantizer to use a symmetric quantization config globally on all nodes\n", - "quantizer = EthosUQuantizer(compile_spec) \n", + "quantizer = EthosUQuantizer(compile_spec)\n", "operator_config = get_symmetric_quantization_config(is_per_channel=False)\n", "quantizer.set_global(operator_config)\n", "\n", "# Post training quantization\n", - "quantized_graph_module = prepare_pt2e(graph_module, quantizer) \n", + "quantized_graph_module = prepare_pt2e(graph_module, quantizer)\n", "quantized_graph_module(*example_inputs) # Calibrate the graph module with the example input\n", "quantized_graph_module = convert_pt2e(quantized_graph_module)\n", "\n", @@ -128,8 +128,8 @@ "metadata": {}, "outputs": [], "source": [ - "import subprocess \n", - "import os \n", + "import subprocess\n", + "import os\n", "\n", "# Setup paths\n", "cwd_dir = os.getcwd()\n", @@ -163,16 +163,16 @@ "metadata": {}, "outputs": [], "source": [ - "from executorch.backends.arm.ethosu_partitioner import EthosUPartitioner\n", + "from executorch.backends.arm import EthosUPartitioner\n", "from executorch.exir import (\n", " EdgeCompileConfig,\n", " ExecutorchBackendConfig,\n", " to_edge_transform_and_lower,\n", ")\n", "from executorch.extension.export_util.utils import save_pte_program\n", - "import platform \n", + "import platform\n", "\n", - "# Create partitioner from compile spec \n", + "# Create partitioner from compile spec\n", "partitioner = EthosUPartitioner(compile_spec)\n", "\n", "# Lower the exported program to the Ethos-U backend\n", @@ -185,8 +185,8 @@ " )\n", "\n", "# Load quantization ops library\n", - "os_aot_lib_names = {\"Darwin\" : \"libquantized_ops_aot_lib.dylib\", \n", - " \"Linux\" : \"libquantized_ops_aot_lib.so\", \n", + "os_aot_lib_names = {\"Darwin\" : \"libquantized_ops_aot_lib.dylib\",\n", + " \"Linux\" : \"libquantized_ops_aot_lib.so\",\n", " \"Windows\": \"libquantized_ops_aot_lib.dll\"}\n", "aot_lib_name = os_aot_lib_names[platform.system()]\n", "\n", @@ -226,7 +226,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Build executorch \n", + "# Build executorch\n", "subprocess.run(os.path.join(script_dir, \"build_executorch.sh\"), shell=True, cwd=et_dir)\n", "\n", "# Build portable kernels\n",