|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/fluid/operators/mean_op.h" |
| 13 | +#include "paddle/fluid/platform/float16.h" |
| 14 | +#include "paddle/fluid/operators/npu_op_runner.h" |
| 15 | + |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +template <typename DeviceContext, typename T> |
| 21 | +class MeanNPUKernel : public framework::OpKernel<T> { |
| 22 | + public: |
| 23 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 24 | + auto* x = ctx.Input<framework::LoDTensor>("X"); |
| 25 | + auto* out = ctx.Output<framework::LoDTensor>("Out"); |
| 26 | + |
| 27 | + auto reduce_ndim = x->dims().size(); |
| 28 | + std::vector<int> axes; |
| 29 | + for (auto i = 0; i < reduce_ndim; ++i) { |
| 30 | + axes.push_back(i); |
| 31 | + } |
| 32 | + |
| 33 | + framework::NPUAttributeMap attr_input = { |
| 34 | + {"keep_dims", false}, |
| 35 | + {"axes", axes}}; |
| 36 | + |
| 37 | + std::vector<int64_t> out_dims; |
| 38 | + out_dims.push_back(1); |
| 39 | + out->Resize(framework::make_ddim(out_dims)); |
| 40 | + out->mutable_data<T>(ctx.GetPlace()); |
| 41 | + |
| 42 | + Tensor reduced_out(x->type()); |
| 43 | + std::vector<int64_t> reduced_dout_dims; |
| 44 | + reduced_dout_dims.push_back(1); |
| 45 | + reduced_out.Resize(framework::make_ddim(reduced_dout_dims)); |
| 46 | + reduced_out.mutable_data<T>(ctx.GetPlace()); |
| 47 | + |
| 48 | + auto runner = NpuOpRunner("ReduceMeanD", |
| 49 | + {*x}, |
| 50 | + {*out}, |
| 51 | + attr_input); |
| 52 | + |
| 53 | + auto stream = |
| 54 | + ctx.template device_context< |
| 55 | + paddle::platform::NPUDeviceContext>() |
| 56 | + .stream(); |
| 57 | + runner.Run(stream); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | + |
| 62 | +template <typename DeviceContext, typename T> |
| 63 | +class MeanGradNPUKernel : public framework::OpKernel<T> { |
| 64 | + public: |
| 65 | + void Compute(const framework::ExecutionContext& context) const override { |
| 66 | + auto stream = |
| 67 | + context.template device_context< |
| 68 | + paddle::platform::NPUDeviceContext>() |
| 69 | + .stream(); |
| 70 | + |
| 71 | + auto grad = context.Input<Tensor>(framework::GradVarName("Out")); |
| 72 | + |
| 73 | + PADDLE_ENFORCE_EQ(grad->numel(), 1, |
| 74 | + platform::errors::InvalidArgument( |
| 75 | + "Mean Gradient Input Tensor len should be 1. But " |
| 76 | + "received Out@Grad's elements num is %d.", |
| 77 | + grad->numel())); |
| 78 | + |
| 79 | + auto IG = context.Output<Tensor>(framework::GradVarName("X")); |
| 80 | + IG->mutable_data<T>(context.GetPlace()); |
| 81 | + |
| 82 | + // ones |
| 83 | + Tensor ones(grad->type()); |
| 84 | + std::vector<int64_t> dout_dims; |
| 85 | + for (auto i = 0; i < IG->dims().size(); ++i) { |
| 86 | + dout_dims.push_back(IG->dims()[i]); |
| 87 | + } |
| 88 | + ones.Resize(framework::make_ddim(dout_dims)); |
| 89 | + ones.mutable_data<T>(context.GetPlace()); |
| 90 | + auto runner_ones = NpuOpRunner("OnesLike", {*IG}, {ones}, {}); |
| 91 | + runner_ones.Run(stream); |
| 92 | + |
| 93 | + // means |
| 94 | + Tensor mean_tensor(grad->type()); |
| 95 | + mean_tensor.Resize({1}); |
| 96 | + mean_tensor.mutable_data<T>(context.GetPlace()); |
| 97 | + std::vector<float> mean_vec; |
| 98 | + mean_vec.push_back(1.0/static_cast<float>(IG->numel())); |
| 99 | + framework::TensorFromVector(mean_vec, |
| 100 | + context.device_context(), |
| 101 | + &mean_tensor); |
| 102 | + |
| 103 | + // means mul ones |
| 104 | + Tensor mean_ma(grad->type()); |
| 105 | + mean_ma.Resize(framework::make_ddim(dout_dims)); |
| 106 | + mean_ma.mutable_data<T>(context.GetPlace()); |
| 107 | + auto runner_mul_1 = NpuOpRunner("Mul", {mean_tensor, ones}, {mean_ma}, {}); |
| 108 | + runner_mul_1.Run(stream); |
| 109 | + |
| 110 | + // and mul grad |
| 111 | + auto runner_mul_2 = NpuOpRunner("Mul", {mean_ma, *grad}, {*IG}, {}); |
| 112 | + runner_mul_2.Run(stream); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | + |
| 117 | +} // namespace operators |
| 118 | +} // namespace paddle |
| 119 | + |
| 120 | +namespace ops = paddle::operators; |
| 121 | +namespace plat = paddle::platform; |
| 122 | +REGISTER_OP_NPU_KERNEL( |
| 123 | + mean, |
| 124 | + ops::MeanNPUKernel<paddle::platform::NPUDeviceContext, int>, |
| 125 | + ops::MeanNPUKernel<paddle::platform::NPUDeviceContext, float>, |
| 126 | + ops::MeanNPUKernel<paddle::platform::NPUDeviceContext, double>, |
| 127 | + ops::MeanNPUKernel<paddle::platform::NPUDeviceContext, plat::float16>) |
| 128 | + |
| 129 | + |
| 130 | +REGISTER_OP_NPU_KERNEL( |
| 131 | + mean_grad, |
| 132 | + ops::MeanGradNPUKernel<paddle::platform::NPUDeviceContext, int>, |
| 133 | + ops::MeanGradNPUKernel<paddle::platform::NPUDeviceContext, float>, |
| 134 | + ops::MeanGradNPUKernel<paddle::platform::NPUDeviceContext, double>, |
| 135 | + ops::MeanGradNPUKernel<paddle::platform::NPUDeviceContext, plat::float16>) |
0 commit comments