mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +08:00
Copy of #126089, with some additional fixes & tests Partial fix for #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: https://github.com/pytorch/pytorch/pull/126126 Approved by: https://github.com/ezyang
64 lines
2.3 KiB
C++
64 lines
2.3 KiB
C++
#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()));
|
|
}
|