forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathet_schema.py
More file actions
419 lines (385 loc) · 15.6 KB
/
et_schema.py
File metadata and controls
419 lines (385 loc) · 15.6 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (c) Meta Platforms, Inc. and 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.
"""
Intermediate Representation of ExecuTorch Concepts in Productivity SDK
"""
from __future__ import annotations
import operator
import warnings
from collections import defaultdict
from enum import Enum
from types import NoneType
from typing import Any, Dict, List, Optional, Set, Tuple
import torch
from executorch import exir
from executorch.sdk.debug_format.base_schema import (
Node,
OperatorGraph,
OperatorNode,
ValueNode,
)
from torch._subclasses import FakeTensor
# Keywords used in debug_format Metadata
class RESERVED_METADATA_ARG(Enum):
DEBUG_HANDLE = "debug_handle"
MODULE_STACK = "nn_module_stack"
SOURCE_FN_STACK = "source_fn_stack"
MODULE_TYPE = "module_type"
PROFILE_START_TIME = "profile_start_time"
PROFILE_END_TIME = "profile_end_time"
LOAD_START_TIME = "load_start_time"
LOAD_END_TIME = "load_end_time"
MEMORY_USAGE = "memory_usage"
DEBUG_ENTRY = "debug_entry"
STACK_TRACE = "stack_trace"
DEBUG_DATA = "debug_data"
METRICS_KEYWORD = "metrics"
PROFILE_SUMMARY_COLDSTART = "Coldstart"
PROFILE_SUMMARY_AVERAGE = "Average"
PROFILE_SUMMARY_P90 = "P90"
PROFILE_SUMMARY_P10 = "P10"
PROFILE_SUMMARY_MIN = "Min"
PROFILE_SUMMARY_MAX = "Max"
AGGREGATED_OP_TABLE = "Aggregated op stats"
RUN_SUMMARY_INDIVIDUAL_RUNS_TABLE = "Run summary individual stats"
RUN_SUMMARY_TABLE = "Aggregated run summary stats"
OP_INSTANCE_SUMMARY_TABLE = "Individual op stats"
TABLES_KEYWORD = "tables"
KV_KEYWORD = "kv"
MODEL_LOAD_TIME_KEY = "Model load time (ms)"
# Representation of an FX GraphModule as an OperatorGraph
class FXOperatorGraph(OperatorGraph):
@staticmethod
def _get_node_name(node: torch.fx.Node) -> str:
if node.target == operator.getitem:
# pyre-ignore[9]: Incompatible variable type
node = node.args[0]
assert isinstance(
node, torch.fx.Node
), f"First argument of getitem must be a torch fx node. Got {node.args[0]}"
# Adding the "_" to the node name prevents TensorBoard from collapsing
# nodes with similar names that only differ by an integer at the end of
# their name.
return node.name + "_"
@staticmethod
def _get_op_name(node: torch.fx.Node) -> str:
# pyre-ignore[16]: has no attribute `__name__`.
return node.target.__name__
# Given a node and its metadata (if containing module stack), update the provided module mappings
@staticmethod
def _update_module_mapping(
node: Node,
module_mapping: Dict[Tuple[str, str], List[Node]],
metadata: Dict[str, Any],
):
if (
source_fn_stack := metadata.get("source_fn_stack")
) is not None and "nn_module_stack" in metadata:
# (module name, module type)
source_fn = source_fn_stack[-1]
module_type = (
source_fn[1] if isinstance(source_fn[1], str) else source_fn[1].__name__
)
module_mapping[(source_fn[0], module_type)].append(node)
@staticmethod
def _parse_args( # noqa: C901
node: torch.fx.Node,
nodes: Dict[str, Node],
const_count: int,
module_mapping: Dict[Tuple[str, str], List[Node]],
enable_module_hierarchy: bool,
) -> Tuple[List[Node], int]:
inputs = []
op = node.op
name = node.name
args = node.args
kwargs = node.kwargs
named_args = None
if node.op == "call_function" and hasattr(node.target, "_schema"):
# pyre-ignore
named_args = node.target._schema.arguments
for index, arg in enumerate(args):
if isinstance(arg, torch.fx.node.Node):
if arg.target == exir.memory.alloc:
continue
arg_name = FXOperatorGraph._get_node_name(arg)
elif isinstance(arg, (int, float, torch.dtype, str)):
# e.g. The "0" from node.args of squeeze_copy (mm_default, 0)
if named_args and len(named_args) > index:
arg_name = named_args[index].name + "_" + str(const_count)
else:
arg_name = "CONST_" + str(const_count)
const_count += 1
const_node = ValueNode(arg_name, val=str(arg))
nodes[arg_name] = const_node
if enable_module_hierarchy:
FXOperatorGraph._update_module_mapping(
const_node, module_mapping, node.meta
)
elif isinstance(arg, list):
arg_name: List[str] = []
for list_arg in arg:
if isinstance(list_arg, (int, float)):
# Consider the whole list of ints/floats as a single constant and
# stringify that.
if named_args and len(named_args) > index:
arg_name = [named_args[index].name + "_" + str(const_count)]
else:
arg_name = ["CONST_" + str(const_count)]
const_count += 1
const_node = ValueNode(arg_name[0], val=arg)
nodes[arg_name[0]] = const_node
if enable_module_hierarchy:
FXOperatorGraph._update_module_mapping(
const_node, module_mapping, node.meta
)
break
elif isinstance(list_arg, torch.fx.node.Node):
arg_name += [FXOperatorGraph._get_node_name(list_arg)]
elif list_arg is None:
arg_name += ["CONST_NONE" + str(const_count)]
const_count += 1
const_node = ValueNode(arg_name[-1], val=str(arg))
nodes[arg_name[-1]] = const_node
if enable_module_hierarchy:
FXOperatorGraph._update_module_mapping(
const_node, module_mapping, node.meta
)
else:
raise Exception(
f"Unsupported argument encountered in list {arg}, {type(arg[0])}"
)
elif isinstance(arg, NoneType):
continue
else:
raise Exception(
f"Unsupported argument encountered {op}, {name}, {arg}, {type(arg)}"
)
if isinstance(arg_name, list):
for val in arg_name:
inputs.append(nodes[val])
else:
inputs.append(nodes[arg_name])
for _, node in kwargs.items():
# We can ignore the out kwarg as that's mostly used to pass in the output tensor
# which has been memory planned. The same value is also returned by the operator
# which is then consumed by other nodes in the graph.
if (
isinstance(node, torch.fx.node.Node)
and node.target == exir.memory.alloc
):
continue
else:
warnings.warn(
f"Unsupported kwarg encountered: {name}, {kwargs}", stacklevel=1
)
return inputs, const_count
# Given an FX GraphModule, parse it into an OperatorGraph
@staticmethod
def gen_operator_graph(
model: torch.fx.GraphModule,
skip_stack_trace: Optional[bool] = False,
enable_module_hierarchy: bool = False,
) -> FXOperatorGraph:
graph: torch.fx.Graph = model.graph
nodes = {}
input_nodes = {}
output_nodes = {}
out_variant_output_nodes = set()
module_mapping = defaultdict(list)
const_count = 0
for fx_node in graph.nodes:
if (
fx_node.target == exir.memory.alloc
or fx_node.target == operator.getitem
):
continue
op = fx_node.op
name = FXOperatorGraph._get_node_name(fx_node)
target = fx_node.target
args = fx_node.args
kwargs = fx_node.kwargs
metadata = FXOperatorGraph._extract_metadata(fx_node.meta, skip_stack_trace)
output_shapes = FXOperatorGraph._extract_output_shapes(
fx_node.meta.get("val")
)
dtype = FXOperatorGraph._extract_output_dtype(fx_node.meta.get("val")) or ""
assert (
op != "call_module"
), f"Module call not yet supported in edge model graph [.toEdge()]: {name}, {str(target)}"
assert (
op != "call_method"
), f"Call Method not yet supported in edge model graph [.toEdge()]: {name}, {str(target)}"
# Input
if op == "placeholder":
node = ValueNode(
name,
output_shapes=output_shapes,
metadata=metadata,
dtype=str(dtype),
) # val is default arg
input_nodes[name] = node
# Constants
elif op == "get_attr":
node = ValueNode(
name,
output_shapes=output_shapes,
metadata=metadata,
dtype=str(dtype),
)
# Output
elif op == "output":
assert len(args) == 1
# Args of op=='output' is a wrapped list of return nodes ([ret_1, ret_2, ...], )
in_nodes = [
(
nodes[FXOperatorGraph._get_node_name(ret)]
if ret is not None
else []
)
for ret in args[0]
]
node = ValueNode(
name,
inputs=in_nodes,
output_shapes=output_shapes,
metadata=metadata,
dtype=str(dtype),
)
output_nodes[name] = node
# Op Calls
elif op == "call_function":
inputs, const_count = FXOperatorGraph._parse_args(
fx_node, nodes, const_count, module_mapping, enable_module_hierarchy
)
named_args = []
if fx_node.op == "call_function" and hasattr(fx_node.target, "_schema"):
named_args = [arg.name for arg in fx_node.target._schema.arguments]
node = OperatorNode(
name,
inputs=inputs,
output_shapes=output_shapes,
metadata=metadata,
op=FXOperatorGraph._get_op_name(fx_node),
named_args=named_args,
)
if enable_module_hierarchy:
FXOperatorGraph._update_module_mapping(
node, module_mapping, fx_node.meta
)
for kwarg_name, kwarg in kwargs.items():
if (
isinstance(kwarg, torch.fx.node.Node)
and kwarg.target == exir.memory.alloc
and kwarg_name == "out"
):
nodes[FXOperatorGraph._get_node_name(kwarg)] = node
out_variant_output_nodes.add(
FXOperatorGraph._get_node_name(kwarg)
)
else:
raise Exception(f"Unsupported op type encountered {op}, {name}")
nodes[name] = node
return FXOperatorGraph._compose_op_graph(
"base",
nodes,
input_nodes,
output_nodes,
out_variant_output_nodes,
module_mapping,
)
@staticmethod
def _compose_op_graph(
name: str,
nodes: Dict[str, Node],
input_nodes: Dict[
str, Node | OperatorGraph
], # Never OperatorGraph, annotated for Pyre
output_nodes: Dict[
str, Node | OperatorGraph
], # Never OperatorGraph, annotated for Pyre
out_variant_output_nodes: Set[str],
module_mapping: Dict[
Tuple[str, str], List[Any]
], # Any used here for Pyre, list of Nodes
):
# Generate Module Graphs
module_graphs: List[OperatorGraph] = []
for module_key, module_nodes in module_mapping.items():
module_element = OperatorGraph(
graph_name=module_key[0],
elements=module_nodes,
metadata={"module_type": module_key[1]},
)
module_graphs.append(module_element)
# Remove module modes from main graph
for node in module_nodes:
nodes.pop(node.name)
main_nodes = [
node
for name, node in nodes.items()
if name not in input_nodes
and name not in output_nodes
and name not in out_variant_output_nodes
]
main_graph = FXOperatorGraph(
graph_name="forward", elements=main_nodes + module_graphs
)
input_graph = FXOperatorGraph(
graph_name="inputs", elements=list(input_nodes.values())
)
output_graph = FXOperatorGraph(
graph_name="outputs", elements=list(output_nodes.values())
)
return FXOperatorGraph(
graph_name=name,
elements=[input_graph, main_graph, output_graph],
)
# Given a dict, extract only the utilized metadata
@staticmethod
def _extract_metadata(
metadata: Dict[str, Any], skip_stack_trace: Optional[bool] = False
) -> Dict[str, Any]:
ret = {}
if RESERVED_METADATA_ARG.DEBUG_HANDLE.value in metadata:
ret[RESERVED_METADATA_ARG.DEBUG_HANDLE.value] = metadata[
RESERVED_METADATA_ARG.DEBUG_HANDLE.value
]
if not skip_stack_trace and RESERVED_METADATA_ARG.STACK_TRACE.value in metadata:
ret[RESERVED_METADATA_ARG.STACK_TRACE.value] = metadata[
RESERVED_METADATA_ARG.STACK_TRACE.value
]
if RESERVED_METADATA_ARG.MODULE_STACK.value in metadata:
ret[RESERVED_METADATA_ARG.MODULE_STACK.value] = metadata[
RESERVED_METADATA_ARG.MODULE_STACK.value
]
return ret
@staticmethod
def _extract_output_shapes(val: Any) -> Optional[List[List[int]]]:
if isinstance(val, (FakeTensor, torch.Tensor)):
# If val is a single tensor
return [list(val.shape)]
elif isinstance(val, tuple) and all(
isinstance(tensor, (FakeTensor, torch.Tensor)) for tensor in val
):
# If val is a tuple of tensors
shapes = [list(fake_tensor.shape) for fake_tensor in val]
return shapes
else:
return None
@staticmethod
def _extract_output_dtype(val: Any) -> Optional[List[torch.dtype]]:
if isinstance(val, (FakeTensor, torch.Tensor)):
# If val is a single tensor
return [val.dtype]
elif isinstance(val, tuple) and all(
isinstance(tensor, (FakeTensor, torch.Tensor)) for tensor in val
):
# If val is a tuple of tensors
dtypes = [fake_tensor.dtype for fake_tensor in val]
return dtypes
else:
return None