serialization.cpp (8496B)
1 #include <cctype> 2 #include <fstream> 3 #include <ios> 4 5 #define BOOST_TEST_DYN_LINK 6 #define BOOST_TEST_MODULE serialization 7 #include <boost/archive/text_iarchive.hpp> 8 #include <boost/archive/text_oarchive.hpp> 9 #include <boost/make_shared.hpp> 10 #include <boost/shared_ptr.hpp> 11 #include <boost/test/unit_test.hpp> 12 13 #include "../nmlp_iostream_exports.hpp" 14 15 16 template<class T> 17 void write_and_read(T &out, T &in){ 18 { 19 std::ofstream ofs("test_serialization_tmp"); 20 boost::archive::text_oarchive oa(ofs); 21 oa << out; 22 } 23 { 24 std::ifstream ifs("test_serialization_tmp"); 25 boost::archive::text_iarchive ia(ifs); 26 ia >> in; 27 } 28 } 29 30 #define TEST_MATRIX( NAME ) \ 31 BOOST_AUTO_TEST_CASE( NAME ## _test ){ \ 32 boost::shared_ptr<NAME> out=boost::make_shared<NAME>(12, 42), in; \ 33 for(std::size_t y=0; y<12; ++y) \ 34 for(std::size_t x=0; x<42; ++x) \ 35 out->setValue(y, x, y*3+x*7); \ 36 write_and_read(out, in); \ 37 BOOST_CHECK_EQUAL(in->getNumberOfRows(), 12); \ 38 BOOST_CHECK_EQUAL(in->getNumberOfColumns(), 42); \ 39 for(std::size_t y=0; y<12; ++y) \ 40 for(std::size_t x=0; x<42; ++x) \ 41 BOOST_CHECK_CLOSE(in->getValue(y, x), y*3+x*7, 1e-5); \ 42 } 43 44 TEST_MATRIX(CPUMatrix) 45 TEST_MATRIX(GPUMatrix) 46 TEST_MATRIX(CPUSparseMatrix) 47 48 BOOST_AUTO_TEST_CASE( Tensor_test ){ 49 boost::shared_ptr<CPUMatrix> m0_out=boost::make_shared<CPUMatrix>(12, 42), m0_in; 50 boost::shared_ptr<GPUMatrix> m1_out=boost::make_shared<GPUMatrix>(12, 42), m1_in; 51 boost::shared_ptr<CPUSparseMatrix> m2_out=boost::make_shared<CPUSparseMatrix>(12, 42), m2_in; 52 for(std::size_t y=0; y<12; ++y) 53 for(std::size_t x=0; x<42; ++x){ 54 m0_out->setValue(y, x, y*2+x*3); 55 m1_out->setValue(y, x, y*5+x*7); 56 m2_out->setValue(y, x, y*3+x*5); 57 } 58 boost::shared_ptr<Tensor> out=boost::make_shared<Tensor>(3), in; 59 out->setMatrix(0, m0_out); 60 out->setMatrix(1, m1_out); 61 out->setMatrix(2, m2_out); 62 63 write_and_read(out, in); 64 BOOST_CHECK_EQUAL(in->getNumberOfMatrices(), 3); 65 m0_in=boost::dynamic_pointer_cast<CPUMatrix>(in->getMatrix(0)); 66 m1_in=boost::dynamic_pointer_cast<GPUMatrix>(in->getMatrix(1)); 67 m2_in=boost::dynamic_pointer_cast<CPUSparseMatrix>(in->getMatrix(2)); 68 BOOST_CHECK_NE(m0_in, boost::shared_ptr<CPUMatrix>()); 69 BOOST_CHECK_NE(m1_in, boost::shared_ptr<GPUMatrix>()); 70 BOOST_CHECK_NE(m2_in, boost::shared_ptr<CPUSparseMatrix>()); 71 for(std::size_t y=0; y<12; ++y) 72 for(std::size_t x=0; x<42; ++x){ 73 BOOST_CHECK_CLOSE(m0_in->getValue(y, x), y*2+x*3, 1e-5); 74 BOOST_CHECK_CLOSE(m1_in->getValue(y, x), y*5+x*7, 1e-5); 75 BOOST_CHECK_CLOSE(m2_in->getValue(y, x), y*3+x*5, 1e-5); 76 } 77 } 78 79 #define TEST_SIZE_CLASS( NAME, PARENT ) \ 80 BOOST_AUTO_TEST_CASE( NAME ## _test ){ \ 81 boost::shared_ptr<PARENT> out=boost::make_shared<NAME>(42), in; \ 82 write_and_read(out, in); \ 83 boost::shared_ptr<NAME> conv=boost::dynamic_pointer_cast<NAME>(in); \ 84 BOOST_CHECK_NE(conv, boost::shared_ptr<NAME>()); \ 85 BOOST_CHECK_EQUAL(conv.get()->*get(NAME ## _size_robber_tag()), 42); \ 86 } 87 88 TEST_SIZE_CLASS(CPUSquareLoss, Criterion) 89 TEST_SIZE_CLASS(CPUHingeLoss, Criterion) 90 TEST_SIZE_CLASS(GPUSquareLoss, Criterion) 91 TEST_SIZE_CLASS(GPUHingeLoss, Criterion) 92 93 BOOST_AUTO_TEST_CASE( CPUMaxLoss_test ){ 94 boost::shared_ptr<Criterion> out=boost::make_shared<CPUMaxLoss>(), in; 95 write_and_read(out, in); 96 boost::shared_ptr<CPUMaxLoss> conv=boost::dynamic_pointer_cast<CPUMaxLoss>(in); 97 BOOST_CHECK_NE(conv, boost::shared_ptr<CPUMaxLoss>()); 98 } 99 100 TEST_SIZE_CLASS(CPUIdentity, Module) 101 TEST_SIZE_CLASS(CPULogistic, Module) 102 TEST_SIZE_CLASS(CPUPositiveShrink, Module) 103 TEST_SIZE_CLASS(CPUTanH, Module) 104 TEST_SIZE_CLASS(GPUIdentity, Module) 105 TEST_SIZE_CLASS(GPUPositiveShrink, Module) 106 TEST_SIZE_CLASS(GPUTanH, Module) 107 108 BOOST_AUTO_TEST_CASE( CPUConcatenation_test ){ 109 boost::shared_ptr<Module> out=boost::make_shared<CPUConcatenation>(42, 12), in; 110 write_and_read(out, in); 111 boost::shared_ptr<CPUConcatenation> conv=boost::dynamic_pointer_cast<CPUConcatenation>(in); 112 BOOST_CHECK_NE(conv, boost::shared_ptr<CPUConcatenation>()); 113 BOOST_CHECK_EQUAL(conv.get()->*get(CPUConcatenation_size1_robber_tag()), 42); 114 BOOST_CHECK_EQUAL(conv.get()->*get(CPUConcatenation_size2_robber_tag()), 12); 115 } 116 117 BOOST_AUTO_TEST_CASE( CPUUnique_test ){ 118 boost::shared_ptr<CPUUnique> out=boost::make_shared<CPUUnique>(12), in; 119 for(std::size_t i=0; i<12; ++i) 120 out->setValue(0, i, i*42); 121 write_and_read(out, in); 122 BOOST_CHECK_EQUAL((in.get()->*get(CPUUnique_parameters_robber_tag()))->getNumberOfColumns(), 12); 123 for(std::size_t i=0; i<12; ++i) 124 BOOST_CHECK_CLOSE((in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*42, 1e-5); 125 } 126 127 BOOST_AUTO_TEST_CASE( GPUUnique_test ){ 128 boost::shared_ptr<GPUUnique> out=boost::make_shared<GPUUnique>(12), in; 129 for(std::size_t i=0; i<12; ++i) 130 (out.get()->*get(GPUUnique_parameters_robber_tag()))->setValue(0, i, i*42); 131 write_and_read(out, in); 132 BOOST_CHECK_EQUAL((in.get()->*get(GPUUnique_parameters_robber_tag()))->getNumberOfColumns(), 12); 133 for(std::size_t i=0; i<12; ++i) 134 BOOST_CHECK_CLOSE((in.get()->*get(GPUUnique_parameters_robber_tag()))->getValue(0, i), i*42, 1e-5); 135 } 136 137 #define TEST_LINEAR_MODULE( NAME ) \ 138 BOOST_AUTO_TEST_CASE( NAME ## _test ){ \ 139 boost::shared_ptr<NAME> out=boost::make_shared<NAME>(4, 2), in; \ 140 for(std::size_t i=0; i<8; ++i) \ 141 (out.get()->*get(TensorModule_parameters_robber_tag()))->setValue(i>>1, i&1, i*3); \ 142 write_and_read(out, in); \ 143 for(std::size_t i=0; i<8; ++i) \ 144 BOOST_CHECK_CLOSE((out.get()->*get(TensorModule_parameters_robber_tag()))->getValue(i>>1, i&1), i*3, 1e-5); \ 145 } 146 147 TEST_LINEAR_MODULE(CPULinear) 148 TEST_LINEAR_MODULE(GPULinear) 149 TEST_LINEAR_MODULE(CPUSparseLinear) 150 151 BOOST_AUTO_TEST_CASE( TableModule_test ){ 152 boost::shared_ptr<CPUUnique> m0_out=boost::make_shared<CPUUnique>(12), m1_out=boost::make_shared<CPUUnique>(12), m2_out=boost::make_shared<CPUUnique>(12), m0_in, m1_in, m2_in; 153 boost::shared_ptr<TableModule> out=boost::make_shared<TableModule>(), in; 154 for(std::size_t i=0; i<12; ++i){ 155 m0_out->setValue(0, i, i*3); 156 m1_out->setValue(0, i, i*5); 157 m2_out->setValue(0, i, i*7); 158 } 159 out->addModule(m0_out); 160 out->addModule(m1_out); 161 out->addModule(m2_out); 162 163 write_and_read(out, in); 164 165 BOOST_CHECK_EQUAL((out.get()->*get(TableModule_modules_robber_tag())).size(), 3); 166 m0_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(TableModule_modules_robber_tag()))[0]); 167 m1_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(TableModule_modules_robber_tag()))[1]); 168 m2_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(TableModule_modules_robber_tag()))[2]); 169 BOOST_CHECK_NE(m0_in, boost::shared_ptr<CPUUnique>()); 170 BOOST_CHECK_NE(m1_in, boost::shared_ptr<CPUUnique>()); 171 BOOST_CHECK_NE(m2_in, boost::shared_ptr<CPUUnique>()); 172 173 for(std::size_t i=0; i<12; ++i){ 174 BOOST_CHECK_CLOSE((m0_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*3, 1e-5); 175 BOOST_CHECK_CLOSE((m1_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*5, 1e-5); 176 BOOST_CHECK_CLOSE((m2_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*7, 1e-5); 177 } 178 } 179 180 BOOST_AUTO_TEST_CASE( SequentialModule_test ){ 181 boost::shared_ptr<CPUUnique> m0_out=boost::make_shared<CPUUnique>(12), m1_out=boost::make_shared<CPUUnique>(12), m2_out=boost::make_shared<CPUUnique>(12), m0_in, m1_in, m2_in; 182 boost::shared_ptr<SequentialModule> out=boost::make_shared<SequentialModule>(), in; 183 for(std::size_t i=0; i<12; ++i){ 184 m0_out->setValue(0, i, i*3); 185 m1_out->setValue(0, i, i*5); 186 m2_out->setValue(0, i, i*7); 187 } 188 out->addModule(m0_out); 189 out->addModule(m1_out); 190 out->addModule(m2_out); 191 192 write_and_read(out, in); 193 194 BOOST_CHECK_EQUAL((out.get()->*get(SequentialModule_modules_robber_tag())).size(), 3); 195 m0_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(SequentialModule_modules_robber_tag()))[0]); 196 m1_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(SequentialModule_modules_robber_tag()))[1]); 197 m2_in=boost::dynamic_pointer_cast<CPUUnique>((out.get()->*get(SequentialModule_modules_robber_tag()))[2]); 198 BOOST_CHECK_NE(m0_in, boost::shared_ptr<CPUUnique>()); 199 BOOST_CHECK_NE(m1_in, boost::shared_ptr<CPUUnique>()); 200 BOOST_CHECK_NE(m2_in, boost::shared_ptr<CPUUnique>()); 201 202 for(std::size_t i=0; i<12; ++i){ 203 BOOST_CHECK_CLOSE((m0_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*3, 1e-5); 204 BOOST_CHECK_CLOSE((m1_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*5, 1e-5); 205 BOOST_CHECK_CLOSE((m2_in.get()->*get(CPUUnique_parameters_robber_tag()))->getValue(0, i), i*7, 1e-5); 206 } 207 } 208