Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into cmake_speed
commit
524ccba4fe
@ -0,0 +1,220 @@
|
||||
/* 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 "ROIPoolLayer.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_LAYER(roi_pool, ROIPoolLayer);
|
||||
|
||||
bool ROIPoolLayer::init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) {
|
||||
Layer::init(layerMap, parameterMap);
|
||||
|
||||
const ROIPoolConfig& layerConf = config_.inputs(0).roi_pool_conf();
|
||||
pooledWidth_ = layerConf.pooled_width();
|
||||
pooledHeight_ = layerConf.pooled_height();
|
||||
spatialScale_ = layerConf.spatial_scale();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ROIPoolLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
|
||||
const ROIPoolConfig& layerConf = config_.inputs(0).roi_pool_conf();
|
||||
height_ = getInput(0).getFrameHeight();
|
||||
if (!height_) height_ = layerConf.height();
|
||||
width_ = getInput(0).getFrameWidth();
|
||||
if (!width_) width_ = layerConf.width();
|
||||
channels_ = getInputValue(0)->getWidth() / width_ / height_;
|
||||
|
||||
size_t batchSize = getInput(0).getBatchSize();
|
||||
size_t numROIs = getInput(1).getBatchSize();
|
||||
|
||||
MatrixPtr dataValue = getInputValue(0);
|
||||
MatrixPtr roiValue = getInputValue(1);
|
||||
resetOutput(numROIs, channels_ * pooledHeight_ * pooledWidth_);
|
||||
MatrixPtr outputValue = getOutputValue();
|
||||
|
||||
if (useGpu_) { // TODO(guosheng): implement on GPU later
|
||||
MatrixPtr dataCpuBuffer;
|
||||
Matrix::resizeOrCreate(dataCpuBuffer,
|
||||
dataValue->getHeight(),
|
||||
dataValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
MatrixPtr roiCpuBuffer;
|
||||
Matrix::resizeOrCreate(roiCpuBuffer,
|
||||
roiValue->getHeight(),
|
||||
roiValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
dataCpuBuffer->copyFrom(*dataValue);
|
||||
roiCpuBuffer->copyFrom(*roiValue);
|
||||
dataValue = dataCpuBuffer;
|
||||
roiValue = roiCpuBuffer;
|
||||
MatrixPtr outputCpuBuffer;
|
||||
Matrix::resizeOrCreate(outputCpuBuffer,
|
||||
outputValue->getHeight(),
|
||||
outputValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
outputCpuBuffer->copyFrom(*outputValue);
|
||||
outputValue = outputCpuBuffer;
|
||||
}
|
||||
|
||||
real* bottomData = dataValue->getData();
|
||||
size_t batchOffset = dataValue->getWidth();
|
||||
size_t channelOffset = height_ * width_;
|
||||
real* bottomROIs = roiValue->getData();
|
||||
size_t roiOffset = roiValue->getWidth();
|
||||
size_t poolChannelOffset = pooledHeight_ * pooledWidth_;
|
||||
|
||||
real* outputData = outputValue->getData();
|
||||
Matrix::resizeOrCreate(maxIdxs_,
|
||||
numROIs,
|
||||
channels_ * pooledHeight_ * pooledWidth_,
|
||||
false,
|
||||
false);
|
||||
real* argmaxData = maxIdxs_->getData();
|
||||
|
||||
for (size_t n = 0; n < numROIs; ++n) {
|
||||
// the first five elememts of each RoI should be:
|
||||
// batch_idx, roi_x_start, roi_y_start, roi_x_end, roi_y_end
|
||||
size_t roiBatchIdx = bottomROIs[0];
|
||||
size_t roiStartW = round(bottomROIs[1] * spatialScale_);
|
||||
size_t roiStartH = round(bottomROIs[2] * spatialScale_);
|
||||
size_t roiEndW = round(bottomROIs[3] * spatialScale_);
|
||||
size_t roiEndH = round(bottomROIs[4] * spatialScale_);
|
||||
CHECK_GE(roiBatchIdx, 0);
|
||||
CHECK_LT(roiBatchIdx, batchSize);
|
||||
size_t roiHeight = std::max(roiEndH - roiStartH + 1, 1UL);
|
||||
size_t roiWidth = std::max(roiEndW - roiStartW + 1, 1UL);
|
||||
real binSizeH =
|
||||
static_cast<real>(roiHeight) / static_cast<real>(pooledHeight_);
|
||||
real binSizeW =
|
||||
static_cast<real>(roiWidth) / static_cast<real>(pooledWidth_);
|
||||
real* batchData = bottomData + batchOffset * roiBatchIdx;
|
||||
for (size_t c = 0; c < channels_; ++c) {
|
||||
for (size_t ph = 0; ph < pooledHeight_; ++ph) {
|
||||
for (size_t pw = 0; pw < pooledWidth_; ++pw) {
|
||||
size_t hstart = static_cast<size_t>(std::floor(ph * binSizeH));
|
||||
size_t wstart = static_cast<size_t>(std::floor(pw * binSizeW));
|
||||
size_t hend = static_cast<size_t>(std::ceil((ph + 1) * binSizeH));
|
||||
size_t wend = static_cast<size_t>(std::ceil((pw + 1) * binSizeW));
|
||||
hstart = std::min(std::max(hstart + roiStartH, 0UL), height_);
|
||||
wstart = std::min(std::max(wstart + roiStartW, 0UL), width_);
|
||||
hend = std::min(std::max(hend + roiStartH, 0UL), height_);
|
||||
wend = std::min(std::max(wend + roiStartW, 0UL), width_);
|
||||
|
||||
bool isEmpty = (hend <= hstart) || (wend <= wstart);
|
||||
size_t poolIndex = ph * pooledWidth_ + pw;
|
||||
if (isEmpty) {
|
||||
outputData[poolIndex] = 0;
|
||||
argmaxData[poolIndex] = -1;
|
||||
}
|
||||
|
||||
for (size_t h = hstart; h < hend; ++h) {
|
||||
for (size_t w = wstart; w < wend; ++w) {
|
||||
size_t index = h * width_ + w;
|
||||
if (batchData[index] > outputData[poolIndex]) {
|
||||
outputData[poolIndex] = batchData[index];
|
||||
argmaxData[poolIndex] = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
batchData += channelOffset;
|
||||
outputData += poolChannelOffset;
|
||||
argmaxData += poolChannelOffset;
|
||||
}
|
||||
bottomROIs += roiOffset;
|
||||
}
|
||||
if (useGpu_) {
|
||||
getOutputValue()->copyFrom(*outputValue);
|
||||
}
|
||||
}
|
||||
|
||||
void ROIPoolLayer::backward(const UpdateCallback& callback) {
|
||||
MatrixPtr inGradValue = getInputGrad(0);
|
||||
MatrixPtr outGradValue = getOutputGrad();
|
||||
MatrixPtr roiValue = getInputValue(1);
|
||||
|
||||
if (useGpu_) {
|
||||
MatrixPtr inGradCpuBuffer;
|
||||
Matrix::resizeOrCreate(inGradCpuBuffer,
|
||||
inGradValue->getHeight(),
|
||||
inGradValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
MatrixPtr outGradCpuBuffer;
|
||||
Matrix::resizeOrCreate(outGradCpuBuffer,
|
||||
outGradValue->getHeight(),
|
||||
outGradValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
MatrixPtr roiCpuBuffer;
|
||||
Matrix::resizeOrCreate(roiCpuBuffer,
|
||||
roiValue->getHeight(),
|
||||
roiValue->getWidth(),
|
||||
false,
|
||||
false);
|
||||
inGradCpuBuffer->copyFrom(*inGradValue);
|
||||
outGradCpuBuffer->copyFrom(*outGradValue);
|
||||
roiCpuBuffer->copyFrom(*roiValue);
|
||||
inGradValue = inGradCpuBuffer;
|
||||
outGradValue = outGradCpuBuffer;
|
||||
roiValue = roiCpuBuffer;
|
||||
}
|
||||
|
||||
real* bottomROIs = roiValue->getData();
|
||||
size_t numROIs = getInput(1).getBatchSize();
|
||||
size_t roiOffset = getInputValue(1)->getWidth();
|
||||
|
||||
real* inDiffData = inGradValue->getData();
|
||||
size_t batchOffset = getInputValue(0)->getWidth();
|
||||
size_t channelOffset = height_ * width_;
|
||||
|
||||
real* outDiffData = outGradValue->getData();
|
||||
size_t poolChannelOffset = pooledHeight_ * pooledWidth_;
|
||||
real* argmaxData = maxIdxs_->getData();
|
||||
|
||||
for (size_t n = 0; n < numROIs; ++n) {
|
||||
size_t roiBatchIdx = bottomROIs[0];
|
||||
real* batchDiffData = inDiffData + batchOffset * roiBatchIdx;
|
||||
for (size_t c = 0; c < channels_; ++c) {
|
||||
for (size_t ph = 0; ph < pooledHeight_; ++ph) {
|
||||
for (size_t pw = 0; pw < pooledWidth_; ++pw) {
|
||||
size_t poolIndex = ph * pooledWidth_ + pw;
|
||||
if (argmaxData[poolIndex] > 0) {
|
||||
size_t index = static_cast<size_t>(argmaxData[poolIndex]);
|
||||
batchDiffData[index] += outDiffData[poolIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
batchDiffData += channelOffset;
|
||||
outDiffData += poolChannelOffset;
|
||||
argmaxData += poolChannelOffset;
|
||||
}
|
||||
bottomROIs += roiOffset;
|
||||
}
|
||||
|
||||
if (useGpu_) {
|
||||
getInputGrad(0)->copyFrom(*inGradValue);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -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 "Layer.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
/**
|
||||
* A layer used by Fast R-CNN to extract feature maps of ROIs from the last
|
||||
* feature map.
|
||||
* - Input: This layer needs two input layers: The first input layer is a
|
||||
* convolution layer; The second input layer contains the ROI data
|
||||
* which is the output of ProposalLayer in Faster R-CNN. layers for
|
||||
* generating bbox location offset and the classification confidence.
|
||||
* - Output: The ROIs' feature map.
|
||||
* Reference:
|
||||
* Shaoqing Ren, Kaiming He, Ross Girshick, and Jian Sun.
|
||||
* Faster R-CNN: Towards Real-Time Object Detection with Region Proposal
|
||||
* Networks
|
||||
*/
|
||||
|
||||
class ROIPoolLayer : public Layer {
|
||||
protected:
|
||||
size_t channels_;
|
||||
size_t width_;
|
||||
size_t height_;
|
||||
size_t pooledWidth_;
|
||||
size_t pooledHeight_;
|
||||
real spatialScale_;
|
||||
|
||||
// Since there is no int matrix, use real maxtrix instead.
|
||||
MatrixPtr maxIdxs_;
|
||||
|
||||
public:
|
||||
explicit ROIPoolLayer(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;
|
||||
};
|
||||
} // namespace paddle
|
@ -0,0 +1,136 @@
|
||||
/* 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/expand_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using framework::Tensor;
|
||||
|
||||
class ExpandOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null.");
|
||||
|
||||
std::vector<int> expand_times =
|
||||
ctx->Attrs().Get<std::vector<int>>("expand_times");
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
|
||||
PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dims.size()), expand_times.size(),
|
||||
"The number of Attr(expand_times)'s value must be equal "
|
||||
"to the rank of Input(X).");
|
||||
PADDLE_ENFORCE_LE(x_dims.size(), 6,
|
||||
"The rank of Input(X) must not be greater than 6.");
|
||||
|
||||
std::vector<int64_t> out_shape(x_dims.size());
|
||||
for (size_t i = 0; i < expand_times.size(); ++i) {
|
||||
PADDLE_ENFORCE_GE(expand_times[i], 1,
|
||||
"Each value of Attr(expand_times) should not be "
|
||||
"less than 1.");
|
||||
out_shape[i] = x_dims[i] * expand_times[i];
|
||||
}
|
||||
|
||||
ctx->SetOutputDim("Out", framework::make_ddim(out_shape));
|
||||
if (out_shape[0] == x_dims[0]) {
|
||||
ctx->ShareLoD("X", "Out");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class ExpandOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
ExpandOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X",
|
||||
"(Tensor, default Tensor<float>) A tensor with rank in [1, 6]."
|
||||
"X is the input tensor to be expanded.");
|
||||
AddOutput("Out",
|
||||
"(Tensor, default Tensor<float>) A tensor with rank in [1, 6]."
|
||||
"The rank of Output(Out) is same as Input(X) except that each "
|
||||
"dimension size of Output(Out) is equal to corresponding "
|
||||
"dimension size of Input(X) multiplying corresponding value of "
|
||||
"Attr(expand_times).");
|
||||
AddAttr<std::vector<int>>("expand_times",
|
||||
"Expand times number for each dimension.");
|
||||
AddComment(R"DOC(
|
||||
Expand operator tiles the input by given times number. You should set times
|
||||
number for each dimension by providing attribute 'expand_times'. The rank of X
|
||||
should be in [1, 6]. Please notice that size of 'expand_times' must be same with
|
||||
X's rank. Following is a using case:
|
||||
|
||||
Input(X) is a 3-D tensor with shape [2, 3, 1]:
|
||||
|
||||
[
|
||||
[[1], [2], [3]],
|
||||
[[4], [5], [6]]
|
||||
]
|
||||
|
||||
Attr(expand_times): [1, 2, 2]
|
||||
|
||||
Output(Out) is a 3-D tensor with shape [2, 6, 2]:
|
||||
|
||||
[
|
||||
[[1, 1], [2, 2], [3, 3], [1, 1], [2, 2], [3, 3]],
|
||||
[[4, 4], [5, 5], [6, 6], [4, 4], [5, 5], [6, 6]]
|
||||
]
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
class ExpandGradOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
|
||||
"Input(Out@GRAD) should not be null.");
|
||||
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
std::vector<int> expand_times =
|
||||
ctx->Attrs().Get<std::vector<int>>("expand_times");
|
||||
auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
|
||||
|
||||
for (size_t i = 0; i < expand_times.size(); ++i) {
|
||||
PADDLE_ENFORCE_EQ(x_dims[i] * expand_times[i], out_dims[i],
|
||||
"Each dimension size of Input(Out@GRAD) should be "
|
||||
"equal to multiplication of crroresponding dimension "
|
||||
"size of Input(X) and Attr(expand_times) value.");
|
||||
}
|
||||
|
||||
auto x_grad_name = framework::GradVarName("X");
|
||||
|
||||
if (ctx->HasOutput(x_grad_name)) {
|
||||
ctx->SetOutputDim(x_grad_name, x_dims);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(expand, ops::ExpandOp, ops::ExpandOpMaker, expand_grad,
|
||||
ops::ExpandGradOp);
|
||||
REGISTER_OP_CPU_KERNEL(expand,
|
||||
ops::ExpandKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
expand_grad, ops::ExpandGradKernel<paddle::platform::CPUPlace, float>);
|
@ -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/operators/expand_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(expand,
|
||||
ops::ExpandKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
expand_grad, ops::ExpandGradKernel<paddle::platform::GPUPlace, float>);
|
@ -0,0 +1,172 @@
|
||||
/* 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 <boost/preprocessor/arithmetic/div.hpp>
|
||||
#include <boost/preprocessor/arithmetic/mod.hpp>
|
||||
#include <boost/preprocessor/comparison/greater.hpp>
|
||||
#include <boost/preprocessor/comparison/greater_equal.hpp>
|
||||
#include <boost/preprocessor/control/if.hpp>
|
||||
#include <boost/preprocessor/repetition/repeat.hpp>
|
||||
#include <iostream>
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
|
||||
#define MAX_RANK_SUPPORTED 6
|
||||
|
||||
#define EXPAND_TEMPLATE(z, n, data) \
|
||||
case n + 1: { \
|
||||
Expand<n + 1>(context); \
|
||||
break; \
|
||||
}
|
||||
#define REP_EXPAND_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_TEMPLATE, ~)
|
||||
#define COND(n) \
|
||||
BOOST_PP_GREATER_EQUAL(BOOST_PP_DIV(n, MAX_RANK_SUPPORTED), \
|
||||
BOOST_PP_MOD(n, MAX_RANK_SUPPORTED))
|
||||
#define EXPAND_GRAD_CASE(n) \
|
||||
case n: { \
|
||||
ExpandBackward<n>(context, reshape_dims_vec, reduce_dims_vec); \
|
||||
break; \
|
||||
}
|
||||
#define EXPAND_GRAD_TEMPLATE(z, n, data) \
|
||||
BOOST_PP_IF(COND(n), EXPAND_GRAD_CASE(n), )
|
||||
#define REP_EXPAND_GRAD_TEMPLATE(n) BOOST_PP_REPEAT(n, EXPAND_GRAD_TEMPLATE, ~)
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
||||
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class ExpandKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto rank = context.Input<Tensor>("X")->dims().size();
|
||||
switch (rank) {
|
||||
REP_EXPAND_TEMPLATE(MAX_RANK_SUPPORTED)
|
||||
default:
|
||||
PADDLE_ENFORCE(false,
|
||||
"Only support tensor with rank being between 1 and 6.");
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
template <int Rank>
|
||||
void Expand(const framework::ExecutionContext& context) const {
|
||||
auto* in0 = context.Input<Tensor>("X");
|
||||
auto& expand_times = context.Attr<std::vector<int>>("expand_times");
|
||||
auto* out0 = context.Output<Tensor>("Out");
|
||||
Eigen::DSizes<int, Rank> bcast_dims;
|
||||
auto x_dims = in0->dims();
|
||||
for (size_t i = 0; i < expand_times.size(); ++i) {
|
||||
bcast_dims[i] = expand_times[i];
|
||||
}
|
||||
auto x = EigenTensor<T, Rank>::From(*in0);
|
||||
out0->mutable_data<T>(context.GetPlace());
|
||||
auto y = EigenTensor<T, Rank>::From(*out0);
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
y.device(place) = x.broadcast(bcast_dims);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class ExpandGradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto* in0 = context.Input<Tensor>("X");
|
||||
auto& expand_times = context.Attr<std::vector<int>>("expand_times");
|
||||
auto x_dims = in0->dims();
|
||||
// 1. reshape_dims_vec is the broadcast parameter. For each dimension i,
|
||||
// if expand_times[i] > 1 and x_dims[i] > 1, i will be splitted to two
|
||||
// dimensions [expand_times[i], x_dims[i]].
|
||||
// 2. reduce_dims_vec is the dimension parameter to compute gradients. For
|
||||
// each dimension expanded, the gradients should be summed to original
|
||||
// size.
|
||||
std::vector<int> reshape_dims_vec;
|
||||
std::vector<int> reduce_dims_vec;
|
||||
for (size_t i = 0; i < expand_times.size(); ++i) {
|
||||
if (expand_times[i] == 1) {
|
||||
reshape_dims_vec.push_back(x_dims[i]);
|
||||
} else {
|
||||
if (x_dims[i] == 1) {
|
||||
reduce_dims_vec.push_back(reshape_dims_vec.size());
|
||||
reshape_dims_vec.push_back(expand_times[i]);
|
||||
} else {
|
||||
reduce_dims_vec.push_back(reshape_dims_vec.size());
|
||||
reshape_dims_vec.push_back(expand_times[i]);
|
||||
reshape_dims_vec.push_back(x_dims[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int dims = reshape_dims_vec.size() * MAX_RANK_SUPPORTED +
|
||||
reduce_dims_vec.size() - MAX_RANK_SUPPORTED - 1;
|
||||
// no need reduce, just copy
|
||||
if (reduce_dims_vec.size() == 0) {
|
||||
auto* in0 = context.Input<Tensor>(framework::GradVarName("Out"));
|
||||
auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
|
||||
out0->mutable_data<T>(context.GetPlace());
|
||||
out0->CopyFrom(*in0, context.GetPlace(), context.device_context());
|
||||
} else {
|
||||
switch (dims) {
|
||||
REP_EXPAND_GRAD_TEMPLATE(72)
|
||||
default:
|
||||
PADDLE_ENFORCE(
|
||||
false, "Only support tensor with rank being between 1 and 6.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
template <int Dims>
|
||||
void ExpandBackward(const framework::ExecutionContext& context,
|
||||
const std::vector<int>& reshape_dims_vec,
|
||||
const std::vector<int>& reduce_dims_vec) const {
|
||||
size_t reshape_size = Dims / MAX_RANK_SUPPORTED + 1;
|
||||
size_t reduce_size = Dims % MAX_RANK_SUPPORTED + 1;
|
||||
PADDLE_ENFORCE_EQ(reshape_size, reshape_dims_vec.size(),
|
||||
"Inconsistent size between template Dims and "
|
||||
"reshape dimensions.");
|
||||
PADDLE_ENFORCE_EQ(reduce_size, reduce_dims_vec.size(),
|
||||
"Inconsistent size between template Dims and "
|
||||
"reduce dimensions.");
|
||||
auto* in0 = context.Input<Tensor>(framework::GradVarName("Out"));
|
||||
auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
|
||||
auto x = EigenVector<T>::Flatten(*(context.Input<Tensor>("X")));
|
||||
out0->mutable_data<T>(context.GetPlace());
|
||||
auto x_grad = EigenVector<T>::Flatten(*out0);
|
||||
Eigen::DSizes<int, Dims / MAX_RANK_SUPPORTED + 1> reshape_dims;
|
||||
for (size_t i = 0; i < reshape_size; ++i) {
|
||||
reshape_dims[i] = reshape_dims_vec[i];
|
||||
}
|
||||
Eigen::DSizes<int, Dims % MAX_RANK_SUPPORTED + 1> reduce_dims;
|
||||
for (size_t i = 0; i < reduce_size; ++i) {
|
||||
reduce_dims[i] = reduce_dims_vec[i];
|
||||
}
|
||||
auto out_grad = EigenVector<T>::Flatten(*in0);
|
||||
x_grad.device(context.GetEigenDevice<Place>()) =
|
||||
out_grad.reshape(reshape_dims).sum(reduce_dims).reshape(x.dimensions());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,50 @@
|
||||
/* 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 <mutex>
|
||||
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
/*
|
||||
The current implementation of std::call_once has a bug described in
|
||||
https://stackoverflow.com/questions/41717579/stdcall-once-hangs-on-second-call-after-callable-threw-on-first-call.
|
||||
This is likely caused by a deeper bug of pthread_once, which is discussed in
|
||||
https://patchwork.ozlabs.org/patch/482350/
|
||||
|
||||
This wrap is a hack to avoid this bug.
|
||||
*/
|
||||
template <class Callable, class... Args>
|
||||
inline void call_once(std::once_flag& flag, Callable&& f, Args&&... args) {
|
||||
bool good = false;
|
||||
std::exception ex;
|
||||
std::call_once(flag, [&]() {
|
||||
try {
|
||||
f(args...);
|
||||
good = true;
|
||||
} catch (const std::exception& e) {
|
||||
ex = e;
|
||||
} catch (...) {
|
||||
ex = std::runtime_error("excption caught in call_once");
|
||||
}
|
||||
});
|
||||
if (!good) {
|
||||
throw std::exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue