Skip to content

Commit

Permalink
Merge pull request #5499 from reyoung/feature/increase_cpu
Browse files Browse the repository at this point in the history
Feature/increase cpu
  • Loading branch information
reyoung committed Nov 9, 2017
2 parents 78fb29b + 9c10db3 commit 133f890
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 128 deletions.
5 changes: 5 additions & 0 deletions paddle/framework/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ std::vector<std::unique_ptr<OpDescBind>> MakeBlockBackward(

for (const auto& desc : op_grads) {
for (const std::string& out_name : desc->OutputArgumentNames()) {
if (out_name.find("@GRAD") == std::string::npos) {
// Not all outputs of a backward operator is a gradient. Only gradient
// need to be sum. Skip variables are not gradient.
continue;
}
dup_out_ops[out_name].emplace_back(grad_desc_idx);
}
++grad_desc_idx;
Expand Down
65 changes: 47 additions & 18 deletions paddle/operators/increment_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,57 @@
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/increment_op.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

class IncrementOp : public framework::OperatorWithKernel {
class IncrementInferShape : public framework::InferShapeBase {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
void operator()(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"),
"Input(X) of IncrementOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of IncrementOp should not be null.");
PADDLE_ENFORCE_EQ(1, framework::product(ctx->GetInputDim("X")));
ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
ctx->ShareLoD("X", /*->*/ "Out");
}
};

struct IncrementFunctor {
IncrementFunctor(const framework::LoDTensor &x, framework::LoDTensor *out,
float value)
: x_(x), out_(out), value_(value) {}

template <typename T>
void operator()() const {
*out_->data<T>() = *x_.data<T>() + static_cast<T>(value_);
}

const framework::LoDTensor &x_;
framework::LoDTensor *out_;
float value_;
};

class IncrementOp : public framework::OperatorBase {
public:
IncrementOp(const std::string &type, const framework::VariableNameMap &inputs,
const framework::VariableNameMap &outputs,
const framework::AttributeMap &attrs)
: OperatorBase(type, inputs, outputs, attrs) {}

void Run(const framework::Scope &scope,
const platform::DeviceContext &dev_ctx) const override {
auto &x = scope.FindVar(Input("X"))->Get<framework::LoDTensor>();
auto &out =
*scope.FindVar(Output("Out"))->GetMutable<framework::LoDTensor>();

PADDLE_ENFORCE(platform::is_cpu_place(x.place()));
out.Resize(x.dims());
out.mutable_data(x.place(), x.type());
float value = Attr<float>("step");
framework::VisitDataType(framework::ToDataType(out.type()),
IncrementFunctor(x, &out, value));
}
};

Expand Down Expand Up @@ -59,10 +94,10 @@ class IncrementGradOpMaker : public framework::SingleGradOpDescMaker {

std::unique_ptr<framework::OpDescBind> Apply() const override {
auto *grad_op = new framework::OpDescBind();
grad_op->SetType("scale");
grad_op->SetInput("X", OutputGrad("Out"));
grad_op->SetOutput("Out", InputGrad("X"));
grad_op->SetAttr("scale", 1.0f);
grad_op->SetType("increment");
grad_op->SetInput("X", Output("Out"));
grad_op->SetOutput("Out", Input("X"));
grad_op->SetAttr("step", -boost::get<float>(GetAttr("step")));
return std::unique_ptr<framework::OpDescBind>(grad_op);
}
};
Expand All @@ -71,11 +106,5 @@ class IncrementGradOpMaker : public framework::SingleGradOpDescMaker {
} // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(increment, ops::IncrementOp, ops::IncrementOpMaker,
ops::IncrementGradOpMaker);
REGISTER_OP_CPU_KERNEL(
increment, ops::IncrementKernel<paddle::platform::CPUPlace, float>,
ops::IncrementKernel<paddle::platform::CPUPlace, double>,
ops::IncrementKernel<paddle::platform::CPUPlace, int>,
ops::IncrementKernel<paddle::platform::CPUPlace, int64_t>);
REGISTER_OPERATOR(increment, ops::IncrementOp, ops::IncrementInferShape,
ops::IncrementOpMaker, ops::IncrementGradOpMaker);
22 changes: 0 additions & 22 deletions paddle/operators/increment_op.cu

This file was deleted.

40 changes: 0 additions & 40 deletions paddle/operators/increment_op.h

This file was deleted.

9 changes: 6 additions & 3 deletions python/paddle/v2/framework/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ def array_to_lod_tensor(x, table, main_program=None):


def fill_constant(shape, dtype, value, main_program=None):
helper = LayerHelper("ones", **locals())
helper = LayerHelper("fill_constant", **locals())
out = helper.create_tmp_variable(dtype=dtype)
helper.append_op(
type='fill_constant',
Expand All @@ -895,9 +895,12 @@ def zeros(shape, dtype, main_program=None):
return fill_constant(value=0.0, **locals())


def increment(x, value=1.0, main_program=None):
def increment(x, value=1.0, in_place=True, main_program=None):
helper = LayerHelper("increment", **locals())
out = helper.create_tmp_variable(dtype=x.data_type)
if in_place:
out = x
else:
out = helper.create_tmp_variable(dtype=x.data_type)
helper.append_op(
type='increment',
inputs={'X': [x]},
Expand Down
6 changes: 2 additions & 4 deletions python/paddle/v2/framework/tests/test_array_read_write_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ def test_read_write(self):
each_x.stop_gradient = False

i = layers.zeros(shape=[1], dtype='int64')
i.stop_gradient = False
arr = layers.array_write(x=x[0], i=i)
i = layers.increment(x=i)
i.stop_gradient = True
arr = layers.array_write(x=x[1], i=i, array=arr)
i = layers.increment(x=i)
i.stop_gradient = True
arr = layers.array_write(x=x[2], i=i, array=arr)

i = layers.zeros(shape=[1], dtype='int64')
i.stop_gradient = False
a0 = layers.array_read(array=arr, i=i)
i = layers.increment(x=i)
i.stop_gradient = True # index should not calculate gradient
a1 = layers.array_read(array=arr, i=i)
i = layers.increment(x=i)
i.stop_gradient = True
a2 = layers.array_read(array=arr, i=i)

mean_a0 = layers.mean(x=a0)
Expand Down
41 changes: 0 additions & 41 deletions python/paddle/v2/framework/tests/test_increment_op.py

This file was deleted.

0 comments on commit 133f890

Please sign in to comment.