commit
9b0f092853
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,138 @@
|
||||
/* Copyright (c) 2017 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 "MKLDNNLayer.h"
|
||||
#include "mkldnn.hpp"
|
||||
|
||||
namespace paddle {
|
||||
typedef mkldnn::batch_normalization_forward bn_fwd;
|
||||
typedef mkldnn::batch_normalization_backward bn_bwd;
|
||||
|
||||
/**
|
||||
* @brief A subclass of MKLDNNLayer BatchNorm layer.
|
||||
*
|
||||
* The config file api is mkldnn_batch_norm
|
||||
*/
|
||||
class MKLDNNBatchNormLayer : public MKLDNNLayer {
|
||||
protected:
|
||||
// save forward primitive_desc, which can be used backward
|
||||
std::shared_ptr<bn_fwd::primitive_desc> fwdPD_;
|
||||
|
||||
// Epsilon value used in the batch normalization formula.
|
||||
static const real EPS;
|
||||
// weight and bias in paddle
|
||||
std::unique_ptr<Weight> weight_;
|
||||
std::unique_ptr<Weight> biases_;
|
||||
// mkldnn use a large buffer store both scale and shift
|
||||
// which are weight and bias in paddle corresponding.
|
||||
MatrixPtr valueScaleShift_;
|
||||
MatrixPtr gradScaleShift_;
|
||||
// Moving average of mean.
|
||||
std::unique_ptr<Weight> movingMean_;
|
||||
// Moving average of variance.
|
||||
std::unique_ptr<Weight> movingVar_;
|
||||
|
||||
// if useGlobalStats_ is true, will use the loaded mean and variance.
|
||||
// otherwise, calculate mean and variance in every mini-batch.
|
||||
bool useGlobalStats_;
|
||||
// used in MKLDNN primitive desc
|
||||
unsigned flags_;
|
||||
// use to compute moving mean and variance.
|
||||
real movingAvgFraction_;
|
||||
// whether the weight has been init
|
||||
bool hasInitedWgt_;
|
||||
|
||||
// local mean and variance
|
||||
// when useGlobalStats_ they are loaded from moving mean and variance
|
||||
// when do not useGlobalStats_ they are calculated from this mini-batch
|
||||
MKLDNNMatrixPtr mean_;
|
||||
MKLDNNMatrixPtr var_;
|
||||
|
||||
public:
|
||||
explicit MKLDNNBatchNormLayer(const LayerConfig& config)
|
||||
: MKLDNNLayer(config), useGlobalStats_(true), hasInitedWgt_(false) {}
|
||||
|
||||
~MKLDNNBatchNormLayer() {}
|
||||
|
||||
bool init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) override;
|
||||
|
||||
void forward(PassType passType) override;
|
||||
|
||||
void reshape(
|
||||
int& bs, int& ic, int& ih, int& iw, int oc, int& oh, int& ow) override;
|
||||
|
||||
void resetFwd(std::vector<mkldnn::primitive>& pipeline,
|
||||
MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& bias,
|
||||
MKLDNNMatrixPtr& out) override;
|
||||
|
||||
void resetBwd(std::vector<mkldnn::primitive>& pipeline,
|
||||
MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& bias,
|
||||
MKLDNNMatrixPtr& out) override;
|
||||
|
||||
void updateWeights(const UpdateCallback& callback) override;
|
||||
|
||||
void convertWeightsFromPaddle() override;
|
||||
|
||||
protected:
|
||||
void initWeight();
|
||||
/**
|
||||
* cal moving mean and variance.
|
||||
* moving = moving * AvgFraction + local * (1 - AvgFraction)
|
||||
*/
|
||||
void calMovingMeanAndVar();
|
||||
/**
|
||||
* Forward functions: reset buffers(input, weight, output),
|
||||
* reset primitive descriptor,
|
||||
* reset pipeline.
|
||||
*/
|
||||
void resetFwdBuffers(MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& out);
|
||||
void resetFwdPD(std::shared_ptr<bn_fwd::primitive_desc>& pd,
|
||||
MKLDNNMatrixPtr in,
|
||||
MKLDNNMatrixPtr wgt,
|
||||
MKLDNNMatrixPtr out);
|
||||
void resetFwdPipeline(std::vector<mkldnn::primitive>& pipeline,
|
||||
std::shared_ptr<bn_fwd::primitive_desc>& pd,
|
||||
MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& out);
|
||||
|
||||
/**
|
||||
* Backward functions: reset buffers(input, weight, output),
|
||||
* reset primitive descriptor,
|
||||
* reset pipeline.
|
||||
*/
|
||||
void resetBwdBuffers(MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& out);
|
||||
void resetBwdPD(std::shared_ptr<bn_bwd::primitive_desc>& pd,
|
||||
MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& out);
|
||||
void resetBwdPipeline(std::vector<mkldnn::primitive>& pipeline,
|
||||
std::shared_ptr<bn_bwd::primitive_desc>& pd,
|
||||
MKLDNNMatrixPtr& in,
|
||||
MKLDNNMatrixPtr& wgt,
|
||||
MKLDNNMatrixPtr& out);
|
||||
};
|
||||
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
@ -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. */
|
||||
|
||||
#include "paddle/operators/fill_constant_batch_size_like_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class FillConstantBatchSizeLikeOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(
|
||||
ctx->HasInput("Input"),
|
||||
"Input(Input) of FillConstantBatchSizeLikeOp should not be null.");
|
||||
PADDLE_ENFORCE(
|
||||
ctx->HasOutput("Out"),
|
||||
"Output(Out) of FillConstantBatchSizeLikeOp should not be null.");
|
||||
|
||||
auto &shape = ctx->Attrs().Get<std::vector<int>>("shape");
|
||||
PADDLE_ENFORCE_GT(shape.size(), 0);
|
||||
std::vector<int64_t> shape_int64(shape.size(), 0);
|
||||
std::transform(shape.begin(), shape.end(), shape_int64.begin(),
|
||||
[](int a) { return static_cast<int64_t>(a); });
|
||||
auto dims = framework::make_ddim(shape_int64);
|
||||
|
||||
dims[0] = ctx->GetInputDim("Input")[0];
|
||||
ctx->SetOutputDim("Out", dims);
|
||||
}
|
||||
|
||||
protected:
|
||||
framework::DataType IndicateDataType(
|
||||
const framework::ExecutionContext &ctx) const override {
|
||||
return static_cast<framework::DataType>(ctx.Attr<int>("data_type"));
|
||||
}
|
||||
};
|
||||
|
||||
class FillConstantBatchSizeLikeOpMaker
|
||||
: public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
FillConstantBatchSizeLikeOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddAttr<int>("data_type",
|
||||
"(int, default 5 (FP32)) "
|
||||
"Output data type")
|
||||
.SetDefault(framework::DataType::FP32);
|
||||
AddAttr<std::vector<int>>("shape", "(vector<int>) The shape of the output");
|
||||
AddAttr<float>("value", "(float, default 0) The value to be filled")
|
||||
.SetDefault(0.0f);
|
||||
AddInput("Input",
|
||||
"(Tensor) Tensor "
|
||||
"whose first dimension is used to specify the batch_size");
|
||||
AddOutput("Out",
|
||||
"(Tensor) Tensor of specified shape will be filled "
|
||||
"with the specified value");
|
||||
AddComment(R"DOC(Fill up a variable with specified constant value.)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(fill_constant_batch_size_like,
|
||||
ops::FillConstantBatchSizeLikeOp,
|
||||
ops::FillConstantBatchSizeLikeOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
fill_constant_batch_size_like,
|
||||
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUPlace, float>,
|
||||
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUPlace, double>);
|
@ -0,0 +1,23 @@
|
||||
/* 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/framework/op_registry.h"
|
||||
#include "paddle/operators/fill_constant_batch_size_like_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
fill_constant_batch_size_like,
|
||||
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::GPUPlace, float>,
|
||||
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::GPUPlace, double>);
|
@ -0,0 +1,37 @@
|
||||
/* 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 FillConstantBatchSizeLikeOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto* out = ctx.Output<framework::Tensor>("Out");
|
||||
out->mutable_data<T>(ctx.GetPlace());
|
||||
auto value = ctx.Attr<float>("value");
|
||||
|
||||
auto out_eigen = framework::EigenVector<T>::Flatten(*out);
|
||||
auto place = ctx.GetEigenDevice<Place>();
|
||||
out_eigen.device(place) = out_eigen.constant(static_cast<T>(value));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,78 @@
|
||||
/* 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/squared_l2_norm_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using framework::Tensor;
|
||||
|
||||
class SquaredL2NormOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should be not null.");
|
||||
|
||||
ctx->SetOutputDim("Out", {1});
|
||||
}
|
||||
};
|
||||
|
||||
class SquaredL2NormGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
|
||||
"Input(Out@GRAD) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
||||
"Output(X@GRAD) should be not null.");
|
||||
|
||||
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
|
||||
}
|
||||
};
|
||||
|
||||
class SquaredL2NormOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
SquaredL2NormOpMaker(framework::OpProto* proto,
|
||||
framework::OpAttrChecker* op_checker)
|
||||
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "(Tensor) The input of squared_l2_norm op.");
|
||||
AddOutput("Out", "(Float) The output of squared_l2_norm op.");
|
||||
AddComment(R"DOC(
|
||||
SquaredL2Norm Operator.
|
||||
|
||||
Computes the squared L2 norm of a tensor.
|
||||
|
||||
Out = sum (X ** 2)
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(squared_l2_norm, ops::SquaredL2NormOp, ops::SquaredL2NormOpMaker,
|
||||
squared_l2_norm_grad, ops::SquaredL2NormGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
squared_l2_norm,
|
||||
ops::SquaredL2NormKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
squared_l2_norm_grad,
|
||||
ops::SquaredL2NormGradKernel<paddle::platform::CPUPlace, float>);
|
@ -0,0 +1,24 @@
|
||||
/* 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/squared_l2_norm_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
squared_l2_norm,
|
||||
ops::SquaredL2NormKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
squared_l2_norm_grad,
|
||||
ops::SquaredL2NormGradKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,64 @@
|
||||
/* 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 {
|
||||
|
||||
// Out = sum(square(X))
|
||||
template <typename Place, typename T>
|
||||
class SquaredL2NormKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &context) const override {
|
||||
const framework::Tensor *X = context.Input<framework::Tensor>("X");
|
||||
framework::Tensor *Out = context.Output<framework::Tensor>("Out");
|
||||
Out->mutable_data<T>(context.GetPlace());
|
||||
|
||||
auto x = framework::EigenVector<T>::Flatten(*X);
|
||||
auto out = framework::EigenVector<T>::Flatten(*Out);
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
|
||||
out.device(place) = x.square().sum();
|
||||
}
|
||||
};
|
||||
|
||||
// dX = X
|
||||
template <typename Place, typename T>
|
||||
class SquaredL2NormGradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &context) const override {
|
||||
const framework::Tensor *X = context.Input<framework::Tensor>("X");
|
||||
const framework::Tensor *dOut =
|
||||
context.Input<framework::Tensor>(framework::GradVarName("Out"));
|
||||
PADDLE_ENFORCE(dOut->numel() == 1,
|
||||
"Squared L2 Norm Gradient should be scalar");
|
||||
framework::Tensor *dX =
|
||||
context.Output<framework::Tensor>(framework::GradVarName("X"));
|
||||
dX->mutable_data<T>(context.GetPlace());
|
||||
|
||||
auto x = framework::EigenVector<T>::Flatten(*X);
|
||||
auto dout = framework::EigenVector<T>::Flatten(*dOut);
|
||||
auto dx = framework::EigenVector<T>::Flatten(*dX);
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
|
||||
Eigen::DSizes<int, 1> x_dsize(X->numel());
|
||||
dx.device(place) = (dout.broadcast(x_dsize) * x) * static_cast<T>(2.0);
|
||||
}
|
||||
};
|
||||
|
||||
} // 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