diff --git a/src/framework/tensor.h b/src/framework/tensor.h index a8f808519130140e76aab1ced34bbc4885314574..a221a26aa1435000646cf7d58321df28f3322834 100644 --- a/src/framework/tensor.h +++ b/src/framework/tensor.h @@ -85,6 +85,12 @@ class Tensor { } } + Tensor(const Tensor &inTensor) { + this->dims_ = inTensor.dims_; + this->holder_ = inTensor.holder_; + this->offset_ = inTensor.offset_; + } + /*! Return a pointer to mutable memory block. */ template inline T *data() { @@ -169,7 +175,9 @@ class Tensor { /*! The internal of two tensors share the same memory block. */ inline Tensor &ShareDataWith(const Tensor &src) { src.check_memory_size(); - *this = src; + if (holder_.get() != src.holder_.get()) { + *this = src; + } return *this; } @@ -198,7 +206,6 @@ class Tensor { size_t base = numel() / dims_[0]; Tensor dst; dst.holder_ = holder_; - dst.set_layout(layout_); DDim dst_dims = dims_; dst_dims[0] = end_idx - begin_idx; dst.Resize(dst_dims); @@ -227,10 +234,6 @@ class Tensor { "Tensor's dims_ is out of bound. "); } - inline DataLayout layout() const { return layout_; } - - inline void set_layout(const DataLayout layout) { layout_ = layout; } - private: /** * @note Placeholder hides type T, so it doesn't appear as a @@ -288,21 +291,6 @@ class Tensor { DDim dims_; - /** - * @brief the layout of memory block, default is NHWC. - * - * @note the memory allocation order, describe how weight/data is - * stored - * For example, in 4-D Tensor(rank=4), there are three - * commonly - * used layout. They are - * NCHW, NHWC, CHWN. - * N,C,H,W for respectively the batch size, the number of - * feature maps, the height, the width. - */ - - DataLayout layout_ = DataLayout::kNHWC; - /** * @brief A PlaceHolder may be shared by more than one tensor. * diff --git a/src/framework/tensor_util.cpp b/src/framework/tensor_util.cpp index 465502cb19173e26361905752e76e75c15229893..6722ec3e37b8219eee9e1b9913799b08d8f902bc 100644 --- a/src/framework/tensor_util.cpp +++ b/src/framework/tensor_util.cpp @@ -20,7 +20,6 @@ namespace framework { void TensorCopy(const Tensor &src, Tensor *dst) { src.check_memory_size(); dst->Resize(src.dims()); - dst->set_layout(src.layout()); auto src_ptr = src.data(); auto dst_ptr = dst->mutable_data(src.type()); auto size = src.numel() * SizeOfType(src.type()); diff --git a/src/io/io.cpp b/src/io/io.cpp index e5ca89f9f6fd495c6adaba9f982f0576a2a1af30..7931432bd1d4528ab9b0cda7ab05ab13c14dfcfe 100644 --- a/src/io/io.cpp +++ b/src/io/io.cpp @@ -477,7 +477,7 @@ std::shared_ptr Executor::Predict( printf("====================[---------]======================\n"); #endif - return std::shared_ptr(output_tensor); + return std::make_shared(framework::Tensor(*output_tensor)); } template std::shared_ptr Executor::Predict( diff --git a/test/net/test_googlenet.cpp b/test/net/test_googlenet.cpp index e1bcd5212de214abbf2df5822ebc977619f8caa3..1695995a8d60d20e0d6c5f8911c39a948426a82a 100644 --- a/test/net/test_googlenet.cpp +++ b/test/net/test_googlenet.cpp @@ -30,7 +30,11 @@ int main() { std::vector dims{1, 3, 224, 224}; GetInput(g_test_image_1x3x224x224, &input, dims); auto time3 = time(); - executor.Predict(input, dims); + + for (int i = 0; i < 10; ++i) { + executor.Predict(input, dims); + } + auto time4 = time(); DLOG << "predict cost :" << time_diff(time3, time4) << "ms\n"; return 0;