ReadFromArray/WriteToArray op (#5407)
* Use stable_sort in lod_rank_table It is easy to debug and test when use `stable_sort`and the time complexity is not changed. * Add LoDTensorArray * Stash * Better debug message for IsInitialized * Stash * Better debug message for IsInitialized * Complete array read/write op unittestsmobile_baidu
parent
f78731c837
commit
c9b57dcc83
@ -0,0 +1,219 @@
|
|||||||
|
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
|
||||||
|
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/framework/lod_tensor_array.h"
|
||||||
|
#include "paddle/framework/op_registry.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
class ArrayOpBase : public framework::OperatorBase {
|
||||||
|
public:
|
||||||
|
ArrayOpBase(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 {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
size_t GetOffset(const framework::Scope &scope,
|
||||||
|
const platform::DeviceContext &dev_ctx) const {
|
||||||
|
auto *i = scope.FindVar(Input("I"));
|
||||||
|
PADDLE_ENFORCE(i != nullptr, "I must be set");
|
||||||
|
auto &i_tensor = i->Get<framework::LoDTensor>();
|
||||||
|
PADDLE_ENFORCE_EQ(i_tensor.numel(), 1);
|
||||||
|
size_t offset;
|
||||||
|
if (platform::is_gpu_place(i_tensor.place())) {
|
||||||
|
// FIXME: Avoid copy from GPU to CPU
|
||||||
|
framework::Tensor t;
|
||||||
|
t.CopyFrom(i_tensor, platform::CPUPlace(), dev_ctx);
|
||||||
|
dev_ctx.Wait();
|
||||||
|
offset = static_cast<size_t>(*t.data<int64_t>());
|
||||||
|
} else {
|
||||||
|
offset = static_cast<size_t>(*i_tensor.data<int64_t>());
|
||||||
|
}
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class WriteToArrayOp : public ArrayOpBase {
|
||||||
|
public:
|
||||||
|
WriteToArrayOp(const std::string &type,
|
||||||
|
const framework::VariableNameMap &inputs,
|
||||||
|
const framework::VariableNameMap &outputs,
|
||||||
|
const framework::AttributeMap &attrs)
|
||||||
|
: ArrayOpBase(type, inputs, outputs, attrs) {}
|
||||||
|
|
||||||
|
void Run(const framework::Scope &scope,
|
||||||
|
const platform::DeviceContext &dev_ctx) const override {
|
||||||
|
auto *x = scope.FindVar(Input("X"));
|
||||||
|
PADDLE_ENFORCE(x != nullptr, "X must be set");
|
||||||
|
auto &x_tensor = x->Get<framework::LoDTensor>();
|
||||||
|
size_t offset = GetOffset(scope, dev_ctx);
|
||||||
|
auto *out =
|
||||||
|
scope.FindVar(Output("Out"))->GetMutable<framework::LoDTensorArray>();
|
||||||
|
if (offset >= out->size()) {
|
||||||
|
out->resize(offset + 1);
|
||||||
|
}
|
||||||
|
auto *out_tensor = &out->at(offset);
|
||||||
|
out_tensor->CopyFrom(x_tensor, dev_ctx.GetPlace(), dev_ctx);
|
||||||
|
out_tensor->set_lod(x_tensor.lod());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class WriteToArrayOpProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
WriteToArrayOpProtoMaker(framework::OpProto *proto,
|
||||||
|
framework::OpAttrChecker *op_checker)
|
||||||
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "(LoDTensor) the tensor will be written to tensor array");
|
||||||
|
AddInput(
|
||||||
|
"I",
|
||||||
|
"(Tensor) the subscript index in tensor array. The number of element "
|
||||||
|
"should be 1");
|
||||||
|
AddOutput("Out", "(TensorArray) the tensor array will be written");
|
||||||
|
AddComment(R"DOC(Write a LoDTensor to a LoDTensor array.
|
||||||
|
|
||||||
|
Assume T is LoDTensor, i is the subscript of the array, and A is the array. The
|
||||||
|
equation is
|
||||||
|
|
||||||
|
A[i] = T
|
||||||
|
)DOC");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class WriteToArrayInferShape : public framework::InferShapeBase {
|
||||||
|
public:
|
||||||
|
void operator()(framework::InferShapeContext *context) const override {
|
||||||
|
PADDLE_ENFORCE(context->HasInput("I"), "Must set the subscript index");
|
||||||
|
PADDLE_ENFORCE_EQ(framework::product(context->GetInputDim("I")), 1,
|
||||||
|
"The number of element of subscript index must be 1");
|
||||||
|
PADDLE_ENFORCE(context->HasInput("X"), NotHasXError());
|
||||||
|
PADDLE_ENFORCE(context->HasOutput("Out"), NotHasOutError());
|
||||||
|
context->SetOutputDim("Out", context->GetInputDim("X"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual const char *NotHasXError() const { return "Must set the lod tensor"; }
|
||||||
|
|
||||||
|
virtual const char *NotHasOutError() const {
|
||||||
|
return "Must set the lod tensor array";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class WriteToArrayInferVarType : public framework::VarTypeInference {
|
||||||
|
public:
|
||||||
|
void operator()(const framework::OpDescBind &op_desc,
|
||||||
|
framework::BlockDescBind *block) const override {
|
||||||
|
VLOG(10) << "I am here?";
|
||||||
|
for (auto &out_var : op_desc.OutputArgumentNames()) {
|
||||||
|
VLOG(10) << "Set Variable " << out_var << " as LOD_TENSOR_ARRAY";
|
||||||
|
block->Var(out_var)->SetType(framework::VarDesc::LOD_TENSOR_ARRAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReadFromArrayOp : public ArrayOpBase {
|
||||||
|
public:
|
||||||
|
ReadFromArrayOp(const std::string &type,
|
||||||
|
const framework::VariableNameMap &inputs,
|
||||||
|
const framework::VariableNameMap &outputs,
|
||||||
|
const framework::AttributeMap &attrs)
|
||||||
|
: ArrayOpBase(type, inputs, outputs, attrs) {}
|
||||||
|
void Run(const framework::Scope &scope,
|
||||||
|
const platform::DeviceContext &dev_ctx) const override {
|
||||||
|
auto *x = scope.FindVar(Input("X"));
|
||||||
|
PADDLE_ENFORCE(x != nullptr, "X must be set");
|
||||||
|
auto &x_array = x->Get<framework::LoDTensorArray>();
|
||||||
|
auto *out = scope.FindVar(Output("Out"));
|
||||||
|
PADDLE_ENFORCE(out != nullptr, "Out must be set");
|
||||||
|
auto *out_tesnor = out->GetMutable<framework::LoDTensor>();
|
||||||
|
size_t offset = GetOffset(scope, dev_ctx);
|
||||||
|
PADDLE_ENFORCE_LT(offset, x_array.size());
|
||||||
|
out_tesnor->CopyFrom(x_array[offset], dev_ctx.GetPlace(), dev_ctx);
|
||||||
|
out_tesnor->set_lod(x_array[offset].lod());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReadFromArrayProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
ReadFromArrayProtoMaker(framework::OpProto *proto,
|
||||||
|
framework::OpAttrChecker *op_checker)
|
||||||
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "(TensorArray) the array will be read from.");
|
||||||
|
AddInput("I",
|
||||||
|
"(Tensor) the subscript index in tensor array. The number of "
|
||||||
|
"element should be 1");
|
||||||
|
AddOutput("Out", "(LoDTensor) the tensor will be read from.");
|
||||||
|
AddComment(R"DOC(Read a LoDTensor from a LoDTensor Array
|
||||||
|
|
||||||
|
Assume T is LoDTensor, i is th e subscript of the array, and A is the array. The
|
||||||
|
equation is
|
||||||
|
|
||||||
|
T = A[i]
|
||||||
|
)DOC");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReadFromArrayInferShape : public WriteToArrayInferShape {
|
||||||
|
protected:
|
||||||
|
const char *NotHasXError() const override {
|
||||||
|
return "The input array X must be set";
|
||||||
|
}
|
||||||
|
const char *NotHasOutError() const override {
|
||||||
|
return "The output tensor out must be set";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class WriteToArrayGradMaker : public framework::SingleGradOpDescMaker {
|
||||||
|
public:
|
||||||
|
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::unique_ptr<framework::OpDescBind> Apply() const override {
|
||||||
|
auto *grad_op = new framework::OpDescBind();
|
||||||
|
grad_op->SetType("read_from_array");
|
||||||
|
grad_op->SetInput("I", Input("I"));
|
||||||
|
grad_op->SetInput("X", OutputGrad("Out"));
|
||||||
|
grad_op->SetOutput("Out", InputGrad("X"));
|
||||||
|
grad_op->SetAttrMap(Attrs());
|
||||||
|
return std::unique_ptr<framework::OpDescBind>(grad_op);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReadFromArrayGradMaker : public framework::SingleGradOpDescMaker {
|
||||||
|
public:
|
||||||
|
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::unique_ptr<framework::OpDescBind> Apply() const override {
|
||||||
|
auto *grad_op = new framework::OpDescBind();
|
||||||
|
grad_op->SetType("write_to_array");
|
||||||
|
grad_op->SetInput("I", Input("I"));
|
||||||
|
grad_op->SetInput("X", OutputGrad("Out"));
|
||||||
|
grad_op->SetOutput("Out", InputGrad("X"));
|
||||||
|
grad_op->SetAttrMap(Attrs());
|
||||||
|
return std::unique_ptr<framework::OpDescBind>(grad_op);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
namespace ops = paddle::operators;
|
||||||
|
REGISTER_OPERATOR(write_to_array, ops::WriteToArrayOp,
|
||||||
|
ops::WriteToArrayInferShape, ops::WriteToArrayOpProtoMaker,
|
||||||
|
ops::WriteToArrayGradMaker, ops::WriteToArrayInferVarType);
|
||||||
|
REGISTER_OPERATOR(read_from_array, ops::ReadFromArrayOp,
|
||||||
|
ops::ReadFromArrayInferShape, ops::ReadFromArrayProtoMaker,
|
||||||
|
ops::ReadFromArrayGradMaker);
|
@ -0,0 +1,66 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
import numpy
|
||||||
|
import paddle.v2.framework.core as core
|
||||||
|
|
||||||
|
import paddle.v2.framework.layers as layers
|
||||||
|
from paddle.v2.framework.executor import Executor
|
||||||
|
|
||||||
|
|
||||||
|
class TestArrayReadWrite(unittest.TestCase):
|
||||||
|
def test_read_write(self):
|
||||||
|
x = [
|
||||||
|
layers.data(
|
||||||
|
name='x0', shape=[100]), layers.data(
|
||||||
|
name='x1', shape=[100]), layers.data(
|
||||||
|
name='x2', shape=[100])
|
||||||
|
]
|
||||||
|
|
||||||
|
for each_x in x:
|
||||||
|
each_x.stop_gradient = False
|
||||||
|
|
||||||
|
i = layers.zeros(shape=[1], dtype='int64')
|
||||||
|
arr = layers.array_write(x=x[0], i=i)
|
||||||
|
layers.increment(x=i)
|
||||||
|
arr = layers.array_write(x=x[1], i=i, array=arr)
|
||||||
|
layers.increment(x=i)
|
||||||
|
arr = layers.array_write(x=x[2], i=i, array=arr)
|
||||||
|
|
||||||
|
i = layers.zeros(shape=[1], dtype='int64')
|
||||||
|
a0 = layers.array_read(array=arr, i=i)
|
||||||
|
layers.increment(x=i)
|
||||||
|
a1 = layers.array_read(array=arr, i=i)
|
||||||
|
layers.increment(x=i)
|
||||||
|
a2 = layers.array_read(array=arr, i=i)
|
||||||
|
|
||||||
|
mean_a0 = layers.mean(x=a0)
|
||||||
|
mean_a1 = layers.mean(x=a1)
|
||||||
|
mean_a2 = layers.mean(x=a2)
|
||||||
|
|
||||||
|
a_sum = layers.sums(input=[mean_a0, mean_a1, mean_a2])
|
||||||
|
|
||||||
|
mean_x0 = layers.mean(x=x[0])
|
||||||
|
mean_x1 = layers.mean(x=x[1])
|
||||||
|
mean_x2 = layers.mean(x=x[2])
|
||||||
|
|
||||||
|
x_sum = layers.sums(input=[mean_x0, mean_x1, mean_x2])
|
||||||
|
|
||||||
|
scope = core.Scope()
|
||||||
|
cpu = core.CPUPlace()
|
||||||
|
|
||||||
|
exe = Executor(cpu)
|
||||||
|
|
||||||
|
tensor = core.LoDTensor()
|
||||||
|
tensor.set(numpy.random.random(size=(100, 100)).astype('float32'), cpu)
|
||||||
|
|
||||||
|
outs = map(numpy.array,
|
||||||
|
exe.run(feed={'x0': tensor,
|
||||||
|
'x1': tensor,
|
||||||
|
'x2': tensor},
|
||||||
|
fetch_list=[a_sum, x_sum],
|
||||||
|
scope=scope))
|
||||||
|
self.assertEqual(outs[0], outs[1])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -0,0 +1,13 @@
|
|||||||
|
import unittest
|
||||||
|
from paddle.v2.framework.framework import Program
|
||||||
|
|
||||||
|
|
||||||
|
class TestDebugStringFramework(unittest.TestCase):
|
||||||
|
def test_debug_str(self):
|
||||||
|
p = Program()
|
||||||
|
p.current_block().create_var(name='t', shape=[0, 1])
|
||||||
|
self.assertRaises(ValueError, callableObj=p.__str__)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
Reference in new issue