|
| 1 | +// Copyright (c) 2019 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/temporal_shift_compute.h" |
| 16 | +#include <string> |
| 17 | +#include "lite/backends/host/math/temporal_shift.h" |
| 18 | +#include "lite/core/op_registry.h" |
| 19 | +#include "lite/core/tensor.h" |
| 20 | +#include "lite/core/type_system.h" |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | +namespace lite { |
| 24 | +namespace kernels { |
| 25 | +namespace host { |
| 26 | + |
| 27 | +template <> |
| 28 | +void TemporalShiftCompute<PRECISION(kFloat), PRECISION(kFloat)>::Run() { |
| 29 | + auto& param = Param<operators::TemporalShiftParam>(); |
| 30 | + const lite::Tensor* input = param.X; |
| 31 | + lite::Tensor* output = param.Out; |
| 32 | + int t = param.seg_num; |
| 33 | + float shift_ratio = param.shift_ratio; |
| 34 | + DataLayoutType data_layout; |
| 35 | + if (param.data_format == "NCHW") { |
| 36 | + data_layout = DATALAYOUT(kNCHW); |
| 37 | + } else if (param.data_format == "NHWC") { |
| 38 | + data_layout = DATALAYOUT(kNHWC); |
| 39 | + } else { |
| 40 | + LOG(FATAL) << "Unknown datalayout"; |
| 41 | + } |
| 42 | + |
| 43 | + auto input_dims = input->dims(); |
| 44 | + const int nt = input_dims[0]; |
| 45 | + const int c = |
| 46 | + data_layout == DATALAYOUT(kNCHW) ? input_dims[1] : input_dims[3]; |
| 47 | + const int h = |
| 48 | + data_layout == DATALAYOUT(kNCHW) ? input_dims[2] : input_dims[1]; |
| 49 | + const int w = |
| 50 | + data_layout == DATALAYOUT(kNCHW) ? input_dims[3] : input_dims[2]; |
| 51 | + |
| 52 | + const int hw = h * w; |
| 53 | + const int chw = c * hw; |
| 54 | + const int tchw = t * chw; |
| 55 | + const int ntchw = nt * chw; |
| 56 | + |
| 57 | + const int c1 = static_cast<int>(c * shift_ratio); |
| 58 | + const int c2 = static_cast<int>(c * 2 * shift_ratio); |
| 59 | + |
| 60 | + DDim out_dims; |
| 61 | + if (data_layout == DATALAYOUT(kNCHW)) { |
| 62 | + out_dims.ConstructFrom({nt, c, h, w}); |
| 63 | + } else { |
| 64 | + out_dims.ConstructFrom({nt, h, w, c}); |
| 65 | + } |
| 66 | + |
| 67 | + const float* input_data = input->data<float>(); |
| 68 | + output->Resize(out_dims); |
| 69 | + float* output_data = output->mutable_data<float>(); |
| 70 | + |
| 71 | + if (data_layout == DATALAYOUT(kNCHW)) { |
| 72 | + lite::host::math::temporalshiftNCHW_func( |
| 73 | + input_data, output_data, ntchw, tchw, chw, hw, t, c1, c2); |
| 74 | + } else { |
| 75 | + lite::host::math::temporalshiftNHWC_func( |
| 76 | + input_data, output_data, ntchw, tchw, chw, t, c, c1, c2); |
| 77 | + } |
| 78 | + return; |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace host |
| 82 | +} // namespace kernels |
| 83 | +} // namespace lite |
| 84 | +} // namespace paddle |
| 85 | +typedef paddle::lite::kernels::host::TemporalShiftCompute<PRECISION(kFloat), |
| 86 | + PRECISION(kFloat)> |
| 87 | + TSfp32; |
| 88 | + |
| 89 | +REGISTER_LITE_KERNEL(temporal_shift, kHost, kFloat, kNCHW, TSfp32, fp32) |
| 90 | + .BindInput("X", {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kFloat))}) |
| 91 | + .BindOutput("Out", |
| 92 | + {LiteType::GetTensorTy(TARGET(kHost), PRECISION(kFloat))}) |
| 93 | + .Finalize(); |
0 commit comments