2
from typing import List, Sequence, Union
4
from torchgen.api import cpp
6
from torchgen.api.types import ArgName, Binding, CType, NamedCType
7
from torchgen.model import (
12
TensorOptionsArguments,
15
from torchgen.utils import assert_never, concatMap
17
# This file describes the translation of JIT schema to the dispatcher
18
# API, the *unboxed* calling convention by which invocations through
19
# the dispatcher are made. Historically, the dispatcher API matched
20
# the C++ API, but with the establishment of the boxed API, we've
21
# made changes to the dispatcher API to so that the unboxed API
22
# better aligns with the boxed API. The dispatcher API hooks heavily
23
# into our template based boxing/unboxing machinery, so changes
24
# to this convention will usually need template updates too.
26
# Prominent characteristics of the dispatcher API:
28
# - dtype, layout, device and pin_memory are represented as separate
33
def name(func: FunctionSchema) -> str:
42
remove_non_owning_ref_types: bool = False,
45
# This is a faux amis. If it makes sense in the future to add
46
# more special cases here, or invert things so cpp.argument_type
47
# calls this, or just completely inline the function, please do
49
return cpp.argumenttype_type(
54
remove_non_owning_ref_types=remove_non_owning_ref_types,
62
remove_non_owning_ref_types: bool = False,
65
return argumenttype_type(
69
remove_non_owning_ref_types=remove_non_owning_ref_types,
74
def returns_type(rs: Sequence[Return], *, symint: bool = True) -> CType:
75
# At present, there is no difference. But there could be!
76
return cpp.returns_type(rs, symint=symint)
79
def jit_arguments(func: FunctionSchema) -> List[Argument]:
81
a: Union[Argument, TensorOptionsArguments, SelfArgument]
83
if isinstance(a, Argument):
85
elif isinstance(a, SelfArgument):
87
elif isinstance(a, TensorOptionsArguments):
88
return [a.dtype, a.layout, a.device, a.pin_memory]
96
func.arguments.positional, func.arguments.kwarg_only, func.arguments.out
103
a: Argument, *, remove_non_owning_ref_types: bool = False, symint: bool = True
106
nctype=argument_type(
109
remove_non_owning_ref_types=remove_non_owning_ref_types,
117
def arguments(func: FunctionSchema, *, symint: bool = True) -> List[Binding]:
118
return [argument(a, symint=symint) for a in jit_arguments(func)]