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
This commit is contained in:
Edward Yang 2018-09-27 18:29:22 -07:00 committed by Facebook Github Bot
parent 2021b26bcb
commit a86a61b004
2 changed files with 26 additions and 33 deletions

View File

@ -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<void*>(
static_cast<char*>(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<void*>(static_cast<char*>(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<T>() 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:

View File

@ -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 <typename T>
@ -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<T>() 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 <typename T>