|
| 1 | +// Copyright (c) 2022 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 | +#include "habanalabs/perf_lib_layer_params.h" |
| 16 | +#include "habanalabs/synapse_api.h" |
| 17 | +#include "habanalabs/synapse_common_types.h" |
| 18 | +#include "kernels/funcs.h" |
| 19 | +#include "kernels/hpu_operator.h" |
| 20 | +#include "utils/utils.h" |
| 21 | + |
| 22 | +namespace custom_kernel { |
| 23 | +class SetTensorValue : public HpuOperator { |
| 24 | + public: |
| 25 | + SetTensorValue(std::string guid_prefix, std::string node_name) |
| 26 | + : HpuOperator(guid_prefix), pName_(node_name) {} |
| 27 | + void AddNode(const std::vector<DIMS>& ins, |
| 28 | + const std::vector<DIMS>& outs, |
| 29 | + synDataType datatype, |
| 30 | + synSliceParams params) { |
| 31 | + assert(ins.size() == 2 && "input size should be 2"); |
| 32 | + assert(outs.size() == 1 && "output size should be 1"); |
| 33 | + |
| 34 | + synSectionHandle section = createSection(); |
| 35 | + synTensor inputs[ins.size()] = { |
| 36 | + createTensor(ins[0].size(), datatype, ins[0], true, "input", section), |
| 37 | + createTensor(ins[1].size(), datatype, ins[1], true, "value")}; |
| 38 | + synTensor outputs[outs.size()] = {createTensor( |
| 39 | + outs[0].size(), datatype, outs[0], true, "output", section)}; |
| 40 | + |
| 41 | + synStatus status = synNodeCreate(graphHandle_, |
| 42 | + inputs, |
| 43 | + outputs, |
| 44 | + ins.size(), |
| 45 | + outs.size(), |
| 46 | + ¶ms, |
| 47 | + sizeof(params), |
| 48 | + guid_.c_str(), |
| 49 | + pName_.c_str(), |
| 50 | + nullptr, |
| 51 | + nullptr); |
| 52 | + PD_CHECK( |
| 53 | + status == synSuccess, "[RUNTIME] synNodeCreate () failed = ", status); |
| 54 | + } |
| 55 | + std::string pName_; |
| 56 | +}; |
| 57 | + |
| 58 | +template <typename T, typename Context> |
| 59 | +void SetTensorValueKernel(const Context& dev_ctx, |
| 60 | + const phi::DenseTensor& x, |
| 61 | + const phi::DenseTensor& value, |
| 62 | + const phi::IntArray& starts, |
| 63 | + const phi::IntArray& ends, |
| 64 | + const phi::IntArray& steps, |
| 65 | + const std::vector<int64_t>& axes, |
| 66 | + const std::vector<int64_t>& decrease_axes, |
| 67 | + const std::vector<int64_t>& none_axes, |
| 68 | + phi::DenseTensor* out) { |
| 69 | + auto starts_v = starts.GetData(); |
| 70 | + auto ends_v = ends.GetData(); |
| 71 | + |
| 72 | + PADDLE_ENFORCE_EQ( |
| 73 | + starts_v.size(), |
| 74 | + axes.size(), |
| 75 | + phi::errors::InvalidArgument( |
| 76 | + "The size of starts must be equal to the size of axes.")); |
| 77 | + PADDLE_ENFORCE_EQ(ends_v.size(), |
| 78 | + axes.size(), |
| 79 | + phi::errors::InvalidArgument( |
| 80 | + "The size of ends must be equal to the size of axes.")); |
| 81 | + |
| 82 | + // allocate memory on device. |
| 83 | + dev_ctx.template Alloc<T>(out); |
| 84 | + const auto& in_dims = x.dims(); |
| 85 | + |
| 86 | + PADDLE_ENFORCE_EQ(x.data<T>(), |
| 87 | + out->data<T>(), |
| 88 | + phi::errors::InvalidArgument( |
| 89 | + "The input ptr must be equal to output ptr.")); |
| 90 | + // ToDo: handle decrease_axes and none_axes in future |
| 91 | + |
| 92 | + synSliceParams params = {{0}}; |
| 93 | + for (int i = 0; i < in_dims.size(); i++) { |
| 94 | + params.axes[i] = i; |
| 95 | + params.steps[i] = 1; |
| 96 | + params.starts[i] = 0; |
| 97 | + params.ends[i] = in_dims[in_dims.size() - 1 - i]; |
| 98 | + } |
| 99 | + for (int i = 0; i < static_cast<int>(axes.size()); i++) { |
| 100 | + params.starts[in_dims.size() - 1 - axes[i]] = starts[i]; |
| 101 | + params.ends[in_dims.size() - 1 - axes[i]] = ends[i]; |
| 102 | + } |
| 103 | + |
| 104 | + std::vector<int64_t> input_dim = phi::vectorize<int64_t>(x.dims()); |
| 105 | + std::vector<int64_t> value_dim = phi::vectorize<int64_t>(value.dims()); |
| 106 | + std::vector<int64_t> outputs_dim = phi::vectorize<int64_t>(out->dims()); |
| 107 | + |
| 108 | + OpCacheOperator op_info; |
| 109 | + op_info.prepareOpInfo<T, synSliceParams>( |
| 110 | + "slice_insert", {input_dim, value_dim}, ¶ms); |
| 111 | + |
| 112 | + auto recipe = op_info.GetRecipe(); |
| 113 | + if (recipe == nullptr) { |
| 114 | + // compile |
| 115 | + SetTensorValue op("slice_insert", "SliceInsert"); |
| 116 | + op.AddNode( |
| 117 | + {input_dim, value_dim}, {outputs_dim}, op_info.datatype_, params); |
| 118 | + op.Compile(); |
| 119 | + op_info.setOp(op); |
| 120 | + recipe = op_info.GetRecipe(); |
| 121 | + } |
| 122 | + |
| 123 | + // runtime |
| 124 | + std::map<std::string, uint64_t> tensors; |
| 125 | + tensors["input"] = reinterpret_cast<uint64_t>(x.data<T>()); |
| 126 | + tensors["value"] = reinterpret_cast<uint64_t>(value.data<T>()); |
| 127 | + tensors["output"] = reinterpret_cast<uint64_t>(out->data<T>()); |
| 128 | + |
| 129 | + RecipeRunner runner(recipe); |
| 130 | + runner.Run(reinterpret_cast<C_Stream>(dev_ctx.stream()), tensors); |
| 131 | +} |
| 132 | + |
| 133 | +// template <typename T, typename Context> |
| 134 | +// void SetValueKernel(const Context& dev_ctx, |
| 135 | +// const phi::DenseTensor& x, |
| 136 | +// const phi::IntArray& starts, |
| 137 | +// const phi::IntArray& ends, |
| 138 | +// const phi::IntArray& steps, |
| 139 | +// const std::vector<int64_t>& axes, |
| 140 | +// const std::vector<int64_t>& decrease_axes, |
| 141 | +// const std::vector<int64_t>& none_axes, |
| 142 | +// const std::vector<int64_t>& shape, |
| 143 | +// const std::vector<phi::Scalar>& values, |
| 144 | +// phi::DenseTensor* out) { |
| 145 | +// std::vector<T> assgin_values; |
| 146 | +// assgin_values.reserve(values.size()); |
| 147 | +// for (const auto& val : values) { |
| 148 | +// assgin_values.push_back(val.to<T>()); |
| 149 | +// } |
| 150 | +// phi::DenseTensor value_tensor; |
| 151 | +// value_tensor.Resize(phi::make_ddim(shape)); |
| 152 | +// custom_kernel::TensorFromVector( |
| 153 | +// dev_ctx, assgin_values, dev_ctx, &value_tensor); |
| 154 | +// value_tensor.Resize(phi::make_ddim(shape)); |
| 155 | + |
| 156 | +// custom_kernel::SetTensorValueKernel<T, Context>(dev_ctx, |
| 157 | +// x, |
| 158 | +// value_tensor, |
| 159 | +// starts, |
| 160 | +// ends, |
| 161 | +// steps, |
| 162 | +// axes, |
| 163 | +// decrease_axes, |
| 164 | +// none_axes, |
| 165 | +// out); |
| 166 | +// } |
| 167 | + |
| 168 | +// |
| 169 | + |
| 170 | +} // namespace custom_kernel |
| 171 | + |
| 172 | +// PD_REGISTER_PLUGIN_KERNEL(set_value, |
| 173 | +// intel_hpu, |
| 174 | +// ALL_LAYOUT, |
| 175 | +// custom_kernel::SetValueKernel, |
| 176 | +// float, |
| 177 | +// phi::dtype::float16, |
| 178 | +// phi::dtype::bfloat16) { |
| 179 | +// } |
| 180 | + |
| 181 | +PD_REGISTER_PLUGIN_KERNEL(set_value_with_tensor, |
| 182 | + intel_hpu, |
| 183 | + ALL_LAYOUT, |
| 184 | + custom_kernel::SetTensorValueKernel, |
| 185 | + float, |
| 186 | + phi::dtype::float16, |
| 187 | + phi::dtype::bfloat16) {} |
0 commit comments