提交 479985c0 编写于 作者: 刘琦

Merge branch 'reshape' into 'master'

Do not preallocate reshape, reuse input tensor instead.

See merge request !562
......@@ -223,16 +223,19 @@ class Tensor {
}
}
inline void ResizeWithBuffer(const std::vector<index_t> &shape,
BufferBase *buffer) {
MACE_CHECK(!has_opencl_image(), "Cannot resize image, use ResizeImage.");
shape_ = shape;
image_shape_.clear();
if (buffer_ != nullptr && is_buffer_owner_) {
// Make this tensor reuse other tensor's buffer.
// This tensor has the same dtype, shape and image_shape.
// It could be reshaped later (with image shape unchanged).
inline void ReuseTensorBuffer(const Tensor &other) {
if (is_buffer_owner_ && buffer_ != nullptr) {
delete buffer_;
}
buffer_ = buffer;
is_buffer_owner_ = false;
buffer_ = other.buffer_;
allocator_ = other.allocator_;
dtype_ = other.dtype_;
shape_ = other.shape_;
image_shape_ = other.image_shape_;
}
inline MaceStatus ResizeImage(const std::vector<index_t> &shape,
......
......@@ -176,7 +176,8 @@ MaceStatus Workspace::CreateOutputTensorBuffer(const NetDef &net_def,
const int op_device =
ProtoArgHelper::GetOptionalArg<OperatorDef, int>(
op, "device", static_cast<int>(device_type));
if (op_device == device_type && !op.mem_id().empty()) {
if (op_device == device_type && !op.mem_id().empty()
&& op.type() != "Reshape") {
auto mem_ids = op.mem_id();
int count = mem_ids.size();
for (int i = 0; i < count; ++i) {
......
......@@ -36,7 +36,8 @@ struct ReshapeFunctor {
Tensor *output,
StatsFuture *future) {
MACE_UNUSED(future);
output->ResizeWithBuffer(out_shape, input->UnderlyingBuffer());
output->ReuseTensorBuffer(*input);
output->Reshape(out_shape);
return MACE_SUCCESS;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册