Skip to content

Commit f5ea528

Browse files
gchananfacebook-github-bot
authored andcommitted
Don't segfault on trying to get data_ptr of sparse tensor. (pytorch#18347)
Summary: Also asserts in storage_initialized that there is a storage. Pull Request resolved: pytorch#18347 Differential Revision: D14582028 Pulled By: gchanan fbshipit-source-id: df3f5d181188f39e361839169fd054539c3b2839
1 parent 647154f commit f5ea528

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

c10/core/TensorImpl.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
564564
template <typename T>
565565
inline T * data() const {
566566
AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged
567+
AT_CHECK(has_storage(),
568+
"Cannot access data pointer of Tensor that doesn't have storage");
567569
AT_ASSERTM(
568570
storage_initialized(),
569571
"The tensor has a non-zero number of elements, but its data is not allocated yet. "
@@ -595,7 +597,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
595597
*/
596598
inline void* data() const {
597599
AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged
598-
AT_ASSERT(storage_initialized());
600+
AT_CHECK(has_storage(),
601+
"Cannot access data pointer of Tensor that doesn't have storage");
599602
AT_ASSERT(dtype_initialized());
600603
return static_cast<void*>(
601604
static_cast<char*>(storage_.data()) +
@@ -1243,7 +1246,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
12431246
* True if a tensor is storage initialized. A tensor may become
12441247
* storage UNINITIALIZED after a Resize() or FreeMemory()
12451248
*/
1246-
bool storage_initialized() const noexcept {
1249+
bool storage_initialized() const {
1250+
AT_ASSERT(has_storage());
12471251
return storage_.data() || numel_ == 0;
12481252
}
12491253

0 commit comments

Comments
 (0)