|
| 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 "paddle/phi/kernels/clip_kernel.h" |
| 16 | +#include "paddle/phi/backends/xpu/xpu_context.h" |
| 17 | +#include "paddle/phi/backends/xpu/xpu_header.h" |
| 18 | +#include "paddle/phi/core/kernel_registry.h" |
| 19 | + |
| 20 | +namespace phi { |
| 21 | + |
| 22 | +template <typename T, typename Context> |
| 23 | +void ClipKernel(const Context& dev_ctx, |
| 24 | + const DenseTensor& x, |
| 25 | + const Scalar& min, |
| 26 | + const Scalar& max, |
| 27 | + DenseTensor* out) { |
| 28 | + dev_ctx.template Alloc<T>(out); |
| 29 | + using XPUDataType = typename XPUTypeTrait<T>::Type; |
| 30 | + auto x_data = reinterpret_cast<const XPUDataType*>(x.data<T>()); |
| 31 | + auto out_data = reinterpret_cast<XPUDataType*>(out->data<T>()); |
| 32 | + int r = xpu::clip_v2(dev_ctx.x_context(), |
| 33 | + x_data, |
| 34 | + out_data, |
| 35 | + x.numel(), |
| 36 | + min.to<float>(), |
| 37 | + max.to<float>()); |
| 38 | + |
| 39 | + PADDLE_ENFORCE_EQ(r, |
| 40 | + XPU_SUCCESS, |
| 41 | + phi::errors::External("XPU API(clip_v2) return wrong " |
| 42 | + "value[%d %s]", |
| 43 | + r, |
| 44 | + XPUAPIErrorMsg[r])); |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace phi |
| 48 | + |
| 49 | +PD_REGISTER_KERNEL(clip, XPU, ALL_LAYOUT, phi::ClipKernel, float) {} |
0 commit comments