arg_min_max_op_base.h 2.5 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 19 20 21 22 23 24
#include <type_traits>
#include <vector>
#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"
25
#include "paddle/phi/core/ddim.h"
S
sneaxiy 已提交
26 27 28 29

namespace paddle {
namespace operators {

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

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.");
45
    AddAttr<bool>("keepdims", "Keep the dim that to reduce.").SetDefault(false);
W
wawltor 已提交
46 47 48
    AddAttr<bool>("flatten",
                  "Flatten the input value, and search the min or max indices")
        .SetDefault(false);
49 50 51 52 53
    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 已提交
54 55
    AddComment(string::Sprintf(R"DOC(
      %s Operator.
S
sneaxiy 已提交
56

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

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