forked from csarofeen/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameterlist.cpp
163 lines (142 loc) · 5.75 KB
/
parameterlist.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <gtest/gtest.h>
#include <c10/util/irange.h>
#include <torch/torch.h>
#include <algorithm>
#include <memory>
#include <vector>
#include <test/cpp/api/support.h>
using namespace torch::nn;
using namespace torch::test;
struct ParameterListTest : torch::test::SeedingFixture {};
TEST_F(ParameterListTest, ConstructsFromSharedPointer) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
ASSERT_TRUE(ta.requires_grad());
ASSERT_FALSE(tb.requires_grad());
ParameterList list(ta, tb, tc);
ASSERT_EQ(list->size(), 3);
}
TEST_F(ParameterListTest, isEmpty) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
ParameterList list;
ASSERT_TRUE(list->is_empty());
list->append(ta);
ASSERT_FALSE(list->is_empty());
ASSERT_EQ(list->size(), 1);
}
TEST_F(ParameterListTest, PushBackAddsAnElement) {
ParameterList list;
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
torch::Tensor td = torch::randn({1, 2, 3});
ASSERT_EQ(list->size(), 0);
ASSERT_TRUE(list->is_empty());
list->append(ta);
ASSERT_EQ(list->size(), 1);
list->append(tb);
ASSERT_EQ(list->size(), 2);
list->append(tc);
ASSERT_EQ(list->size(), 3);
list->append(td);
ASSERT_EQ(list->size(), 4);
}
TEST_F(ParameterListTest, ForEachLoop) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
torch::Tensor td = torch::randn({1, 2, 3});
ParameterList list(ta, tb, tc, td);
std::vector<torch::Tensor> params = {ta, tb, tc, td};
ASSERT_EQ(list->size(), 4);
int idx = 0;
for (const auto& pair : *list) {
ASSERT_TRUE(
torch::all(torch::eq(pair.value(), params[idx++])).item<bool>());
}
}
TEST_F(ParameterListTest, AccessWithAt) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
torch::Tensor td = torch::randn({1, 2, 3});
std::vector<torch::Tensor> params = {ta, tb, tc, td};
ParameterList list;
for (auto& param : params) {
list->append(param);
}
ASSERT_EQ(list->size(), 4);
// returns the correct module for a given index
for (const auto i : c10::irange(params.size())) {
ASSERT_TRUE(torch::all(torch::eq(list->at(i), params[i])).item<bool>());
}
for (const auto i : c10::irange(params.size())) {
ASSERT_TRUE(torch::all(torch::eq(list[i], params[i])).item<bool>());
}
// throws for a bad index
ASSERT_THROWS_WITH(list->at(params.size() + 100), "Index out of range");
ASSERT_THROWS_WITH(list->at(params.size() + 1), "Index out of range");
ASSERT_THROWS_WITH(list[params.size() + 1], "Index out of range");
}
TEST_F(ParameterListTest, ExtendPushesParametersFromOtherParameterList) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
torch::Tensor td = torch::randn({1, 2, 3});
torch::Tensor te = torch::randn({1, 2});
torch::Tensor tf = torch::randn({1, 2, 3});
ParameterList a(ta, tb);
ParameterList b(tc, td);
a->extend(*b);
ASSERT_EQ(a->size(), 4);
ASSERT_TRUE(torch::all(torch::eq(a[0], ta)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(a[1], tb)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(a[2], tc)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(a[3], td)).item<bool>());
ASSERT_EQ(b->size(), 2);
ASSERT_TRUE(torch::all(torch::eq(b[0], tc)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(b[1], td)).item<bool>());
std::vector<torch::Tensor> c = {te, tf};
b->extend(c);
ASSERT_EQ(b->size(), 4);
ASSERT_TRUE(torch::all(torch::eq(b[0], tc)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(b[1], td)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(b[2], te)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(b[3], tf)).item<bool>());
}
TEST_F(ParameterListTest, PrettyPrintParameterList) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
ParameterList list(ta, tb, tc);
ASSERT_EQ(
c10::str(list),
"torch::nn::ParameterList(\n"
"(0): Parameter containing: [Float of size [1, 2]]\n"
"(1): Parameter containing: [Float of size [1, 2]]\n"
"(2): Parameter containing: [Float of size [1, 2]]\n"
")");
}
TEST_F(ParameterListTest, IncrementAdd) {
torch::Tensor ta = torch::randn({1, 2}, torch::requires_grad(true));
torch::Tensor tb = torch::randn({1, 2}, torch::requires_grad(false));
torch::Tensor tc = torch::randn({1, 2});
torch::Tensor td = torch::randn({1, 2, 3});
torch::Tensor te = torch::randn({1, 2});
torch::Tensor tf = torch::randn({1, 2, 3});
ParameterList listA(ta, tb, tc);
ParameterList listB(td, te, tf);
std::vector<torch::Tensor> tensors{ta, tb, tc, td, te, tf};
int idx = 0;
*listA += *listB;
ASSERT_TRUE(torch::all(torch::eq(listA[0], ta)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(listA[1], tb)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(listA[2], tc)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(listA[3], td)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(listA[4], te)).item<bool>());
ASSERT_TRUE(torch::all(torch::eq(listA[5], tf)).item<bool>());
for (const auto& P : listA->named_parameters(false))
ASSERT_TRUE(torch::all(torch::eq(P.value(), tensors[idx++])).item<bool>());
ASSERT_EQ(idx, 6);
}