BatchingMetaprogramming.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. // All rights reserved.
  3. //
  4. // This source code is licensed under the BSD-style license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. #pragma once
  7. #include <ATen/Tensor.h>
  8. #include <ATen/VmapGeneratedPlumbing.h>
  9. // This file contains template metaprogramming things that are used for our
  10. // batching rules.
  11. //
  12. // See NOTE: [vmap plumbing] for more details on why this is necessary.
  13. // The plumbing has a bunch of metaprogramming hacks for determining the signature
  14. // of a batching rule from the signature of the operator, many of which use the
  15. // helper functions in this file.
  16. namespace at::functorch {
  17. // Metaprogramming things
  18. template <class... Items> using typelist = c10::guts::typelist::typelist<Items...>;
  19. template <class TypeList> using head_t = c10::guts::typelist::head_t<TypeList>;
  20. template <class TL1, class TL2> using concat_t = c10::guts::typelist::concat_t<TL1, TL2>;
  21. template <typename T> class debug_t;
  22. // tail operation
  23. template<class TypeList>
  24. struct tail final {
  25. static_assert(c10::guts::false_t<TypeList>::value,
  26. "In typelist::tail<T>, the T argument must be typelist<...>.");
  27. };
  28. template<class Head, class... Tail>
  29. struct tail<typelist<Head, Tail...>> final {
  30. using type = typelist<Tail...>;
  31. };
  32. template<class TypeList> using tail_t = typename tail<TypeList>::type;
  33. template <class First, class Second, class Next, class Tail>
  34. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext {
  35. using type = Next;
  36. };
  37. template <class Next, class Tail>
  38. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<Tensor, optional<int64_t>, Next, Tail> {
  39. using type = Tail;
  40. };
  41. template <class Next, class Tail>
  42. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<const Tensor&, optional<int64_t>, Next, Tail> {
  43. using type = Tail;
  44. };
  45. template <class Next, class Tail>
  46. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<Tensor&, optional<int64_t>, Next, Tail> {
  47. using type = Tail;
  48. };
  49. template <class Next, class Tail>
  50. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<optional<Tensor>, optional<int64_t>, Next, Tail> {
  51. using type = Tail;
  52. };
  53. template <class Next, class Tail>
  54. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<const optional<Tensor>&, optional<int64_t>, Next, Tail> {
  55. using type = Tail;
  56. };
  57. template <class Next, class Tail>
  58. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<optional<Tensor>&, optional<int64_t>, Next, Tail> {
  59. using type = Tail;
  60. };
  61. template <class Next, class Tail>
  62. struct IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<std::vector<Tensor>, optional<int64_t>, Next, Tail> {
  63. using type = Tail;
  64. };
  65. template <class TypeList> struct RemoveBatchDimAfterTensor {
  66. using first = head_t<TypeList>;
  67. using next = tail_t<TypeList>;
  68. using second = head_t<next>;
  69. using tail = tail_t<next>;
  70. using type = concat_t<
  71. typelist<first>,
  72. typename RemoveBatchDimAfterTensor<
  73. typename IfFirstIsTensorAndSecondisBatchDimThenTailElseNext<first, second, next, tail>::type
  74. >::type
  75. >;
  76. };
  77. template <class Type> struct RemoveBatchDimAfterTensor<typelist<Type>> {
  78. using type = typelist<Type>;
  79. };
  80. template <> struct RemoveBatchDimAfterTensor<typelist<>> {
  81. using type = typelist<>;
  82. };
  83. template<class TypeList> using remove_batch_dim_after_tensor_t = typename RemoveBatchDimAfterTensor<TypeList>::type;
  84. template <typename T> struct UnpackSingleItemTuple {
  85. using type = T;
  86. };
  87. template <typename T> struct UnpackSingleItemTuple<std::tuple<T>> {
  88. using type = T;
  89. };
  90. template <typename T> using unpack_single_item_tuple_t = typename UnpackSingleItemTuple<T>::type;
  91. template <typename Return, typename TupleArgs> struct BuildFunctionHelper;
  92. template <typename Return, typename... Args> struct BuildFunctionHelper<Return, std::tuple<Args...>> {
  93. using type = Return(Args...);
  94. };
  95. template <typename Return, typename TL>
  96. struct BuildFunction {
  97. using type = typename BuildFunctionHelper<Return, c10::guts::typelist::to_tuple_t<TL>>::type;
  98. };
  99. template <typename Return, typename TL> using build_function_t = typename BuildFunction<Return, TL>::type;
  100. template <typename batch_rule_t> struct ToOperatorType {
  101. using batch_rule_return_type = typename c10::guts::function_traits<batch_rule_t>::return_type;
  102. using batch_rule_parameter_types = typename c10::guts::function_traits<batch_rule_t>::parameter_types;
  103. using operator_parameter_types = remove_batch_dim_after_tensor_t<batch_rule_parameter_types>;
  104. using operator_return_type =
  105. unpack_single_item_tuple_t<
  106. c10::guts::typelist::to_tuple_t<
  107. remove_batch_dim_after_tensor_t<
  108. c10::guts::typelist::from_tuple_t<batch_rule_return_type>>>>;
  109. using type = build_function_t<operator_return_type, operator_parameter_types>;
  110. };
  111. template <typename batch_rule_t> using to_operator_t = typename ToOperatorType<batch_rule_t>::type;
  112. } // namespace at::functorch