Skip to content

Commit f9a3ca8

Browse files
authored
[Backend Tester] Add CoreML tester implementation (pytorch#11959)
Add a tester implementation for Core ML. This uses the common backend tester framework to allow Core ML to plug into the test harness using pybindings.
1 parent 7400d5c commit f9a3ca8

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

backends/apple/coreml/test/tester.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from typing import Any, List, Optional, Tuple
8+
9+
import executorch
10+
import executorch.backends.test.harness.stages as BaseStages
11+
12+
import torch
13+
from executorch.backends.apple.coreml.partition import CoreMLPartitioner
14+
from executorch.backends.test.harness import Tester as TesterBase
15+
from executorch.backends.test.harness.stages import StageType
16+
from executorch.exir import EdgeCompileConfig
17+
from executorch.exir.backend.partitioner import Partitioner
18+
19+
20+
class Partition(BaseStages.Partition):
21+
def __init__(self, partitioner: Optional[Partitioner] = None):
22+
super().__init__(
23+
partitioner=partitioner or CoreMLPartitioner,
24+
)
25+
26+
27+
class ToEdgeTransformAndLower(BaseStages.ToEdgeTransformAndLower):
28+
def __init__(
29+
self,
30+
partitioners: Optional[List[Partitioner]] = None,
31+
edge_compile_config: Optional[EdgeCompileConfig] = None,
32+
):
33+
super().__init__(
34+
default_partitioner_cls=CoreMLPartitioner,
35+
partitioners=partitioners,
36+
edge_compile_config=edge_compile_config,
37+
)
38+
39+
40+
class CoreMLTester(TesterBase):
41+
def __init__(
42+
self,
43+
module: torch.nn.Module,
44+
example_inputs: Tuple[torch.Tensor],
45+
dynamic_shapes: Optional[Tuple[Any]] = None,
46+
):
47+
# Specialize for XNNPACK
48+
stage_classes = (
49+
executorch.backends.test.harness.Tester.default_stage_classes()
50+
| {
51+
StageType.PARTITION: Partition,
52+
StageType.TO_EDGE_TRANSFORM_AND_LOWER: ToEdgeTransformAndLower,
53+
}
54+
)
55+
56+
super().__init__(
57+
module=module,
58+
stage_classes=stage_classes,
59+
example_inputs=example_inputs,
60+
dynamic_shapes=dynamic_shapes,
61+
)

backends/test/operators/test_facto.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
# pyre-strict
7+
# pyre-unsafe
8+
9+
#
10+
# This file contains logic to run generated operator tests using the FACTO
11+
# library (https://github.com/pytorch-labs/FACTO). To run the tests, first
12+
# clone and install FACTO by running pip install . from the FACTO source
13+
# directory. Then, from the executorch root directory, run the following:
14+
#
15+
# python -m unittest backends.test.operators.test_facto.FactoTestsXNNPACK
16+
#
817

918
import copy
1019
import functools
@@ -26,9 +35,9 @@
2635
CombinedSpecDB = SpecDictDB | ExtraSpecDB
2736

2837
COMMON_TENSOR_CONSTRAINTS = [
29-
cp.Rank.Ge(lambda deps: 1),
38+
cp.Rank.Ge(lambda deps: 1), # Avoid zero and high rank tensors.
3039
cp.Rank.Le(lambda deps: 4),
31-
cp.Size.Ge(lambda deps, r, d: 1),
40+
cp.Size.Ge(lambda deps, r, d: 1), # Keep sizes reasonable.
3241
cp.Size.Le(lambda deps, r, d: 2**9),
3342
]
3443

@@ -171,7 +180,7 @@ def get_runtime_input_count(spec: Spec):
171180
def setUp(self):
172181
torch.set_printoptions(threshold=3)
173182

174-
def _test_op(self, op: OpOverload) -> None: # noqa
183+
def _test_op(self, op: OpOverload) -> None: # noqa: C901
175184
random_manager.seed(0)
176185

177186
# Strip namespace

0 commit comments

Comments
 (0)