Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into develop
commit
416f590912
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 56 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 49 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 30 KiB |
@ -0,0 +1,20 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/adam_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(adam,
|
||||
ops::AdamOpKernel<paddle::platform::GPUPlace, float>);
|
||||
@ -0,0 +1,82 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place, typename T>
|
||||
class AdamOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
|
||||
auto moment1_out_tensor = ctx.Output<framework::Tensor>("Moment1Out");
|
||||
auto moment2_out_tensor = ctx.Output<framework::Tensor>("Moment2Out");
|
||||
auto beta1_pow_out_tensor = ctx.Output<framework::Tensor>("Beta1PowOut");
|
||||
auto beta2_pow_out_tensor = ctx.Output<framework::Tensor>("Beta2PowOut");
|
||||
|
||||
param_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
moment1_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
moment2_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
beta1_pow_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
beta2_pow_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
|
||||
float beta1 = ctx.Attr<float>("beta1");
|
||||
float beta2 = ctx.Attr<float>("beta2");
|
||||
float epsilon = ctx.Attr<float>("epsilon");
|
||||
|
||||
auto param = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Param"));
|
||||
auto grad = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Grad"));
|
||||
auto moment1 = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Moment1"));
|
||||
auto moment2 = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Moment2"));
|
||||
auto lr = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("LearningRate"));
|
||||
auto beta1_pow = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Beta1Pow"));
|
||||
auto beta2_pow = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Beta2Pow"));
|
||||
auto param_out = framework::EigenVector<T>::Flatten(*param_out_tensor);
|
||||
auto moment1_out = framework::EigenVector<T>::Flatten(*moment1_out_tensor);
|
||||
auto moment2_out = framework::EigenVector<T>::Flatten(*moment2_out_tensor);
|
||||
auto beta1_pow_out =
|
||||
framework::EigenVector<T>::Flatten(*beta1_pow_out_tensor);
|
||||
auto beta2_pow_out =
|
||||
framework::EigenVector<T>::Flatten(*beta2_pow_out_tensor);
|
||||
auto place = ctx.GetEigenDevice<Place>();
|
||||
|
||||
moment1_out.device(place) = beta1 * moment1 + (1 - beta1) * grad;
|
||||
moment2_out.device(place) = beta2 * moment2 + (1 - beta2) * grad.square();
|
||||
beta1_pow_out.device(place) = beta1_pow * beta1;
|
||||
beta2_pow_out.device(place) = beta2_pow * beta2;
|
||||
// All of these are tensors of 1 element
|
||||
auto lr_t = lr * (1 - beta2_pow_out).sqrt() / (1 - beta1_pow_out);
|
||||
// Eigen does not support automatic broadcast
|
||||
// Get dimensions of moment vector to broadcast lr_t
|
||||
Eigen::DSizes<int, 1> m_dsize(moment1_out_tensor->numel());
|
||||
param_out.device(place) =
|
||||
param -
|
||||
lr_t.broadcast(m_dsize) *
|
||||
(moment1_out / (moment2_out.sqrt() + epsilon));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
@ -0,0 +1,47 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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 "paddle/operators/conv2d_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class CudnnConvOpMaker : public Conv2DOpMaker {
|
||||
public:
|
||||
CudnnConvOpMaker(framework::OpProto* proto,
|
||||
framework::OpAttrChecker* op_checker)
|
||||
: Conv2DOpMaker(proto, op_checker) {
|
||||
AddAttr<std::vector<int>>("dilations", "dilations of convolution operator.")
|
||||
.SetDefault(std::vector<int>{1, 1});
|
||||
AddAttr<int>("workspace_size_MB",
|
||||
"workspace size for cudnn, in MB, "
|
||||
"workspace is a section of GPU memory which will be "
|
||||
"allocated/freed each time the operator runs, larger "
|
||||
"workspace size can increase performance but also requires "
|
||||
"better hardward. This size should be carefully setted.")
|
||||
.SetDefault(4096);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(conv_cudnn, ops::Conv2DOp, ops::CudnnConvOpMaker, conv_cudnn_grad,
|
||||
ops::Conv2DOpGrad);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
conv_cudnn, ops::GemmConv2DKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
conv_cudnn_grad,
|
||||
ops::GemmConvGrad2DKernel<paddle::platform::CPUPlace, float>);
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,96 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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 "paddle/operators/decayed_adagrad_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class DecayedAdagradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("Param"),
|
||||
"Input(Param) of DecayedAdagradOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Grad"),
|
||||
"Input(Grad) of DecayedAdagradOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Moment"),
|
||||
"Input(Moment) of DecayedAdagradOp should not be null.");
|
||||
PADDLE_ENFORCE(
|
||||
ctx->HasInput("LearningRate"),
|
||||
"Input(LearningRate) of DecayedAdagradOp should not be null.");
|
||||
|
||||
PADDLE_ENFORCE(ctx->HasOutput("ParamOut"),
|
||||
"Output(ParamOut) of DecayedAdagradOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("MomentOut"),
|
||||
"Output(MomentOut) of DecayedAdagradOp should not be null.");
|
||||
|
||||
auto lr_dims = ctx->GetInputDim("LearningRate");
|
||||
PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1,
|
||||
"LearningRate should have one element");
|
||||
auto param_dims = ctx->GetInputDim("Param");
|
||||
PADDLE_ENFORCE_EQ(param_dims, ctx->GetInputDim("Grad"),
|
||||
"Param and Grad input of DecayedAdagradOp should have "
|
||||
"the same dimension.");
|
||||
PADDLE_ENFORCE_EQ(param_dims, ctx->GetInputDim("Moment"),
|
||||
"Param and Moment input of DecayedAdagradOp should have "
|
||||
"the same dimension.");
|
||||
|
||||
ctx->SetOutputDim("ParamOut", param_dims);
|
||||
ctx->SetOutputDim("MomentOut", param_dims);
|
||||
}
|
||||
};
|
||||
|
||||
class DecayedAdagradOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
DecayedAdagradOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("Param", "(Tensor) Input parameter");
|
||||
AddInput("Grad", "(Tensor) Input gradient");
|
||||
AddInput("Moment", "(Tensor) Second moment");
|
||||
AddInput("LearningRate", "(Tensor) Learning rate");
|
||||
|
||||
AddOutput("ParamOut", "(Tensor) Output parameter");
|
||||
AddOutput("MomentOut", "(Tensor) Output second moment");
|
||||
|
||||
AddAttr<float>("decay",
|
||||
"(float, default 0.95) "
|
||||
"Discounting factor for coming gradient")
|
||||
.SetDefault(0.95);
|
||||
AddAttr<float>("epsilon",
|
||||
"(float, default 1.0e-6) "
|
||||
"Constant for numerical stability")
|
||||
.SetDefault(1.0e-6f);
|
||||
AddComment(R"DOC(
|
||||
|
||||
Decayed Adagrad
|
||||
|
||||
moment_out = decay * moment + (1 - decay) * grad * grad
|
||||
param_out = param - learning_rate * grad / (sqrt(moment_out) + epsilon)
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(decayed_adagrad, ops::DecayedAdagradOp,
|
||||
ops::DecayedAdagradOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
decayed_adagrad,
|
||||
ops::DecayedAdagradOpKernel<paddle::platform::CPUPlace, float>);
|
||||
@ -0,0 +1,21 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/decayed_adagrad_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
decayed_adagrad,
|
||||
ops::DecayedAdagradOpKernel<paddle::platform::GPUPlace, float>);
|
||||
@ -0,0 +1,56 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place, typename T>
|
||||
class DecayedAdagradOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto param_out_tensor = ctx.Output<framework::Tensor>("ParamOut");
|
||||
auto moment_out_tensor = ctx.Output<framework::Tensor>("MomentOut");
|
||||
|
||||
param_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
moment_out_tensor->mutable_data<T>(ctx.GetPlace());
|
||||
|
||||
float decay = ctx.Attr<float>("decay");
|
||||
float epsilon = ctx.Attr<float>("epsilon");
|
||||
|
||||
auto param = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Param"));
|
||||
auto grad = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Grad"));
|
||||
auto moment = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("Moment"));
|
||||
auto lr = framework::EigenVector<T>::Flatten(
|
||||
*ctx.Input<framework::Tensor>("LearningRate"));
|
||||
|
||||
auto param_out = framework::EigenVector<T>::Flatten(*param_out_tensor);
|
||||
auto moment_out = framework::EigenVector<T>::Flatten(*moment_out_tensor);
|
||||
auto place = ctx.GetEigenDevice<Place>();
|
||||
|
||||
moment_out.device(place) = decay * moment + (1 - decay) * grad * grad;
|
||||
Eigen::DSizes<int, 1> m_dsize(moment_out_tensor->numel());
|
||||
param_out.device(place) =
|
||||
param - lr.broadcast(m_dsize) * grad / (moment_out.sqrt() + epsilon);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue