mpc_mean_op.cc 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
J
jingqinghe 已提交
14 15

#include "paddle/fluid/framework/op_registry.h"
16
#include "mpc_mean_op.h"
J
jingqinghe 已提交
17 18 19 20 21 22 23 24

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

class MpcMeanOp : public framework::OperatorWithKernel {
public:
25 26 27 28 29 30 31 32 33 34 35
    using framework::OperatorWithKernel::OperatorWithKernel;

    void InferShape(framework::InferShapeContext* ctx) const override {
        PADDLE_ENFORCE_EQ(
            ctx->HasInput("X"), true,
            platform::errors::NotFound("Input(X) of MpcMeanOp should not be null."));
        PADDLE_ENFORCE_EQ(
            ctx->HasOutput("Out"), true,
            platform::errors::NotFound("Output(Out) of MpcMeanOp should not be null."));
        ctx->SetOutputDim("Out", {2, 1});
    }
J
jingqinghe 已提交
36 37 38 39
};

class MpcMeanOpMaker : public framework::OpProtoAndCheckerMaker {
public:
40 41 42 43
    void Make() override {
        AddInput("X", "(Tensor), The first input tensor of mpc mean op.");
        AddOutput("Out", "(Tensor), The output tensor of mpc mean op.");
        AddComment(R"DOC(
J
jingqinghe 已提交
44 45
MPC mean Operator calculates the mean of all elements in X.
)DOC");
46
    }
J
jingqinghe 已提交
47 48 49 50
};

class MpcMeanOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
protected:
51 52 53 54 55
    std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
            const override {
        static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
        return m;
    }
J
jingqinghe 已提交
56 57 58 59
};

class MpcMeanGradOp : public framework::OperatorWithKernel {
public:
60 61 62 63 64 65 66
    using framework::OperatorWithKernel::OperatorWithKernel;
    using Tensor = framework::Tensor;
    
    void InferShape(framework::InferShapeContext *ctx) const override {
        ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
        ctx->ShareLoD("X", framework::GradVarName("X"));
    }
J
jingqinghe 已提交
67 68 69 70

};

template <typename T>
71
class MpcMeanOpGradMaker : public framework::SingleGradOpMaker<T> {
J
jingqinghe 已提交
72
public:
73
    using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
J
jingqinghe 已提交
74 75

protected:
76 77 78 79 80 81
    void Apply(GradOpPtr<T> grad) const override {
        grad->SetType("mpc_mean_grad");
        grad->SetInput("X", this->Input("X"));
        grad->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
        grad->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    }
J
jingqinghe 已提交
82 83
};

84 85
}  // namespace operators
}  // namespace paddle
J
jingqinghe 已提交
86 87

namespace ops = paddle::operators;
88
REGISTER_OPERATOR(mpc_mean, ops::MpcMeanOp,
89
                 ops::MpcMeanOpMaker, 
90
                 ops::MpcMeanOpInferVarType,
91
                 ops::MpcMeanOpGradMaker<paddle::framework::OpDesc>);
J
jingqinghe 已提交
92 93 94 95

REGISTER_OPERATOR(mpc_mean_grad, ops::MpcMeanGradOp);

REGISTER_OP_CPU_KERNEL(
96 97
    mpc_mean, 
    ops::MpcMeanKernel<paddle::platform::CPUDeviceContext, int64_t>);
J
jingqinghe 已提交
98 99

REGISTER_OP_CPU_KERNEL(
100
    mpc_mean_grad, 
J
jingqinghe 已提交
101
    ops::MpcMeanGradKernel<paddle::platform::CPUDeviceContext, int64_t>);