Skip to content

Commit

Permalink
Use object identity for deepcopy memo (pytorch#126126)
Browse files Browse the repository at this point in the history
Copy of pytorch#126089, with some additional fixes & tests

Partial fix for pytorch#125635: previously, the deepcopy implementation would group together any tensors with any aliasing relationship and assign them to the same tensor. This was sort of good if you have two tensors `b = a.detach()`, because then if you deepcopy `list = [a, b]` to `list2 = list.deepcopy()`, then writes to `list2[0]` will also modify `list2[1]`. But for the most part, it's bad; (1) if you have `b = a.as_strided((4, 4), (16, 1), 16)`, then it'll make `b == a` in the deepcopied implementation, which is completely wrong; and (2) even if you have `b = a.detach()`, these are still initially two different tensors which become the same tensor after the old deepcopy implementation.

The new implementation only groups together tensors that have the same identity. This is a partial fix, but it's more reasonable. What changes:
* (becomes more correct): different views of the same base tensor will no longer all become equal after deepcopying
* (still kind of wrong): views won't actually alias each other after deepcopying.
* (arguably a minor regression): equivalent views of the same tensor will no longer be copied to the same tensor - so they won't alias.

BC breaking: C++ deepcopy interface changes from accepting `IValue::HashAliasedIValueMap memo` to accepting `IValue::HashIdentityIValueMap memo`. If there are objections, we can keep the old API. However, it seems likely that users generally won't try to deepcopy from C++.

Differential Revision: [D57406306](https://our.internmc.facebook.com/intern/diff/D57406306)
Pull Request resolved: pytorch#126126
Approved by: https://github.com/ezyang
  • Loading branch information
davidberard98 authored and ZelboK committed May 19, 2024
1 parent 3bbd7fa commit ac162de
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 14 deletions.
8 changes: 4 additions & 4 deletions aten/src/ATen/core/ivalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,12 @@ c10::intrusive_ptr<ivalue::Object> ivalue::Object::create(
}

IValue IValue::deepcopy(std::optional<at::Device> device) const {
IValue::HashAliasedIValueMap memo;
IValue::HashIdentityIValueMap memo;
return deepcopy(memo, device);
}

IValue IValue::deepcopy(
IValue::HashAliasedIValueMap& memo,
IValue::HashIdentityIValueMap& memo,
std::optional<at::Device> device) const {
if (memo.count(*this)) {
return memo.at(*this);
Expand Down Expand Up @@ -1028,12 +1028,12 @@ c10::intrusive_ptr<ivalue::Object> ivalue::Object::copy_to_weak_compilation_ref(

c10::intrusive_ptr<ivalue::Object> ivalue::Object::deepcopy(
std::optional<at::Device> device) const {
IValue::HashAliasedIValueMap memo;
IValue::HashIdentityIValueMap memo;
return deepcopy(memo, device);
}

c10::intrusive_ptr<ivalue::Object> ivalue::Object::deepcopy(
IValue::HashAliasedIValueMap& memo,
IValue::HashIdentityIValueMap& memo,
std::optional<at::Device> device) const {
auto cu = type_.cu_;
auto object = ivalue::Object::create(WeakOrStrongTypePtr(type_.cu_, type_.type_), type()->numAttributes());
Expand Down
19 changes: 18 additions & 1 deletion aten/src/ATen/core/ivalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,23 @@ struct TORCH_API IValue final {
using HashAliasedIValueMap =
std::unordered_map<IValue, IValue, HashAliasedIValue, CompAliasedIValues>;

struct HashIdentityIValue {
size_t operator()(const IValue& val) const {
return val.payload.u.as_int;
}
};

struct CompIdentityIValues {
bool operator()(const IValue& lhs, const IValue& rhs) const {
return lhs.is(rhs);
}
};

using HashIdentityIValues =
std::unordered_set<IValue, HashIdentityIValue, CompIdentityIValues>;
using HashIdentityIValueMap =
std::unordered_map<IValue, IValue, HashIdentityIValue, CompIdentityIValues>;

// Chechs if this and rhs has a subvalues in common.
// [t1,t2] and [t2, t3] returns true.
bool overlaps(const IValue& rhs) const;
Expand All @@ -1130,7 +1147,7 @@ struct TORCH_API IValue final {
void visit(const std::function<bool(const IValue&)>& visitor) const;
IValue deepcopy(std::optional<at::Device> device = c10::nullopt) const;
IValue deepcopy(
HashAliasedIValueMap& memo,
HashIdentityIValueMap& memo,
std::optional<at::Device> device = c10::nullopt) const;

private:
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/core/ivalue_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ struct C10_EXPORT ivalue::Object final : c10::intrusive_ptr_target {
std::optional<at::Device> device = c10::nullopt) const;

c10::intrusive_ptr<Object> deepcopy(
IValue::HashAliasedIValueMap& memo,
IValue::HashIdentityIValueMap& memo,
std::optional<at::Device> device = c10::nullopt) const;

bool is_weak_compilation_ref() const {
Expand Down
1 change: 1 addition & 0 deletions test/cpp/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(TORCH_API_TEST_SOURCES
${TORCH_API_TEST_DIR}/functional.cpp
${TORCH_API_TEST_DIR}/init.cpp
${TORCH_API_TEST_DIR}/integration.cpp
${TORCH_API_TEST_DIR}/ivalue.cpp
${TORCH_API_TEST_DIR}/jit.cpp
${TORCH_API_TEST_DIR}/memory.cpp
${TORCH_API_TEST_DIR}/meta_tensor.cpp
Expand Down
63 changes: 63 additions & 0 deletions test/cpp/api/ivalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <gtest/gtest.h>

#include <ATen/core/ivalue.h>

#include <c10/util/flat_hash_map.h>
#include <c10/util/irange.h>
#include <c10/util/tempfile.h>

#include <torch/torch.h>

#include <test/cpp/api/support.h>

#include <cstdio>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

using namespace torch::test;
using namespace torch::nn;
using namespace torch::optim;

TEST(IValueTest, DeepcopyTensors) {
torch::Tensor t0 = torch::randn({2, 3});
torch::Tensor t1 = torch::randn({3, 4});
torch::Tensor t2 = t0.detach();
torch::Tensor t3 = t0;
torch::Tensor t4 = t1.as_strided({2, 3}, {3, 1}, 2);
std::vector<torch::Tensor> tensor_vector = {t0, t1, t2, t3, t4};
c10::List<torch::Tensor> tensor_list(tensor_vector);
torch::IValue tensor_list_ivalue(tensor_list);

c10::IValue::CompIdentityIValues ivalue_compare;

// Make sure our setup configuration is correct
ASSERT_TRUE(ivalue_compare(tensor_list[0].get(), tensor_list[3].get()));
ASSERT_FALSE(ivalue_compare(tensor_list[0].get(), tensor_list[1].get()));
ASSERT_FALSE(ivalue_compare(tensor_list[0].get(), tensor_list[2].get()));
ASSERT_FALSE(ivalue_compare(tensor_list[1].get(), tensor_list[4].get()));
ASSERT_TRUE(tensor_list[0].get().isAliasOf(tensor_list[2].get()));

c10::IValue copied_ivalue = tensor_list_ivalue.deepcopy();
c10::List<torch::IValue> copied_list = copied_ivalue.toList();

// Make sure our setup configuration is correct
ASSERT_TRUE(ivalue_compare(copied_list[0].get(), copied_list[3].get()));
ASSERT_FALSE(ivalue_compare(copied_list[0].get(), copied_list[1].get()));
ASSERT_FALSE(ivalue_compare(copied_list[0].get(), copied_list[2].get()));
ASSERT_FALSE(ivalue_compare(copied_list[1].get(), copied_list[4].get()));
// NOTE: this is actually incorrect. Ideally, these _should_ be aliases.
ASSERT_FALSE(copied_list[0].get().isAliasOf(copied_list[2].get()));

ASSERT_TRUE(copied_list[0].get().toTensor().allclose(
tensor_list[0].get().toTensor()));
ASSERT_TRUE(copied_list[1].get().toTensor().allclose(
tensor_list[1].get().toTensor()));
ASSERT_TRUE(copied_list[2].get().toTensor().allclose(
tensor_list[2].get().toTensor()));
ASSERT_TRUE(copied_list[3].get().toTensor().allclose(
tensor_list[3].get().toTensor()));
ASSERT_TRUE(copied_list[4].get().toTensor().allclose(
tensor_list[4].get().toTensor()));
}
6 changes: 3 additions & 3 deletions torch/csrc/jit/api/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ Module Module::deepcopy(std::optional<at::Device> device) const {

Module Module::clone(bool inplace) const {
std::unordered_map<TypePtr, TypePtr> type_remap;
IValue::HashAliasedIValueMap memo;
IValue::HashIdentityIValueMap memo;
const std::unordered_set<std::string> ignored_methods;
const std::unordered_set<std::string> ignored_attributes;
return clone_impl(
Expand All @@ -335,15 +335,15 @@ Module Module::clone(
const std::unordered_set<std::string>& ignored_methods,
const std::unordered_set<std::string>& ignored_attributes) const {
std::unordered_map<TypePtr, TypePtr> type_remap;
IValue::HashAliasedIValueMap memo;
IValue::HashIdentityIValueMap memo;
return clone_impl(
type_remap, inplace, memo, ignored_methods, ignored_attributes);
}

Module Module::clone_impl(
std::unordered_map<TypePtr, TypePtr>& type_remap,
bool inplace,
IValue::HashAliasedIValueMap memo,
IValue::HashIdentityIValueMap memo,
const std::unordered_set<std::string>& ignored_methods,
const std::unordered_set<std::string>& ignored_attributes) const {
// Create a new _ivalue in the same compilation unit.
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/api/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ struct TORCH_API Module : public Object {
Module clone_impl(
std::unordered_map<TypePtr, TypePtr>& type_remap,
bool inplace,
IValue::HashAliasedIValueMap memo,
IValue::HashIdentityIValueMap memo,
const std::unordered_set<std::string>& ignored_methods,
const std::unordered_set<std::string>& ignored_attributes) const;

Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/jit/passes/quantization/insert_observers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ModuleCloneHelper {
const ModuleQConfigMap& module_qconfig_map,
bool inplace = false) {
std::unordered_map<TypePtr, QConfigTypePtrMap> type_remap;
IValue::HashAliasedIValueMap memo;
IValue::HashIdentityIValueMap memo;
return clone_impl(
module, module_qconfig_map, type_remap, inplace, std::move(memo));
}
Expand All @@ -103,7 +103,7 @@ class ModuleCloneHelper {
const ModuleQConfigMap& module_qconfig_map,
std::unordered_map<TypePtr, QConfigTypePtrMap>& type_remap,
bool inplace,
IValue::HashAliasedIValueMap memo) {
IValue::HashIdentityIValueMap memo) {
auto qconfig = module_qconfig_map.at(module._ivalue());
auto type = module.type();
// Create a new _ivalue in the same compilation unit.
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/jit/python/script_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,13 @@ static constexpr std::array<const char*, 48> magic_method_names = {
};

struct DeepCopyMemoTable {
std::shared_ptr<IValue::HashAliasedIValueMap> map;
std::shared_ptr<IValue::HashIdentityIValueMap> map;
};

IValue pyIValueDeepcopy(const IValue& ivalue, const py::dict& memo) {
if (!memo.contains(py::str("__torch_script_memo_table"))) {
memo["__torch_script_memo_table"] =
DeepCopyMemoTable{std::make_shared<IValue::HashAliasedIValueMap>()};
DeepCopyMemoTable{std::make_shared<IValue::HashIdentityIValueMap>()};
}
auto& ivalue_memo =
*py::cast<DeepCopyMemoTable>(memo["__torch_script_memo_table"]).map;
Expand Down

0 comments on commit ac162de

Please sign in to comment.