-
Notifications
You must be signed in to change notification settings - Fork 972
Expand file tree
/
Copy patharm_compile_spec.py
More file actions
355 lines (305 loc) · 13.2 KB
/
arm_compile_spec.py
File metadata and controls
355 lines (305 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Copyright 2023-2026 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.
#
# Main implementation of AoT flow to partition and preprocess for Arm target
# backends. Converts via TOSA as an intermediate form supported by AoT and
# JIT compiler flows.
#
import json
import warnings
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum
from executorch.backends.arm.common.pipeline_config import (
ArmPassPipelineConfig,
SoftmaxDecompositionConfig,
)
from executorch.backends.arm.tosa import TosaSpecification
from executorch.exir._warnings import deprecated
from executorch.exir.backend.compile_spec_schema import CompileSpec
@dataclass(init=False)
class ArmCompileSpec(ABC):
class DebugMode(Enum):
JSON = 1
TOSA = 2
tosa_spec: TosaSpecification
compiler_flags: list[str] = field(default_factory=list)
path_for_intermediates: str | None = None
tosa_debug_mode: DebugMode | None = None
tosa_dev_mode: bool | None = None
_TOSA_SPEC_KEY = "tosa_spec"
_COMPILE_FLAGS_KEY = "compile_flags"
_OUTPUT_FORMAT_KEY = "output_format"
_DEBUG_ARTIFACT_KEY = "debug_artifact_path"
_DEBUG_MODE_KEY = "dump_debug_info"
_OUTPUT_REORDER_KEY = "ouput_reorder_workaround"
_TRANSFORM_PIPELINE_CONFIG_KEY = "transform_pipeline_config"
_TOSA_DEV_MODE = "tosa_sw_dev_mode"
def _set_compile_specs(
self,
tosa_spec: TosaSpecification,
compiler_flags: list[str],
path_for_intermediates: str | None = None,
tosa_debug_mode: DebugMode | None = None,
output_order_workaround: bool = False,
pipeline_config: ArmPassPipelineConfig | None = None,
tosa_dev_mode: bool | None = None,
):
"""Set all values of dataclass directly."""
self.tosa_spec = tosa_spec
self.compiler_flags = compiler_flags
self.path_for_intermediates = path_for_intermediates
self.tosa_debug_mode = tosa_debug_mode
self._pipeline_config = pipeline_config
self.output_order_workaround = output_order_workaround
self.tosa_dev_mode = tosa_dev_mode
if output_order_workaround:
warnings.warn(
"ArmCompileSpec(output_order_workaround=True) is deprecated and will be "
"removed in v1.5; please remove this flag.",
DeprecationWarning,
stacklevel=2,
)
@classmethod
def _from_list(cls, compile_specs: list[CompileSpec]): # noqa: C901
tosa_spec: TosaSpecification | None = None
output_format: str | None = None
compiler_flags: list[str] | None = None
path_for_intermediates: str | None = None
tosa_debug_mode: ArmCompileSpec.DebugMode | None = None
output_order_workaround: bool = False
pipeline_config: ArmPassPipelineConfig | None = None
tosa_dev_mode: bool | None = None
unknown_specs: dict[str, str] = {}
for spec in compile_specs:
key = spec.key
val = (
spec.value.decode()
if isinstance(spec.value, (bytes, bytearray))
else spec.value
)
if key == ArmCompileSpec._TOSA_SPEC_KEY:
if tosa_spec is not None:
raise ValueError("More than one tosa_spec entry in compile spec.")
tosa_spec = TosaSpecification.create_from_string(val)
elif key == ArmCompileSpec._COMPILE_FLAGS_KEY:
if compiler_flags is not None:
raise ValueError(
"More than one compiler flags entry in compile spec."
)
compiler_flags = val.split(" ")
elif key == ArmCompileSpec._OUTPUT_FORMAT_KEY:
if output_format is not None:
raise ValueError(
"More than one output format entry in compile spec."
)
output_format = val
elif key == ArmCompileSpec._DEBUG_ARTIFACT_KEY:
if path_for_intermediates is not None:
raise ValueError(
"More than one debug artifact path entry in compile spec."
)
path_for_intermediates = val
elif key == ArmCompileSpec._DEBUG_MODE_KEY:
if tosa_debug_mode is not None:
raise ValueError(
"More than one tosa_debug_mode entry in compile spec."
)
tosa_debug_mode = ArmCompileSpec.DebugMode[val]
elif key == ArmCompileSpec._OUTPUT_REORDER_KEY:
output_order_workaround = val # type: ignore[assignment]
if output_order_workaround:
warnings.warn(
"The 'output_order_workaround' compile spec entry is deprecated and will be removed in v1.5; please remove this entry.",
DeprecationWarning,
stacklevel=2,
)
elif key == ArmCompileSpec._TRANSFORM_PIPELINE_CONFIG_KEY:
if pipeline_config is not None:
raise ValueError(
"More than one transform pipeline entry in compile spec."
)
pipeline_config = ArmPassPipelineConfig.from_dict(json.loads(val))
elif key == ArmCompileSpec._TOSA_DEV_MODE:
if tosa_dev_mode is not None:
raise ValueError(
"More than one tosa_sw_dev_mode entry in compile spec."
)
raw = bytes(spec.value)
if raw == b"\x01":
tosa_dev_mode = True
elif raw == b"\x00":
tosa_dev_mode = False
else:
raise ValueError(
f"Invalid tosa_sw_dev_mode byte value: {raw!r}, expected b'\\x00' or b'\\x01'."
)
else:
unknown_specs[key] = val
if tosa_spec is None:
raise ValueError("No tosa_spec in compile spec.")
if output_format is None:
raise ValueError("No output_format in compile spec.")
if output_format != cls._get_output_format():
raise ValueError(
f"Incorrect output format '{output_format}' for {cls.__name__}, expected '{cls._get_output_format()}'"
)
if compiler_flags is None:
compiler_flags = []
# Create new object from class, but bypass __init__ and use _set_compile_specs instead.
compile_spec = cls.__new__(cls)
compile_spec._set_compile_specs(
tosa_spec=tosa_spec,
compiler_flags=compiler_flags,
path_for_intermediates=path_for_intermediates,
tosa_debug_mode=tosa_debug_mode,
output_order_workaround=output_order_workaround,
pipeline_config=pipeline_config,
tosa_dev_mode=tosa_dev_mode,
)
cls._from_list_hook(compile_spec, unknown_specs)
compile_spec._validate()
return compile_spec
@classmethod
def _from_list_hook(cls, compile_spec, specs: dict[str, str]): # noqa: B027
"""Allows subclasses to hook into parsing compile spec lists."""
pass
@abstractmethod
def _validate(self):
"""Throws an error if the compile spec is not valid."""
def _to_list(self):
"""Get the ArmCompileSpec in list form."""
if not self.tosa_spec:
raise ValueError("tosa_spec must be set before calling _to_list()")
# Always supply a TOSA version
compile_spec = [
CompileSpec(ArmCompileSpec._TOSA_SPEC_KEY, str(self.tosa_spec).encode())
]
# Add compile flags, these are backend specific, refer to the backend
# documentation.
if len(self.compiler_flags) > 0:
compile_spec += [
CompileSpec(
ArmCompileSpec._COMPILE_FLAGS_KEY,
" ".join(self.compiler_flags).encode(),
),
]
# Add output format to identify kind of compile spec.
compile_spec.append(
CompileSpec(
ArmCompileSpec._OUTPUT_FORMAT_KEY, self._get_output_format().encode()
)
)
if self.path_for_intermediates is not None:
compile_spec.append(
CompileSpec(
ArmCompileSpec._DEBUG_ARTIFACT_KEY,
self.path_for_intermediates.encode(),
)
)
if self.tosa_debug_mode is not None:
if not self.path_for_intermediates:
raise ValueError(
"dump_debug_info() must be used in conjunction with dump_intermediate_artifacts_to()"
)
compile_spec.append(
CompileSpec(
ArmCompileSpec._DEBUG_MODE_KEY, self.tosa_debug_mode.name.encode()
)
)
if not self.output_order_workaround:
compile_spec.append(
CompileSpec(
ArmCompileSpec._OUTPUT_REORDER_KEY,
bytes(self.output_order_workaround),
)
)
if self._pipeline_config is not None and not self._pipeline_config.is_default():
compile_spec.append(
CompileSpec(
ArmCompileSpec._TRANSFORM_PIPELINE_CONFIG_KEY,
self._pipeline_config.serialize(),
)
)
if self.tosa_dev_mode is not None:
compile_spec.append(
CompileSpec(
ArmCompileSpec._TOSA_DEV_MODE,
b"\x01" if self.tosa_dev_mode else b"\x00",
)
)
return compile_spec
def _get_pass_pipeline_config(self) -> ArmPassPipelineConfig:
"""Returns configuration that controls how the Arm pass pipeline should
behave.
Subclasses may override to tweak defaults for specific targets.
"""
if self._pipeline_config is None:
self._pipeline_config = self._create_default_pipeline_config()
return self._pipeline_config
def set_pass_pipeline_config(self, config: ArmPassPipelineConfig) -> None:
"""Sets the configuration that controls how the Arm pass pipeline should
behave. Subclasses may override to tweak defaults for specific targets.
Args:
config: The custom ArmPassPipelineConfig to set.
"""
self._pipeline_config = config
def _create_default_pipeline_config(self) -> ArmPassPipelineConfig:
config = ArmPassPipelineConfig()
if self.tosa_spec.is_U55_subset:
# Keep U55 on STABLE instead of the generic MASKED default:
# MASKED also enables masked_fill decomposition, which lowers to
# where/full_like and is not a good default fit for U55.
config.softmax = SoftmaxDecompositionConfig.STABLE
return config
def _get_intermediate_path(self) -> str | None:
"""Gets the path used for dumping intermediate results such as tosa and
pte.
Returns:
Path where intermediate results are saved.
"""
return self.path_for_intermediates
def dump_intermediate_artifacts_to(self, output_path: str | None):
"""Sets a path for dumping intermediate results during such as tosa and
pte.
Args:
output_path: Path to dump intermediate results to.
"""
self.path_for_intermediates = output_path
return self
def dump_debug_info(self, debug_mode: DebugMode | None):
"""Dump debugging information into the intermediates path.
Args:
debug_mode: The debug mode to use for dumping debug information.
"""
self.tosa_debug_mode = debug_mode
return self
def _set_tosa_dev_mode(self, tosa_dev_mode: bool):
"""Sets whether to enable TOSA software development mode.
Args:
tosa_dev_mode: Boolean indicating whether to enable TOSA software development mode.
"""
self.tosa_dev_mode = tosa_dev_mode
return self
@deprecated(
"set_output_order_workaround() is deprecated and will be removed in v1.5; please remove this call."
)
def set_output_order_workaround(self, output_order_workaround: bool):
"""Sets whether to apply the output order workaround.
Args:
output_order_workaround: Boolean indicating whether to apply the workaround.
"""
self.output_order_workaround = output_order_workaround
return self
@deprecated(
"get_output_order_workaround() is deprecated and will be removed in v1.5; please remove this call."
)
def get_output_order_workaround(self) -> bool:
"""Gets whether the output order workaround is being applied."""
return self.output_order_workaround
@classmethod
@abstractmethod
def _get_output_format(cls) -> str:
"""Returns a constant string that is the output format of the class."""