argsort_op.cc 4.2 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include <memory>
Y
Yibing Liu 已提交
16

L
Linjie Chen 已提交
17 18 19 20 21
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

Y
Yibing Liu 已提交
22 23 24 25 26 27 28 29
namespace paddle {
namespace operators {

class ArgsortOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class ArgsortGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
    ctx->ShareLoD("X", /*-->*/ framework::GradVarName("X"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
  }
};

Y
Yibing Liu 已提交
48 49 50 51
class ArgsortOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor) The input of Argsort op.");
52 53 54
    AddOutput("Out",
              "(Tensor) The sorted tensor of Argsort op, with the same "
              "shape as Input(X).");
Y
Yibing Liu 已提交
55
    AddOutput("Indices",
56 57
              "(Tensor) The indices of a tensor giving the sorted order, with "
              "the same shape as Input(X).");
Y
Yibing Liu 已提交
58 59 60
    AddComment(R"DOC(
Argsort operator

61 62 63
Performs sorting on the input tensor along the given axis and outputs two
tensors, Output(Out) and Output(Indices). They reserve the same shape
with Input(X), and Output(Out) represents the sorted tensor while
Y
Yibing Liu 已提交
64 65 66 67
Output(Indices) gives the sorted order along the given axis Attr(axis).

 )DOC");
    AddAttr<int>("axis",
68 69 70
                 "(int, default -1) The axis along which to sort the tensor. "
                 "When axis < 0, the actual axis will be the |axis|'th "
                 "counting backwards. Default -1, the last dimension.")
Y
Yibing Liu 已提交
71
        .SetDefault(-1);
72 73 74 75 76 77 78
    AddAttr<bool>(
        "descending",
        "(bool, default false) The descending attribute is a flag to tell"
        "algorithm how to sort the input data."
        "If descending is true, will sort by descending order,"
        "else if false, sort by ascending order. Default value is false.")
        .SetDefault(false);
Y
Yibing Liu 已提交
79 80 81
  }
};

82 83 84 85 86 87
template <typename T>
class ArgsortGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
88
  void Apply(GradOpPtr<T> op) const override {
89 90 91 92 93 94 95 96 97
    op->SetType("argsort_grad");
    op->SetInput("Indices", this->Output("Indices"));
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
  }
};

98
DECLARE_NO_NEED_BUFFER_VARS_INFERER(ArgsortGradNoNeedBufferVarsInferer, "X");
99

Y
Yibing Liu 已提交
100 101 102 103
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
104 105
DECLARE_INFER_SHAPE_FUNCTOR(argsort,
                            ArgsortInferShapeFunctor,
L
Linjie Chen 已提交
106
                            PD_INFER_META(phi::ArgsortInferMeta));
107 108 109
REGISTER_OPERATOR(argsort,
                  ops::ArgsortOp,
                  ops::ArgsortOpMaker,
110
                  ops::ArgsortGradOpMaker<paddle::framework::OpDesc>,
L
Linjie Chen 已提交
111 112
                  ops::ArgsortGradOpMaker<paddle::imperative::OpBase>,
                  ArgsortInferShapeFunctor);
113 114
REGISTER_OPERATOR(argsort_grad,
                  ops::ArgsortGradOp,
115
                  ops::ArgsortGradNoNeedBufferVarsInferer);