forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsym_to_tensor_pass.py
More file actions
64 lines (54 loc) · 2.19 KB
/
sym_to_tensor_pass.py
File metadata and controls
64 lines (54 loc) · 2.19 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
# 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.
# pyre-strict
from typing import Union
import torch
from executorch.exir.pass_base import ExportPass, map_args, NodeMetadata, ProxyValue
from torch import SymBool, SymFloat, SymInt
from torch.utils._pytree import PyTree
class SymToTensorPass(ExportPass):
"""
The dispatcher implicitly converts SymInt/SymFloats to tensors, but
sometimes this doesn't comply with the operator's schema which ExecuTorch
heavily relies on. So this pass inserts a
torch.ops.aten.scalar_tensor.default operator before these SymInts are used
so that it matches the schema of the operator.
"""
# pyre-ignore
def call_operator(self, op, args, kwargs, meta: NodeMetadata):
# pyre-ignore
def is_sym(value, arg) -> bool:
if isinstance(value, ProxyValue) and not value.is_tensor():
if isinstance(arg.type, torch.TensorType) and type(value.data) in {
SymInt,
SymFloat,
SymBool,
}:
return True
return False
def corresponding_dtype(
symbol: Union[SymInt, SymFloat, SymBool]
) -> torch.dtype:
if isinstance(symbol, SymInt):
return torch.int32
elif isinstance(symbol, SymFloat):
return torch.float32
elif isinstance(symbol, SymBool):
return torch.bool
else:
raise AssertionError(f"Unsupported data type: {type(symbol)}")
def try_coerce(value: PyTree, arg: torch.Argument) -> PyTree:
if is_sym(value, arg):
return self.call_operator(
torch.ops.aten.scalar_tensor.default,
(value,),
{"dtype": corresponding_dtype(value.data)},
meta,
)
else:
return value
args, kwargs = map_args(op, try_coerce, args, kwargs)
return super().call_operator(op, args, kwargs, meta)