Skip to content

Commit

Permalink
Fix sparse fake tensors detach (#125679)
Browse files Browse the repository at this point in the history
As in the title.

Fixes a bug reported in #117907 (comment)

Pull Request resolved: #125679
Approved by: https://github.com/amjames, https://github.com/lezcano
  • Loading branch information
pearu authored and pytorchmergebot committed May 9, 2024
1 parent 7e86a7c commit 0241ed9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 34 deletions.
2 changes: 2 additions & 0 deletions aten/src/ATen/SparseCsrTensorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ SparseCsrTensorImpl::SparseCsrTensorImpl(
TORCH_INTERNAL_ASSERT(((key_set.has(DispatchKey::SparseCsrCPU) && device().type() == kCPU)
|| (key_set.has(DispatchKey::SparseCsrCUDA) && device().type() == kCUDA)
|| (key_set.has(DispatchKey::SparseCsrMeta) && device().type() == kMeta)
|| (key_set.has(DispatchKey::SparseCsrCPU) && device().type() == kMeta) // fake tensor
|| (key_set.has(DispatchKey::SparseCsrCUDA) && device().type() == kMeta) // fake tensor
|| (key_set.has(DispatchKey::SparseCsrPrivateUse1) && device().type() == kPrivateUse1)),
"Inconsistent key_set (=", key_set, ") and device (=", device(), ")");

Expand Down
56 changes: 38 additions & 18 deletions aten/src/ATen/SparseCsrTensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ATen/Tensor.h>
#include <c10/core/TensorImpl.h>
#include <c10/core/impl/TorchDispatchModeTLS.h>
#include <c10/util/Exception.h>
namespace at {

Expand Down Expand Up @@ -107,6 +108,39 @@ struct TORCH_API SparseCsrTensorImpl : public TensorImpl {
}
}

template <typename VariableVersion>
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach_core(
VariableVersion&& version_counter,
bool allow_tensor_metadata_change) const {
const auto mode_stack_len = c10::impl::TorchDispatchModeTLS::stack_len();
c10::impl::PyInterpreter&& interpreter = nullptr;
if (mode_stack_len > 0 &&
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
const auto& cur_torch_dispatch_mode_state =
c10::impl::TorchDispatchModeTLS::get_stack_at(mode_stack_len - 1);
interpreter = cur_torch_dispatch_mode_state->pyinterpreter();
} else if (
key_set_.has(DispatchKey::Python) &&
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
interpreter = pyobj_slot_.load_pyobj_interpreter();
} else {
// otherwise just copy the SparseTensorImpl and not the PyObject.
auto impl = c10::make_intrusive<SparseCsrTensorImpl>(
key_set(), device(), layout_impl(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/version_counter,
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
}
auto r = interpreter->detach(this);
r->set_version_counter(std::forward<VariableVersion>(version_counter));
r->set_allow_tensor_metadata_change(allow_tensor_metadata_change);
return r;
}

/**
* Return a TensorImpl that is a shallow-copy of this TensorImpl.
*
Expand All @@ -116,15 +150,8 @@ struct TORCH_API SparseCsrTensorImpl : public TensorImpl {
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<SparseCsrTensorImpl>(
key_set(), device(), layout_impl(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/version_counter,
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
return shallow_copy_and_detach_core(
version_counter, allow_tensor_metadata_change);
}

/**
Expand All @@ -136,15 +163,8 @@ struct TORCH_API SparseCsrTensorImpl : public TensorImpl {
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
c10::VariableVersion&& version_counter,
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<SparseCsrTensorImpl>(
key_set(), device(), layout_impl(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/std::move(version_counter),
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
return shallow_copy_and_detach_core(
std::move(version_counter), allow_tensor_metadata_change);
}

private:
Expand Down
53 changes: 37 additions & 16 deletions aten/src/ATen/SparseTensorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ATen/Tensor.h>
#include <c10/core/TensorImpl.h>
#include <c10/core/impl/TorchDispatchModeTLS.h>
#include <c10/util/Exception.h>
#include <c10/util/irange.h>

Expand Down Expand Up @@ -306,6 +307,38 @@ struct TORCH_API SparseTensorImpl : public TensorImpl {
const Tensor& indices,
const Tensor& values);

template <typename VariableVersion>
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach_core(
VariableVersion&& version_counter,
bool allow_tensor_metadata_change) const {
const auto mode_stack_len = c10::impl::TorchDispatchModeTLS::stack_len();
c10::impl::PyInterpreter&& interpreter = nullptr;
if (mode_stack_len > 0 &&
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
const auto& cur_torch_dispatch_mode_state =
c10::impl::TorchDispatchModeTLS::get_stack_at(mode_stack_len - 1);
interpreter = cur_torch_dispatch_mode_state->pyinterpreter();
} else if (
key_set_.has(DispatchKey::Python) &&
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
interpreter = pyobj_slot_.load_pyobj_interpreter();
} else {
// otherwise just copy the SparseTensorImpl and not the PyObject.
auto impl = c10::make_intrusive<SparseTensorImpl>(key_set(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/version_counter,
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
}
auto r = interpreter->detach(this);
r->set_version_counter(std::forward<VariableVersion>(version_counter));
r->set_allow_tensor_metadata_change(allow_tensor_metadata_change);
return r;
}

/**
* Return a TensorImpl that is a shallow-copy of this TensorImpl.
*
Expand All @@ -315,14 +348,8 @@ struct TORCH_API SparseTensorImpl : public TensorImpl {
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
const c10::VariableVersion& version_counter,
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<SparseTensorImpl>(key_set(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/version_counter,
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
return shallow_copy_and_detach_core(
version_counter, allow_tensor_metadata_change);
}

/**
Expand All @@ -334,14 +361,8 @@ struct TORCH_API SparseTensorImpl : public TensorImpl {
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
c10::VariableVersion&& version_counter,
bool allow_tensor_metadata_change) const override {
auto impl = c10::make_intrusive<SparseTensorImpl>(key_set(), dtype());
copy_tensor_metadata(
/*src_sparse_impl=*/this,
/*dest_sparse_impl=*/impl.get(),
/*version_counter=*/std::move(version_counter),
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
impl->refresh_numel();
return impl;
return shallow_copy_and_detach_core(
std::move(version_counter), allow_tensor_metadata_change);
}

/**
Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/inductor/test_torchinductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ def fn(a):
sample[-1] = 1
self.common(fn, (sample,))

@skipCPUIf(IS_MACOS, "fails on macos")
def test_multilayer_var(self):
def fn(a):
return torch.var(a)
Expand Down
4 changes: 4 additions & 0 deletions test/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4446,6 +4446,10 @@ def test_fake(self, dtype, layout):
self.assertIsInstance(f, FakeTensor)
self.assertEqualMeta(f, t, 0)

d = f.detach()
self.assertIsInstance(d, FakeTensor)
self.assertEqualMeta(d, t, 0)

@all_sparse_layouts('layout', include_strided=False)
@parametrize("dtype", [torch.float64])
def test_zeros_like_fake(self, dtype, layout):
Expand Down

0 comments on commit 0241ed9

Please sign in to comment.