From d5d817e145b1b8d60d8ae3781c24aa0916f4ddf7 Mon Sep 17 00:00:00 2001 From: wang sheng Date: Mon, 14 Sep 2015 16:57:10 +0800 Subject: [PATCH] SINGA-51 Improve the convolution and pooling operations minor changes - format test_common.cc - format include patterns in driver, layers all cc file include own h file in first line --- include/driver.h | 9 +- include/neuralnet/connection_layer.h | 7 +- include/neuralnet/input_layer.h | 2 + include/neuralnet/layer.h | 6 +- include/neuralnet/loss_layer.h | 5 +- include/neuralnet/neuron_layer.h | 4 +- include/neuralnet/output_layer.h | 2 + src/driver.cc | 4 +- src/neuralnet/connection_layer.cc | 6 +- src/neuralnet/input_layer.cc | 7 +- src/neuralnet/layer.cc | 2 + src/neuralnet/loss_layer.cc | 7 +- src/neuralnet/neuron_layer.cc | 21 ++-- src/neuralnet/output_layer.cc | 3 +- src/test/test_common.cc | 169 +++++++++++++-------------- src/utils/common.cc | 17 ++- 16 files changed, 142 insertions(+), 129 deletions(-) diff --git a/include/driver.h b/include/driver.h index 5a9ddfc3fa..b16cef323f 100644 --- a/include/driver.h +++ b/include/driver.h @@ -1,5 +1,6 @@ #ifndef SINGA_DRIVER_H_ #define SINGA_DRIVER_H_ + #include "singa.h" namespace singa { @@ -110,24 +111,28 @@ int Driver::RegisterParam(const Type& type) { factory->Register(type, CreateInstance(Subclass, Param)); return 1; } + template int Driver::RegisterParamGenerator(const Type& type) { auto factory = Singleton>::Instance(); factory->Register(type, CreateInstance(Subclass, ParamGenerator)); return 1; } + template int Driver::RegisterUpdater(const Type& type) { auto factory = Singleton>::Instance(); factory->Register(type, CreateInstance(Subclass, Updater)); return 1; } + template int Driver::RegisterLRGenerator(const Type& type) { auto factory = Singleton>::Instance(); factory->Register(type, CreateInstance(Subclass, LRGenerator)); return 1; } + template int Driver::RegisterWorker(const Type& type) { auto factory = Singleton>::Instance(); @@ -135,8 +140,6 @@ int Driver::RegisterWorker(const Type& type) { return 1; } - } // namespace singa -#endif // SINGA_DRIVER_H_ - +#endif // SINGA_DRIVER_H_ diff --git a/include/neuralnet/connection_layer.h b/include/neuralnet/connection_layer.h index e44f4f3717..233714dcf2 100644 --- a/include/neuralnet/connection_layer.h +++ b/include/neuralnet/connection_layer.h @@ -1,5 +1,7 @@ #ifndef SINGA_NEURALNET_CONNECTION_LAYER_H_ #define SINGA_NEURALNET_CONNECTION_LAYER_H_ + +#include #include "neuralnet/layer.h" /** @@ -120,6 +122,7 @@ class SplitLayer : public ConnectionLayer { protected: Blob grads_; }; -} -// namespace singa + +} // namespace singa + #endif // SINGA_NEURALNET_CONNECTION_LAYER_H_ diff --git a/include/neuralnet/input_layer.h b/include/neuralnet/input_layer.h index 62595c631e..67af2eb167 100644 --- a/include/neuralnet/input_layer.h +++ b/include/neuralnet/input_layer.h @@ -1,6 +1,7 @@ #ifndef SINGA_NEURALNET_INPUT_LAYER_H_ #define SINGA_NEURALNET_INPUT_LAYER_H_ +#include #include #include "neuralnet/layer.h" #include "utils/data_shard.h" @@ -165,6 +166,7 @@ class PrefetchLayer : public Layer { protected: std::thread thread_; }; + } // namespace singa #endif // SINGA_NEURALNET_INPUT_LAYER_H_ diff --git a/include/neuralnet/layer.h b/include/neuralnet/layer.h index 56201f55b4..4f153d33c2 100644 --- a/include/neuralnet/layer.h +++ b/include/neuralnet/layer.h @@ -5,7 +5,6 @@ #include #include #include - #include "proto/common.pb.h" #include "proto/job.pb.h" #include "utils/common.h" @@ -13,8 +12,6 @@ #include "utils/param.h" namespace singa { -using std::vector; -using std::string; /** * Base layer class. @@ -207,10 +204,11 @@ class LossLayer : public Layer { }; } // namespace singa + #include "neuralnet/connection_layer.h" #include "neuralnet/input_layer.h" #include "neuralnet/loss_layer.h" #include "neuralnet/neuron_layer.h" #include "neuralnet/output_layer.h" -#endif // SINGA_NEURALNET_BASE_LAYER_H_ +#endif // SINGA_NEURALNET_LAYER_H_ diff --git a/include/neuralnet/loss_layer.h b/include/neuralnet/loss_layer.h index 8358bd677a..c9f668152e 100644 --- a/include/neuralnet/loss_layer.h +++ b/include/neuralnet/loss_layer.h @@ -41,6 +41,7 @@ class SoftmaxLossLayer : public LossLayer { float scale_; int topk_; }; -} -// namespace singa + +} // namespace singa + #endif // SINGA_NEURALNET_LOSS_LAYER_H_ diff --git a/include/neuralnet/neuron_layer.h b/include/neuralnet/neuron_layer.h index dd45eecfc9..86b55a3576 100644 --- a/include/neuralnet/neuron_layer.h +++ b/include/neuralnet/neuron_layer.h @@ -1,9 +1,10 @@ #ifndef SINGA_NEURALNET_NEURON_LAYER_H_ #define SINGA_NEURALNET_NEURON_LAYER_H_ -#include +#include #include "neuralnet/layer.h" #include "proto/job.pb.h" + /** * \file this file includes the declarations neuron layer classes that conduct * the transformation of features. @@ -221,7 +222,6 @@ class RBMHidLayer: public RBMLayer { RBMLayer *vis_layer_; }; - } // namespace singa #endif // SINGA_NEURALNET_NEURON_LAYER_H_ diff --git a/include/neuralnet/output_layer.h b/include/neuralnet/output_layer.h index c507e1c5a7..ac83d008de 100644 --- a/include/neuralnet/output_layer.h +++ b/include/neuralnet/output_layer.h @@ -1,4 +1,6 @@ #ifndef SINGA_NEURALNET_OUTPUT_LAYER_H_ #define SINGA_NEURALNET_OUTPUT_LAYER_H_ + // currently no output sub-classes are defined + #endif // SINGA_NEURALNET_OUTPUT_LAYER_H_ diff --git a/src/driver.cc b/src/driver.cc index a891a08d16..88bf4aa8f6 100644 --- a/src/driver.cc +++ b/src/driver.cc @@ -1,10 +1,8 @@ +#include "driver.h" #include #include #include - -#include "singa.h" - #include "utils/tinydir.h" namespace singa { diff --git a/src/neuralnet/connection_layer.cc b/src/neuralnet/connection_layer.cc index a3d0a75f3b..e2471616a9 100644 --- a/src/neuralnet/connection_layer.cc +++ b/src/neuralnet/connection_layer.cc @@ -1,7 +1,9 @@ -#include -#include "neuralnet/layer.h" +#include "neuralnet/connection_layer.h" namespace singa { + +using std::vector; + /************* Implementation for ConcateLayer ***********/ void ConcateLayer::Setup(const LayerProto& proto, int npartitions) { // CHECK_EQ(npartitions, 1); diff --git a/src/neuralnet/input_layer.cc b/src/neuralnet/input_layer.cc index b1c69865fc..f7167da0f2 100644 --- a/src/neuralnet/input_layer.cc +++ b/src/neuralnet/input_layer.cc @@ -1,8 +1,7 @@ -#include -#include +#include "neuralnet/input_layer.h" -#include "neuralnet/layer.h" #include "mshadow/tensor.h" + namespace singa { using namespace mshadow; @@ -10,6 +9,8 @@ using mshadow::cpu; using mshadow::Shape4; using mshadow::Tensor; +using std::string; +using std::vector; /************* Implementation for ParserLayer ***********/ void ParserLayer::ComputeFeature(int flag, Metric *perf) { diff --git a/src/neuralnet/layer.cc b/src/neuralnet/layer.cc index 7e2e107e70..f38d5926d2 100644 --- a/src/neuralnet/layer.cc +++ b/src/neuralnet/layer.cc @@ -9,6 +9,8 @@ namespace singa { +using std::string; + Layer* Layer::Create(const LayerProto& proto) { auto* factory = Singleton>::Instance(); Layer* layer = nullptr; diff --git a/src/neuralnet/loss_layer.cc b/src/neuralnet/loss_layer.cc index 118456a4b4..f9b80a9946 100644 --- a/src/neuralnet/loss_layer.cc +++ b/src/neuralnet/loss_layer.cc @@ -1,9 +1,10 @@ +#include "neuralnet/loss_layer.h" + #include -#include "neuralnet/layer.h" #include "mshadow/tensor.h" - namespace singa { + using namespace mshadow; using mshadow::cpu; @@ -14,6 +15,8 @@ using mshadow::Shape3; using mshadow::Shape4; using mshadow::Tensor; +using std::string; +using std::vector; /********** * Implementation for EuclideanLossLayer*************************/ void EuclideanLossLayer::ComputeFeature(int flag, Metric* perf) { diff --git a/src/neuralnet/neuron_layer.cc b/src/neuralnet/neuron_layer.cc index edfa022ad8..a98b40dbcc 100644 --- a/src/neuralnet/neuron_layer.cc +++ b/src/neuralnet/neuron_layer.cc @@ -1,10 +1,11 @@ +#include "neuralnet/neuron_layer.h" + #include #include - -#include "neuralnet/layer.h" #include "utils/singleton.h" #include "mshadow/tensor.h" #include "mshadow/cxxnet_op.h" + namespace singa { using namespace mshadow; @@ -165,7 +166,7 @@ void CConvolutionLayer::ComputeGradient(int flag, Metric* perf) { if (gsrcblob != nullptr) gsrc.dptr = gsrcblob->mutable_cpu_data(); gbias = expr::sumall_except_dim<1>(grad); - for(int n = 0; n < batchsize_; n++) { + for (int n = 0; n < batchsize_; n++) { Im2col(src[n].dptr, channels_, height_, width_, kernel_, kernel_, pad_, pad_, stride_, stride_, col.dptr); gweight += dot(grad[n], col.T()); @@ -230,7 +231,7 @@ Blob* RBMLayer::Sample(int flag) { &sample_ : &neg_sample_; } void RBMLayer::Setup(const LayerProto& proto, int npartitions) { - CHECK_EQ(npartitions, 1); // TODO test for npartitions > 1 + CHECK_EQ(npartitions, 1); // TODO(wangwei) test for npartitions > 1 Layer::Setup(proto, npartitions); hdim_ = proto.rbm_conf().hdim(); gaussian_ = proto.rbm_conf().gaussian(); @@ -523,15 +524,15 @@ void PoolingLayer::ComputeGradient(int flag, Metric* perf) { void CPoolingLayer::Setup(const LayerProto& proto, int npartitions) { PoolingLayer::Setup(proto, npartitions); - if(pool_ == PoolingProto_PoolMethod_MAX) - mask_.ReshapeLike(data_); + if (pool_ == PoolingProto_PoolMethod_MAX) + mask_.ReshapeLike(data_); } void CPoolingLayer::ComputeFeature(int flag, Metric* perf) { - if(pool_ == PoolingProto_PoolMethod_MAX) + if (pool_ == PoolingProto_PoolMethod_MAX) ForwardMaxPooling(srclayers_[0]->mutable_data(this)->mutable_cpu_data(), batchsize_, channels_, height_, width_, kernel_, kernel_, pad_, pad_, stride_, stride_, data_.mutable_cpu_data(), mask_.mutable_cpu_data()); - else if(pool_ == PoolingProto_PoolMethod_AVG) + else if (pool_ == PoolingProto_PoolMethod_AVG) ForwardAvgPooling(srclayers_[0]->mutable_data(this)->mutable_cpu_data(), batchsize_, channels_, height_, width_, kernel_, kernel_, pad_, pad_, stride_, stride_, data_.mutable_cpu_data()); @@ -540,11 +541,11 @@ void CPoolingLayer::ComputeFeature(int flag, Metric* perf) { } void CPoolingLayer::ComputeGradient(int flag, Metric* perf) { - if(pool_ == PoolingProto_PoolMethod_MAX) + if (pool_ == PoolingProto_PoolMethod_MAX) BackwardMaxPooling(grad_.cpu_data(), mask_.cpu_data(), batchsize_, channels_, height_, width_, kernel_, kernel_, pad_, pad_, stride_, stride_,srclayers_[0]->mutable_grad(this)->mutable_cpu_data()); - else if(pool_ == PoolingProto_PoolMethod_AVG) + else if (pool_ == PoolingProto_PoolMethod_AVG) BackwardAvgPooling(grad_.cpu_data(), batchsize_, channels_, height_, width_, kernel_, kernel_, pad_, pad_, stride_, stride_,srclayers_[0]->mutable_grad(this)->mutable_cpu_data()); diff --git a/src/neuralnet/output_layer.cc b/src/neuralnet/output_layer.cc index dfc547b0fa..535480e56d 100644 --- a/src/neuralnet/output_layer.cc +++ b/src/neuralnet/output_layer.cc @@ -2,5 +2,4 @@ namespace singa { - -} +} // namespace singa diff --git a/src/test/test_common.cc b/src/test/test_common.cc index b84e860d41..e30c9cbf5b 100644 --- a/src/test/test_common.cc +++ b/src/test/test_common.cc @@ -1,113 +1,112 @@ -#include "gtest/gtest.h" -#include "utils/common.h" -#include #include +#include #include +#include "gtest/gtest.h" +#include "utils/common.h" using std::string; using std::vector; using namespace singa; TEST(CommonTest, TestIntVecToString) { - - vector num_vec {2, 3, 5, 7, 11}; - string str = "(2, 3, 5, 7, 11, )"; - ASSERT_EQ(str, IntVecToString(num_vec)); + vector num_vec {2, 3, 5, 7, 11}; + string str = "(2, 3, 5, 7, 11, )"; + ASSERT_EQ(str, IntVecToString(num_vec)); } TEST(CommonTest, TestStringPrintf) { - const char* str_a = "abc"; - const char* str_b = "edfgh"; - const char* str_c = " !@#"; - const char* str_d = "1"; - const char* str_e = "2"; - const char* str_f = "3"; + const char* str_a = "abc"; + const char* str_b = "edfgh"; + const char* str_c = " !@#"; + const char* str_d = "1"; + const char* str_e = "2"; + const char* str_f = "3"; - string fmt_a = "%s%s%s"; - string fmt_b = "[%s] [%s] [%s] "; + string fmt_a = "%s%s%s"; + string fmt_b = "[%s] [%s] [%s] "; - string str_d_a = "abcedfgh !@#"; - string str_d_b = "[1] [2] [3] "; + string str_d_a = "abcedfgh !@#"; + string str_d_b = "[1] [2] [3] "; - ASSERT_EQ(str_d_a, StringPrintf(fmt_a, str_a, str_b, str_c)); - ASSERT_EQ(str_d_b, StringPrintf(fmt_b, str_d, str_e, str_f)); + ASSERT_EQ(str_d_a, StringPrintf(fmt_a, str_a, str_b, str_c)); + ASSERT_EQ(str_d_b, StringPrintf(fmt_b, str_d, str_e, str_f)); } TEST(CommonTest, TestGCDLCM) { - int a = 2, b = 5, c = 10, d = 15; + int a = 2, b = 5, c = 10, d = 15; - ASSERT_EQ(1, gcd(a, b)); - ASSERT_EQ(5, gcd(c, d)); - ASSERT_EQ(10, LeastCommonMultiple(b, c)); - ASSERT_EQ(30, LeastCommonMultiple(c, d)); + ASSERT_EQ(1, gcd(a, b)); + ASSERT_EQ(5, gcd(c, d)); + ASSERT_EQ(10, LeastCommonMultiple(b, c)); + ASSERT_EQ(30, LeastCommonMultiple(c, d)); } TEST(CommonTest, TestMetric) { - string str, msg; - Metric metric; - metric.Add("a", 0.5); - metric.Add("b", 0.5); - metric.Add("a", 1.5); - str = metric.ToLogString(); - msg = metric.ToString(); - metric.Reset(); - metric.ParseFrom(msg); - ASSERT_EQ(str, metric.ToLogString()); + string str, msg; + Metric metric; + metric.Add("a", 0.5); + metric.Add("b", 0.5); + metric.Add("a", 1.5); + str = metric.ToLogString(); + msg = metric.ToString(); + metric.Reset(); + metric.ParseFrom(msg); + ASSERT_EQ(str, metric.ToLogString()); } TEST(CommonTest, TestSlice) { - vector> slices_0; - vector sizes {14112, 96, 256, 884736, 384}; - ASSERT_EQ(slices_0, Slice(0, sizes)); - - vector> slices_1 { - { 14112 }, - { 96 }, - { 256 }, - { 884736 }, - { 384 }, - }; - - vector> slices_2 { - { 14112 }, - { 96 }, - { 256 }, - { 435328, 449408 }, - { 384 }, - }; - - vector> slices_4 { - { 14112 }, - { 96 }, - { 256 }, - { 210432,224896,224896,224512 }, - { 384 }, - }; - - vector> slices_8 { - { 14112 }, - { 96 }, - { 256 }, - { 97984,112448,112448,112448,112448,112448,112448,112064 }, - { 384 }, - }; - - ASSERT_EQ(slices_1, Slice(1, sizes)); - ASSERT_EQ(slices_2, Slice(2, sizes)); - ASSERT_EQ(slices_4, Slice(4, sizes)); - ASSERT_EQ(slices_8, Slice(8, sizes)); + vector> slices_0; + vector sizes {14112, 96, 256, 884736, 384}; + ASSERT_EQ(slices_0, Slice(0, sizes)); + + vector> slices_1 { + {14112}, + {96}, + {256}, + {884736}, + {384}, + }; + + vector> slices_2 { + {14112}, + {96}, + {256}, + {435328, 449408}, + {384}, + }; + + vector> slices_4 { + {14112}, + {96}, + {256}, + {210432, 224896, 224896, 224512}, + {384}, + }; + + vector> slices_8 { + {14112}, + {96}, + {256}, + {97984, 112448, 112448, 112448, 112448, 112448, 112448, 112064}, + {384}, + }; + + ASSERT_EQ(slices_1, Slice(1, sizes)); + ASSERT_EQ(slices_2, Slice(2, sizes)); + ASSERT_EQ(slices_4, Slice(4, sizes)); + ASSERT_EQ(slices_8, Slice(8, sizes)); } TEST(CommonTest, TestPartitionSlices) { - vector slices { - 97984,112448,112448,112448,112448,112448,112448,112064 - }; - vector box_1 { 0, 0, 0, 0, 0, 0, 0, 0 }; - vector box_2 { 0, 0, 0, 0, 1, 1, 1, 1 }; - vector box_4 { 0, 0, 1, 1, 2, 2, 3, 3 }; - vector box_8 { 0, 1, 2, 3, 4, 5, 6, 7 }; - ASSERT_EQ(box_1, PartitionSlices(1, slices)); - ASSERT_EQ(box_2, PartitionSlices(2, slices)); - ASSERT_EQ(box_4, PartitionSlices(4, slices)); - ASSERT_EQ(box_8, PartitionSlices(8, slices)); + vector slices { + 97984, 112448, 112448, 112448, 112448, 112448, 112448, 112064 + }; + vector box_1 {0, 0, 0, 0, 0, 0, 0, 0}; + vector box_2 {0, 0, 0, 0, 1, 1, 1, 1}; + vector box_4 {0, 0, 1, 1, 2, 2, 3, 3}; + vector box_8 {0, 1, 2, 3, 4, 5, 6, 7}; + ASSERT_EQ(box_1, PartitionSlices(1, slices)); + ASSERT_EQ(box_2, PartitionSlices(2, slices)); + ASSERT_EQ(box_4, PartitionSlices(4, slices)); + ASSERT_EQ(box_8, PartitionSlices(8, slices)); } diff --git a/src/utils/common.cc b/src/utils/common.cc index 3c3dc395df..4cf9a89c4b 100644 --- a/src/utils/common.cc +++ b/src/utils/common.cc @@ -357,8 +357,8 @@ void ForwardMaxPooling(const float* bottom, const int num, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, float* top, float* mask) { - int top_height = (height + pad_h * 2 -kernel_h ) / stride_h + 1; - int top_width = (width + pad_w * 2 -kernel_w ) / stride_w + 1; + int top_height = (height + pad_h * 2 -kernel_h) / stride_h + 1; + int top_width = (width + pad_w * 2 -kernel_w) / stride_w + 1; int top_count = num * top_height * top_width * channels; for (int i = 0; i < top_count; i++) { mask[i] = -1; @@ -402,8 +402,8 @@ void BackwardMaxPooling(const float* top, const float* mask, const int num, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, float* bottom) { - int top_height = (height + pad_h * 2 -kernel_h ) / stride_h + 1; - int top_width = (width + pad_w * 2 -kernel_w ) / stride_w + 1; + int top_height = (height + pad_h * 2 -kernel_h) / stride_h + 1; + int top_width = (width + pad_w * 2 -kernel_w) / stride_w + 1; const int top_offset = top_height * top_width; const int bottom_offset = height * width; memset(bottom, 0, sizeof(float) * num * channels * bottom_offset); @@ -427,8 +427,8 @@ void ForwardAvgPooling(const float* bottom, const int num, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, float* top) { - int top_height = (height + pad_h * 2 -kernel_h ) / stride_h + 1; - int top_width = (width + pad_w * 2 -kernel_w ) / stride_w + 1; + int top_height = (height + pad_h * 2 -kernel_h) / stride_h + 1; + int top_width = (width + pad_w * 2 -kernel_w) / stride_w + 1; int top_count = num * top_height * top_width * channels; for (int i = 0; i < top_count; i++) { top[i] = 0; @@ -470,8 +470,8 @@ void BackwardAvgPooling(const float* top, const int num, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, float* bottom) { - int top_height = (height + pad_h * 2 -kernel_h ) / stride_h + 1; - int top_width = (width + pad_w * 2 -kernel_w ) / stride_w + 1; + int top_height = (height + pad_h * 2 -kernel_h) / stride_h + 1; + int top_width = (width + pad_w * 2 -kernel_w) / stride_w + 1; const int top_offset = top_height * top_width; const int bottom_offset = height * width; memset(bottom, 0, sizeof(float) * num * channels * bottom_offset); @@ -495,7 +495,6 @@ void BackwardAvgPooling(const float* top, const int num, const int channels, bottom[index] += top[top_index] / pool_size; } } - } } top += top_offset;