|
| 1 | +/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/cvm_op.h" |
| 16 | +#include <memory> |
| 17 | +#include "paddle/fluid/operators/math/math_function.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | + |
| 22 | +using Tensor = framework::Tensor; |
| 23 | + |
| 24 | +class CVMOp : public framework::OperatorWithKernel { |
| 25 | + public: |
| 26 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 27 | + |
| 28 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 29 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("CVM"), "Input(CVM) should be not null."); |
| 31 | + PADDLE_ENFORCE(ctx->HasOutput("Y"), "Output(Y) should be not null."); |
| 32 | + |
| 33 | + auto x_dims = ctx->GetInputDim("X"); |
| 34 | + auto cvm_dims = ctx->GetInputDim("CVM"); |
| 35 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "Input(X)'s rank should be 2."); |
| 36 | + PADDLE_ENFORCE_EQ(cvm_dims.size(), 2UL, "Input(CVM)'s rank should be 2."); |
| 37 | + PADDLE_ENFORCE_EQ(cvm_dims[1], 2UL, |
| 38 | + "The 2nd dimension of " |
| 39 | + "Input(CVM) should be 2."); |
| 40 | + |
| 41 | + if (ctx->Attrs().Get<bool>("use_cvm")) { |
| 42 | + ctx->SetOutputDim("Y", {x_dims[0], x_dims[1]}); |
| 43 | + } else { |
| 44 | + ctx->SetOutputDim("Y", {x_dims[0], x_dims[1] - 2}); |
| 45 | + } |
| 46 | + ctx->ShareLoD("X", /*->*/ "Y"); |
| 47 | + } |
| 48 | + |
| 49 | + protected: |
| 50 | + // Explicitly set that the data type of computation kernel of |
| 51 | + // cvm |
| 52 | + // is determined by its input "X". |
| 53 | + framework::OpKernelType GetExpectedKernelType( |
| 54 | + const framework::ExecutionContext& ctx) const override { |
| 55 | + return framework::OpKernelType(ctx.Input<Tensor>("X")->type(), |
| 56 | + platform::CPUPlace()); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +class CVMGradientOp : public framework::OperatorWithKernel { |
| 61 | + public: |
| 62 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 63 | + |
| 64 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 65 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null."); |
| 66 | + PADDLE_ENFORCE(ctx->HasInput("CVM"), "Input(CVM) should be not null."); |
| 67 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Y")), |
| 68 | + "Input(Y@GRAD) should be not null."); |
| 69 | + PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")), |
| 70 | + "Output(X@GRAD) should be not null."); |
| 71 | + |
| 72 | + auto x_dims = ctx->GetInputDim("X"); |
| 73 | + auto cvm_dims = ctx->GetInputDim("CVM"); |
| 74 | + auto dy_dims = ctx->GetInputDim(framework::GradVarName("Y")); |
| 75 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2."); |
| 76 | + PADDLE_ENFORCE_EQ(dy_dims.size(), 2, "Input(Y@Grad)'s rank should be 2."); |
| 77 | + PADDLE_ENFORCE_EQ(cvm_dims.size(), 2, "Input(CVM)'s rank should be 2."); |
| 78 | + |
| 79 | + PADDLE_ENFORCE_EQ(x_dims[0], dy_dims[0], |
| 80 | + "The 1st dimension of Input(X) and Input(Y@Grad) should " |
| 81 | + "be equal."); |
| 82 | + |
| 83 | + PADDLE_ENFORCE_EQ(cvm_dims[1], 2, |
| 84 | + "When Attr(soft_label) == false, the 2nd dimension of " |
| 85 | + "Input(CVM) should be 2."); |
| 86 | + ctx->SetOutputDim(framework::GradVarName("X"), x_dims); |
| 87 | + ctx->ShareLoD("X", framework::GradVarName("X")); |
| 88 | + } |
| 89 | + |
| 90 | + protected: |
| 91 | + // Explicitly set that the data type of computation kernel of |
| 92 | + // cvm |
| 93 | + // is determined by its input "X". |
| 94 | + framework::OpKernelType GetExpectedKernelType( |
| 95 | + const framework::ExecutionContext& ctx) const override { |
| 96 | + return framework::OpKernelType(ctx.Input<Tensor>("X")->type(), |
| 97 | + platform::CPUPlace()); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +class CVMOpMaker : public framework::OpProtoAndCheckerMaker { |
| 102 | + public: |
| 103 | + void Make() override { |
| 104 | + AddInput("X", |
| 105 | + "(LodTensor, default LodTensor<float>), a 2-D tensor with shape " |
| 106 | + "[N x D]," |
| 107 | + " where N is the batch size and D is the emebdding dim. "); |
| 108 | + AddInput("CVM", |
| 109 | + "(Tensor), a 2-D Tensor with shape [N x 2], where N is the batch " |
| 110 | + "size, 2 is show and click."); |
| 111 | + AddOutput("Y", |
| 112 | + "(LodTensor, default LodTensor<float>), a 2-D tensor with shape " |
| 113 | + "[N x K]."); |
| 114 | + AddAttr<bool>("use_cvm", "bool, use cvm or not").SetDefault(true); |
| 115 | + AddComment(R"DOC( |
| 116 | +CVM Operator. |
| 117 | +
|
| 118 | + We assume that input X is a embedding vector with cvm_feature(show and click), which shape is [N * D] (D is 2(cvm_feature) + embedding dim, N is batch_size) |
| 119 | + if use_cvm is True, we will log(cvm_feature), and output shape is [N * D]. |
| 120 | + if use_cvm is False, we will remove cvm_feature from input, and output shape is [N * (D - 2)]. |
| 121 | +
|
| 122 | +)DOC"); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +class CVMGradOpDescMaker : public framework::SingleGradOpDescMaker { |
| 127 | + public: |
| 128 | + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; |
| 129 | + |
| 130 | + protected: |
| 131 | + std::unique_ptr<framework::OpDesc> Apply() const override { |
| 132 | + std::unique_ptr<framework::OpDesc> op(new framework::OpDesc()); |
| 133 | + op->SetType("cvm_grad"); |
| 134 | + op->SetInput("X", Input("X")); |
| 135 | + op->SetInput("CVM", Input("CVM")); |
| 136 | + op->SetInput(framework::GradVarName("Y"), OutputGrad("Y")); |
| 137 | + op->SetOutput(framework::GradVarName("X"), InputGrad("X")); |
| 138 | + op->SetAttrMap(Attrs()); |
| 139 | + return op; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace operators |
| 144 | +} // namespace paddle |
| 145 | + |
| 146 | +namespace ops = paddle::operators; |
| 147 | +REGISTER_OPERATOR(cvm, ops::CVMOp, ops::CVMOpMaker, ops::CVMGradOpDescMaker); |
| 148 | + |
| 149 | +REGISTER_OPERATOR(cvm_grad, ops::CVMGradientOp); |
| 150 | + |
| 151 | +REGISTER_OP_CPU_KERNEL(cvm, ops::CVMOpKernel<float>, ops::CVMOpKernel<double>); |
| 152 | + |
| 153 | +REGISTER_OP_CPU_KERNEL(cvm_grad, ops::CVMGradOpKernel<float>, |
| 154 | + ops::CVMGradOpKernel<double>); |
0 commit comments