|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// 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 | +#include "kernels/funcs.h" |
| 16 | +#include "kernels/hpu_funcs.h" |
| 17 | +#include "kernels/hpu_operator.h" |
| 18 | +#include "utils/utils.h" |
| 19 | + |
| 20 | +namespace custom_kernel { |
| 21 | + |
| 22 | +class Stack : public HpuFusedOperator { |
| 23 | + public: |
| 24 | + explicit Stack(synDataType dtype) |
| 25 | + : HpuFusedOperator("stack"), dtype_(dtype) {} |
| 26 | + |
| 27 | + void AddNode(ConvertTensors& ct, unsigned params) { |
| 28 | + auto inputs = ct.GetTensors(); |
| 29 | + auto outputs = ct.GetTensors(false); |
| 30 | + |
| 31 | + std::vector<synTensor> syn_inputs; |
| 32 | + for (size_t i = 0; i < inputs.size(); i++) { |
| 33 | + syn_inputs.push_back(createTensorFromCT(&ct, i)); |
| 34 | + } |
| 35 | + |
| 36 | + auto concat_dims = outputs[0].dims; |
| 37 | + |
| 38 | + // Merge concat_dims[params] and concat_dims[params+1] |
| 39 | + auto reduce_dim = concat_dims.size() - 2 - params; |
| 40 | + concat_dims[reduce_dim] *= concat_dims[reduce_dim + 1]; |
| 41 | + concat_dims.erase(concat_dims.begin() + reduce_dim + 1); |
| 42 | + |
| 43 | + std::vector<synTensor> outputs_concat; |
| 44 | + auto concated = createTensorNoPresist("concat", dtype_, concat_dims); |
| 45 | + outputs_concat.push_back(concated); |
| 46 | + |
| 47 | + synConcatenateParams concatParams; |
| 48 | + concatParams.axis = params; |
| 49 | + AddNodeConcat(syn_inputs, outputs_concat, concatParams, guid_ + "concat"); |
| 50 | + |
| 51 | + std::vector<synTensor> syn_outputs; |
| 52 | + auto stacked = createTensorFromCT(&ct, 0, false); |
| 53 | + syn_outputs.push_back(stacked); |
| 54 | + |
| 55 | + AddNodeReshape(outputs_concat, syn_outputs, guid_ + "reshape"); |
| 56 | + } |
| 57 | + |
| 58 | + protected: |
| 59 | + synDataType dtype_; |
| 60 | +}; |
| 61 | + |
| 62 | +template <typename T, typename Context> |
| 63 | +void StackKernel(const Context& dev_ctx, |
| 64 | + const std::vector<const phi::DenseTensor*>& x, |
| 65 | + int axis, |
| 66 | + phi::DenseTensor* y) { |
| 67 | + dev_ctx.template Alloc<T>(y); |
| 68 | + |
| 69 | + ConvertTensors ct; |
| 70 | + for (size_t i = 0; i < x.size(); i++) { |
| 71 | + ct.Add(x[i]); |
| 72 | + } |
| 73 | + ct.Add(y, false); |
| 74 | + |
| 75 | + axis = CanonicalAxis(static_cast<int64_t>(axis), |
| 76 | + static_cast<int64_t>(x[0]->dims().size())); |
| 77 | + axis = static_cast<int64_t>(x[0]->dims().size()) - 1 - axis; |
| 78 | + unsigned params = static_cast<unsigned>(axis); |
| 79 | + |
| 80 | + std::vector<DIMS> inputs_dims = ct.GetDims(); |
| 81 | + OpCacheOperator op_info; |
| 82 | + op_info.prepareOpInfo<T, unsigned>("StackKernel", inputs_dims, ¶ms); |
| 83 | + auto recipe = op_info.GetRecipe(); |
| 84 | + |
| 85 | + if (recipe == nullptr) { |
| 86 | + Stack op(op_info.datatype_); |
| 87 | + op.AddNode(ct, params); |
| 88 | + op.Compile(); |
| 89 | + op_info.setOp(op); |
| 90 | + recipe = op_info.GetRecipe(); |
| 91 | + } |
| 92 | + |
| 93 | + RecipeRunner runner(recipe); |
| 94 | + auto tensors = ct.GetDeviceAddr(); |
| 95 | + runner.Run(reinterpret_cast<C_Stream>(dev_ctx.stream()), tensors); |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace custom_kernel |
| 99 | + |
| 100 | +PD_REGISTER_PLUGIN_KERNEL(stack, |
| 101 | + intel_hpu, |
| 102 | + ALL_LAYOUT, |
| 103 | + custom_kernel::StackKernel, |
| 104 | + float, |
| 105 | + int64_t, |
| 106 | + phi::dtype::float16, |
| 107 | + phi::dtype::bfloat16, |
| 108 | + phi::dtype::float8_e4m3fn) {} |
0 commit comments