gather_compute.cc 1.8 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 23 24 25 26 27 28 29
// 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 {

void GatherCompute::Run() {
  auto& param = this->Param<operators::GatherParam>();

  auto* p_output = param.Out->mutable_data<float>();
  auto index_size = param.Index->dims()[0];
  auto src_dims = param.X->dims();
  const float* p_src = param.X->data<float>();
J
juncaipeng 已提交
30
  const int* p_index = param.Index->data<int>();
T
TianXiaogang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  int slice_size = 1;
  for (int i = 1; i < src_dims.size(); ++i) {
    slice_size *= src_dims[i];
  }
  for (int i = 0; i < index_size; ++i) {
    int index_ = p_index[i];
    memcpy(p_output + i * slice_size,
           p_src + index_ * slice_size,
           slice_size * sizeof(float));
  }
}

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

REGISTER_LITE_KERNEL(
50
    gather, kARM, kAny, kNCHW, paddle::lite::kernels::arm::GatherCompute, def)
J
juncaipeng 已提交
51 52 53
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Index",
               {LiteType::GetTensorTy(TARGET(kARM), PRECISION(kInt32))})
T
TianXiaogang 已提交
54 55
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
    .Finalize();