|
| 1 | +/* Copyright (c) 2016 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 | +#ifdef PADDLE_WITH_XPU |
| 16 | +#include "paddle/fluid/operators/clip_by_norm_op.h" |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | + |
| 22 | +template <typename DeviceContext, typename T> |
| 23 | +class XPUClipByNormKernel : public framework::OpKernel<T> { |
| 24 | + public: |
| 25 | + void Compute(const framework::ExecutionContext& context) const override { |
| 26 | + auto max_norm = context.Attr<T>("max_norm"); |
| 27 | + auto in_var = context.InputVar("X"); |
| 28 | + |
| 29 | + Tensor* output = nullptr; |
| 30 | + const Tensor* input = nullptr; |
| 31 | + if (in_var->IsType<framework::LoDTensor>()) { |
| 32 | + input = context.Input<Tensor>("X"); |
| 33 | + |
| 34 | + output = context.Output<Tensor>("Out"); |
| 35 | + output->mutable_data<T>(context.GetPlace()); |
| 36 | + } else { |
| 37 | + PADDLE_THROW(platform::errors::InvalidArgument( |
| 38 | + "Invalid input variable type, only support LodTensor" |
| 39 | + "type, but got type is %s.", |
| 40 | + framework::ToTypeName(in_var->Type()))); |
| 41 | + } |
| 42 | + |
| 43 | + PADDLE_ENFORCE_NOT_NULL(input, |
| 44 | + platform::errors::InvalidArgument( |
| 45 | + "Input(X) of ClipByNormOp should not be null. " |
| 46 | + "Please check if it is created correctly.")); |
| 47 | + auto& dev_ctx = context.template device_context<DeviceContext>(); |
| 48 | + const auto& x_dims = input->dims(); |
| 49 | + std::vector<int> xshape(x_dims.size()); |
| 50 | + std::vector<int> rdims(x_dims.size()); |
| 51 | + for (int i = 0; i < x_dims.size(); i++) { |
| 52 | + xshape[i] = x_dims[i]; |
| 53 | + rdims[i] = i; |
| 54 | + } |
| 55 | + int r = xpu::clip_by_norm<T>(dev_ctx.x_context(), input->data<T>(), |
| 56 | + output->data<T>(), max_norm, xshape, rdims); |
| 57 | + PADDLE_ENFORCE_EQ( |
| 58 | + r, XPU_SUCCESS, |
| 59 | + platform::errors::External("XPU API(clip_by_norm) return " |
| 60 | + "wrong value[%d], please check whether " |
| 61 | + "Baidu Kunlun Card is properly installed.", |
| 62 | + r)); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +} // namespace operators |
| 67 | +} // namespace paddle |
| 68 | + |
| 69 | +namespace ops = paddle::operators; |
| 70 | +REGISTER_OP_XPU_KERNEL( |
| 71 | + clip_by_norm, |
| 72 | + ops::XPUClipByNormKernel<paddle::platform::XPUDeviceContext, float>); |
| 73 | + |
| 74 | +#endif // PADDLE_WITH_XPU |
0 commit comments