forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer_test.cc
182 lines (159 loc) · 4.89 KB
/
observer_test.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <gtest/gtest.h>
#include "c10/util/Registry.h"
#include "caffe2/core/common.h"
#include "caffe2/core/net.h"
#include "caffe2/core/net_simple.h"
#include "caffe2/core/observer.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/scope_guard.h"
namespace caffe2 {
namespace {
static std::atomic<int> counter;
template <class T>
class DummyObserver final : public ObserverBase<T> {
public:
explicit DummyObserver<T>(T* subject_) : ObserverBase<T>(subject_) {}
void Start() override;
void Stop() override;
~DummyObserver() override {}
};
template <>
void DummyObserver<NetBase>::Start() {
vector<OperatorBase*> operators = subject_->GetOperators();
for (auto& op : operators) {
op->AttachObserver(std::make_unique<DummyObserver<OperatorBase>>(op));
}
counter.fetch_add(1000);
}
template <>
void DummyObserver<OperatorBase>::Start() {
counter.fetch_add(100);
}
template <>
void DummyObserver<NetBase>::Stop() {
counter.fetch_add(10);
}
template <>
void DummyObserver<OperatorBase>::Stop() {
counter.fetch_add(1);
}
class ObsTestDummyOp final : public OperatorBase {
public:
using OperatorBase::OperatorBase;
bool Run(int /* unused */) override {
StartAllObservers();
StopAllObservers();
return true;
}
};
REGISTER_CPU_OPERATOR(ObsTestDummy, ObsTestDummyOp);
REGISTER_CUDA_OPERATOR(ObsTestDummy, ObsTestDummyOp);
OPERATOR_SCHEMA(ObsTestDummy)
.NumInputs(0, INT_MAX)
.NumOutputs(0, INT_MAX)
.AllowInplace({{0, 0}, {1, 1}});
unique_ptr<NetBase> CreateNetTestHelper(Workspace* ws, bool isDAG = false) {
NetDef net_def;
if (isDAG) {
net_def.set_type("dag");
}
{
auto& op = *(net_def.add_op());
op.set_type("ObsTestDummy");
op.add_input("in");
op.add_output("hidden");
}
{
auto& op = *(net_def.add_op());
op.set_type("ObsTestDummy");
op.add_input("hidden");
op.add_output("out");
}
net_def.add_external_input("in");
net_def.add_external_output("out");
return CreateNet(net_def, ws);
}
}
TEST(ObserverTest, TestNotify) {
auto count_before = counter.load();
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
unique_ptr<NetBase> net(CreateNetTestHelper(&ws));
EXPECT_EQ(caffe2::dynamic_cast_if_rtti<SimpleNet*>(net.get()), net.get());
unique_ptr<DummyObserver<NetBase>> net_ob =
make_unique<DummyObserver<NetBase>>(net.get());
net.get()->AttachObserver(std::move(net_ob));
net.get()->Run();
auto count_after = counter.load();
EXPECT_EQ(1212, count_after - count_before);
}
TEST(ObserverTest, TestUniqueMap) {
auto count_before = counter.load();
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
unique_ptr<NetBase> net(CreateNetTestHelper(&ws));
EXPECT_EQ(caffe2::dynamic_cast_if_rtti<SimpleNet*>(net.get()), net.get());
unique_ptr<DummyObserver<NetBase>> net_ob =
make_unique<DummyObserver<NetBase>>(net.get());
auto* ref = net.get()->AttachObserver(std::move(net_ob));
net.get()->Run();
unique_ptr<Observable<NetBase>::Observer> test =
net.get()->DetachObserver(ref);
auto count_after = counter.load();
EXPECT_EQ(1212, count_after - count_before);
}
TEST(ObserverTest, TestNotifyAfterDetach) {
auto count_before = counter.load();
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
unique_ptr<NetBase> net(CreateNetTestHelper(&ws));
unique_ptr<DummyObserver<NetBase>> net_ob =
make_unique<DummyObserver<NetBase>>(net.get());
auto* ob = net.get()->AttachObserver(std::move(net_ob));
net.get()->DetachObserver(ob);
net.get()->Run();
auto count_after = counter.load();
EXPECT_EQ(0, count_after - count_before);
}
TEST(ObserverTest, TestDAGNetBase) {
auto count_before = counter.load();
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
unique_ptr<NetBase> net(CreateNetTestHelper(&ws, true));
unique_ptr<DummyObserver<NetBase>> net_ob =
make_unique<DummyObserver<NetBase>>(net.get());
net.get()->AttachObserver(std::move(net_ob));
net.get()->Run();
auto count_after = counter.load();
EXPECT_EQ(1212, count_after - count_before);
}
#if 0
// This test intermittently segfaults,
// see https://github.com/pytorch/pytorch/issues/9137
TEST(ObserverTest, TestMultipleNetBase) {
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
unique_ptr<NetBase> net(CreateNetTestHelper(&ws, true));
EXPECT_EQ(caffe2::dynamic_cast_if_rtti<NetBase*>(net.get()), net.get());
// There may be some default observers
const size_t prev_num = net.get()->NumObservers();
const int num_tests = 100;
vector<const Observable<NetBase>::Observer*> observers;
for (int i = 0; i < num_tests; ++i) {
unique_ptr<DummyObserver<NetBase>> net_ob =
make_unique<DummyObserver<NetBase>>(net.get());
observers.emplace_back(net.get()->AttachObserver(std::move(net_ob)));
}
net.get()->Run();
for (const auto& observer : observers) {
net.get()->DetachObserver(observer);
}
EXPECT_EQ(net.get()->NumObservers(), prev_num);
}
#endif
} // namespace caffe2