Skip to content

Commit 3408d9d

Browse files
Dmytro Dzhulgakovfacebook-github-bot
Dmytro Dzhulgakov
authored andcommitted
Clean up Storage/StorageImpl constructors (pytorch#16948)
Summary: Small cleanup while doing pytorch#16857: - rename C2 constructors as create_legacy - remove duplicated constructors - make resizable flag non-default Pull Request resolved: pytorch#16948 Differential Revision: D14062755 Pulled By: dzhulgakov fbshipit-source-id: 3b7b4ec9cdf67d2628cccc001156e040006b673e
1 parent 11816af commit 3408d9d

File tree

6 files changed

+32
-43
lines changed

6 files changed

+32
-43
lines changed

aten/src/ATen/templates/TypeDefault.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ Tensor TypeDefault::tensorWithAllocator(IntArrayRef sizes, IntArrayRef strides,
9595
}
9696

9797
Storage TypeDefault::storageFromBlob(void * data, int64_t size, const std::function<void(void*)> & deleter) const {
98-
return Storage(
98+
return Storage(
9999
typeMeta(),
100-
InefficientStdFunctionContext::makeDataPtr(data, deleter, getDeviceFromPtr(data)),
101100
size,
102-
deleter);
101+
InefficientStdFunctionContext::makeDataPtr(
102+
data, deleter, getDeviceFromPtr(data)),
103+
/*allocator=*/nullptr,
104+
/*resizable=*/false);
103105
}
104106
Storage TypeDefault::storageWithAllocator(int64_t size, Allocator* allocator) const {
105-
return Storage(typeMeta(), size, allocator);
107+
// Potentially the storage might be marked as resizable too here
108+
return Storage(typeMeta(), size, allocator, /*resizable=*/false);
106109
}
107110
Tensor TypeDefault::unsafeTensorFromTH(void * th_pointer, bool retain) const {
108111
auto tensor_impl = c10::intrusive_ptr<TensorImpl, UndefinedTensorImpl>::reclaim(static_cast<TensorImpl*>(th_pointer));

c10/core/Storage.h

+18-22
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,22 @@ struct C10_API Storage {
88
public:
99
Storage() {}
1010
Storage(c10::intrusive_ptr<StorageImpl> ptr) : storage_impl_(std::move(ptr)) {}
11+
12+
// Allocates memory buffer using given allocator and creates a storage with it
1113
Storage(
1214
caffe2::TypeMeta data_type,
1315
size_t size,
1416
Allocator* allocator,
15-
bool resizable = false)
17+
bool resizable)
1618
: storage_impl_(c10::make_intrusive<StorageImpl>(
1719
data_type,
1820
size,
1921
allocator,
2022
resizable)) {}
2123

22-
Storage(
23-
caffe2::TypeMeta data_type,
24-
at::DataPtr data_ptr,
25-
size_t size,
26-
const std::function<void(void*)>& deleter,
27-
bool resizable = false)
28-
: storage_impl_(c10::make_intrusive<StorageImpl>(
29-
data_type,
30-
size,
31-
std::move(data_ptr),
32-
/* allocator */ nullptr,
33-
resizable)) {}
34-
35-
Storage(at::DeviceType device_type)
36-
: storage_impl_(
37-
c10::make_intrusive<StorageImpl>(at::Device(device_type))) {}
38-
Storage(at::Device device)
39-
: storage_impl_(c10::make_intrusive<StorageImpl>(device)) {}
40-
Storage(at::Device device, caffe2::TypeMeta data_type)
41-
: storage_impl_(c10::make_intrusive<StorageImpl>(device, data_type)) {}
42-
24+
// Creates storage with pre-allocated memory buffer. Allocator is given for
25+
// potential future reallocations, however it can be nullptr if the storage
26+
// is non-resizable
4327
Storage(
4428
caffe2::TypeMeta data_type,
4529
int64_t numel,
@@ -53,6 +37,18 @@ struct C10_API Storage {
5337
allocator,
5438
resizable)) {}
5539

40+
// Legacy constructor for partially initialized (dtype or memory) storages
41+
// that can be temporarily created with Caffe2 APIs. See the note on top of
42+
// TensorImpl.h for details.
43+
static Storage create_legacy(at::Device device, caffe2::TypeMeta data_type) {
44+
return Storage(c10::make_intrusive<StorageImpl>(
45+
data_type,
46+
0,
47+
at::DataPtr(nullptr, device),
48+
GetAllocator(device.type()),
49+
true));
50+
}
51+
5652
template <typename T>
5753
inline bool IsType() const {
5854
return storage_impl_->IsType<T>();

c10/core/StorageImpl.h

-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ struct C10_API StorageImpl final : public c10::intrusive_ptr_target {
4545
allocator,
4646
resizable) {}
4747

48-
explicit StorageImpl(at::Device device)
49-
: StorageImpl(device, caffe2::TypeMeta()) {}
50-
51-
StorageImpl(at::Device device, caffe2::TypeMeta data_type)
52-
: StorageImpl(
53-
data_type,
54-
0,
55-
at::DataPtr(nullptr, device),
56-
GetAllocator(device.type()),
57-
true) {}
58-
5948
StorageImpl& operator=(StorageImpl&& other) = default;
6049
StorageImpl& operator=(const StorageImpl&) = delete;
6150
StorageImpl() = delete;

c10/core/TensorImpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
10701070
*/
10711071
inline void FreeMemory() {
10721072
// We'll detach from the old Storage and create a new one
1073-
storage_ = Storage(storage_.device(), data_type_);
1073+
storage_ = Storage::create_legacy(storage_.device(), data_type_);
10741074
storage_offset_ = 0;
10751075
}
10761076

@@ -1168,7 +1168,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
11681168
storage_.set_dtype(meta);
11691169
} else {
11701170
if (data_type_ != meta) {
1171-
storage_ = Storage(storage_.device(), meta);
1171+
storage_ = Storage::create_legacy(storage_.device(), meta);
11721172
}
11731173
}
11741174
data_type_ = meta;

caffe2/core/tensor.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CAFFE2_API Tensor final {
7171
*/
7272
explicit Tensor(at::Device device)
7373
: impl_(c10::make_intrusive<TensorImpl, UndefinedTensorImpl>(
74-
Storage(device),
74+
Storage::create_legacy(device, TypeMeta()),
7575
c10::computeTensorTypeId(at::device(device).layout(at::kStrided)),
7676
/*is_variable=*/ false
7777
)) {
@@ -228,7 +228,7 @@ class CAFFE2_API Tensor final {
228228
if (impl_->dtype() != src.impl_->dtype()) {
229229
// NB: copy preserves device_type
230230
// This storage will get initialized by the mutable_data call below.
231-
impl_->set_storage(at::Storage(impl_->device_type(), src.impl_->dtype()));
231+
impl_->set_storage(at::Storage::create_legacy(impl_->device_type(), src.impl_->dtype()));
232232
}
233233
impl_->Resize(src.impl_->sizes());
234234

torch/csrc/jit/import.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ at::Tensor ScriptModuleDeserializer::loadTensor(
162162
std::tie(storage_ptr, record_size) = reader_.getRecord(record_key);
163163
auto cpu_storage = at::Storage(
164164
at::CPU(type).typeMeta(),
165-
std::move(storage_ptr),
166165
record_size / at::CPU(type).typeMeta().itemsize(),
167-
nullptr); // NB: we didn't set any allocator for the tensor
166+
std::move(storage_ptr),
167+
/*allocator=*/nullptr,
168+
/*resizable=*/false); // NB: we didn't set any allocator for the tensor
168169
if (device.type() == at::DeviceType::CPU) {
169170
storage_it =
170171
storageMap.insert(std::make_pair(record_key, cpu_storage)).first;

0 commit comments

Comments
 (0)