structured.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from torchgen.model import (
  2. Argument,
  3. BaseTy,
  4. BaseType,
  5. ListType,
  6. NativeFunctionsGroup,
  7. OptionalType,
  8. SelfArgument,
  9. TensorOptionsArguments,
  10. Type,
  11. )
  12. from torchgen.api.types import (
  13. ArgName,
  14. BaseCType,
  15. Binding,
  16. ArrayRefCType,
  17. ConstRefCType,
  18. OptionalCType,
  19. NamedCType,
  20. tensorT,
  21. scalarT,
  22. intArrayRefT,
  23. dimnameListT,
  24. optionalTensorRefT,
  25. optionalScalarRefT,
  26. optionalIntArrayRefT,
  27. iTensorListRefT,
  28. iOptTensorListRefT,
  29. )
  30. from torchgen.api import cpp
  31. from torchgen.utils import assert_never
  32. from typing import Union, List
  33. # This file describes the translation of JIT schema to the structured functions API.
  34. # This is similar to native API, but a number of historical problems with native
  35. # API have been fixed.
  36. # Translation of types occuring in JIT arguments to a C++ argument type.
  37. # NB: For now, mutable doesn't do anything; but it could if we make
  38. # some more nominal types
  39. def argumenttype_type(t: Type, *, mutable: bool, binds: ArgName) -> NamedCType:
  40. # If it's a value type, do the value type translation
  41. r = cpp.valuetype_type(t, binds=binds)
  42. if r is not None:
  43. return r
  44. if isinstance(t, BaseType):
  45. if t.name == BaseTy.Tensor:
  46. return NamedCType(binds, ConstRefCType(BaseCType(tensorT)))
  47. elif t.name == BaseTy.Scalar:
  48. return NamedCType(binds, ConstRefCType(BaseCType(scalarT)))
  49. else:
  50. raise AssertionError(f"base type should have been value type {t}")
  51. elif isinstance(t, OptionalType):
  52. if t.elem == BaseType(BaseTy.Tensor):
  53. return NamedCType(binds, BaseCType(optionalTensorRefT))
  54. elif t.elem == BaseType(BaseTy.Scalar):
  55. return NamedCType(binds, BaseCType(optionalScalarRefT))
  56. elif isinstance(t.elem, ListType) and str(t.elem.elem) == "int":
  57. return NamedCType(binds, BaseCType(optionalIntArrayRefT))
  58. elem = argumenttype_type(t.elem, mutable=mutable, binds=binds)
  59. return NamedCType(binds, OptionalCType(elem.type))
  60. elif isinstance(t, ListType):
  61. if t.elem == BaseType(BaseTy.Tensor):
  62. return NamedCType(binds, BaseCType(iTensorListRefT))
  63. elif t.elem == OptionalType(BaseType(BaseTy.Tensor)):
  64. return NamedCType(binds, BaseCType(iOptTensorListRefT))
  65. # TODO: delete these special cases; see torchgen.api.cpp--these
  66. # must be changed in tandem, but there are problems; see
  67. # https://github.com/pytorch/pytorch/pull/51485
  68. elif str(t.elem) == "int":
  69. return NamedCType(binds, BaseCType(intArrayRefT))
  70. elif str(t.elem) == "Dimname":
  71. return NamedCType(binds, BaseCType(dimnameListT))
  72. elem = argumenttype_type(t.elem, mutable=mutable, binds=binds)
  73. return NamedCType(binds, ArrayRefCType(elem.type))
  74. else:
  75. raise AssertionError(f"unrecognized type {repr(t)}")
  76. def argument_type(a: Argument, *, binds: ArgName) -> NamedCType:
  77. return argumenttype_type(a.type, mutable=a.is_write, binds=binds)
  78. # returns_type intentionally omitted, because structured kernels never "return";
  79. # instead, they always indirectly report their outputs (in the case of a meta
  80. # function, by calling set_output; in the case of an impl function, by writing
  81. # directly into the provided out argument).
  82. # Structured kernels are never defaulted
  83. def argument(a: Union[Argument, SelfArgument, TensorOptionsArguments]) -> List[Binding]:
  84. if isinstance(a, Argument):
  85. return [
  86. Binding(
  87. nctype=argument_type(a, binds=a.name),
  88. name=a.name,
  89. default=None,
  90. argument=a,
  91. )
  92. ]
  93. elif isinstance(a, SelfArgument):
  94. return argument(a.argument)
  95. elif isinstance(a, TensorOptionsArguments):
  96. raise AssertionError("structured kernels don't support TensorOptions yet")
  97. else:
  98. assert_never(a)
  99. def impl_arguments(g: NativeFunctionsGroup) -> List[Binding]:
  100. args: List[Union[Argument, TensorOptionsArguments, SelfArgument]] = []
  101. if g.out.precomputed:
  102. # A list of parameters for the impl function with
  103. # certain parameters replaced with precomputed counterparts
  104. # as specified in native_functions.yaml.
  105. non_out_args_replaced: List[
  106. Union[Argument, TensorOptionsArguments, SelfArgument]
  107. ] = []
  108. for a in g.out.func.arguments.non_out:
  109. if isinstance(a, Argument) and a.name in g.out.precomputed.replace:
  110. # If a is in precompute.replace, append the parameters
  111. # that should replace it onto non_out_args_replaced.
  112. for replacement in g.out.precomputed.replace[a.name]:
  113. non_out_args_replaced.append(replacement)
  114. else:
  115. # If not, push a as it is.
  116. non_out_args_replaced.append(a)
  117. args.extend(non_out_args_replaced)
  118. # g.out.precomputed.add is the list of parameters that are added
  119. # without replacement after the non out args and just before the out args
  120. args.extend(g.out.precomputed.add)
  121. else:
  122. args.extend(g.out.func.arguments.non_out)
  123. args.extend(g.out.func.arguments.out)
  124. return [r for arg in args for r in argument(arg)]
  125. def meta_arguments(g: NativeFunctionsGroup) -> List[Binding]:
  126. args: List[Union[Argument, TensorOptionsArguments, SelfArgument]] = []
  127. args.extend(g.functional.func.arguments.non_out)
  128. return [r for arg in args for r in argument(arg)]
  129. def out_arguments(g: NativeFunctionsGroup) -> List[Binding]:
  130. args: List[Union[Argument, TensorOptionsArguments, SelfArgument]] = []
  131. args.extend(g.out.func.arguments.out)
  132. return [r for arg in args for r in argument(arg)]