flip_op.cc 3.8 KB
Newer Older
W
Wilber 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2020 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 <string>
#include <unordered_map>
#include <vector>
Y
Yang 已提交
18

19
#include "paddle/fluid/framework/infershape_utils.h"
Y
Yang 已提交
20
#include "paddle/fluid/framework/op_registry.h"
21
#include "paddle/fluid/framework/op_version_registry.h"
22 23
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
W
Wilber 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

namespace paddle {
namespace operators {

using framework::OpKernelType;

class FlipOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const {
    framework::LibraryType library = framework::LibraryType::kPlain;
    framework::DataLayout layout = framework::DataLayout::kAnyLayout;
    int customized_type_value =
        framework::OpKernelType::kDefaultCustomizedTypeValue;
    auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
41 42 43 44 45
    return framework::OpKernelType(input_data_type,
                                   ctx.GetPlace(),
                                   layout,
                                   library,
                                   customized_type_value);
W
Wilber 已提交
46 47 48 49 50 51 52 53
  }
};

class FlipOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor), The input tensor of flip op.");
    AddOutput("Out", "(Tensor), The output tensor of flip op.");
Y
yaoxuefeng 已提交
54
    AddAttr<std::vector<int>>("axis", "The axes to flip on.");
W
Wilber 已提交
55 56
    AddComment(R"DOC(
          Flip Operator.
Y
yaoxuefeng 已提交
57
          Reverse the order of a n-D tensor along given axis in axes.
W
Wilber 已提交
58 59 60 61 62 63
      )DOC");
  }
};

class FlipOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
64
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
W
Wilber 已提交
65
      const override {
66 67
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
    return m;
W
Wilber 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  }
};

template <typename T>
class FlipOpGradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> retv) const override {
    retv->SetType("flip");
    retv->SetInput("X", this->OutputGrad("Out"));
    retv->SetOutput("Out", this->InputGrad("X"));
    retv->SetAttrMap(this->Attrs());
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
89
namespace plat = paddle::platform;
90 91
DECLARE_INFER_SHAPE_FUNCTOR(flip,
                            FlipInferShapeFunctor,
92
                            PD_INFER_META(phi::FlipInferMeta));
93 94 95 96
REGISTER_OPERATOR(flip,
                  ops::FlipOp,
                  ops::FlipOpMaker,
                  ops::FlipOpInferVarType,
W
Wilber 已提交
97
                  ops::FlipOpGradMaker<paddle::framework::OpDesc>,
98 99
                  ops::FlipOpGradMaker<paddle::imperative::OpBase>,
                  FlipInferShapeFunctor);
100 101

/* ==========================  register checkpoint ===========================*/
102 103 104
REGISTER_OP_VERSION(flip).AddCheckpoint(
    R"ROC(Upgrade flip, add new attr [axis] and delete attr [dims].)ROC",
    paddle::framework::compatible::OpVersionDesc()
105 106
        .NewAttr("axis",
                 "The added attr 'axis' doesn't set default value.",
107 108
                 paddle::none)
        .DeleteAttr("dims", "The attr 'dims' is deleted."));