Skip to content

Commit 1205999

Browse files
Arm backend: Add docstrings to tosa/backend.py (#15552)
Signed-off-by: Sebastian Larsson <[email protected]>
1 parent fd4eb9d commit 1205999

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

backends/arm/tosa/backend.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
5+
"""Provide TOSA backend entry points for the Arm ExecuTorch integration.
56
7+
Implement the Ahead-of-Time (AoT) preprocessing path that lowers an
8+
``ExportedProgram`` to a TOSA flatbuffer using Arm's lowering pipeline. Use
9+
this module either as a standalone backend that produces a TOSA artifact or as
10+
part of a composed pipeline for hardware backends that consume TOSA as an
11+
intermediate form.
12+
13+
Use ``TOSABackend.preprocess`` to return the serialized TOSA flatbuffer that
14+
subsequent stages (for example, JIT or hardware-specific compilers) consume.
15+
16+
"""
617

7-
#
8-
# Main implementation of AoT flow to partition and preprocess for Arm target
9-
# backends. Converts via TOSA as an intermediate form supported by AoT and
10-
# JIT compiler flows.
11-
#
1218
import logging
1319
import tempfile
1420
from collections import deque
@@ -89,7 +95,16 @@ def _sort_key(t: Node) -> int:
8995

9096

9197
def arm_get_first_delegation_tag(graph_module) -> str:
92-
"""Get the first delegation tag from the graph_module or return empty string."""
98+
"""Return the first delegation tag from the FX graph.
99+
100+
Args:
101+
graph_module: FX GraphModule produced by the Arm passes.
102+
103+
Returns:
104+
str: The first non-empty delegation tag found on any node, or an empty
105+
string if none is present.
106+
107+
"""
93108
for node in graph_module.graph.nodes:
94109
tag = node.meta.get("delegation_tag")
95110
if tag:
@@ -101,10 +116,11 @@ def arm_get_first_delegation_tag(graph_module) -> str:
101116

102117
@final
103118
class TOSABackend(BackendDetails):
104-
"""
105-
BackendDetails subclass for lowering to TOSA.
106-
Is used either by itself to get to a TOSA representation, or with composition
107-
to be used as a separate step to target TOSA compliant hardware.
119+
"""Provide a backend for lowering programs to TOSA.
120+
121+
Use this class standalone to produce a TOSA representation, or as part of a
122+
composed pipeline for hardware backends that consume TOSA.
123+
108124
"""
109125

110126
@staticmethod
@@ -118,6 +134,31 @@ def _preprocess( # noqa: C901
118134
edge_program: ExportedProgram,
119135
compile_spec: TosaCompileSpec,
120136
) -> PreprocessResult:
137+
"""Lower an exported program to a TOSA flatbuffer.
138+
139+
Apply Arm transformation passes to ``edge_program``, then walk the
140+
transformed FX graph to emit a TOSA graph via the serializer. When
141+
requested in ``compile_spec``, write additional debug artifacts.
142+
143+
Args:
144+
edge_program (ExportedProgram): Program to lower to TOSA.
145+
compile_spec (List[CompileSpec]): Backend options. Recognized keys:
146+
- output_format: Must be "tosa".
147+
- tosa_spec: Target TOSA version/capabilities.
148+
- debug_artifact_path: Directory for debug outputs.
149+
- compile_flags: Optional backend flags.
150+
- dump_debug_info: Enable extra debug JSON dump.
151+
152+
Returns:
153+
PreprocessResult: Result containing processed_bytes with the
154+
serialized TOSA flatbuffer.
155+
156+
Raises:
157+
ValueError: If output_format is not "tosa" or the TOSA
158+
specification is missing from compile_spec.
159+
RuntimeError: If an unsupported FX node type is encountered.
160+
161+
"""
121162
# if a debug/test build capture output files from TOSA stage
122163
artifact_path = compile_spec.get_intermediate_path()
123164
tosa_spec = compile_spec.tosa_spec
@@ -258,6 +299,15 @@ def filter_tosa_compile_specs(
258299
the TOSA flatbuffer representation as an intermediate step. The TOSA
259300
flatbuffer can then be consumed by the backend targetting specific
260301
hardware.
302+
303+
Args:
304+
compile_spec (ArmCompileSpec): Compile specification that may
305+
include both TOSA and hardware-specific options.
306+
307+
Returns:
308+
TosaCompileSpec: TOSA-only specification ready for
309+
``TOSABackend.preprocess``.
310+
261311
"""
262312

263313
return (

0 commit comments

Comments
 (0)