From 6373291c7787c83335cc64d56294756872493301 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Tue, 8 Aug 2017 19:34:57 +0800 Subject: [PATCH] add test case use_mkldnn_wgt --- paddle/gserver/layers/MkldnnBase.h | 2 - paddle/gserver/layers/MkldnnFcLayer.cpp | 4 ++ paddle/gserver/layers/MkldnnLayer.h | 3 ++ paddle/gserver/tests/MkldnnTester.cpp | 60 +++++++++++-------------- paddle/gserver/tests/MkldnnTester.h | 4 +- paddle/gserver/tests/test_Mkldnn.cpp | 1 + paddle/trainer/TrainerConfigHelper.cpp | 2 + paddle/utils/Flags.cpp | 1 + paddle/utils/Flags.h | 1 + python/paddle/trainer/config_parser.py | 5 ++- 10 files changed, 45 insertions(+), 38 deletions(-) diff --git a/paddle/gserver/layers/MkldnnBase.h b/paddle/gserver/layers/MkldnnBase.h index eba72e58e57..260dbe45e44 100644 --- a/paddle/gserver/layers/MkldnnBase.h +++ b/paddle/gserver/layers/MkldnnBase.h @@ -23,8 +23,6 @@ typedef enum { DNN_TESTS = 1, DNN_SIZES, DNN_FMTS, - DNN_TESTS_DETAILS, - DNN_TESTS_MORE, DNN_ALL, } DNN_LOG_LEVEL; diff --git a/paddle/gserver/layers/MkldnnFcLayer.cpp b/paddle/gserver/layers/MkldnnFcLayer.cpp index 29b2cc184d3..7e09ed33d26 100644 --- a/paddle/gserver/layers/MkldnnFcLayer.cpp +++ b/paddle/gserver/layers/MkldnnFcLayer.cpp @@ -51,6 +51,10 @@ bool MkldnnFcLayer::init(const LayerMap& layerMap, } void MkldnnFcLayer::cvtWgtFromPaddle() { + if (FLAGS_use_mkldnn_wgt) { + return; + } + if (hasInitedWgt_) { return; } diff --git a/paddle/gserver/layers/MkldnnLayer.h b/paddle/gserver/layers/MkldnnLayer.h index a9eb9f79da2..c653eb99850 100644 --- a/paddle/gserver/layers/MkldnnLayer.h +++ b/paddle/gserver/layers/MkldnnLayer.h @@ -19,6 +19,9 @@ limitations under the License. */ #include "MkldnnBase.h" #include "mkldnn.hpp" +DECLARE_bool(use_mkldnn); +DECLARE_bool(use_mkldnn_wgt); + namespace paddle { class MkldnnLayer; diff --git a/paddle/gserver/tests/MkldnnTester.cpp b/paddle/gserver/tests/MkldnnTester.cpp index ecf0f9124db..ef99b384a93 100644 --- a/paddle/gserver/tests/MkldnnTester.cpp +++ b/paddle/gserver/tests/MkldnnTester.cpp @@ -118,7 +118,7 @@ void MkldnnTester::checkForward() { printTopDatas(); double delta = compareMatrix(testLayers_[DNN]->getOutputValue(), testLayers_[REF]->getOutputValue()); - VLOG(DNN_TESTS_DETAILS) << "Check Forward"; + VLOG(DNN_ALL) << "Check Forward"; EXPECT_LE(fabs(delta), eps_); } @@ -162,7 +162,7 @@ void MkldnnTester::checkBackwardWgts() { EXPECT_LE(fabs(delta), eps_); } - VLOG(DNN_TESTS_DETAILS) << "Restore dnn weights before comapre"; + VLOG(DNN_ALL) << "Restore dnn weights before comapre"; restoreWgt(dnnWgts, parameters_[DNN]); } @@ -275,8 +275,8 @@ double MkldnnTester::getDelta(const real* d1, EXPECT_TRUE(std::isnormal(sum)); EXPECT_FALSE(std::isinf(sum)); EXPECT_FALSE(std::isnan(delta)); - VLOG(DNN_TESTS_MORE) << "reference avg data: " << sum / len - << ", delta: " << delta / sum << ", failCnt:" << failCnt; + VLOG(DNN_ALL) << "reference avg data: " << sum / len + << ", delta: " << delta / sum << ", failCnt:" << failCnt; return (failCnt / (float)len) > failRate ? maxOut : delta / sum; } @@ -330,43 +330,37 @@ void MkldnnTester::run(const TestConfig& dnn, log_ = log; lvl_ = level; - // Firstly always set flag false to initial from paddle weight - TestConfig first = dnn; - + // Firstly test FLAGS_use_mkldnn_wgt = false + FLAGS_use_mkldnn_wgt = false; // reset and run once - reset(first, ref, batchSize); + reset(dnn, ref, batchSize); randomWgtDatas(); clearWgtDiffs(); clearBotDiffs(); + for (size_t i = 0; i < iter_; ++i) { + VLOG(DNN_TESTS) << "Check Iteration " << i; + runOnce(); + } - VLOG(DNN_TESTS) << "Check Iteration 0"; - runOnce(); - - // firstly get the flag - bool initWgtFromMkldnn = false; - - if (initWgtFromMkldnn) { - // after run once the mkldnn weight has been stored in dnnlayer - // then save the weigths and restart again - vector dnnWgts, refWgts; - CHECK_EQ(parameters_[DNN].size(), parameters_[REF].size()); - saveWgt(parameters_[DNN], dnnWgts); - saveWgt(parameters_[REF], refWgts); - - // restart again with flag true - reset(dnn, ref, batchSize); + // Then test FLAGS_use_mkldnn_wgt = true + FLAGS_use_mkldnn_wgt = true; + // after run once the mkldnn weight has been stored in dnnlayer + // then save the weigths and restart again + vector dnnWgts, refWgts; + CHECK_EQ(parameters_[DNN].size(), parameters_[REF].size()); + saveWgt(parameters_[DNN], dnnWgts); + saveWgt(parameters_[REF], refWgts); - // restore wgt - restoreWgt(dnnWgts, parameters_[DNN]); - restoreWgt(refWgts, parameters_[REF]); - clearWgtDiffs(); - clearBotDiffs(); + // restart again with flag true + reset(dnn, ref, batchSize); - // at least run once - runOnce(); - } + // restore wgt + restoreWgt(dnnWgts, parameters_[DNN]); + restoreWgt(refWgts, parameters_[REF]); + clearWgtDiffs(); + clearBotDiffs(); - for (size_t i = 1; i < iter_; ++i) { + for (size_t i = 0; i < iter_; ++i) { VLOG(DNN_TESTS) << "Check Iteration " << i; runOnce(); } diff --git a/paddle/gserver/tests/MkldnnTester.h b/paddle/gserver/tests/MkldnnTester.h index 16b0970a8ec..8b3049b5c26 100644 --- a/paddle/gserver/tests/MkldnnTester.h +++ b/paddle/gserver/tests/MkldnnTester.h @@ -58,7 +58,7 @@ public: iter_ = iter; eps_ = epsilon; log_ = false; - lvl_ = DNN_TESTS_MORE; + lvl_ = DNN_ALL; } ~MkldnnTester() {} @@ -72,7 +72,7 @@ public: size_t iter = 3, float epsilon = 1e-4, bool log = false, - int level = DNN_TESTS_MORE); + int level = DNN_ALL); void setLogLevel(int lvl) { lvl_ = lvl; } private: diff --git a/paddle/gserver/tests/test_Mkldnn.cpp b/paddle/gserver/tests/test_Mkldnn.cpp index 1d367e61803..0516a059de0 100644 --- a/paddle/gserver/tests/test_Mkldnn.cpp +++ b/paddle/gserver/tests/test_Mkldnn.cpp @@ -23,6 +23,7 @@ using namespace paddle; // NOLINT DECLARE_bool(thread_local_rand_use_global_seed); DECLARE_bool(use_gpu); DECLARE_bool(use_mkldnn); +DECLARE_bool(use_mkldnn_wgt); struct testFCDesc { int bs; diff --git a/paddle/trainer/TrainerConfigHelper.cpp b/paddle/trainer/TrainerConfigHelper.cpp index a0a365aa0bb..eba40862b92 100644 --- a/paddle/trainer/TrainerConfigHelper.cpp +++ b/paddle/trainer/TrainerConfigHelper.cpp @@ -29,6 +29,7 @@ DECLARE_bool(with_gpu); DECLARE_bool(parallel_nn); DECLARE_string(config_args); DECLARE_bool(use_mkldnn); +DECLARE_bool(use_mkldnn_wgt); const char *kConfigParserModuleName = "paddle.trainer.config_parser"; const char *kConfigParserFuncName = "parse_config_and_serialize"; @@ -46,6 +47,7 @@ TrainerConfigHelper::TrainerConfigHelper(const std::string &configFilePath) << ",with_cost=" << FLAGS_with_cost << ",use_gpu=" << FLAGS_use_gpu << ",parallel_nn=" << FLAGS_parallel_nn << ",use_mkldnn=" << FLAGS_use_mkldnn + << ",use_mkldnn_wgt=" << FLAGS_use_mkldnn_wgt << ",cudnn_version=" << hl_get_cudnn_lib_version(); if (!FLAGS_config_args.empty()) { configArgs << "," << FLAGS_config_args; diff --git a/paddle/utils/Flags.cpp b/paddle/utils/Flags.cpp index ab1c181c62c..600c83a8487 100644 --- a/paddle/utils/Flags.cpp +++ b/paddle/utils/Flags.cpp @@ -27,6 +27,7 @@ DEFINE_bool(use_mkldnn, false, "Default still keep use CPU training"); DEFINE_bool(use_mkldnn, false, "Only support CPU training"); #endif +DEFINE_bool(use_mkldnn_wgt, false, "Init weight from CPU weight"); DEFINE_bool(parallel_nn, false, "Whether to use multi-threads to calculate one neural network." diff --git a/paddle/utils/Flags.h b/paddle/utils/Flags.h index 1832bb515ec..0aca4c0ee03 100644 --- a/paddle/utils/Flags.h +++ b/paddle/utils/Flags.h @@ -41,3 +41,4 @@ DECLARE_string(predict_file); DECLARE_bool(prev_batch_state); DECLARE_string(init_model_path); DECLARE_bool(use_mkldnn); +DECLARE_bool(use_mkldnn_wgt); diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index ae39abc081e..dd79f3a043c 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -1619,6 +1619,8 @@ class FCLayer(LayerBase): config_assert( len(inputs) == 1, "MkldnnFCLayer support one and only one input!") + use_mkldnn_wgt = bool( + int(g_command_config_args.get("use_mkldnn_wgt", 0))) super(FCLayer, self).__init__( name, self.layer_type, size, inputs=inputs, **xargs) for input_index in xrange(len(self.inputs)): @@ -1627,9 +1629,10 @@ class FCLayer(LayerBase): format = self.inputs[input_index].format sparse = format == "csr" or format == "csc" if use_mkldnn: - dims = [self.config.size, input_layer.size] config_assert(not sparse, "MkldnnFCLayer do not support sparse format yet") + if use_mkldnn and use_mkldnn_wgt: + dims = [self.config.size, input_layer.size] else: dims = [input_layer.size, self.config.size] if sparse: -- GitLab