Skip to content

Commit e434774

Browse files
committed
implement the sparse submanifold without kernel center consideration
1 parent bb89c03 commit e434774

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

FEATURES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ where4_layer
9999
DIV and MIN in EltwiseOp
100100
axis in EltwiseParameter (broadcasting support for 2nd bottom blob in eltwise_layer)
101101
min_first in ArgMaxParameter
102+
submanifold_sparse in ConvolutionParameter
102103
pad_type (deprecated, "SAME" style padding) in ConvolutionParameter and PoolingParameter
103104
pad_l, pad_r, pad_t and pad_b (arbitrary 2D padding) in ConvolutionParameter and PoolingParameter
104105
AVE_EXC_PAD (average pooling excluding the paddings), AVE_TF (deprecated, alias for AVE_EXC_PAD) in PoolingParameter

include/caffe/layers/base_conv_layer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class BaseConvolutionLayer : public Layer<Dtype> {
111111
bool per_channel_scale_weight_; //CUSTOMIZATION
112112
bool per_channel_scale_output_; //CUSTOMIZATION
113113
int quantize_method_; //CUSTOMIZATION
114+
bool submanifold_sparse_;
114115

115116
private:
116117
// wrap im2col/col2im so we don't have to remember the (long) argument lists

src/caffe/layers/base_conv_layer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ void BaseConvolutionLayer<Dtype>::LayerSetUpInternal(LayerParam conv_param,
8787
quantize_method_ = conv_param.quantize_method();
8888
per_channel_scale_weight_ = conv_param.per_channel_scale_weight();
8989
per_channel_scale_output_ = conv_param.per_channel_scale_output();
90+
91+
submanifold_sparse_ = conv_param.submanifold_sparse();
9092
//CUSTOMIZATION-->
9193

9294
// Setup pad dimensions (pad_).

src/caffe/layers/conv_layer.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,55 @@ void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
233233
weight_mutable += slice;
234234
}
235235
}
236+
237+
if(this->submanifold_sparse_)
238+
{
239+
if(bottom[0]->num_axes()==4)
240+
{
241+
CHECK_EQ(bottom[0]->height(), top[0]->height())<<
242+
"Input and output blob height not equal! Submanifold sparse computation is invalid!";
243+
CHECK_EQ(bottom[0]->width(), top[0]->width())<<
244+
"Input and output blob width not equal! Submanifold sparse computation is invalid!";
245+
}
246+
else if(bottom[0]->num_axes()==5)
247+
{
248+
CHECK_EQ(bottom[0]->shape(2), top[0]->shape(2))<<
249+
"Input and output blob depth not equal! Submanifold sparse computation is invalid!";
250+
CHECK_EQ(bottom[0]->shape(3), top[0]->shape(3))<<
251+
"Input and output blob height not equal! Submanifold sparse computation is invalid!";
252+
CHECK_EQ(bottom[0]->shape(4), top[0]->shape(4))<<
253+
"Input and output blob width not equal! Submanifold sparse computation is invalid!";
254+
}
255+
else
256+
{
257+
CHECK_EQ(bottom[0]->num_axes(), 3)<<"Not support Submanifold sparse computation for such blob dimension yet!";
258+
CHECK_EQ(bottom[0]->shape(2), top[0]->shape(2))<<
259+
"Input and output blob length not equal! Submanifold sparse computation is invalid!";
260+
}
261+
LOG(INFO)<<"Starts submanifold sparse computation.";
262+
263+
for(int index=0; index<bottom[0]->count(2); index++)
264+
{
265+
bool active=false;
266+
// TODO: add handling for pre-quantized model with non 0 zero-points
267+
for(int in_c=0; in_c<bottom[0]->shape(1); in_c++)
268+
{
269+
Dtype data=bottom[0]->cpu_data()[in_c*bottom[0]->count(2)+index];
270+
if(data!=Dtype(0))
271+
{
272+
active = true;
273+
break;
274+
}
275+
}
276+
if(!active)
277+
{
278+
for(int out_c=0; out_c<top[0]->shape(1); out_c++)
279+
{
280+
top[0]->mutable_cpu_data()[out_c*bottom[0]->count(2)+index]=0;
281+
}
282+
}
283+
}
284+
}
236285
}
237286

238287
template <typename Dtype>

src/caffe/proto/caffe.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ message ConvolutionParameter {
14281428
optional bool per_channel_scale_weight = 39 [default = false];
14291429
// CUSTOMIZATION, whether to have per-channel scale & zero_points for output (scale data will be stored in caffemodel)
14301430
optional bool per_channel_scale_output = 41 [default = false];
1431-
1431+
optional bool submanifold_sparse = 42 [default = false];
14321432

14331433
//<--CUSTOMIZATION
14341434
enum SaturateMethod {
@@ -2746,7 +2746,7 @@ message SqueezeConvolutionParameter {
27462746
// CUSTOMIZATION, whether to have per-channel scale & zero_points for weights/bias (data will be stored in caffemodel)
27472747
optional bool per_channel_scale_weight = 59 [default = false];
27482748
optional bool per_channel_scale_output = 61 [default = false];
2749-
2749+
optional bool submanifold_sparse = 62 [default = false];
27502750

27512751
//<--CUSTOMIZATION
27522752
enum SaturateMethod {

0 commit comments

Comments
 (0)