forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivalue.cpp
63 lines (51 loc) · 2.33 KB
/
ivalue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()));
}