Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into dilate_conv
commit
2c8b3c4430
@ -0,0 +1,107 @@
|
||||
/* 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 "Layer.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
/**
|
||||
* A layer applies a linear transformation to each element in each row of
|
||||
* the input matrix. For each element, the layer first re-scale it and then
|
||||
* adds a bias to it.
|
||||
*
|
||||
* \f[
|
||||
* y = wx + b
|
||||
* \f]
|
||||
*
|
||||
* Here, w is the scale and b is the bias. Both w and b are trainable scalars.
|
||||
*
|
||||
*/
|
||||
|
||||
class ScaleShiftLayer : public Layer {
|
||||
protected:
|
||||
std::unique_ptr<Weight> scale_;
|
||||
std::unique_ptr<Weight> offset_;
|
||||
|
||||
public:
|
||||
explicit ScaleShiftLayer(const LayerConfig& config) : Layer(config) {}
|
||||
|
||||
bool init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) override;
|
||||
|
||||
void forward(PassType passType) override;
|
||||
void backward(const UpdateCallback& callback = nullptr) override;
|
||||
};
|
||||
|
||||
REGISTER_LAYER(scale_shift, ScaleShiftLayer);
|
||||
|
||||
bool ScaleShiftLayer::init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) {
|
||||
Layer::init(layerMap, parameterMap);
|
||||
CHECK_EQ(inputLayers_.size(), 1U);
|
||||
scale_.reset(new Weight(1, 1, parameters_[0]));
|
||||
if (biasParameter_.get() != NULL) {
|
||||
offset_ = std::unique_ptr<Weight>(new Weight(1, 1, biasParameter_));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScaleShiftLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
|
||||
MatrixPtr inV = getInputValue(0);
|
||||
resetOutput(inV->getHeight(), inV->getWidth());
|
||||
MatrixPtr outV = getOutputValue();
|
||||
real scaleValue = scale_->getW()->getElement(0, 0);
|
||||
outV->mulScalar(*inV, scaleValue);
|
||||
if (offset_) {
|
||||
real offsetValue = offset_->getW()->getElement(0, 0);
|
||||
outV->add(offsetValue);
|
||||
}
|
||||
}
|
||||
|
||||
void ScaleShiftLayer::backward(const UpdateCallback& callback) {
|
||||
MatrixPtr inV = getInputValue(0);
|
||||
MatrixPtr inG = getInputGrad(0);
|
||||
MatrixPtr outV = getOutputValue();
|
||||
MatrixPtr outG = getOutputGrad();
|
||||
|
||||
/* Calculate the parameter gradient for the current layer */
|
||||
if (scale_->getWGrad()) {
|
||||
MatrixPtr rowSumMtx;
|
||||
Matrix::resizeOrCreate(rowSumMtx, outG->getHeight(), 1, false, useGpu_);
|
||||
// this_i = scaleDest * this_i + scaleSum * \sum_j b_{ij} * c_{ij}
|
||||
rowSumMtx->sumOfProducts(
|
||||
/* b= */ *inV, /* c= */ *outG, /* scaleSum= */ 1, /* scaleDest= */ 0.);
|
||||
// this_i = scaleDest * this_i + scaleSum * \sum_j b_{ji}
|
||||
scale_->getWGrad()->sumCols(
|
||||
/* b= */ *rowSumMtx, /* scaleSum= */ 1., /* scaleDest= */ 1.);
|
||||
scale_->getParameterPtr()->incUpdate(callback);
|
||||
}
|
||||
if (offset_ && offset_->getWGrad()) {
|
||||
MatrixPtr rowSumMtx;
|
||||
Matrix::resizeOrCreate(rowSumMtx, outG->getHeight(), 1, false, useGpu_);
|
||||
rowSumMtx->sumRows(*outG, 1., 0.);
|
||||
offset_->getWGrad()->sumCols(*rowSumMtx, 1., 1.);
|
||||
offset_->getParameterPtr()->incUpdate(callback);
|
||||
}
|
||||
|
||||
/* Calculate the input layers error */
|
||||
if (inG) {
|
||||
real scaleValue = scale_->getW()->getElement(0, 0);
|
||||
inG->add(*outG, scaleValue);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,72 @@
|
||||
/* 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/gather_op.h"
|
||||
#include "paddle/framework/ddim.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class GatherOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
int batch_size = ctx.Input<Tensor>("Index")->dims()[0];
|
||||
PADDLE_ENFORCE_GE(batch_size, 0, "Batch size must be >0");
|
||||
framework::DDim output_dims(ctx.Input<Tensor>("X")->dims());
|
||||
output_dims[0] = batch_size;
|
||||
ctx.Output<Tensor>("Out")->Resize(output_dims);
|
||||
}
|
||||
};
|
||||
|
||||
class GatherGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
auto X_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
|
||||
auto X = ctx.Input<Tensor>("X");
|
||||
|
||||
X_grad->Resize(X->dims());
|
||||
}
|
||||
};
|
||||
|
||||
class GatherOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
GatherOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "The source input of gather op");
|
||||
AddInput("Index", "The index input of gather op");
|
||||
AddOutput("Out", "The output of add op");
|
||||
AddComment(R"DOC(
|
||||
Gather Operator by selecting from the first axis,
|
||||
|
||||
Out = X[Index]
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(gather, ops::GatherOp, ops::GatherOpMaker, gather_grad,
|
||||
ops::GatherGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(gather,
|
||||
ops::GatherOpKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
gather_grad,
|
||||
ops::GatherGradientOpKernel<paddle::platform::CPUPlace, float>);
|
@ -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/gather_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(gather,
|
||||
ops::GatherOpKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,53 @@
|
||||
/* 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 "gather.h"
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "scatter.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class GatherOpKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *X = ctx.Input<Tensor>("X");
|
||||
auto *Index = ctx.Input<Tensor>("Index");
|
||||
auto *Y = ctx.Output<Tensor>("Out");
|
||||
|
||||
Y->mutable_data<T>(ctx.GetPlace());
|
||||
Gather<T>(ctx.GetPlace(), X, Index, Y);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class GatherGradientOpKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext &ctx) const override {
|
||||
auto *Index = ctx.Input<Tensor>("Index");
|
||||
auto *dX = ctx.Output<Tensor>(framework::GradVarName("X"));
|
||||
auto *dO = ctx.Input<Tensor>(framework::GradVarName("Out"));
|
||||
|
||||
dX->mutable_data<T>(ctx.GetPlace());
|
||||
ScatterUpdate<T>(ctx.GetPlace(), dO, Index, dX);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -1,53 +1,65 @@
|
||||
/* 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 <memory>
|
||||
#include <random>
|
||||
#include "paddle/platform/dynload/curand.h"
|
||||
#include "paddle/platform/gpu_info.h"
|
||||
|
||||
#include <thrust/device_ptr.h>
|
||||
#include <thrust/iterator/counting_iterator.h>
|
||||
#include <thrust/random.h>
|
||||
#include <thrust/transform.h>
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename T>
|
||||
class GaussianRandomKernel : public framework::OpKernel {
|
||||
struct GaussianGenerator {
|
||||
T mean_, std_;
|
||||
unsigned int seed_;
|
||||
|
||||
__host__ __device__ GaussianGenerator(T mean, T std, int seed)
|
||||
: mean_(mean), std_(std), seed_(seed) {}
|
||||
|
||||
__host__ __device__ T operator()(const unsigned int n) const {
|
||||
thrust::minstd_rand rng;
|
||||
rng.seed(seed_);
|
||||
thrust::normal_distribution<T> dist(mean_, std_);
|
||||
rng.discard(n);
|
||||
return dist(rng);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class GPUGaussianRandomKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
float mean = context.op_.GetAttr<float>("mean");
|
||||
float std = context.op_.GetAttr<float>("std");
|
||||
auto* tensor = context.Output<framework::Tensor>(0);
|
||||
auto* tensor = context.Output<framework::Tensor>("Out");
|
||||
T* data = tensor->mutable_data<T>(context.GetPlace());
|
||||
|
||||
int seed = context.op_.GetAttr<int>("seed");
|
||||
unsigned int seed =
|
||||
static_cast<unsigned int>(context.op_.GetAttr<int>("seed"));
|
||||
if (seed == 0) {
|
||||
std::random_device rd;
|
||||
seed = rd();
|
||||
}
|
||||
curandGenerator_t g;
|
||||
PADDLE_ENFORCE(platform::dynload::curandCreateGenerator(
|
||||
&g, CURAND_RNG_PSEUDO_DEFAULT));
|
||||
PADDLE_ENFORCE(
|
||||
platform::dynload::curandSetPseudoRandomGeneratorSeed(g, seed));
|
||||
platform::dynload::curandGenerateNormal(
|
||||
g, data, framework::product(tensor->dims()), mean, std);
|
||||
T mean = static_cast<T>(context.op_.GetAttr<float>("mean"));
|
||||
T std = static_cast<T>(context.op_.GetAttr<float>("std"));
|
||||
thrust::counting_iterator<unsigned int> index_sequence_begin(0);
|
||||
ssize_t N = framework::product(tensor->dims());
|
||||
thrust::transform(index_sequence_begin, index_sequence_begin + N,
|
||||
thrust::device_ptr<T>(data),
|
||||
GaussianGenerator<T>(mean, std, seed));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(gaussian_random, ops::GaussianRandomKernel<float>);
|
||||
REGISTER_OP_GPU_KERNEL(gaussian_random,
|
||||
paddle::operators::GPUGaussianRandomKernel<float>);
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue