|
| 1 | +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" |
| 17 | +#include "paddle/phi/core/distributed/type_defs.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | + |
| 21 | +using CustomSpmdInferTensorArg = |
| 22 | + paddle::variant<phi::distributed::DistMetaTensor, |
| 23 | + std::vector<phi::distributed::DistMetaTensor>>; |
| 24 | + |
| 25 | +using CustomSpmdInferAttrArg = paddle::any; |
| 26 | +template <typename T> |
| 27 | +struct SpmdInferHelperTypeEnd {}; |
| 28 | + |
| 29 | +#define PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(attr_type) \ |
| 30 | + template <typename... Tail> \ |
| 31 | + struct SpmdInferHelper<attr_type, Tail...> { \ |
| 32 | + template <int in_idx, int attr_idx, typename... PreviousArgs> \ |
| 33 | + static phi::distributed::SpmdInfo InferSpmd( \ |
| 34 | + const std::vector<CustomSpmdInferTensorArg>& inputs, \ |
| 35 | + const std::vector<CustomSpmdInferAttrArg>& attrs, \ |
| 36 | + const PreviousArgs&... pargs) { \ |
| 37 | + try { \ |
| 38 | + attr_type arg = paddle::any_cast<attr_type>(attrs[attr_idx]); \ |
| 39 | + return SpmdInferHelper<Tail...>::template InferSpmd<in_idx, \ |
| 40 | + attr_idx + 1>( \ |
| 41 | + inputs, attrs, pargs..., arg); \ |
| 42 | + } catch (paddle::bad_any_cast&) { \ |
| 43 | + PD_THROW( \ |
| 44 | + "Attribute cast error in custom operator SpmdInferFunc " \ |
| 45 | + "function. " \ |
| 46 | + "Expected " #attr_type \ |
| 47 | + " value. SpmdInferFunc's attribute list must be exactly " \ |
| 48 | + "same " \ |
| 49 | + "as " \ |
| 50 | + "Forward " \ |
| 51 | + "KernelFn's attribute list except std::vector<int64_t> " \ |
| 52 | + "attribute."); \ |
| 53 | + } \ |
| 54 | + } \ |
| 55 | + } |
| 56 | + |
| 57 | +template <typename F, F f> |
| 58 | +struct SpmdInferImpl; |
| 59 | + |
| 60 | +template <typename... Args, phi::distributed::SpmdInfo (*impl_fn)(Args...)> |
| 61 | +struct SpmdInferImpl<phi::distributed::SpmdInfo (*)(Args...), impl_fn> { |
| 62 | + static phi::distributed::SpmdInfo InferSpmd( |
| 63 | + const std::vector<CustomSpmdInferTensorArg>& inputs, |
| 64 | + const std::vector<CustomSpmdInferAttrArg>& attrs) { |
| 65 | + return SpmdInferHelper<Args..., SpmdInferHelperTypeEnd<int>>:: |
| 66 | + template InferSpmd<0, 0>(inputs, attrs); |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + template <typename... RemainingArgs> |
| 71 | + struct SpmdInferHelper; |
| 72 | + |
| 73 | + // Handle args for general tensor input case |
| 74 | + template <typename... Tail> |
| 75 | + struct SpmdInferHelper<const phi::distributed::DistMetaTensor&, Tail...> { |
| 76 | + template <int in_idx, int attr_idx, typename... PreviousArgs> |
| 77 | + static phi::distributed::SpmdInfo InferSpmd( |
| 78 | + const std::vector<CustomSpmdInferTensorArg>& inputs, |
| 79 | + const std::vector<CustomSpmdInferAttrArg>& attrs, |
| 80 | + PreviousArgs&... pargs) { |
| 81 | + auto& arg = |
| 82 | + PADDLE_GET_CONST(phi::distributed::DistMetaTensor, inputs[in_idx]); |
| 83 | + return SpmdInferHelper<Tail...>::template InferSpmd<in_idx + 1, attr_idx>( |
| 84 | + inputs, attrs, pargs..., arg); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + // Handle args for vector of Tensor input case |
| 89 | + template <typename... Tail> |
| 90 | + struct SpmdInferHelper<const std::vector<phi::distributed::DistMetaTensor>&, |
| 91 | + Tail...> { |
| 92 | + template <int in_idx, int attr_idx, typename... PreviousArgs> |
| 93 | + static phi::distributed::SpmdInfo InferSpmd( |
| 94 | + const std::vector<CustomSpmdInferTensorArg>& inputs, |
| 95 | + const std::vector<CustomSpmdInferAttrArg>& attrs, |
| 96 | + PreviousArgs&... pargs) { |
| 97 | + auto& arg = PADDLE_GET_CONST( |
| 98 | + std::vector<phi::distributed::DistMetaTensor>, inputs[in_idx]); |
| 99 | + return SpmdInferHelper<Tail...>::template InferSpmd<in_idx + 1, attr_idx>( |
| 100 | + inputs, attrs, pargs..., arg); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(bool); |
| 105 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(int); |
| 106 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(float); |
| 107 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(int64_t); |
| 108 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::string&); |
| 109 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::vector<int>&); |
| 110 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::vector<float>&); |
| 111 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::vector<std::string>&); |
| 112 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::vector<int64_t>&); |
| 113 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const bool&); |
| 114 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const int&); |
| 115 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const float&); |
| 116 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const int64_t&); |
| 117 | + |
| 118 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(std::string); |
| 119 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(std::vector<int>); |
| 120 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(std::vector<float>); |
| 121 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(std::vector<std::string>); |
| 122 | + PD_SPECIALIZE_SpmdInferHelper_FOR_AttrType(const std::vector<int64_t>); |
| 123 | + |
| 124 | + // end: base template |
| 125 | + template <typename T> |
| 126 | + struct SpmdInferHelper<SpmdInferHelperTypeEnd<T>> { |
| 127 | + template <int in_idx, int attr_idx, typename... PreviousArgs> |
| 128 | + static phi::distributed::SpmdInfo InferSpmd( |
| 129 | + const std::vector<CustomSpmdInferTensorArg>& inputs, |
| 130 | + const std::vector<CustomSpmdInferAttrArg>& attrs, |
| 131 | + PreviousArgs&... pargs) { |
| 132 | + return impl_fn(pargs...); |
| 133 | + } |
| 134 | + }; |
| 135 | +}; |
| 136 | + |
| 137 | +#define PD_INFER_SPMD_RULE(...) \ |
| 138 | + ::paddle::SpmdInferImpl<decltype(&__VA_ARGS__), &__VA_ARGS__>::InferSpmd |
| 139 | + |
| 140 | +} // namespace paddle |
0 commit comments