diff options
author | Gregory Chanan <gchanan@fb.com> | 2019-03-25 08:53:42 -0700 |
---|---|---|
committer | Facebook Github Bot <facebook-github-bot@users.noreply.github.com> | 2019-03-25 08:59:53 -0700 |
commit | f5ea52868777b63283200c2261e85001999913f5 (patch) | |
tree | eb72f0a39b244e178b0318c1198817b95f386de0 | |
parent | 647154f82ac2c57769f080c41452b3e5960ab94f (diff) | |
download | pytorch-f5ea52868777b63283200c2261e85001999913f5.tar.gz pytorch-f5ea52868777b63283200c2261e85001999913f5.tar.bz2 pytorch-f5ea52868777b63283200c2261e85001999913f5.zip |
Don't segfault on trying to get data_ptr of sparse tensor. (#18347)
Summary:
Also asserts in storage_initialized that there is a storage.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18347
Differential Revision: D14582028
Pulled By: gchanan
fbshipit-source-id: df3f5d181188f39e361839169fd054539c3b2839
-rw-r--r-- | c10/core/TensorImpl.h | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/c10/core/TensorImpl.h b/c10/core/TensorImpl.h index 6a8f408f2f..24ddaac302 100644 --- a/c10/core/TensorImpl.h +++ b/c10/core/TensorImpl.h @@ -564,6 +564,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target { template <typename T> inline T * data() const { AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged + AT_CHECK(has_storage(), + "Cannot access data pointer of Tensor that doesn't have storage"); AT_ASSERTM( storage_initialized(), "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 { */ inline void* data() const { AT_ASSERT(!is_variable()); // TODO: remove this when Variable and Tensor are merged - AT_ASSERT(storage_initialized()); + AT_CHECK(has_storage(), + "Cannot access data pointer of Tensor that doesn't have storage"); AT_ASSERT(dtype_initialized()); return static_cast<void*>( static_cast<char*>(storage_.data()) + @@ -1243,7 +1246,8 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target { * True if a tensor is storage initialized. A tensor may become * storage UNINITIALIZED after a Resize() or FreeMemory() */ - bool storage_initialized() const noexcept { + bool storage_initialized() const { + AT_ASSERT(has_storage()); return storage_.data() || numel_ == 0; } |