Don't segfault on trying to get data_ptr of sparse tensor. (#18347)
authorGregory Chanan <gchanan@fb.com>
Mon, 25 Mar 2019 15:53:42 +0000 (08:53 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 25 Mar 2019 15:59:53 +0000 (08:59 -0700)
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

c10/core/TensorImpl.h

index 6a8f408..24ddaac 100644 (file)
@@ -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;
   }