Skip to content

Commit c1d6cf0

Browse files
committed
Ensure that deleter is called even for a no-data tensor.
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: ghstack-source-id: b5f8bf8 Pull Request resolved: #117418
1 parent 23d53a4 commit c1d6cf0

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

aten/src/ATen/ops/from_blob.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class TORCH_API TensorMaker {
8383

8484
std::size_t computeStorageSize() const noexcept;
8585

86-
DataPtr makeDataPtrFromDeleter() const;
86+
DataPtr makeDataPtrFromDeleter() noexcept;
8787

8888
DataPtr makeDataPtrFromContext() noexcept;
8989

aten/src/ATen/templates/Functions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Tensor TensorMaker::make_tensor() {
7878
return storage_size;
7979
}
8080

81-
inline DataPtr TensorMaker::makeDataPtrFromDeleter() const {
82-
return InefficientStdFunctionContext::makeDataPtr(data_, deleter_, *device_);
81+
inline DataPtr TensorMaker::makeDataPtrFromDeleter() noexcept {
82+
return InefficientStdFunctionContext::makeDataPtr(data_, std::move(deleter_), *device_);
8383
}
8484

8585
inline DataPtr TensorMaker::makeDataPtrFromContext() noexcept {

c10/core/Allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ static void deleteInefficientStdFunctionContext(void* ptr) {
2727

2828
at::DataPtr InefficientStdFunctionContext::makeDataPtr(
2929
void* ptr,
30-
const std::function<void(void*)>& deleter,
30+
std::function<void(void*)> deleter,
3131
Device device) {
3232
return {
3333
ptr,
34-
new InefficientStdFunctionContext({ptr, deleter}),
34+
new InefficientStdFunctionContext(ptr, std::move(deleter)),
3535
&deleteInefficientStdFunctionContext,
3636
device};
3737
}

c10/core/Allocator.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,18 @@ struct C10_API Allocator {
223223
// allocation InefficientStdFunctionContext, on top of the dynamic
224224
// allocation which is implied by std::function itself.
225225
struct C10_API InefficientStdFunctionContext {
226-
std::unique_ptr<void, std::function<void(void*)>> ptr_;
227-
InefficientStdFunctionContext(
228-
std::unique_ptr<void, std::function<void(void*)>>&& ptr)
229-
: ptr_(std::move(ptr)) {}
226+
void* ptr_;
227+
std::function<void(void*)> deleter_;
228+
InefficientStdFunctionContext(void* ptr, std::function<void(void*)> deleter)
229+
: ptr_(ptr), deleter_(std::move(deleter)) {}
230+
~InefficientStdFunctionContext() {
231+
if (deleter_) {
232+
deleter_(ptr_);
233+
}
234+
}
230235
static DataPtr makeDataPtr(
231236
void* ptr,
232-
const std::function<void(void*)>& deleter,
237+
std::function<void(void*)> deleter,
233238
Device device);
234239
};
235240

0 commit comments

Comments
 (0)