From a86a61b004889e30563f184f67867788c6dba14e Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Thu, 27 Sep 2018 18:29:22 -0700 Subject: [PATCH] Implement caffe2::Tensor::raw_data() in terms of data() Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/12097 Reviewed By: jerryzh168 Differential Revision: D10051202 fbshipit-source-id: b4b61869363a606ab465d1500558226efae30d06 --- aten/src/ATen/core/TensorImpl.h | 37 ++++++--------------------------- caffe2/core/tensor.h | 22 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/aten/src/ATen/core/TensorImpl.h b/aten/src/ATen/core/TensorImpl.h index 938815201c4..24b5789ee5c 100644 --- a/aten/src/ATen/core/TensorImpl.h +++ b/aten/src/ATen/core/TensorImpl.h @@ -253,6 +253,7 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { inline void* data() const { AT_ASSERT(!is_variable()); + CAFFE_ENFORCE_WITH_CALLER(storage_.data() || numel_ == 0); return static_cast( static_cast(storage_.data()) + data_type_.itemsize() * storage_offset_); @@ -427,7 +428,7 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { CAFFE_ENFORCE( src.device_type() == ::at::DeviceType::CPU, "In CopyFrom source and dest tensors must both be CPU for meta copy"); - data_type_.copy()(src.raw_data(), raw_mutable_data(), size()); + data_type_.copy()(src.data(), raw_mutable_data(data_type_), size()); } else { // We'll need to use a non-CPU context to perform the copy if // one of the context is not CPU since only non-CPU context @@ -435,20 +436,20 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { if (src.device_type() != ::at::DeviceType::CPU || device_type() == ::at::DeviceType::CPU) { if (!context) { src.CreateContext()->CopyBytesToDevice( - nbytes(), src.raw_data(), raw_mutable_data(), device_type()); + nbytes(), src.data(), raw_mutable_data(data_type_), device_type()); } else { CAFFE_ENFORCE( context->device_type() == src.device_type(), "Type for provided context does not match the type of source"); context->CopyBytesToDevice( - nbytes(), src.raw_data(), raw_mutable_data(), device_type()); + nbytes(), src.data(), raw_mutable_data(data_type_), device_type()); } } else { // In case source context is CPU, and target context is non-CPU // We'll have to create a Context from target and perform the // copy using that context CreateContext()->CopyBytesFromCPU( - nbytes(), src.raw_data(), raw_mutable_data()); + nbytes(), src.data(), raw_mutable_data(data_type_)); } } } @@ -681,15 +682,6 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { } } - /** - * Returns a const raw void* pointer of the underlying storage. mutable_data() - * or raw_mutable_data() must have been called prior to this function call. - */ - inline const void* raw_data() const { - CAFFE_ENFORCE_WITH_CALLER(storage_.data() || numel_ == 0); - return static_cast(static_cast(storage_.data()) + storage_offset_ * storage_.itemsize()); - } - /** * Returns a mutable raw pointer of the underlying storage. Since we will need * to know the type of the data for allocation, a TypeMeta object is passed in @@ -771,23 +763,6 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { } } - /** - * Returns a mutable raw pointer of the underlying storage. This can only be - * used when you know for sure that the underlying storage of the tensor is - * already created via an earlier raw_mutable_data(meta) call or a - * mutable_data() call. - * - * If the existing data does not match the desired type, it will be deleted - * and a new storage will be created. - */ - inline void* raw_mutable_data() { - CAFFE_ENFORCE_WITH_CALLER( - data_type_.id() != caffe2::TypeIdentifier::uninitialized(), - "Calling raw_mutable_data() without meta, but the current meta is " - "of unknown type."); - return raw_mutable_data(data_type_); - } - /** * Returns a typed pointer of the underlying storage. * @@ -920,7 +895,7 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target { void ExtractDeviceOption(caffe2::DeviceOption* device) const { auto* context = GetStaticContext(); CHECK(context); - context->ExtractDeviceOption(device, raw_data()); + context->ExtractDeviceOption(device, data()); } protected: diff --git a/caffe2/core/tensor.h b/caffe2/core/tensor.h index f84efb8b1db..8e670858e89 100644 --- a/caffe2/core/tensor.h +++ b/caffe2/core/tensor.h @@ -272,8 +272,12 @@ class CAFFE2_API Tensor final { impl_.get()->ShareExternalPointer(std::move(data_ptr), data_type, capacity); } + /** + * Returns a const raw void* pointer of the underlying storage. mutable_data() + * or raw_mutable_data() must have been called prior to this function call. + */ inline const void* raw_data() const { - return impl_.get()->raw_data(); + return impl_->data(); } template @@ -285,8 +289,22 @@ class CAFFE2_API Tensor final { return impl_.get()->raw_mutable_data(meta); } + /** + * Returns a mutable raw pointer of the underlying storage. This can only be + * used when you know for sure that the underlying storage of the tensor is + * already created via an earlier raw_mutable_data(meta) call or a + * mutable_data() call. + * + * If the existing data does not match the desired type, it will be deleted + * and a new storage will be created. + */ inline void* raw_mutable_data() const { - return impl_.get()->raw_mutable_data(); + const auto& data_type = impl_->dtype(); + CAFFE_ENFORCE_WITH_CALLER( + data_type.id() != caffe2::TypeIdentifier::uninitialized(), + "Calling raw_mutable_data() without meta, but the current meta is " + "of unknown type."); + return raw_mutable_data(data_type); } template