Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions backends/arm/test/passes/test_cast_int64_pass.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
# Copyright 2025 Arm Limited and/or its 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
from typing import Tuple

import torch
from executorch.backends.arm._passes.cast_int64_pass import CastInt64ToInt32Pass

from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.test_pipeline import TestPassPipeline

from executorch.backends.arm.test.tester.arm_tester import ArmTester, RunPasses
input_t = Tuple[torch.Tensor] # Input x


class Int64Model(torch.nn.Module):

def forward(self, x: torch.Tensor):
return x + 3

def get_inputs(self):
def get_inputs(self) -> input_t:
return (torch.rand(4),)


class TestCastInt64Pass(unittest.TestCase):

def test_int64_model(self):
module = Int64Model()
test_pass_stage = RunPasses(passes_with_exported_program=[CastInt64ToInt32Pass])
tester = (
ArmTester(
module,
example_inputs=module.get_inputs(),
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"),
)
.export()
.to_edge()
.run_passes(test_pass_stage)
.run_method_and_compare_outputs()
)
exported_program = tester.get_artifact("RunPasses").exported_program()
for state in exported_program.state_dict:
assert exported_program.state_dict[state].dtype != torch.int64
def test_int64_model_tosa_BI():
module = Int64Model()
op_checks = {
"executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default": 1,
"executorch_exir_dialects_edge__ops_aten_add_Tensor": 1,
}
pipeline = TestPassPipeline[input_t](
module,
module.get_inputs(),
tosa_version="TOSA-0.80+BI",
ops_before_pass=op_checks,
ops_after_pass=op_checks,
passes_with_exported_program=[CastInt64ToInt32Pass],
)
pipeline.pop_stage("quantize")
pipeline.run()

exported_program = pipeline.tester.get_artifact("RunPasses").exported_program()
for state in exported_program.state_dict:
assert exported_program.state_dict[state].dtype == torch.int32
63 changes: 25 additions & 38 deletions backends/arm/test/passes/test_fold_qdq_pass.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
# Copyright 2024-2025 Arm Limited and/or its 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
from typing import Tuple

import torch
from executorch.backends.arm._passes.fold_qdq_with_annotated_qparams_pass import (
FoldAndAnnotateQParamsPass,
)
from executorch.backends.arm.test.tester.test_pipeline import TestPassPipeline

from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.arm_tester import ArmTester

from executorch.backends.xnnpack.test.tester.tester import RunPasses
input_t = Tuple[torch.Tensor, torch.Tensor] # Input x, y


class SimpleQuantizeModel(torch.nn.Module):
def forward(self, x, y):
return x + torch.max((x + x), (y + y))

def get_inputs(self):
def get_inputs(self) -> input_t:
return (torch.rand(1, 1280, 7, 7), torch.rand(1, 1280, 7, 7))


class TestFoldAndAnnotateQParamsPass(unittest.TestCase):
def test_fold_qdq_pass_tosa_BI():
"""
Tests the FoldAndAnnotateQParamsPass which folds dq/q nodes into
the node and stores the quantization parameters in meta.
"""

def test_fold_qdq_pass(self):
"""
Check that the pass runs for add operation and that one q node and one dq node
is removed from the representation.
"""
module = SimpleQuantizeModel()
test_pass_stage = RunPasses([FoldAndAnnotateQParamsPass])
(
ArmTester(
module,
example_inputs=module.get_inputs(),
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"),
)
.quantize()
.export()
.to_edge()
.check_count(
{
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 7,
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 6,
}
)
.run_passes(test_pass_stage)
.check_count(
{
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1,
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2,
}
)
)
Check that the pass runs for add operation and that one q node and one dq node
is removed from the representation.
"""
module = SimpleQuantizeModel()
pipeline = TestPassPipeline[input_t](
module,
module.get_inputs(),
tosa_version="TOSA-0.80+BI",
ops_before_pass={
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 7,
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 6,
},
ops_after_pass={
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1,
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2,
},
pass_list=[FoldAndAnnotateQParamsPass],
)
pipeline.pop_stage(-1) # Do not compare output
pipeline.run()
65 changes: 28 additions & 37 deletions backends/arm/test/passes/test_fuse_batchnorm_pass.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Copyright 2025 Arm Limited and/or its 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

from typing import Tuple

import torch
from executorch.backends.arm._passes.fuse_batchnorm2d_pass import FuseBatchnorm2DPass
from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.arm_tester import ArmTester, RunPasses
from parameterized import parameterized
from executorch.backends.arm.test.tester.test_pipeline import TestPassPipeline

input_t = Tuple[torch.Tensor] # Input x


class MergeOneOfTwoBN(torch.nn.Module):
Expand All @@ -35,7 +36,7 @@ def __init__(self, affine: bool):
self.batch_norm2d.bias = torch.nn.Parameter(torch.rand(3))
self.relu6 = torch.nn.ReLU6()

def get_inputs(self) -> tuple[torch.Tensor]:
def get_inputs(self) -> input_t:
return (torch.randn(1, 3, 256, 256),)

def forward(self, x):
Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(self, affine: bool):
self.batch_norm2d.bias = torch.nn.Parameter(torch.rand(3))
self.relu6 = torch.nn.ReLU6()

def get_inputs(self) -> tuple[torch.Tensor]:
def get_inputs(self) -> input_t:
return (torch.randn(1, 3, 256, 256),)

def forward(self, x):
Expand Down Expand Up @@ -110,7 +111,7 @@ def __init__(self, affine: bool):
self.batch_norm2d.bias = torch.nn.Parameter(torch.rand(3))
self.relu6 = torch.nn.ReLU6()

def get_inputs(self) -> tuple[torch.Tensor]:
def get_inputs(self) -> input_t:
return (torch.randn(1, 3, 256, 256),)

def forward(self, x):
Expand All @@ -126,33 +127,23 @@ def forward(self, x):
return z, a


modules = [
MergeOneOfTwoBN(True),
MergeOneOfTwoBN(False),
MergeTwosOfTwoBN(True),
MergeNoBN(True),
]


class TestFuseBatchnormPass(unittest.TestCase):

@parameterized.expand(modules)
def test_fuse_batchnorm_tosa_MI(self, module):
"""Test various cases where the batchnorm should and shouldn't be fused."""
inputs = module.get_inputs()
test_pass_stage = RunPasses(passes_with_exported_program=[FuseBatchnorm2DPass])
(
(
ArmTester(
module,
example_inputs=inputs,
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+MI"),
)
.export()
.to_edge()
.check_count(module.ops_before_pass)
.run_passes(test_pass_stage)
.check_count(module.ops_after_pass)
.run_method_and_compare_outputs()
)
)
modules = {
"merge_one_of_two_bn_affine": MergeOneOfTwoBN(True),
"merge_one_of_two_bn": MergeOneOfTwoBN(False),
"merge_two_of_two_bn_affine": MergeTwosOfTwoBN(True),
"merge_no_bn_affine": MergeNoBN(True),
}


@common.parametrize("module", modules)
def test_fuse_batchnorm_tosa_MI(module):
"""Test various cases where the batchnorm should and shouldn't be fused."""
pipeline = TestPassPipeline[input_t](
module,
module.get_inputs(),
tosa_version="TOSA-0.80+MI",
ops_before_pass=module.ops_before_pass,
ops_after_pass=module.ops_after_pass,
passes_with_exported_program=[FuseBatchnorm2DPass],
)
pipeline.run()
57 changes: 24 additions & 33 deletions backends/arm/test/passes/test_insert_table_ops_pass.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
# Copyright 2025 Arm Limited and/or its 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

from typing import Tuple

import torch
from executorch.backends.arm._passes.fold_qdq_with_annotated_qparams_pass import (
FoldAndAnnotateQParamsPass,
)
from executorch.backends.arm._passes.insert_table_ops import InsertTableOpsPass
from executorch.backends.arm.test.tester.test_pipeline import TestPassPipeline

from executorch.backends.arm.test import common

from executorch.backends.arm.test.tester.arm_tester import ArmTester, RunPasses
input_t = Tuple[torch.Tensor] # Input x


class Sigmoid(torch.nn.Module):

def forward(self, x: torch.Tensor):
return x.sigmoid()

def get_inputs(self):
def get_inputs(self) -> input_t:
return (torch.rand(4),)


class TestInsertTablePass(unittest.TestCase):

def test_insert_table_tosa_BI(self):
module = Sigmoid()
test_pass_stage = RunPasses(
[FoldAndAnnotateQParamsPass],
passes_with_exported_program=[InsertTableOpsPass],
)
(
ArmTester(
module,
example_inputs=module.get_inputs(),
compile_spec=common.get_tosa_compile_spec("TOSA-0.80+BI"),
)
.quantize()
.export()
.to_edge()
.run_passes(test_pass_stage)
.check("tosa._table")
.check_count(
{
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 1,
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1,
}
)
.check_not(["aten_sigmoid_default"])
)
def test_insert_table_tosa_BI():
module = Sigmoid()
pipeline = TestPassPipeline[input_t](
module,
module.get_inputs(),
tosa_version="TOSA-0.80+BI",
ops_before_pass={},
ops_after_pass={
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 1,
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1,
"tosa._table": 1,
},
ops_not_after_pass=["aten_sigmoid_default"],
pass_list=[FoldAndAnnotateQParamsPass],
passes_with_exported_program=[InsertTableOpsPass],
)
pipeline.pop_stage(-1) # Do not compare output

pipeline.run()
Loading
Loading