|
| 1 | +// Copyright (c) 2020 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 "lite/kernels/host/unstack_compute.h" |
| 16 | +#include <cstring> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace lite { |
| 21 | +namespace kernels { |
| 22 | +namespace host { |
| 23 | + |
| 24 | +template <typename T, PrecisionType PType> |
| 25 | +void UnstackCompute<T, PType>::Run() { |
| 26 | + auto& param = this->template Param<operators::UnstackParam>(); |
| 27 | + auto x = param.X; |
| 28 | + auto outs = param.Out; |
| 29 | + auto x_dims = x->dims(); |
| 30 | + int axis = param.axis; |
| 31 | + if (axis < 0) { |
| 32 | + axis += x_dims.size(); |
| 33 | + } |
| 34 | + |
| 35 | + size_t stride_copy = 1; |
| 36 | + for (size_t i = axis + 1; i < x_dims.size(); i++) { |
| 37 | + stride_copy *= static_cast<size_t>(x_dims[i]); |
| 38 | + } |
| 39 | + size_t stride_move = stride_copy * static_cast<size_t>(x_dims[axis]); |
| 40 | + size_t copy_times = static_cast<size_t>(x_dims.production()) / stride_move; |
| 41 | + |
| 42 | + const T* x_data = x->template data<T>(); |
| 43 | + for (size_t i = 0; i < outs.size(); i++) { |
| 44 | + const T* x_ptr = x_data + i * stride_copy; |
| 45 | + T* out_ptr = outs[i]->template mutable_data<T>(); |
| 46 | + for (size_t j = 0; j < copy_times; j++) { |
| 47 | + std::memcpy(out_ptr, x_ptr, sizeof(T) * stride_copy); |
| 48 | + x_ptr += stride_move; |
| 49 | + out_ptr += stride_copy; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace host |
| 55 | +} // namespace kernels |
| 56 | +} // namespace lite |
| 57 | +} // namespace paddle |
| 58 | + |
| 59 | +using unstack_float = |
| 60 | + paddle::lite::kernels::host::UnstackCompute<float, PRECISION(kFloat)>; |
| 61 | +REGISTER_LITE_KERNEL(unstack, kHost, kFloat, kAny, unstack_float, def) |
| 62 | + .BindInput("X", |
| 63 | + {LiteType::GetTensorTy( |
| 64 | + TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kAny), -1)}) |
| 65 | + .BindOutput("Y", |
| 66 | + {LiteType::GetTensorTy( |
| 67 | + TARGET(kHost), PRECISION(kFloat), DATALAYOUT(kAny), -1)}) |
| 68 | + .Finalize(); |
0 commit comments