Skip to content

Commit 935da89

Browse files
authored
Merge branch 'main' into torchao-build
2 parents eab0f73 + da0c80a commit 935da89

39 files changed

+1917
-1005
lines changed

backends/apple/coreml/TARGETS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ runtime.python_library(
1717
name = "backend",
1818
srcs = glob([
1919
"compiler/*.py",
20+
"logging.py",
2021
]),
2122
visibility = [
2223
"@EXECUTORCH_CLIENTS",
@@ -33,6 +34,7 @@ runtime.python_library(
3334
name = "partitioner",
3435
srcs = glob([
3536
"partition/*.py",
37+
"logging.py",
3638
]),
3739
visibility = [
3840
"@EXECUTORCH_CLIENTS",

backends/apple/coreml/compiler/coreml_preprocess.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
import coremltools as ct
1818
import coremltools.optimize as cto
19-
2019
from executorch.backends.apple.coreml import executorchcoreml
20+
from executorch.backends.apple.coreml.logging import get_coreml_log_level
2121
from executorch.exir.backend.backend_details import (
2222
BackendDetails,
2323
ExportedProgram,
2424
PreprocessResult,
2525
)
2626
from executorch.exir.backend.compile_spec_schema import CompileSpec
2727

28-
logger = logging.getLogger(__name__)
29-
logger.setLevel(logging.WARNING)
30-
3128
from executorch.backends.apple.coreml.compiler.torch_ops import * # noqa: F401, F403
3229

30+
logger = logging.getLogger(__name__)
31+
logger.setLevel(get_coreml_log_level(default_level=logging.WARNING))
32+
3333

3434
class COMPILE_SPEC_KEYS(Enum):
3535
COMPUTE_UNITS = "compute_units"
@@ -409,6 +409,7 @@ def preprocess(
409409
edge_program: ExportedProgram,
410410
compile_specs: List[CompileSpec],
411411
) -> PreprocessResult:
412+
logger.info(f"Edge program: {edge_program}")
412413
model_type: CoreMLBackend.MODEL_TYPE = (
413414
CoreMLBackend.model_type_from_compile_specs(
414415
compile_specs,

backends/apple/coreml/compiler/torch_ops.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
# the op to the coremltools library.
1010

1111
import torch as _torch
12-
from coremltools import _logger as logger
12+
from coremltools import _logger
1313
from coremltools.converters.mil.frontend import _utils
1414
from coremltools.converters.mil.frontend.torch.ops import (
1515
_get_inputs,
16+
_get_kwinputs,
1617
NUM_TO_NUMPY_DTYPE,
1718
NUM_TO_TORCH_DTYPE,
1819
split,
20+
to,
1921
transpose,
2022
unbind,
2123
)
@@ -24,6 +26,7 @@
2426
register_torch_op,
2527
)
2628
from coremltools.converters.mil.mil import types
29+
from executorch.exir.dim_order_utils import get_memory_format
2730

2831

2932
# https://github.com/apple/coremltools/pull/2556
@@ -44,6 +47,26 @@ def split_copy(context, node):
4447
split(context, node)
4548

4649

50+
@register_torch_op(
51+
torch_alias=[
52+
"dim_order_ops::_to_dim_order_copy",
53+
"dim_order_ops._to_dim_order_copy",
54+
],
55+
override=False,
56+
)
57+
def _to_dim_order_copy(context, node):
58+
dim_order = _get_kwinputs(context, node, "dim_order", default=[None])[0]
59+
node.kwinputs.pop("dim_order")
60+
61+
# In CoreML, dim_order.val will be an ndarray, so we convert it to a list
62+
dim_order = [int(d) for d in dim_order.val]
63+
memory_format = get_memory_format(dim_order)
64+
assert (
65+
memory_format == _torch.contiguous_format
66+
), "Only contiguous memory format is supported in CoreML"
67+
to(context, node)
68+
69+
4770
# https://github.com/apple/coremltools/pull/2558
4871
@register_torch_op(
4972
torch_alias=["torchao::dequantize_affine", "torchao.dequantize_affine"],
@@ -88,7 +111,7 @@ def dequantize_affine(context, node):
88111
out_np_dtype = None
89112
if len(inputs) > 7:
90113
out_np_dtype = NUM_TO_NUMPY_DTYPE[inputs[7].val]
91-
logger.warning(
114+
_logger.warning(
92115
f"Core ML ignores output_dtype {out_np_dtype} on torchao.dequantize_affine and instead uses the native precision."
93116
)
94117

backends/apple/coreml/logging.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright © 2023 Apple Inc. All rights reserved.
2+
#
3+
# Please refer to the license found in the LICENSE file in the root directory of the source tree.
4+
5+
import logging
6+
import os
7+
from typing import Optional
8+
9+
10+
def get_coreml_log_level(default_level: int) -> Optional[str]:
11+
level_str = os.environ.get("ET_COREML_LOG_LEVEL", "").upper()
12+
if level_str == "":
13+
return default_level
14+
15+
level_map = {
16+
"DEBUG": logging.DEBUG,
17+
"INFO": logging.INFO,
18+
"WARNING": logging.WARNING,
19+
"ERROR": logging.ERROR,
20+
"CRITICAL": logging.CRITICAL,
21+
}
22+
if level_str not in level_map:
23+
raise ValueError(f"Invalid ET_COREML_LOG_LEVEL: {level_str}")
24+
return level_map[level_str]

backends/apple/coreml/partition/coreml_partitioner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import torch
1111

1212
from executorch.backends.apple.coreml.compiler import CoreMLBackend
13+
14+
from executorch.backends.apple.coreml.logging import get_coreml_log_level
1315
from executorch.exir.backend.compile_spec_schema import CompileSpec
1416

1517
from executorch.exir.backend.partitioner import (
@@ -23,7 +25,7 @@
2325
from torch.fx.passes.operator_support import OperatorSupportBase
2426

2527
logger = logging.getLogger(__name__)
26-
logger.setLevel(logging.INFO)
28+
logger.setLevel(get_coreml_log_level(default_level=logging.INFO))
2729

2830

2931
def _is_view_op(op: torch._ops.OpOverload) -> bool:

backends/arm/_passes/unsqueeze_scalar_placeholders_pass.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import torch
99
from executorch.exir.pass_base import ExportPass, PassResult
10+
from torch._export.utils import is_buffer, is_param
1011

1112

1213
class UnsqueezeScalarPlaceholdersPass(ExportPass):
@@ -19,23 +20,27 @@ def __init__(self, exported_program):
1920
self.exported_program = exported_program
2021
super().__init__()
2122

22-
def _is_inputs_to_buffers_or_parameters(self, node):
23-
return (
24-
node.name in self.exported_program.graph_signature.inputs_to_buffers
25-
or node.name in self.exported_program.graph_signature.inputs_to_parameters
26-
)
27-
2823
def call(self, graph_module: torch.fx.GraphModule):
2924
for node in graph_module.graph.nodes:
3025
if node.op != "placeholder":
3126
continue
3227
rank = node.meta["val"].dim()
3328
if rank == 0:
34-
if not self._is_inputs_to_buffers_or_parameters(node):
29+
if is_buffer(self.exported_program, node):
30+
name = self.exported_program.graph_signature.inputs_to_buffers[
31+
node.name
32+
]
33+
elif is_param(self.exported_program, node):
34+
name = self.exported_program.graph_signature.inputs_to_parameters[
35+
node.name
36+
]
37+
else:
3538
continue
36-
tensor = self.exported_program.state_dict[node.name]
39+
40+
tensor = self.exported_program.state_dict[name]
41+
3742
if tensor.dim() == 0:
38-
self.exported_program.state_dict[node.name] = tensor.unsqueeze(0)
43+
self.exported_program.state_dict[name] = tensor.unsqueeze(0)
3944
node.meta["val"] = node.meta["val"].fake_mode.from_tensor(
4045
tensor.unsqueeze(0), static_shapes=True
4146
)
@@ -53,6 +58,9 @@ def ensures(self, graph_module: torch.fx.GraphModule):
5358
if node.op == "placeholder":
5459
rank = node.meta["val"].dim()
5560
if rank == 0:
56-
if not self._is_inputs_to_buffers_or_parameters(node):
61+
if not (
62+
is_buffer(self.exported_program, node)
63+
or is_param(self.exported_program, node)
64+
):
5765
continue
5866
raise ValueError("Placeholders of rank 0 are not supported!")

backends/cadence/aot/compiler_funcs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def trace(
3535
decomp_table = torch.export.default_decompositions()
3636
# pyre-fixme[6]: For 1st argument expected `Dict[typing.Callable[..., typing.Any
3737
remove_decompositions(decomp_table, ops_to_keep)
38-
program = torch.export.export_for_training(
39-
model, inputs, strict=strict
40-
).run_decompositions(decomp_table)
38+
program = torch.export.export(model, inputs, strict=strict).run_decompositions(
39+
decomp_table
40+
)
4141

4242
return program
4343

backends/cadence/hifi/operators/operators.h

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88

99
#pragma once
1010

11+
#include "executorch/runtime/core/exec_aten/exec_aten.h"
12+
#include "executorch/runtime/kernel/kernel_runtime_context.h"
13+
1114
#define ET_FORALL_CADENCE_QUANTIZED_TYPES(_) \
1215
_(uint8_t, Byte) \
1316
_(int8_t, Char)
1417

15-
using ::executorch::aten::IntArrayRef;
16-
using ::executorch::aten::optional;
17-
using ::executorch::aten::ScalarType;
18-
using ::executorch::aten::Tensor;
19-
using ::executorch::runtime::KernelRuntimeContext;
20-
2118
namespace cadence {
2219
namespace impl {
2320
namespace HiFi {
@@ -62,68 +59,68 @@ void quantized_relu_out(
6259
::executorch::aten::Tensor& output);
6360

6461
void quantized_linear_out(
65-
__ET_UNUSED KernelRuntimeContext& ctx,
66-
const Tensor& in,
67-
const Tensor& weight,
68-
const Tensor& bias,
62+
::executorch::runtime::KernelRuntimeContext& ctx,
63+
const ::executorch::aten::Tensor& in,
64+
const ::executorch::aten::Tensor& weight,
65+
const ::executorch::aten::Tensor& bias,
6966
int64_t in_zero_point,
70-
const Tensor& weight_zero_point,
71-
const Tensor& out_multiplier,
72-
const Tensor& out_shift,
67+
const ::executorch::aten::Tensor& weight_zero_point,
68+
const ::executorch::aten::Tensor& out_multiplier,
69+
const ::executorch::aten::Tensor& out_shift,
7370
int64_t out_zero_point,
74-
__ET_UNUSED const optional<Tensor>& offset,
75-
Tensor& out);
71+
const ::executorch::aten::optional<::executorch::aten::Tensor>& offset,
72+
::executorch::aten::Tensor& out);
7673

7774
void quantized_linear_per_tensor_out(
78-
__ET_UNUSED KernelRuntimeContext& ctx,
79-
const Tensor& in,
80-
const Tensor& weight,
81-
const Tensor& bias,
75+
::executorch::runtime::KernelRuntimeContext& ctx,
76+
const ::executorch::aten::Tensor& in,
77+
const ::executorch::aten::Tensor& weight,
78+
const ::executorch::aten::Tensor& bias,
8279
int64_t in_zero_point,
8380
int64_t weight_zero_point,
8481
int64_t out_multiplier,
8582
int64_t out_shift,
8683
int64_t out_zero_point,
87-
__ET_UNUSED const optional<Tensor>& offset,
88-
Tensor& out);
84+
const ::executorch::aten::optional<::executorch::aten::Tensor>& offset,
85+
::executorch::aten::Tensor& out);
8986

9087
void quantized_conv_out(
91-
__ET_UNUSED KernelRuntimeContext& ctx,
92-
const Tensor& input,
93-
const Tensor& weight,
94-
const Tensor& bias,
95-
IntArrayRef stride,
96-
IntArrayRef padding,
97-
IntArrayRef dilation,
88+
::executorch::runtime::KernelRuntimeContext& ctx,
89+
const ::executorch::aten::Tensor& input,
90+
const ::executorch::aten::Tensor& weight,
91+
const ::executorch::aten::Tensor& bias,
92+
::executorch::aten::IntArrayRef stride,
93+
::executorch::aten::IntArrayRef padding,
94+
::executorch::aten::IntArrayRef dilation,
9895
int64_t groups,
9996
int64_t in_zero_point,
100-
const Tensor& weight_zero_point,
101-
const Tensor& bias_scale,
97+
const ::executorch::aten::Tensor& weight_zero_point,
98+
const ::executorch::aten::Tensor& bias_scale,
10299
double output_scale,
103100
int64_t output_zero_point,
104-
__ET_UNUSED const Tensor& out_multiplier,
105-
__ET_UNUSED const Tensor& out_shift,
101+
const ::executorch::aten::Tensor& out_multiplier,
102+
const ::executorch::aten::Tensor& out_shift,
106103
bool channel_last,
107-
Tensor& out);
104+
::executorch::aten::Tensor& out);
108105

109106
void quantized_conv_per_tensor_out(
110-
__ET_UNUSED KernelRuntimeContext& ctx,
111-
const Tensor& input,
112-
const Tensor& weight,
113-
const Tensor& bias,
114-
IntArrayRef stride,
115-
IntArrayRef padding,
116-
IntArrayRef dilation,
107+
::executorch::runtime::KernelRuntimeContext& ctx,
108+
const ::executorch::aten::Tensor& input,
109+
const ::executorch::aten::Tensor& weight,
110+
const ::executorch::aten::Tensor& bias,
111+
::executorch::aten::IntArrayRef stride,
112+
::executorch::aten::IntArrayRef padding,
113+
::executorch::aten::IntArrayRef dilation,
117114
int64_t groups,
118115
int64_t in_zero_point,
119116
int64_t weight_zero_point,
120117
double bias_scale,
121118
double output_scale,
122119
int64_t output_zero_point,
123-
__ET_UNUSED int64_t out_multiplier,
124-
__ET_UNUSED int64_t out_shift,
120+
int64_t out_multiplier,
121+
int64_t out_shift,
125122
bool channel_last,
126-
Tensor& out);
123+
::executorch::aten::Tensor& out);
127124

128125
} // namespace native
129126
} // namespace HiFi

backends/vulkan/quantizer/TARGETS

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ oncall("executorch")
44

55
python_library(
66
name = "vulkan_quantizer",
7-
srcs = [
8-
"vulkan_quantizer.py",
7+
srcs = ["vulkan_quantizer.py"],
8+
deps = [
9+
":vulkan_quantizer_utils",
10+
"//caffe2:torch",
911
],
12+
)
13+
14+
python_library(
15+
name = "vulkan_quantizer_utils",
16+
srcs = ["vulkan_quantizer_utils.py"],
1017
deps = [
1118
"//caffe2:torch",
12-
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer_utils",
1319
],
1420
)

backends/vulkan/quantizer/vulkan_quantizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import Callable, Optional
1313

1414
import torch
15-
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import (
15+
from executorch.backends.vulkan.quantizer.vulkan_quantizer_utils import (
1616
_convert_scalars_to_attrs,
1717
OP_TO_ANNOTATOR,
1818
propagate_annotation,

0 commit comments

Comments
 (0)