gather_compute.cc 3.2 KB
Newer Older
T
TianXiaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "lite/kernels/arm/gather_compute.h"
#include <vector>
#include "lite/backends/arm/math/funcs.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

23
template <typename IndexType, typename DataType>
24
void GatherFunc(const operators::GatherParam& param) {
T
TianXiaogang 已提交
25
  auto src_dims = param.X->dims();
26
  auto index_size = param.Index->dims()[0];
27 28 29
  auto* p_src = param.X->data<DataType>();
  const IndexType* p_index = param.Index->data<IndexType>();
  auto* p_output = param.Out->mutable_data<DataType>();
T
TianXiaogang 已提交
30 31

  int slice_size = 1;
32
  for (size_t i = 1; i < src_dims.size(); ++i) {
T
TianXiaogang 已提交
33 34 35
    slice_size *= src_dims[i];
  }
  for (int i = 0; i < index_size; ++i) {
36
    IndexType index_ = p_index[i];
T
TianXiaogang 已提交
37 38
    memcpy(p_output + i * slice_size,
           p_src + index_ * slice_size,
39
           slice_size * sizeof(DataType));
40 41 42
  }
}

43 44 45
template <typename IndexType>
void GatherCompute<IndexType>::Run() {
  auto& param = this->template Param<operators::GatherParam>();
46 47 48

  switch (param.X->precision()) {
    case PRECISION(kFloat):
49
      GatherFunc<IndexType, float>(param);
50 51
      break;
    case PRECISION(kInt8):
52
      GatherFunc<IndexType, int8_t>(param);
53 54
      break;
    case PRECISION(kInt16):
55
      GatherFunc<IndexType, int16_t>(param);
56 57
      break;
    case PRECISION(kInt32):
58
      GatherFunc<IndexType, int32_t>(param);
59 60
      break;
    case PRECISION(kInt64):
61
      GatherFunc<IndexType, int64_t>(param);
62 63 64 65
      break;
    default:
      LOG(FATAL) << "Gather does not implement for the "
                 << "input type:" << static_cast<int>(param.X->precision());
T
TianXiaogang 已提交
66 67 68 69 70 71 72 73
  }
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

74 75 76 77 78 79
REGISTER_LITE_KERNEL(gather,
                     kARM,
                     kAny,
                     kNCHW,
                     paddle::lite::kernels::arm::GatherCompute<int32_t>,
                     def)
80
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    .BindInput("Index",
               {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
    .Finalize();

REGISTER_LITE_KERNEL(gather,
                     kARM,
                     kAny,
                     kNCHW,
                     paddle::lite::kernels::arm::GatherCompute<int64_t>,
                     def_int64_idx)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
    .BindInput("Index",
               {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt64))})
95
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kAny))})
T
TianXiaogang 已提交
96
    .Finalize();