|
|
@ -21,7 +21,7 @@ class SGDOp : public framework::OperatorWithKernel {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
|
|
|
|
|
|
|
void InferShape(framework::InferShapeContext *ctx) const override {
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Param"),
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Param"),
|
|
|
|
"Input(Param) of SGDOp should not be null.");
|
|
|
|
"Input(Param) of SGDOp should not be null.");
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Grad"),
|
|
|
|
PADDLE_ENFORCE(ctx->HasInput("Grad"),
|
|
|
@ -35,15 +35,15 @@ class SGDOp : public framework::OperatorWithKernel {
|
|
|
|
PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1,
|
|
|
|
PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1,
|
|
|
|
"Learning rate should have 1 element");
|
|
|
|
"Learning rate should have 1 element");
|
|
|
|
auto param_dim = ctx->GetInputDim("Param");
|
|
|
|
auto param_dim = ctx->GetInputDim("Param");
|
|
|
|
PADDLE_ENFORCE_EQ(param_dim, ctx->GetInputDim("Grad"),
|
|
|
|
// TODO(qijun): check dimensions of Param and Grad at complie
|
|
|
|
"Two input of SGD Op's dimension must be same.");
|
|
|
|
// and run time.
|
|
|
|
ctx->SetOutputDim("ParamOut", param_dim);
|
|
|
|
ctx->SetOutputDim("ParamOut", param_dim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SGDOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
class SGDOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
SGDOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
|
|
|
SGDOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker)
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
AddInput("Param", "Input parameter");
|
|
|
|
AddInput("Param", "Input parameter");
|
|
|
|
AddInput("LearningRate", "Learning rate of SGD");
|
|
|
|
AddInput("LearningRate", "Learning rate of SGD");
|
|
|
@ -58,6 +58,38 @@ param_out = param - learning_rate * grad;
|
|
|
|
)DOC");
|
|
|
|
)DOC");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
struct SparseSGDFunctor<platform::CPUPlace, T> {
|
|
|
|
|
|
|
|
void operator()(const platform::DeviceContext& ctx,
|
|
|
|
|
|
|
|
const framework::SelectedRows& input,
|
|
|
|
|
|
|
|
const framework::Tensor& learning_rate,
|
|
|
|
|
|
|
|
framework::Tensor* output) {
|
|
|
|
|
|
|
|
auto in_height = input.height();
|
|
|
|
|
|
|
|
auto out_dims = output->dims();
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(in_height, out_dims[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto& in_value = input.value();
|
|
|
|
|
|
|
|
auto& in_rows = input.rows();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int64_t in_row_numel = in_value.numel() / in_rows.size();
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(in_row_numel, output->numel() / in_height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto* in_data = in_value.data<T>();
|
|
|
|
|
|
|
|
auto* out_data = output->data<T>();
|
|
|
|
|
|
|
|
auto* lr = learning_rate.data<T>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < in_rows.size(); i++) {
|
|
|
|
|
|
|
|
for (int64_t j = 0; j < in_row_numel; j++) {
|
|
|
|
|
|
|
|
out_data[in_rows[i] * in_row_numel + j] -=
|
|
|
|
|
|
|
|
lr[0] * in_data[i * in_row_numel + j];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template struct SparseSGDFunctor<platform::CPUPlace, float>;
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace operators
|
|
|
|
} // namespace operators
|
|
|
|
} // namespace paddle
|
|
|
|
} // namespace paddle
|
|
|
|
|
|
|
|
|
|
|
|