Skip to content

Commit 4fedcf2

Browse files
Merge branch 'keyword' into dscnn
2 parents f415f3c + 19a82b5 commit 4fedcf2

File tree

20 files changed

+569
-82
lines changed

20 files changed

+569
-82
lines changed

c_reference/include/conv1d.h

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,68 @@
1-
#include<stdlib.h>
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
24
#ifndef __CONVLAYERS_H__
35
#define __CONVLAYERS_H__
46

5-
#define ERR_INTERMIDIATE_NOT_INIT -1
6-
#define ERR_TEMPW_NOT_INIT -2
7-
#define ERR_TEMPLRU_NOT_INIT -3
8-
#define ERR_NORMFEATURES_NOT_INIT -4
9-
107
/**
118
* @brief Model paramters for the 1D Convolution Layer
12-
* @var mean pointer to mean of input vector for normalization, size inputDims
13-
* @var stdDev pointer to standard dev of input for normalization, size inputDims*steps
14-
* @var W pointer to convolutional weights W
15-
* @var B pointer to the bias vector for the convolution
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]
1611
*/
1712
typedef struct ConvLayers_Params{
1813
float* W;
1914
float* B;
2015
} ConvLayers_Params;
2116

22-
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+
*/
2334
int Conv1D(float *output_signal, unsigned out_T, unsigned out_channels, const float *input_signal,
2435
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
2536
const void* params, int activations);
2637

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+
*/
2754
int Conv1D_Depth(float *output_signal, unsigned out_T, const float *input_signal,
2855
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
2956
const void* params, int activations);
3057

31-
// Low Rank
58+
59+
// Low Rank Convolution
3260
/**
33-
* @brief Model paramters for the Low Rank 1D Convolution Layer
34-
* @var mean pointer to mean of input vector for normalization, size inputDims
35-
* @var stdDev pointer to standard dev of input for normalization, size inputDims
36-
* @var W1 pointer to first low-rank component of the convolutional weight W
37-
* @var W2 pointer to second low-rank component of the convolutional weight W
38-
* @var Rank rank of W matrix
39-
* @var B pointer to the bias vector for the convolution
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
4066
*/
4167
typedef struct ConvLayers_LR_Params{
4268
float* W1;
@@ -45,18 +71,82 @@ typedef struct ConvLayers_LR_Params{
4571
unsigned rank;
4672
} ConvLayers_LR_Params;
4773

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+
*/
4892
int Conv1D_LR(float *output_signal, unsigned out_T, unsigned out_channels, const float *input_signal,
4993
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
5094
const void* params, int activations);
5195

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+
*/
52113
int Conv1D_Depth_LR(float *output_signal, unsigned out_T, const float *input_signal,
53114
unsigned in_T, unsigned in_channels, int padding, unsigned kernel_size,
54115
const void* params, int activations);
55116

56117
// 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+
*/
57133
int AvgPool1D(float *output_signal, unsigned out_T, const float *input_signal, unsigned in_T, unsigned in_channels,
58134
int padding, unsigned kernel_size, int activations);
59135

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+
*/
60150
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);
151+
float* mean, float* var, unsigned affine, float* gamma , float * beta, unsigned in_place, float eps);
62152
#endif

c_reference/include/conv_utils.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
14
#ifndef __CONVLAYER_UTILS__
25
#define __CONVLAYER_UTILS__
36

4-
#include <math.h>
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);
517

6-
int prepareLowRankConvMat(float* out, float* W1, float* W2, unsigned rank, unsigned I, unsigned J);
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+
*/
725
int TanhGate(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels);
8-
float sigmoid(float x);
9-
float relu(float x);
1026

1127
#endif

c_reference/include/dscnn.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
14
#ifndef __DSCNN__
25
#define __DSCNN__
36

@@ -6,10 +9,80 @@
69
#include<stdlib.h>
710
#include<math.h>
811

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+
*/
938
int DSCNN_LR(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
1039
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned cnn_hidden, int cnn_padding, unsigned cnn_kernel_size,
1140
const void* cnn_params, int cnn_activations);
1241

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+
*/
1386
int DSCNN_LR_Point_Depth(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
1487
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned depth_cnn_hidden, int depth_cnn_padding,
1588
unsigned depth_cnn_kernel_size, const void* depth_cnn_params, int depth_cnn_activations, unsigned point_cnn_hidden,

0 commit comments

Comments
 (0)