Skip to content

Commit 475fdf1

Browse files
Anirudh B HAnirudhBHarish
authored andcommitted
# This is a combination of 7 commits.
# This is the 1st commit message: Conv1d and batchnorm1d layers # This is the commit message #2: Created BatchNorm1d, TanhGate activation and dscnn blocks # This is the commit message #3: Trials for the keyword spotting sub-modules # This is the commit message #4: Update README # This is the commit message #5: Keyword spotting network and test-benches created # This is the commit message #6: Clocking keyword detection trials # This is the commit message #7: Moving large files via git lfs
1 parent c7349dd commit 475fdf1

31 files changed

+1255
-3
lines changed

.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,16 @@ c_reference/models/q_scut_head_b_face4_model/mbconv2.h filter=lfs diff=lfs merge
6060
c_reference/models/q_scut_head_b_face4_model/mbconv4.h filter=lfs diff=lfs merge=lfs -text
6161
c_reference/models/q_scut_head_b_face4_model/rnn2.h filter=lfs diff=lfs merge=lfs -text
6262
c_reference/models/q_scut_head_b_face4_model/detection2.h filter=lfs diff=lfs merge=lfs -text
63+
c_reference/tests/kws/keyword_spotting_io_1.h filter=lfs diff=lfs merge=lfs -text
64+
c_reference/tests/kws/keyword_spotting_io_2.h filter=lfs diff=lfs merge=lfs -text
65+
c_reference/tests/kws/keyword_spotting_io_3.h filter=lfs diff=lfs merge=lfs -text
66+
c_reference/tests/conv1d/avg_pool/avg_io.h filter=lfs diff=lfs merge=lfs -text
67+
c_reference/tests/conv1d/conv1d_regular/conv_param.h filter=lfs diff=lfs merge=lfs -text
68+
c_reference/tests/conv1d/conv1d_lr/conv_param_lr.h filter=lfs diff=lfs merge=lfs -text
69+
c_reference/tests/conv1d/conv1d_lr_depthwise/conv_param_lr_depth.h filter=lfs diff=lfs merge=lfs -text
70+
c_reference/tests/conv1d/conv1d_depthwise/conv_param_depth.h filter=lfs diff=lfs merge=lfs -text
71+
c_reference/tests/kws/precnn_params.h filter=lfs diff=lfs merge=lfs -text
72+
c_reference/tests/kws/postcnn_params.h filter=lfs diff=lfs merge=lfs -text
73+
c_reference/tests/kws/rnn_params.h filter=lfs diff=lfs merge=lfs -text
74+
c_reference/tests/dscnn/dscnn_param_lr.h filter=lfs diff=lfs merge=lfs -text
75+
c_reference/tests/dscnn/dscnn_param_lr_depth_point.h filter=lfs diff=lfs merge=lfs -text

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Algorithms that shine in this setting in terms of both model size and compute, n
1717
- **EMI-RNN**: Training routine to recover the critical signature from time series data for faster and accurate RNN predictions.
1818
- **Shallow RNN**: A meta-architecture for training RNNs that can be applied to streaming data.
1919
- **FastRNN & FastGRNN - FastCells**: **F**ast, **A**ccurate, **S**table and **T**iny (**G**ated) RNN cells.
20+
- **Conv1D**: 1-D regular and low-rank convolution architectures for time-series data.
2021
- **DROCC**: **D**eep **R**obust **O**ne-**C**lass **C**lassfiication for training robust anomaly detectors.
2122
- **RNNPool**: An efficient non-linear pooling operator for RAM constrained inference.
2223

c_reference/include/conv1d.h

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
#ifndef __CONVLAYERS_H__
5+
#define __CONVLAYERS_H__
6+
7+
/**
8+
* @brief Model paramters for the 1D Convolution Layer
9+
* @var W pointer to convolutional weights W, size for regular = out_channels*in_channels*kernel_size, size for depth based = out_channels*kernel_size
10+
* @var B pointer to the bias vector for the convolution, shape = [out_channels]
11+
*/
12+
typedef struct ConvLayers_Params{
13+
float* W;
14+
float* B;
15+
} ConvLayers_Params;
16+
17+
/**
18+
* @brief Model definition for the 1D Convolution Layer
19+
* @param[out] output_signal pointer to the output signal, size = out_T * out_channels
20+
* @param[in] out_T number of time steps in the output
21+
* @param[in] out_channels number of output channels for the ouput of the conv layer
22+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
23+
* @param[in] in_T number of time steps in the input
24+
* @param[in] in_channels number of input channels
25+
* @param[in] padding padding applied to the input before the conv is performed. Note: padding is applied to both the start and end
26+
* @param[in] kernel_size kernel size of the conv filter
27+
* @param[in] params weights, bias and other essential parameters used to describe the layer
28+
* @param[in] activations an integer to choose the type of activation function.
29+
* 0: none
30+
* 1: sigmoid
31+
* 2: tanh
32+
* 3: relu
33+
*/
34+
int Conv1D(float *output_signal, unsigned out_T, unsigned out_channels, const float *input_signal,
35+
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
36+
const void* params, int activations);
37+
38+
/**
39+
* @brief Model definition for the 1D Depthwise Convolution Layer
40+
* @param[out] output_signal pointer to the output signal, size = out_T * in_channels
41+
* @param[in] out_T number of time steps in the output
42+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
43+
* @param[in] in_T number of time steps in the input
44+
* @param[in] in_channels number of input channels. The output will have the same number of channels
45+
* @param[in] padding padding applied to the input before the conv is performed. Note: padding is applied to both the start and end
46+
* @param[in] kernel_size kernel size of the conv filter
47+
* @param[in] params weights, bias and other essential parameters used to describe the layer
48+
* @param[in] activations an integer to choose the type of activation function.
49+
* 0: none
50+
* 1: sigmoid
51+
* 2: tanh
52+
* 3: relu
53+
*/
54+
int Conv1D_Depth(float *output_signal, unsigned out_T, const float *input_signal,
55+
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
56+
const void* params, int activations);
57+
58+
59+
// Low Rank Convolution
60+
/**
61+
* @brief Model paramters for the 1D Convolution Layer
62+
* @var W1 pointer to the 1st low-rank component of the weights, size = out_channels * rank
63+
* @var W2 pointer to the 2nd low-rank component of the weights, size for regular = rank * in_channels * kernel_size, size for depthwise = rank * kernel_size
64+
* @var B pointer to the bias vector for the convolution, shape = [out_channels]
65+
* @var rank rank of the weight tensor. A low rank decomposition typically used to reduce computation and storage
66+
*/
67+
typedef struct ConvLayers_LR_Params{
68+
float* W1;
69+
float* W2;
70+
float* B;
71+
unsigned rank;
72+
} ConvLayers_LR_Params;
73+
74+
/**
75+
* @brief Model definition for the 1D Low Rank Convolution Layer
76+
* @brief Identical to the non-low-rank form. One modification is the mulitplication of the weights handeled witin the layer
77+
* @param[out] output_signal pointer to the output signal, size = out_T * out_channels
78+
* @param[in] out_T number of time steps in the output
79+
* @param[in] out_channels number of output channels for the ouput of the conv layer
80+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
81+
* @param[in] in_T number of time steps in the input
82+
* @param[in] in_channels number of input channels
83+
* @param[in] padding padding applied to the input before the conv is performed. Note: padding is applied to both the start and end
84+
* @param[in] kernel_size kernel size of the conv filter
85+
* @param[in] params weights, bias and other essential parameters used to describe the layer
86+
* @param[in] activations an integer to choose the type of activation function.
87+
* 0: none
88+
* 1: sigmoid
89+
* 2: tanh
90+
* 3: relu
91+
*/
92+
int Conv1D_LR(float *output_signal, unsigned out_T, unsigned out_channels, const float *input_signal,
93+
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
94+
const void* params, int activations);
95+
96+
/**
97+
* @brief Model definition for the 1D Depthwise Convolution Layer
98+
* @brief Identical to the non-low-rank form. One modification is the mulitplication of the weights handeled witin the layer
99+
* @param[out] output_signal pointer to the output signal, size = out_T * in_channels
100+
* @param[in] out_T number of time steps in the output
101+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
102+
* @param[in] in_T number of time steps in the input
103+
* @param[in] in_channels number of input channels. The output will have the same number of channels
104+
* @param[in] padding padding applied to the input before the conv is performed. Note: padding is applied to both the start and end
105+
* @param[in] kernel_size kernel size of the conv filter
106+
* @param[in] params weights, bias and other essential parameters used to describe the layer
107+
* @param[in] activations an integer to choose the type of activation function.
108+
* 0: none
109+
* 1: sigmoid
110+
* 2: tanh
111+
* 3: relu
112+
*/
113+
int Conv1D_Depth_LR(float *output_signal, unsigned out_T, const float *input_signal,
114+
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
115+
const void* params, int activations);
116+
117+
// Auxillary Layers
118+
/**
119+
* @brief Model definition for the 1D Average Pooling Layer
120+
* @param[out] output_signal pointer to the output signal, size = out_T * in_channels. Provide Null/0 incase of inplace computation
121+
* @param[in] out_T number of time steps in the output
122+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
123+
* @param[in] in_T number of time steps in the input
124+
* @param[in] in_channels number of input channels. The output will have the same number of channels
125+
* @param[in] padding padding applied to the input before the pool is performed. Note: padding is applied to both the start and end
126+
* @param[in] kernel_size kernel size of the pool filter
127+
* @param[in] activations an integer to choose the type of activation function.
128+
* 0: none
129+
* 1: sigmoid
130+
* 2: tanh
131+
* 3: relu
132+
*/
133+
int AvgPool1D(float *output_signal, unsigned out_T, const float *input_signal, unsigned in_T, unsigned in_channels,
134+
int padding, unsigned kernel_size, int activations);
135+
136+
/**
137+
* @brief Model definition for the 1D batch Normalization Layer
138+
* @param[out] output_signal pointer to the output signal, size = out_T * in_channels. Provide Null/0 incase of inplace computation
139+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
140+
* @param[in] in_T number of time steps in the input
141+
* @param[in] in_channels number of input channels. The output will have the same number of channels
142+
* @param[in] mean pointer to the mean for the batch normalization, size = in_channels
143+
* @param[in] var pointer to the variance for the batch normalization, size = in_channels
144+
* @param[in] affine whether the affine operations are applied
145+
* @param[in] gamma pointer to the scaling factors for the post-norm affine operation, size = in_channels
146+
* @param[in] beta pointer to the scalar offsets for the post-norm affine operation, size = in_channels
147+
* @param[in] in_place in place computation of the batchnorm i.e. the output is stored in place of the input signal. Storage efficient
148+
* @param[in] eps a very small +ve value to avoid division by 0. For the default value, assign = 0.00001
149+
*/
150+
int BatchNorm1d(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels,
151+
float* mean, float* var, unsigned affine, float* gamma , float * beta, unsigned in_place, float eps);
152+
#endif

c_reference/include/conv_utils.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
#ifndef __CONVLAYER_UTILS__
5+
#define __CONVLAYER_UTILS__
6+
7+
/**
8+
* @brief Definition for the Utility Fucntion for Preparing the Low Rank Conv Weights
9+
* @param[out] out pointer to the output signal, size (regular) = out_T * in_channels * kernel_size, size (depthwise) = out_t * kernel_size
10+
* @param[in] W1 1st component of the low rank weight tensor. size = out_channels * rank
11+
* @param[in] W2 2nd component of the low rank weight tensor. size (regular) = rank * in_channels * kernel_size, size (depthwise) = rank * kernel_size
12+
* @param[in] rank rank of the weight tensor. Low Rank
13+
* @param[in] I dim 0 for W1, value = out_channels
14+
* @param[in] J dim 1 for W2, value = in_channels * kernel_size, (or for depthwise) = kernel_size
15+
*/
16+
int MatMul(float* out, float* W1, float* W2, unsigned rank, unsigned I, unsigned J);
17+
18+
/**
19+
* @brief Definition for the Custom non-linear layer : The TanhGate
20+
* @param[out] output_signal pointer to the output signal, size = out_T * in_channels
21+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
22+
* @param[in] in_T number of time steps in the input
23+
* @param[in] in_channels number of input channels. The output will have the half the number of channels. Recommended in_channel % 2 == 0
24+
*/
25+
int TanhGate(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels);
26+
27+
#endif

c_reference/include/dscnn.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
#ifndef __DSCNN__
5+
#define __DSCNN__
6+
7+
#include"conv1d.h"
8+
#include"conv_utils.h"
9+
#include<stdlib.h>
10+
#include<math.h>
11+
12+
/**
13+
* @brief Model definition for the 1D Convolution sub-block applied before the RNN
14+
* @brief sub-layers : BatchNorm1d -> Conv1D_LR
15+
*
16+
* @param[out] output_signal pointer to the final output signal, minimum size = out_T * in_channels. out_T has to be calculated based on the reduction from all the conv and pool layers
17+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
18+
* @param[in] in_T number of time steps in the input
19+
* @param[in] in_channels number of input channels. The output will have the same number of channels
20+
21+
* @param[in] mean pointer to the mean for the batch normalization, size = in_channels
22+
* @param[in] var pointer to the variance for the batch normalization, size = in_channels
23+
* @param[in] affine whether the affine operations are applied
24+
* @param[in] gamma pointer to the scaling factors for the post-norm affine operation, size = in_channels
25+
* @param[in] beta pointer to the scalar offsets for the post-norm affine operation, size = in_channels
26+
* @param[in] in_place in place computation of the batchnorm. Storage efficient
27+
*
28+
* @param[in] cnn_hidden hidden state/out_channels dimensions for the CNN
29+
* @param[in] cnn_padding padding for the CNN layer. Note: applied to both sides of the input
30+
* @param[in] cnn_kernel_size kernel size of the CNN
31+
* @param[in] cnn_params weights, bias and other essential parameters used to describe the CNN
32+
* @param[in] cnn_activations an integer to choose the type of activation function.
33+
* 0: none
34+
* 1: sigmoid
35+
* 2: tanh
36+
* 3: relu
37+
*/
38+
int DSCNN_LR(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
39+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned cnn_hidden, int cnn_padding, unsigned cnn_kernel_size,
40+
const void* cnn_params, int cnn_activations);
41+
42+
/**
43+
* @brief Model definition for the 1D Convolution sub-block applied after the RNN
44+
* @brief sub-layers : TanhGate(custom) nonlinearity -> BatchNorm1d -> Conv1D_Depth -> Conv1d_LR -> AvgPool
45+
*
46+
* @param[out] output_signal pointer to the final output signal, minimum size = out_T * in_channels. out_T has to be calculated based on the reduction from all the conv and pool layers
47+
* @param[in] input_signal pointer to the input signal. size = in_T * in_channels
48+
* @param[in] in_T number of time steps in the input
49+
* @param[in] in_channels number of input channels. The output will have the same number of channels
50+
51+
* @param[in] mean pointer to the mean for the batch normalization, size = in_channels
52+
* @param[in] var pointer to the variance for the batch normalization, size = in_channels
53+
* @param[in] affine whether the affine operations are applied
54+
* @param[in] gamma pointer to the scaling factors for the post-norm affine operation, size = in_channels
55+
* @param[in] beta pointer to the scalar offsets for the post-norm affine operation, size = in_channels
56+
* @param[in] in_place in place computation of the batchnorm. Storage efficient
57+
*
58+
* @param[in] depth_cnn_hidden hidden state/out_channels dimensions for the depth CNN
59+
* @param[in] depth_cnn_padding padding for the depth CNN layer. Note: applied to both sides of the input
60+
* @param[in] depth_cnn_kernel_size kernel size of the depth CNN
61+
* @param[in] depth_cnn_params weights, bias and other essential parameters used to describe the depth CNN
62+
* @param[in] depth_cnn_activations an integer to choose the type of activation function.
63+
* 0: none
64+
* 1: sigmoid
65+
* 2: tanh
66+
* 3: relu
67+
*
68+
* @param[in] point_cnn_hidden hidden state/out_channels dimensions for the point CNN
69+
* @param[in] point_cnn_padding padding for the point CNN layer. Note: applied to both sides of the input
70+
* @param[in] point_cnn_kernel_size kernel size of the point CNN
71+
* @param[in] point_cnn_params weights, bias and other essential parameters used to describe the point CNN
72+
* @param[in] point_cnn_activations an integer to choose the type of activation function.
73+
* 0: none
74+
* 1: sigmoid
75+
* 2: tanh
76+
* 3: relu
77+
*
78+
* @param[in] pool_padding padding for the pool layer. Note: applied to both sides of the input
79+
* @param[in] pool_kernel_size kernel size of the pool
80+
* @param[in] pool_activations an integer to choose the type of activation function.
81+
* 0: none
82+
* 1: sigmoid
83+
* 2: tanh
84+
* 3: relu
85+
*/
86+
int DSCNN_LR_Point_Depth(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
87+
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned depth_cnn_hidden, int depth_cnn_padding,
88+
unsigned depth_cnn_kernel_size, const void* depth_cnn_params, int depth_cnn_activations, unsigned point_cnn_hidden,
89+
int point_cnn_padding, unsigned point_cnn_kernel_size, const void* point_cnn_params, int point_cnn_activations,
90+
int pool_padding, unsigned pool_kernel_size, int pool_activation);
91+
92+
#endif

c_reference/src/Makefile

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

9-
all: 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 $^
13+
14+
conv_utils.o : conv_utils.c
15+
$(CC) -o $@ $(IFLAGS) $(CFLAGS) -c $^
16+
17+
conv1d.o : conv1d.c
18+
$(CC) -o $@ $(IFLAGS) $(CFLAGS) -c $^
1019

1120
utils.o: utils.c
1221
$(CC) -o $@ $(IFLAGS) $(CFLAGS) -c $^

0 commit comments

Comments
 (0)