|
| 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/range_op.h" |
| 17 | +#include "paddle/fluid/framework/op_registry.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | + |
| 22 | +template <typename T> |
| 23 | +class XPURangeKernel : public framework::OpKernel<T> { |
| 24 | + public: |
| 25 | + void Compute(const framework::ExecutionContext& context) const override { |
| 26 | + auto* start_t = context.Input<framework::Tensor>("Start"); |
| 27 | + auto* end_t = context.Input<framework::Tensor>("End"); |
| 28 | + auto* step_t = context.Input<framework::Tensor>("Step"); |
| 29 | + auto* out = context.Output<framework::Tensor>("Out"); |
| 30 | + |
| 31 | + framework::Tensor n; |
| 32 | + framework::TensorCopy(*start_t, platform::CPUPlace(), &n); |
| 33 | + T start = n.data<T>()[0]; |
| 34 | + framework::TensorCopy(*end_t, platform::CPUPlace(), &n); |
| 35 | + T end = n.data<T>()[0]; |
| 36 | + framework::TensorCopy(*step_t, platform::CPUPlace(), &n); |
| 37 | + T step = n.data<T>()[0]; |
| 38 | + |
| 39 | + int64_t size = 0; |
| 40 | + GetSize(start, end, step, &size); |
| 41 | + out->Resize(framework::make_ddim({size})); |
| 42 | + |
| 43 | + T* out_data = out->mutable_data<T>(context.GetPlace()); |
| 44 | + |
| 45 | + framework::Tensor out_cpu; |
| 46 | + T* out_cpu_data_ptr = |
| 47 | + out_cpu.mutable_data<T>(platform::CPUPlace(), out->numel() * sizeof(T)); |
| 48 | + T value = start; |
| 49 | + for (int64_t i = 0; i < size; ++i) { |
| 50 | + out_cpu_data_ptr[i] = value; |
| 51 | + value += step; |
| 52 | + } |
| 53 | + int ret = xpu_memcpy(out_data, out_cpu_data_ptr, out->numel() * sizeof(T), |
| 54 | + XPUMemcpyKind::XPU_HOST_TO_DEVICE); |
| 55 | + PADDLE_ENFORCE_EQ(ret, XPU_SUCCESS, |
| 56 | + platform::errors::External("XPU xpu_memcpy return wrong " |
| 57 | + "value[%d %s]", |
| 58 | + ret, XPUAPIErrorMsg[ret])); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +} // namespace operators |
| 63 | +} // namespace paddle |
| 64 | + |
| 65 | +namespace ops = paddle::operators; |
| 66 | +REGISTER_OP_XPU_KERNEL(range, ops::XPURangeKernel<int>, |
| 67 | + ops::XPURangeKernel<int64_t>, ops::XPURangeKernel<float>, |
| 68 | + ops::XPURangeKernel<double>); |
| 69 | +#endif // PADDLE_WITH_XPU |
0 commit comments