arg_min_max_op_base.h 2.6 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 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
Y
yuyang18 已提交
16
#include <string>
S
sneaxiy 已提交
17 18
#include <type_traits>
#include <vector>
19

S
sneaxiy 已提交
20 21 22 23 24 25
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/string/printf.h"
26
#include "paddle/phi/core/ddim.h"
S
sneaxiy 已提交
27 28 29 30

namespace paddle {
namespace operators {

Y
yuyang18 已提交
31
class ArgMinMaxOp : public framework::OperatorWithKernel {
S
sneaxiy 已提交
32 33
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
Y
yuyang18 已提交
34
};
S
sneaxiy 已提交
35 36 37 38 39 40 41 42 43 44 45

class BaseArgMinMaxOpMaker : public framework::OpProtoAndCheckerMaker {
 protected:
  virtual const char* OpName() const = 0;
  virtual const char* Name() const = 0;

 public:
  void Make() override {
    AddInput("X", "Input tensor.");
    AddOutput("Out", "Output tensor.");
    AddAttr<int64_t>("axis", "The axis in which to compute the arg indics.");
46
    AddAttr<bool>("keepdims", "Keep the dim that to reduce.").SetDefault(false);
W
wawltor 已提交
47 48 49
    AddAttr<bool>("flatten",
                  "Flatten the input value, and search the min or max indices")
        .SetDefault(false);
50 51 52 53 54
    AddAttr<int>("dtype",
                 "(int, 3), the dtype of indices, the indices dtype must be "
                 "int32, int64."
                 "default dtype is int64, and proto value is 3.")
        .SetDefault(3);
Y
yuyang18 已提交
55 56
    AddComment(string::Sprintf(R"DOC(
      %s Operator.
S
sneaxiy 已提交
57

Y
yuyang18 已提交
58 59
      Computes the indices of the %s elements of the input tensor's element
      along the provided axis.
S
sneaxiy 已提交
60
)DOC",
61 62
                               OpName(),
                               Name()));
S
sneaxiy 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  }
};

class ArgMinOpMaker : public BaseArgMinMaxOpMaker {
 protected:
  const char* OpName() const override { return "ArgMin"; }
  const char* Name() const override { return "min"; }
};

class ArgMaxOpMaker : public BaseArgMinMaxOpMaker {
 protected:
  const char* OpName() const override { return "ArgMax"; }
  const char* Name() const override { return "max"; }
};
}  // namespace operators
}  // namespace paddle