argsort_op.h 2.8 KB
Newer Older
Y
Yibing Liu 已提交
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
/* Copyright (c) 2016 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. */

#pragma once
#include <algorithm>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class ArgsortKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
Y
Yibing Liu 已提交
28 29 30
    auto* input = ctx.Input<framework::Tensor>("X");
    auto* output = ctx.Output<framework::Tensor>("Out");
    auto* indices = ctx.Output<framework::Tensor>("Indices");
Y
Yibing Liu 已提交
31
    int axis = ctx.Attr<int>("axis");
Y
Yibing Liu 已提交
32 33

    auto in_dims = input->dims();
34
    axis = (axis < 0) ? (in_dims.size() + axis) : axis;
Y
Yibing Liu 已提交
35 36 37

    const T* in_data = input->data<T>();
    T* out_data = output->mutable_data<T>(ctx.GetPlace());
Y
Yibing Liu 已提交
38
    int64_t* ids_data = indices->mutable_data<int64_t>(ctx.GetPlace());
Y
Yibing Liu 已提交
39

Y
Yibing Liu 已提交
40 41 42 43 44 45 46
    int64_t groups = input->numel() / in_dims[axis];
    int64_t stride = (axis == in_dims.size() - 1)
                         ? 1
                         : framework::product(framework::slice_ddim(
                               in_dims, axis + 1, in_dims.size()));

    for (int64_t i = 0; i < groups; ++i) {
Y
Yibing Liu 已提交
47
      int64_t idx = i;
Y
Yibing Liu 已提交
48
      std::vector<int64_t> shape_vec(in_dims.size(), 0);
Y
Yibing Liu 已提交
49 50
      for (int64_t dim = in_dims.size() - 1; dim >= 0; --dim) {
        if (dim != axis) {
Y
Yibing Liu 已提交
51
          shape_vec[dim] = idx % in_dims[dim];
Y
Yibing Liu 已提交
52 53 54
          idx /= in_dims[dim];
        }
      }
Y
Yibing Liu 已提交
55 56 57 58 59 60 61 62 63

      int64_t start_index = shape_vec[0];
      for (int64_t dim = 0; dim < in_dims.size() - 1; ++dim) {
        start_index = start_index * in_dims[dim + 1] + shape_vec[dim + 1];
      }

      std::vector<int64_t> org_index_vec(in_dims[axis], start_index);
      for (int64_t j = 1; j < in_dims[axis]; ++j) {
        org_index_vec[j] += j * stride;
Y
Yibing Liu 已提交
64 65
      }

Y
Yibing Liu 已提交
66 67 68 69
      std::sort(org_index_vec.begin(), org_index_vec.end(),
                [in_data](const int64_t v1, const int64_t v2) {
                  return in_data[v1] < in_data[v2];
                });
Y
Yibing Liu 已提交
70 71

      for (size_t j = 0; j < org_index_vec.size(); ++j) {
Y
Yibing Liu 已提交
72 73 74
        int64_t index = start_index + j * stride;
        out_data[index] = in_data[org_index_vec[j]];
        ids_data[index] = (org_index_vec[j] - start_index) / stride;
Y
Yibing Liu 已提交
75 76 77 78 79 80 81
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle