Skip to content

Commit 90cc3bd

Browse files
issue/811 fix tensor to blob and resume
1 parent f00c06d commit 90cc3bd

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

include/infinicore/graph/graph.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class GraphManager;
1212
class GraphTensor : public Tensor {
1313
public:
1414
GraphTensor(const Tensor &);
15-
void resume() const;
1615
};
1716

1817
class GraphOperator {

include/infinicore/tensor.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class Tensor {
9090
Tensor(std::shared_ptr<TensorImpl> impl) : impl_(std::move(impl)) {}
9191
std::shared_ptr<TensorImpl> impl_;
9292
friend class TensorImpl;
93-
94-
void resume_from_blob_() const;
9593
};
9694

9795
class TensorImpl : public std::enable_shared_from_this<TensorImpl> {
@@ -135,7 +133,18 @@ class TensorImpl : public std::enable_shared_from_this<TensorImpl> {
135133

136134
void debug() const;
137135

138-
Tensor to_blob() const;
136+
/**
137+
* Unsafe API that returns a new tensor with the same raw memory untracked by allocator
138+
* This API is used for loosely tracking a piece of memory while allowing it to be reused,
139+
* typically in a compute graph scenario.
140+
*/
141+
Tensor to_blob_() const;
142+
143+
/**
144+
* Unsafe API that returns a new tensor with the same memory and let allocator retracks the memory.
145+
* Should only be used on the tensor returned by to_blob_().
146+
*/
147+
Tensor resume_from_blob_() const;
139148

140149
///
141150
/// Data Transfer APIs
@@ -301,6 +310,10 @@ class TensorImpl : public std::enable_shared_from_this<TensorImpl> {
301310
protected:
302311
TensorMetaData meta_;
303312
TensorData data_;
313+
314+
private:
315+
// Mark to indicate if the tensor is created from to_blob_()
316+
bool to_blob_mark_ = false;
304317
};
305318

306319
} // namespace infinicore

src/infinicore/context/allocators/pinnable_block_allocator.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,19 @@ std::byte *PinnableBlockAllocator::allocate(size_t size) {
5252
if (size <= cls.block_size) {
5353
if (!cls.free_blocks.empty()) {
5454
block = cls.free_blocks.back();
55-
cls.free_blocks.pop_back();
56-
block->in_use = true;
57-
return reinterpret_cast<std::byte *>(block->ptr);
55+
while (block != nullptr && block->in_use) {
56+
cls.free_blocks.pop_back();
57+
if (cls.free_blocks.empty()) {
58+
block = nullptr;
59+
break;
60+
}
61+
block = cls.free_blocks.back();
62+
}
63+
if (block != nullptr) {
64+
cls.free_blocks.pop_back();
65+
block->in_use = true;
66+
return reinterpret_cast<std::byte *>(block->ptr);
67+
}
5868
}
5969
// Allocate a new block for this class
6070
block = std::make_shared<Block>();

src/infinicore/graph/graph.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ namespace infinicore::graph {
1010
* GraphTensor
1111
* ========================= */
1212

13-
GraphTensor::GraphTensor(const Tensor &tensor) : Tensor(tensor->to_blob()) {
14-
}
15-
16-
void GraphTensor::resume() const {
17-
resume_from_blob_();
13+
GraphTensor::GraphTensor(const Tensor &tensor) : Tensor(tensor->to_blob_()) {
1814
}
1915

2016
/* =========================

src/infinicore/tensor/copy.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void TensorImpl::copy_from(Tensor src) {
3838
} else {
3939
auto local_src = Tensor::empty(this->shape(), this->dtype(), this->device());
4040
context::setDevice(src->device());
41-
context::memcpyD2H(local_src->data(), src->data(), this->data_.memory->size());
41+
context::memcpyD2H(local_src->data(), src->data(), copy_size);
4242
op::rearrange_(Tensor(const_cast<TensorImpl *>(this)->shared_from_this()), local_src);
4343
}
4444
} else if (src->device().getType() == Device::Type::CPU) {

src/infinicore/tensor/tensor.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ Tensor::operator bool() const {
6565
return impl_ != nullptr;
6666
}
6767

68-
void Tensor::resume_from_blob_() const {
69-
context::reinstantiateBlob(impl_->data_.memory);
70-
}
71-
7268
TensorMetaData::TensorMetaData(const Shape &_shape, const Strides &_strides, const DataType &_dtype)
7369
: shape(_shape), strides(_strides), dtype(_dtype) {
7470
INFINICORE_CHECK_ERROR(infiniopCreateTensorDescriptor(&desc, shape.size(), shape.data(), strides.data(), (infiniDtype_t)dtype));
@@ -280,10 +276,22 @@ std::shared_ptr<TensorImpl> TensorImpl::strided_from_blob(
280276
return t;
281277
}
282278

283-
Tensor TensorImpl::to_blob() const {
279+
Tensor TensorImpl::to_blob_() const {
284280
auto t = std::shared_ptr<TensorImpl>(new TensorImpl(shape(), strides(), dtype()));
285281
t->data_.offset = this->data_.offset;
286282
t->data_.memory = std::make_shared<Memory>(this->data_.memory->data(), this->data_.memory->size(), this->data_.memory->device(), nullptr);
283+
t->to_blob_mark_ = true;
284+
return Tensor{t};
285+
}
286+
287+
Tensor TensorImpl::resume_from_blob_() const {
288+
auto t = std::shared_ptr<TensorImpl>(new TensorImpl(shape(), strides(), dtype()));
289+
t->data_.offset = this->data_.offset;
290+
if (to_blob_mark_) {
291+
t->data_.memory = context::reinstantiateBlob(this->data_.memory);
292+
} else {
293+
t->data_.memory = this->data_.memory;
294+
}
287295

288296
return Tensor{t};
289297
}

0 commit comments

Comments
 (0)