Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aten/src/ATen/OpaqueTensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct CAFFE2_API OpaqueTensorImpl : public TensorImpl {
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<OpaqueTensorImpl<OpaqueHandle>>(
type_id(), dtype(), device(), opaque_handle_, sizes_);
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/this,
/*dest_impl=*/impl.get(),
/*version_counter=*/version_counter,
Expand All @@ -106,7 +106,7 @@ struct CAFFE2_API OpaqueTensorImpl : public TensorImpl {
void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) override {
AT_ASSERT(typeid(*(impl.get())) == typeid(OpaqueTensorImpl<OpaqueHandle>));
auto opaque_impl = static_cast<const OpaqueTensorImpl<OpaqueHandle>*>(impl.get());
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/opaque_impl,
/*dest_impl=*/this,
/*version_counter=*/version_counter(),
Expand All @@ -122,17 +122,17 @@ struct CAFFE2_API OpaqueTensorImpl : public TensorImpl {
OpaqueHandle opaque_handle_;

/**
* Copy the storage pointer and the tensor metadata fields (e.g. sizes / strides / storage_offset)
* Copy the tensor metadata fields (e.g. sizes / strides / storage pointer / storage_offset)
* from one TensorImpl to another TensorImpl.
*
* For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE [ TensorImpl Shallow-Copying ].
*/
static void copy_tensor_data(
static void copy_tensor_metadata(
const OpaqueTensorImpl<OpaqueHandle>* src_opaque_impl,
OpaqueTensorImpl<OpaqueHandle>* dest_opaque_impl,
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) {
TensorImpl::copy_tensor_data(src_opaque_impl, dest_opaque_impl, version_counter, allow_tensor_metadata_change);
TensorImpl::copy_tensor_metadata(src_opaque_impl, dest_opaque_impl, version_counter, allow_tensor_metadata_change);

// OpaqueTensorImpl-specific fields.
dest_opaque_impl->opaque_handle_ = src_opaque_impl->opaque_handle_;
Expand Down
10 changes: 5 additions & 5 deletions aten/src/ATen/SparseTensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ struct CAFFE2_API SparseTensorImpl : public TensorImpl {
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<SparseTensorImpl>(type_id(), dtype());
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/this,
/*dest_impl=*/impl.get(),
/*version_counter=*/version_counter,
Expand All @@ -211,7 +211,7 @@ struct CAFFE2_API SparseTensorImpl : public TensorImpl {
void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) override {
AT_ASSERT(typeid(*(impl.get())) == typeid(SparseTensorImpl));
auto sparse_impl = static_cast<const SparseTensorImpl*>(impl.get());
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/sparse_impl,
/*dest_impl=*/this,
/*version_counter=*/version_counter(),
Expand All @@ -222,17 +222,17 @@ struct CAFFE2_API SparseTensorImpl : public TensorImpl {
explicit SparseTensorImpl(at::TensorTypeId, const caffe2::TypeMeta&, at::Tensor indices, at::Tensor values);

/**
* Copy the storage pointer and the tensor metadata fields (e.g. sizes / strides / storage_offset)
* Copy the tensor metadata fields (e.g. sizes / strides / storage pointer / storage_offset)
* from one TensorImpl to another TensorImpl.
*
* For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE [ TensorImpl Shallow-Copying ].
*/
static void copy_tensor_data(
static void copy_tensor_metadata(
const SparseTensorImpl* src_sparse_impl,
SparseTensorImpl* dest_sparse_impl,
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) {
TensorImpl::copy_tensor_data(src_sparse_impl, dest_sparse_impl, version_counter, allow_tensor_metadata_change);
TensorImpl::copy_tensor_metadata(src_sparse_impl, dest_sparse_impl, version_counter, allow_tensor_metadata_change);

// Sparse-specific fields
dest_sparse_impl->sparse_dim_ = src_sparse_impl->sparse_dim();
Expand Down
10 changes: 5 additions & 5 deletions aten/src/ATen/quantized/QTensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct CAFFE2_API QTensorImpl : public c10::TensorImpl {
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<QTensorImpl>(
Storage(storage()), type_id(), quantizer_);
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/this,
/*dest_impl=*/impl.get(),
/*version_counter=*/version_counter,
Expand All @@ -59,7 +59,7 @@ struct CAFFE2_API QTensorImpl : public c10::TensorImpl {
void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) override {
AT_ASSERT(typeid(*(impl.get())) == typeid(QTensorImpl));
auto q_impl = static_cast<const QTensorImpl*>(impl.get());
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/q_impl,
/*dest_impl=*/this,
/*version_counter=*/version_counter(),
Expand All @@ -72,17 +72,17 @@ struct CAFFE2_API QTensorImpl : public c10::TensorImpl {
QuantizerPtr quantizer_;

/**
* Copy the storage pointer and the tensor metadata fields (e.g. sizes / strides / storage_offset)
* Copy the tensor metadata fields (e.g. sizes / strides / storage pointer / storage_offset)
* from one TensorImpl to another TensorImpl.
*
* For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE [ TensorImpl Shallow-Copying ].
*/
static void copy_tensor_data(
static void copy_tensor_metadata(
const QTensorImpl* src_q_impl,
QTensorImpl* dest_q_impl,
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) {
TensorImpl::copy_tensor_data(src_q_impl, dest_q_impl, version_counter, allow_tensor_metadata_change);
TensorImpl::copy_tensor_metadata(src_q_impl, dest_q_impl, version_counter, allow_tensor_metadata_change);

// OpaqueTensorImpl-specific fields.
dest_q_impl->quantizer_ = src_q_impl->quantizer_;
Expand Down
25 changes: 13 additions & 12 deletions c10/core/TensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,24 +878,25 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {

// NOTE [ TensorImpl Shallow-Copying ]
//
// TensorImpl shallow-copying is used when we want to have two Variables share the same storage pointer
// and tensor metadata, but each with a different autograd history. Example call sites:
// TensorImpl shallow-copying is used when we want to have two Variables share the same tensor metadata
// (e.g. sizes / strides / storage pointer / storage_offset), but each with a different autograd history.
// Example call sites:
//
// 1. `var_detached = var.detach()` uses `shallow_copy_and_detach()` to create `var_detached` that shares
// the same storage pointer and tensor metadata with `var`, but with a completely new autograd history.
// 2. `var.set_data(tensor)` uses `shallow_copy_from()` to copy storage pointer and tensor metadata from
// the same tensor metadata with `var`, but with a completely new autograd history.
// 2. `var.set_data(tensor)` uses `shallow_copy_from()` to copy tensor metadata from
// `tensor` into `var`, while keeping `var`'s original AutogradMeta.
//
// Functions that shallow-copy a TensorImpl (such as `shallow_copy_and_detach()` / `shallow_copy_from()` /
// `copy_tensor_data()`) copy the storage pointer and the tensor metadata fields (e.g. sizes / strides /
// `copy_tensor_metadata()`) copy the tensor metadata fields (e.g. sizes / strides / storage pointer /
// storage_offset) by value. However, the following fields are not copied:
//
// 1. the AutogradMeta pointer, because it is unique for each Variable.
// 2. the version counter, because the destination TensorImpl's version counter is either set to the
// passed-in `version_counter` (in `shallow_copy_and_detach()` and `copy_tensor_data()`), or it is kept
// passed-in `version_counter` (in `shallow_copy_and_detach()` and `copy_tensor_metadata()`), or it is kept
// intact (in `shallow_copy_from()`). See NOTE [ Version Counter Sharing ] for details.
//
// In `shallow_copy_and_detach()` and `copy_tensor_data()`, the passed-in `allow_tensor_metadata_change`
// In `shallow_copy_and_detach()` and `copy_tensor_metadata()`, the passed-in `allow_tensor_metadata_change`
// determines whether the TensorImpl shallow-copy allows changes to its metadata (e.g. sizes / strides /
// storage / storage_offset). See NOTE [ Metadata Change for a Detached Tensor ] for details.
//
Expand All @@ -913,7 +914,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) const {
auto impl = c10::make_intrusive<TensorImpl>(Storage(storage()), type_id());
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/this,
/*dest_impl=*/impl.get(),
/*version_counter=*/version_counter,
Expand All @@ -930,7 +931,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
* see NOTE [ TensorImpl Shallow-Copying ].
*/
virtual void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) {
copy_tensor_data(
copy_tensor_metadata(
/*src_impl=*/impl.get(),
/*dest_impl=*/this,
/*version_counter=*/version_counter(),
Expand Down Expand Up @@ -1457,12 +1458,12 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
}

/**
* Copy the storage pointer and the tensor metadata fields (e.g. sizes / strides / storage_offset)
* Copy the tensor metadata fields (e.g. sizes / strides / storage pointer / storage_offset)
* from one TensorImpl to another TensorImpl.
*
* For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE [ TensorImpl Shallow-Copying ].
*/
static void copy_tensor_data(
static void copy_tensor_metadata(
const TensorImpl* src_impl,
TensorImpl* dest_impl,
const c10::VariableVersion& version_counter,
Expand Down Expand Up @@ -1553,7 +1554,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
// tensor to also be updated.
//
// NOTE: For a full list of tensor metadata fields, please see
// `shallow_copy_and_detach()` in TensorImpl and its subclasses to find
// `copy_tensor_metadata()` in TensorImpl and its subclasses to find
// which fields are copied by value.
bool allow_tensor_metadata_change_ = true;

Expand Down