Skip to content

Commit 1df773f

Browse files
committed
Added unit test for new model update strategy
1 parent 9cc342c commit 1df773f

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

include/chimbuko/param/hbos_param.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ namespace chimbuko {
195195
* @brief Set the algorithm parameters from the input JSON structure
196196
*/
197197
void set_json(const nlohmann::json &from) override;
198+
199+
bool operator==(const HbosParam &r) const;
200+
inline bool operator!=(const HbosParam &r) const{ return !(*this == r); }
198201

199202
private:
200203
/**

src/ad/ADOutlier.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void ADOutlier::updateGlobalModel()
112112
timer.start();
113113

114114
if ( m_sync_call_count % m_global_model_sync_freq == 0 ){
115+
verboseStream << "ADOutlier performing synchronization of local and global model" << std::endl;
115116
PerfTimer utimer;
116117
utimer.start();
117118

@@ -123,6 +124,8 @@ void ADOutlier::updateGlobalModel()
123124
m_perf->add("param_sent_MB", (double)msgsz.first / 1000000.0); // MB
124125
m_perf->add("param_recv_MB", (double)msgsz.second / 1000000.0); // MB
125126
}
127+
}else{
128+
verboseStream << "ADOutlier NOT synchronizing global model on this step" << std::endl;
126129
}
127130

128131
++m_sync_call_count;

src/param/hbos_param.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ void HbosParam::generate_histogram(const unsigned long func_id, const std::vecto
224224
verboseStream << hist << std::endl;
225225
}
226226
}
227+
228+
229+
bool HbosParam::operator==(const HbosParam &r) const{
230+
for(auto it = r.m_hbosstats.begin(); it != r.m_hbosstats.end(); ++it){
231+
auto lit = this->m_hbosstats.find(it->first);
232+
if(lit == this->m_hbosstats.end()) return false;
233+
if(lit->second != it->second) return false;
234+
}
235+
return true;
236+
}

test/unit_tests/ad/ADOutlier.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class ADOutlierHBOSTest: public ADOutlierHBOS{
4848
( (HbosParam*)m_param )->copy(p);
4949
}
5050

51+
HbosParam & get_local_parameters() const{ return *((HbosParam*)m_local_param); }
52+
53+
void test_updateGlobalModel(){ this->updateGlobalModel(); }
5154
};
5255

5356
class ADOutlierCOPODTest: public ADOutlierCOPOD{
@@ -107,6 +110,67 @@ TEST(ADOutlierHBOSTestSyncParamWithoutPS, Works){
107110
EXPECT_EQ(local_params_ps.get_hbosstats(), g->get_hbosstats());
108111
}
109112

113+
114+
115+
116+
TEST(ADOutlierHBOSTestSyncParamFrequencyWithoutPS, Works){
117+
HbosParam local_params_ps;
118+
119+
std::default_random_engine gen;
120+
std::normal_distribution<double> dist(500.,100.), dist2(1000.,200.);
121+
122+
int N = 50;
123+
124+
ADOutlierHBOSTest outlier;
125+
outlier.setGlobalModelSyncFrequency(3);
126+
127+
HbosParam &local_params = outlier.get_local_parameters();
128+
HbosParam l, l2, g;
129+
{
130+
Histogram &h = l[0].getHistogram();
131+
std::vector<double> runtime(N);
132+
for(int i=0;i<N;i++) runtime[i] = dist(gen);
133+
h.create_histogram(runtime);
134+
}
135+
136+
const HbosParam & global_params = *( (HbosParam const*)outlier.get_global_parameters() );
137+
138+
//On first update we expect the global model to be set to be the same as the local model, then the local model flushed
139+
local_params.assign(l);
140+
g.assign(l);
141+
outlier.test_updateGlobalModel();
142+
143+
EXPECT_EQ(g, global_params);
144+
EXPECT_EQ(local_params.size(), 0);
145+
146+
//Second update should maintain the state of the local model
147+
local_params.assign(l);
148+
outlier.test_updateGlobalModel();
149+
150+
EXPECT_EQ(g, global_params);
151+
EXPECT_EQ(local_params.size(), 1);
152+
EXPECT_EQ(l, local_params);
153+
154+
//Third update we update the state of the local model as if we were adding new data, but the global model will again remain fixed
155+
local_params.update(l);
156+
l2.assign(l);
157+
l2.update(l);
158+
outlier.test_updateGlobalModel();
159+
160+
EXPECT_EQ(g, global_params);
161+
EXPECT_EQ(local_params.size(), 1);
162+
EXPECT_EQ(l2, local_params);
163+
164+
//Fourth update should update the global model and flush the local
165+
local_params.update(l);
166+
l2.update(l);
167+
g.update(l2);
168+
outlier.test_updateGlobalModel();
169+
170+
EXPECT_EQ(g, global_params);
171+
EXPECT_EQ(local_params.size(), 0);
172+
}
173+
110174
TEST(ADOutlierSSTDTestSyncParamWithoutPS, Works){
111175
SstdParam local_params_ps;
112176

0 commit comments

Comments
 (0)