Skip to content

Commit 9293eb9

Browse files
Created BatchNorm1d, TanhGate activation and dscnn blocks
1 parent 664eaa9 commit 9293eb9

File tree

12 files changed

+295
-7
lines changed

12 files changed

+295
-7
lines changed

c_reference/include/conv1d.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ int Conv1D_Depth_LR(float *output_signal, unsigned out_T, const float *input_sig
5353
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
5454
const void* params, int activations);
5555

56-
//Pool
56+
// Auxillary Layers
5757
int AvgPool1D(float *output_signal, unsigned out_T, const float *input_signal, unsigned in_T, unsigned in_channels,
5858
int padding, unsigned kernel_size, int activations);
5959

60+
int BatchNorm1d(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels,
61+
float* mean, float* var, unsigned affine, float* gamma , float * beta, unsigned in_place);
6062
#endif

c_reference/include/conv_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#define __CONVLAYER_UTILS__
33

44
#include <math.h>
5-
#include <float.h>
65

76
int prepareLowRankConvMat(float* out, float* W1, float* W2, unsigned rank, unsigned I, unsigned J);
7+
int TanhGate(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels);
88
float sigmoid(float x);
99
float relu(float x);
1010

c_reference/include/dscnn.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __DSCNN__
2+
#define __DSCNN__
3+
4+
#include"conv1d.h"
5+
#include"conv_utils.h"
6+
#include<stdlib.h>
7+
#include<math.h>
8+
9+
int DSCNN_LR(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
10+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned cnn_hidden, int cnn_padding, unsigned cnn_kernel_size,
11+
const void* cnn_params, int cnn_activations);
12+
13+
int DSCNN_LR_Point_Depth(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
14+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned depth_cnn_hidden, int depth_cnn_padding,
15+
unsigned depth_cnn_kernel_size, const void* depth_cnn_params, int depth_cnn_activations, unsigned point_cnn_hidden,
16+
int point_cnn_padding, unsigned point_cnn_kernel_size, const void* point_cnn_params, int point_cnn_activations,
17+
int pool_padding, unsigned pool_kernel_size, int pool_activation);
18+
19+
#endif

c_reference/src/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ include ../config.mk
66
INCLUDE_DIR=../include
77
IFLAGS = -I $(INCLUDE_DIR)
88

9-
all: conv1d.o conv_utils.o utils.o fastgrnn.o classifier.o rnnpool.o quantized_utils.o quantized_fastgrnn.o quantized_rnnpool.o quantized_mbconv.o
9+
all: dscnn.o conv1d.o conv_utils.o utils.o fastgrnn.o classifier.o rnnpool.o quantized_utils.o quantized_fastgrnn.o quantized_rnnpool.o quantized_mbconv.o
10+
11+
dscnn.o : dscnn.c
12+
$(CC) -o $@ $(IFLAGS) $(CFLAGS) -c $^
1013

1114
conv_utils.o : conv_utils.c
1215
$(CC) -o $@ $(IFLAGS) $(CFLAGS) -c $^

c_reference/src/conv1d.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,44 @@ int AvgPool1D(float *output_signal, unsigned out_T, const float *input_signal, u
204204
else{
205205
output_signal[t * in_channels + ci] = sum/(float)kernel_size;
206206
}
207-
207+
}
208+
}
209+
return 0;
210+
}
211+
212+
int BatchNorm1d(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels,
213+
float* mean, float* var, unsigned affine, float* gamma , float * beta, unsigned in_place){
214+
float eps = 0.00001;
215+
if(affine){
216+
if(in_place){
217+
for(int t = 0; t < in_T ; t++){
218+
for(int d = 0 ; d < in_channels ; d++){
219+
input_signal[t * in_channels + d] = gamma[d]*((input_signal[t * in_channels + d] - mean[d])/sqrt(var[d] + eps)) + beta[d];
220+
}
221+
}
222+
}
223+
else{
224+
for(int t = 0; t < in_T ; t++){
225+
for(int d = 0 ; d < in_channels ; d++){
226+
output_signal[t * in_channels + d] = gamma[d]*((input_signal[t * in_channels + d] - mean[d])/sqrt(var[d] + eps)) + beta[d];
227+
}
228+
}
229+
}
230+
}
231+
else{
232+
if(in_place){
233+
for(int t = 0; t < in_T ; t++){
234+
for(int d = 0 ; d < in_channels ; d++){
235+
input_signal[t * in_channels + d] = ((input_signal[t * in_channels + d] - mean[d])/sqrt(var[d] + eps));
236+
}
237+
}
238+
}
239+
else{
240+
for(int t = 0; t < in_T ; t++){
241+
for(int d = 0 ; d < in_channels ; d++){
242+
output_signal[t * in_channels + d] = ((input_signal[t * in_channels + d] - mean[d])/sqrt(var[d] + eps));
243+
}
244+
}
208245
}
209246
}
210247
return 0;

c_reference/src/conv_utils.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include"conv_utils.h"
22
#include <math.h>
3-
#include <float.h>
43

54
int prepareLowRankConvMat(float* out, float* W1, float* W2, unsigned rank, unsigned I, unsigned J){
65
for(int i = 0 ; i < I; i++){
@@ -15,6 +14,16 @@ int prepareLowRankConvMat(float* out, float* W1, float* W2, unsigned rank, unsig
1514
return 0;
1615
}
1716

17+
int TanhGate(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels){
18+
unsigned int piv = in_channels>>1;
19+
for(int t = 0 ; t < in_T ; t++){
20+
for(int d = 0 ; d < piv ; d++){
21+
output_signal[t * piv + d] = sigmoid(input_signal[t * in_channels + d]) * tanh(input_signal[t * in_channels + (d + piv)]);
22+
}
23+
}
24+
return 0;
25+
}
26+
1827
float relu(float x) {
1928
if (x < 0.0) return 0.0;
2029
else return x;

c_reference/src/dscnn.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include"dscnn.h"
2+
#include"conv1d.h"
3+
#include"conv_utils.h"
4+
#include<stdio.h>
5+
6+
int DSCNN_LR(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
7+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned cnn_hidden, int cnn_padding, unsigned cnn_kernel_size,
8+
const void* cnn_params, int cnn_activations){
9+
unsigned out_T;
10+
// BatchNorm
11+
float* norm_out = (float*)malloc(in_T*in_channels*sizeof(float));
12+
BatchNorm1d(norm_out, input_signal, in_T, in_channels,
13+
mean, var, affine, gamma, beta, in_place);
14+
15+
// CNN
16+
out_T = in_T - cnn_kernel_size + 2*cnn_padding + 1;
17+
Conv1D_LR(output_signal, out_T, cnn_hidden, norm_out,
18+
in_T, in_channels, cnn_padding, cnn_kernel_size,
19+
cnn_params, cnn_activations);
20+
free(norm_out);
21+
22+
return 0;
23+
}
24+
25+
int DSCNN_LR_Point_Depth(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
26+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned depth_cnn_hidden, int depth_cnn_padding,
27+
unsigned depth_cnn_kernel_size, const void* depth_cnn_params, int depth_cnn_activations, unsigned point_cnn_hidden,
28+
int point_cnn_padding, unsigned point_cnn_kernel_size, const void* point_cnn_params, int point_cnn_activations,
29+
int pool_padding, unsigned pool_kernel_size, int pool_activation){
30+
31+
// Activation
32+
unsigned out_T;
33+
float* act_out= (float*)malloc(in_T * (in_channels>>1) * sizeof(float));
34+
TanhGate(act_out, input_signal, in_T, in_channels);
35+
36+
// Norm
37+
in_channels >>= 1;
38+
// float* norm_out = (float*)malloc(in_T*in_channels*sizeof(float));
39+
BatchNorm1d(0, act_out, in_T, in_channels,
40+
mean, var, affine, gamma, beta, in_place);
41+
// free(act_out);
42+
43+
// Depth CNN
44+
out_T = in_T - depth_cnn_kernel_size + 2*depth_cnn_padding + 1;
45+
float* depth_out = (float*)malloc(out_T * depth_cnn_hidden * sizeof(float));
46+
Conv1D_Depth(depth_out, out_T, act_out,
47+
in_T, in_channels, depth_cnn_padding, depth_cnn_kernel_size,
48+
depth_cnn_params, depth_cnn_activations);
49+
// free(norm_out);
50+
free(act_out);
51+
52+
// Point CNN
53+
in_T = out_T;
54+
out_T = in_T - point_cnn_kernel_size + 2*point_cnn_padding + 1;
55+
float* point_out = (float*)malloc(out_T * point_cnn_hidden * sizeof(float));
56+
Conv1D_LR(point_out, out_T, point_cnn_hidden, depth_out,
57+
in_T, depth_cnn_hidden, point_cnn_padding, point_cnn_kernel_size,
58+
point_cnn_params, point_cnn_activations);
59+
free(depth_out);
60+
61+
// Pool
62+
in_T = out_T;
63+
out_T = in_T - pool_kernel_size + 2*pool_padding + 1;
64+
AvgPool1D(output_signal, out_T, point_out, in_T, point_cnn_hidden,
65+
pool_padding, pool_kernel_size, pool_activation);
66+
free(point_out);
67+
68+
return 0;
69+
}

c_reference/tests/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ MODEL_DIR=../models
88
SRC_DIR=../src
99
IFLAGS = -I $(INCLUDE_DIR) -I $(MODEL_DIR)
1010

11-
all: test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast
11+
all: test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv
12+
13+
DSCNN_DIR=dscnn
14+
test_dscnn_lr: $(DSCNN_DIR)/test_dscnn_lr.c $(SRC_DIR)/conv_utils.o $(SRC_DIR)/conv1d.o $(SRC_DIR)/dscnn.o
15+
$(CC) -o $@ $^ $(IFLAGS) $(CFLAGS) -lm
16+
test_dscnn_lr_depth_point : $(DSCNN_DIR)/test_dscnn_lr_depth_point.c $(SRC_DIR)/conv_utils.o $(SRC_DIR)/conv1d.o $(SRC_DIR)/dscnn.o
17+
$(CC) -o $@ $^ $(IFLAGS) $(CFLAGS) -lm
1218

1319
CONV1D_DIR=conv1d
1420
test_conv1d: $(CONV1D_DIR)/conv1d_regular/test_conv1d.c $(SRC_DIR)/conv_utils.o $(SRC_DIR)/conv1d.o
@@ -51,7 +57,7 @@ test_quantized_face_detection_fast: $(FACE_DETECTION_DIR)/test_quantized_face_de
5157
.PHONY: clean cleanest
5258

5359
clean:
54-
rm -f *.o *.gch test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast
60+
rm -f *.o *.gch test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_dscnn_lr_depth_point test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast
5561

5662
cleanest: clean
5763
rm *~

c_reference/tests/dscnn/dscnn_param_lr.h

Lines changed: 25 additions & 0 deletions
Large diffs are not rendered by default.

c_reference/tests/dscnn/dscnn_param_lr_depth_point.h

Lines changed: 27 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)