Skip to content

Commit

Permalink
[torchgen] Introduce Executorch types and signatures (pytorch#90591)
Browse files Browse the repository at this point in the history
Retry of pytorch#89595. Accidentally closed.

## Forked `BaseCppType`

Created a module for Executorch: `torchgen.executorch`.

In `torchgen.executorch.api.types.types`:
* Define `BaseCppType` with `torch::executor` namespace.

In `torchgen.executorch.api.et_cpp`:
* Help generate `NamedCType` for `ExecutorchCppSignature` arguments.

In `torchgen.executorch.api.types.signatures`:
* Define the signature using these types. (`ExecutorchCppSignature`)

In `torchgen.executorch.api.types.__init__`:
* Suppress flake8 error for `import *`.

Differential Revision: [D41501836](https://our.internmc.facebook.com/intern/diff/D41501836/)
Pull Request resolved: pytorch#90591
Approved by: https://github.com/iseeyuan
  • Loading branch information
larryliu0820 authored and pytorchmergebot committed Dec 10, 2022
1 parent de6beca commit ddf00c8
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ ignore =
B007,B008,
# these ignores are from flake8-comprehensions; please fix!
C400,C401,C402,C403,C404,C405,C407,C411,C413,C414,C415
per-file-ignores = __init__.py: F401 torch/utils/cpp_extension.py: B950 torchgen/api/types/__init__.py: F401,F403
per-file-ignores =
__init__.py: F401
torch/utils/cpp_extension.py: B950
torchgen/api/types/__init__.py: F401,F403
torchgen/executorch/api/types/__init__.py: F401,F403
optional-ascii-coding = True
exclude =
./.git,
Expand Down
109 changes: 109 additions & 0 deletions tools/test/test_executorch_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import unittest

from torchgen import local
from torchgen.api.types import (
BaseCType,
ConstRefCType,
CType,
longT,
MutRefCType,
NamedCType,
OptionalCType,
TupleCType,
VectorCType,
voidT,
)
from torchgen.executorch.api.et_cpp import argument_type, return_type, returns_type
from torchgen.executorch.api.types import ArrayRefCType, scalarT, tensorListT, tensorT
from torchgen.model import Argument, FunctionSchema, Return


class ExecutorchCppTest(unittest.TestCase):
"""
Test torchgen.executorch.api.cpp
"""

def _test_argumenttype_type(self, arg_str: str, expected: NamedCType) -> None:
arg = Argument.parse(arg_str)
self.assertEqual(str(argument_type(arg, binds=arg.name)), str(expected))

@local.parametrize(
use_const_ref_for_mutable_tensors=False, use_ilistref_for_tensor_lists=False
)
def test_argumenttype_type(self) -> None:
data = [
("Tensor self", NamedCType("self", ConstRefCType(BaseCType(tensorT)))),
("Tensor(a!) out", NamedCType("out", MutRefCType(BaseCType(tensorT)))),
(
"Tensor? opt",
NamedCType("opt", ConstRefCType(OptionalCType(BaseCType(tensorT)))),
),
("Scalar scalar", NamedCType("scalar", ConstRefCType(BaseCType(scalarT)))),
(
"Scalar? scalar",
NamedCType("scalar", ConstRefCType(OptionalCType(BaseCType(scalarT)))),
),
("int[] size", NamedCType("size", ArrayRefCType(BaseCType(longT)))),
("int? dim", NamedCType("dim", OptionalCType(BaseCType(longT)))),
("Tensor[] weight", NamedCType("weight", BaseCType(tensorListT))),
(
"Scalar[] spacing",
NamedCType("spacing", ArrayRefCType(ConstRefCType(BaseCType(scalarT)))),
),
(
"Tensor?[] weight",
NamedCType("weight", ArrayRefCType(OptionalCType(BaseCType(tensorT)))),
),
(
"SymInt[]? output_size",
NamedCType(
"output_size", OptionalCType(ArrayRefCType(BaseCType(longT)))
),
),
(
"int[]? dims",
NamedCType("dims", OptionalCType(ArrayRefCType(BaseCType(longT)))),
),
]
for d in data:
self._test_argumenttype_type(*d)

def _test_returntype_type(self, ret_str: str, expected: CType) -> None:
ret = Return.parse(ret_str)
self.assertEqual(str(return_type(ret)), str(expected))

@local.parametrize(
use_const_ref_for_mutable_tensors=False, use_ilistref_for_tensor_lists=False
)
def test_returntype_type(self) -> None:
data = [
("Tensor", BaseCType(tensorT)),
("Tensor(a!)", MutRefCType(BaseCType(tensorT))),
("Tensor[]", VectorCType(BaseCType(tensorT))),
]
for d in data:
self._test_returntype_type(*d)

@local.parametrize(
use_const_ref_for_mutable_tensors=False, use_ilistref_for_tensor_lists=False
)
def test_returns_type(self) -> None:
func = FunctionSchema.parse(
"min.dim(Tensor self, int dim, bool keepdim=False) -> (Tensor values, Tensor indices)"
)
expected = TupleCType([BaseCType(tensorT), BaseCType(tensorT)])
self.assertEqual(str(returns_type(func.returns)), str(expected))

@local.parametrize(
use_const_ref_for_mutable_tensors=False, use_ilistref_for_tensor_lists=False
)
def test_void_return_type(self) -> None:
func = FunctionSchema.parse(
"_foreach_add_.Scalar(Tensor(a!)[] self, Scalar scalar) -> ()"
)
expected = BaseCType(voidT)
self.assertEqual(str(returns_type(func.returns)), str(expected))


if __name__ == "__main__":
unittest.main()
Empty file added torchgen/executorch/__init__.py
Empty file.
Empty file.
Loading

0 comments on commit ddf00c8

Please sign in to comment.