forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_constant_dim_order_pass.py
More file actions
86 lines (71 loc) · 2.8 KB
/
convert_constant_dim_order_pass.py
File metadata and controls
86 lines (71 loc) · 2.8 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
import torch
from torch.export import ExportedProgram
from torch.export.graph_signature import InputKind
def _should_transform(tensor: torch.Tensor) -> bool:
"""
Returns whether the given tensor should be transformed by the pass to
contiguous dim order.
"""
return not tensor.is_contiguous() and not tensor.is_contiguous(
memory_format=torch.channels_last
)
def _update_placeholder_meta(
exported_program: ExportedProgram,
target: str,
kind: InputKind,
):
input_spec = next(
(
spec
for spec in exported_program.graph_signature.input_specs
if spec.kind == kind and spec.target == target
),
None,
)
if input_spec is None:
raise RuntimeError(f"Missing input spec for lifted tensor {target}.")
placeholder_node = next(
(
n
for n in exported_program.graph.nodes
if n.op == "placeholder" and n.name == input_spec.arg.name
),
None,
)
if input_spec is None:
raise RuntimeError(f"Missing placeholder for {input_spec.arg.name}.")
placeholder_node.meta["val"] = placeholder_node.meta["val"].contiguous()
def convert_constant_dim_order_pass(
exported_program: ExportedProgram,
) -> ExportedProgram:
"""
Normalize the dim order of constant tensors, ensuring that all constant tensors
have either default or channels_last dim order. Tensors with other dim orders or
striding are converted to contiguous tensors. This pass acts in-place on the
unlifted exported program.
Args:
exported_program: The ExportedProgram to transform.
Returns:
The modified ExportedProgram with normalized constant dim order.
"""
for key, const in exported_program.constants.items():
if isinstance(const, torch.Tensor) and _should_transform(const):
exported_program.constants[key] = const.contiguous()
# Also update the corresponding placeholder node meta value. This doesn't
# get automatically updated during retracing as ExportPass uses the placeholder
# meta as the source of truth. TODO(?)
_update_placeholder_meta(exported_program, key, InputKind.CONSTANT_TENSOR)
# Convert buffers.
non_persistent_buffer_names = set(
exported_program.graph_signature.non_persistent_buffers
)
for key, buffer in exported_program.named_buffers():
if (
key not in non_persistent_buffer_names
and isinstance(buffer, torch.Tensor)
and _should_transform(buffer)
):
# Persistent buffers are stored in the state dict.
exported_program.state_dict[key] = buffer.contiguous()
_update_placeholder_meta(exported_program, key, InputKind.BUFFER)
return exported_program