提交 919fffa3 编写于 作者: 饶先宏's avatar 饶先宏

202108011518

上级 5a7a4c39
......@@ -761,11 +761,12 @@ int setup_coeff(cnn_layer_node* node, FILE* pCodeFile, cnn_blob * coeff, cnn_blo
offset &= ~3;
fileheader.groupInfo[1].offset = offset;
sprintf(coefffilename, WORKDIR"/hdl4se/examples/hdl4secnn/googlenet/model-data/cnn_coeff_%d.coeff", node->layer_info->layer_index);
pCoeffFile = fopen(coefffilename, "w");
pCoeffFile = fopen(coefffilename, "wb");
if (pCoeffFile == NULL) {
printf("%s create failed\n", coefffilename);
}
else {
int offset;
int count;
fwrite(&fileheader, 1, sizeof(fileheader), pCoeffFile);
count = coeff->n * coeff->c * coeff->h * coeff->w * 2;
......@@ -773,6 +774,7 @@ int setup_coeff(cnn_layer_node* node, FILE* pCodeFile, cnn_blob * coeff, cnn_blo
count &= 3;
if (count != 0)
fwrite(&count, 1, 4 - count, pCoeffFile);
offset = ftell(pCoeffFile);
count = bias->n * bias->c * bias->h * bias->w * 2;
fwrite(bias->data, 1, count, pCoeffFile);
count &= 3;
......
......@@ -123,6 +123,13 @@ static int cnn_layer_setup_convolution(cnn_convolution_layer_node * node)
#define LINEVER 1
#define CONVDEBUG 0
#if CONVDEBUG
FILE* pDumpFile = NULL;
#endif
static int cnn_layer_pushdata_convolution(cnn_convolution_layer_node * node, int port, CNNData * data)
{
int i, hh, linesize, lineind, linefrom;
......@@ -154,6 +161,11 @@ static int cnn_layer_pushdata_convolution(cnn_convolution_layer_node * node, int
int ww, cc;
float * line_in;
float * line_out;
#if CONVDEBUG
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/convdebug_cnn.txt", "wt");
}
#endif
#if LINEVER
line_in = node->base.data_buffer_input;// +hh * layer_info->stride_h * linesize;
#else
......@@ -194,6 +206,10 @@ static int cnn_layer_pushdata_convolution(cnn_convolution_layer_node * node, int
data_acc += bias[cc] * layer_info->kernel_bias.scale;
/* store it to output buffer */
line_out[ww * node->base.outputsize[0][2] + cc] = data_acc;
#if CONVDEBUG
if (pDumpFile != NULL)
fprintf(pDumpFile, "%d, %d, %d, %f\n", hh, ww, cc, data_acc);
#endif
}
}
#if LINEVER
......@@ -214,6 +230,10 @@ static int cnn_layer_pushdata_convolution(cnn_convolution_layer_node * node, int
node->base.activation_func(node->base.activation_func_param,
node->base.outputsize[0][1] * node->base.outputsize[0][2],
line_out);
#if CONVDEBUG
if (pDumpFile != NULL)
fflush(pDumpFile);
#endif
}
node->base.ready = 1;
return 0;
......
......@@ -80,7 +80,6 @@ IDLIST
VID(state),
VID(index),
VID(line),
VID(writeline),
VID(readline),
VID(lineindex),
......@@ -124,22 +123,29 @@ DEFINE_FUNC(cnncell_convolution_gen_output_coeff) {
int index;
coeffvalid = vget(wCoeffReadValid);
index = vget(index);
vput(wCoeffRead, 1);
if (coeffvalid)
vput(bCoeffReadAddr, index + 1 + vget(bCoeffOffset));
else
vput(bCoeffReadAddr, index + vget(bCoeffOffset));
if ((index * 2 + 1) < (pobj->coeffcount - 1)) {
vput(wCoeffRead, 1);
}
if (coeffvalid) {
vput(bCoeffReadAddr, (index + 1) * 4 + vget(bCoeffOffset));
}
else {
vput(bCoeffReadAddr, (index * 4) + vget(bCoeffOffset));
}
}
else if (state == READ_BIAS) {
int index;
int coeffvalid;
int biasoffset;
biasoffset = vget(bCoeffBiasOffset);
coeffvalid = vget(wCoeffReadValid);
index = vget(index);
if (coeffvalid)
vput(bCoeffReadAddr, index + 1 + vget(bCoeffBiasOffset));
vput(bCoeffReadAddr, (index + 1) * 4 + biasoffset);
else
vput(bCoeffReadAddr, index + vget(bCoeffBiasOffset));
vput(wCoeffRead, 1);
vput(bCoeffReadAddr, (index * 4) + biasoffset);
if ((index * 2 + 1) < (pobj->output_c - 1))
vput(wCoeffRead, 1);
}
}
} END_DEFINE_FUNC
......@@ -149,20 +155,16 @@ DEFINE_FUNC(cnncell_convolution_gen_output_read) {
if (vget(nwReset) != 0) {
int state = vget(state);
if (state == CONV_START) {
int index = vget(index);
if (index < pobj->input_c * pobj->input_w - 1) {
if (vget(wDataReadValid)) {
if (vget(wDataReadValid)) {
vput(wDataRead, 1);
}
}
}
}
else if (state == CONV_STRIPLINE) {
int readline = vget(readline);
int index = vget(index);
int line = vget(line);
if ((index < pobj->input_c * pobj->input_w - 1) && (line + vget(readline) >= pobj->input_h) ) {
if (vget(wDataReadValid)) {
vput(wDataRead, 1);
}
if (index < pobj->input_c * pobj->input_w * (pobj->input_h - readline))
if (vget(wDataReadValid)) {
vput(wDataRead, 1);
}
}
}
......@@ -179,9 +181,23 @@ DEFINE_FUNC(cnncell_convolution_gen_output_write) {
}
} END_DEFINE_FUNC
#define CONVDEBUG 0
#if CONVDEBUG
FILE* pDumpFile = NULL;
#endif
DEFINE_FUNC(cnncell_convolution_gen_line) {
int ww, cc;
int lineindex = vget(lineindex);
#if CONVDEBUG
if (strcmp(pobj->data.name, "convolution_2") == 0) {
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/convdebug.txt", "wt");
}
}
#endif
for (ww = 0; ww < pobj->output_w; ww++) {
for (cc = 0; cc < pobj->output_c; cc++) {
float data_acc;
......@@ -205,6 +221,10 @@ DEFINE_FUNC(cnncell_convolution_gen_line) {
/* bias */
//if (layer_info->bias_term)
data_acc += pobj->bias[cc] * pobj->bias_scale;
#if CONVDEBUG
if (pDumpFile != NULL)
fprintf(pDumpFile, "%d, %d, %d, %f\n", vget(writeline), ww, cc, data_acc);
#endif
/* active function */
if (pobj->actfunc == 1) {/* RELU */
if (data_acc < 0)
......@@ -214,6 +234,10 @@ DEFINE_FUNC(cnncell_convolution_gen_line) {
pobj->line_out[ww * pobj->output_c + cc] = data_acc;
}
}
#if CONVDEBUG
if (pDumpFile != NULL)
fflush(pDumpFile);
#endif
}
END_DEFINE_FUNC
......@@ -222,7 +246,6 @@ DEFINE_FUNC(cnncell_convolution_clktick) {
if (vget(nwReset) == 0) {
vput(state, READ_COEFF);
vput(index, 0);
vput(line, 0);
vput(readline, 0);
vput(writeline, 0);
}
......@@ -257,11 +280,10 @@ DEFINE_FUNC(cnncell_convolution_clktick) {
unsigned int v;
index = vget(index);
v = vget(bCoeffReadData);
pobj->coeff[index * 2 + 0] = (v & 0xffff);
pobj->coeff[index * 2 + 1] = (v >> 16);
pobj->bias[index * 2 + 0] = (v & 0xffff);
pobj->bias[index * 2 + 1] = (v >> 16);
if ((index * 2 + 1) == (pobj->output_c - 1)) {
vput(index, 0);
vput(line, 0);
vput(state, CONV_START);
memset(pobj->line_in, 0, pobj->kh * pobj->linesize * sizeof(float));
*(unsigned int*)&pobj->bias_scale = vget(bCoeffBiasScale);
......@@ -273,28 +295,23 @@ DEFINE_FUNC(cnncell_convolution_clktick) {
}
}
else if (state == CONV_START) { /* 读kh-ph行数据进来,构成做一行卷积所需要的数据 */
int index = vget(index);
int line = vget(line);
if (index == pobj->input_c * pobj->input_w - 1) {
index = 0;
line = line + 1;
if (line == pobj->kh - pobj->ph) {
line = 0;
if (vget(wDataReadValid)) {
int index = vget(index);
int line = index / (pobj->input_w * pobj->input_c);
int ind = index - line * (pobj->input_w * pobj->input_c);
*(unsigned int*)(&pobj->line_in[(line + pobj->ph) * pobj->linesize + pobj->pw * pobj->input_c + ind]) = vget(bDataReadData);
if (index == pobj->input_c * pobj->input_w * (pobj->kh - pobj->ph) - 1) {
index = 0;
vput(state, CONV_CALCLINE);
vput(writeline, 0);
vput(readline, pobj->kh - pobj->ph);
vput(lineindex, 0);
}
}
else {
if (vget(wDataReadValid)) {
//wDataRead=1
*(unsigned int*)(&pobj->line_in[(line - pobj->ph) * pobj->linesize + pobj->pw * pobj->input_c + index]) = vget(bDataReadData);
}
else {
index = index + 1;
}
vput(index, index);
}
vput(index, index);
vput(line, line);
}
else if (state == CONV_CALCLINE) {
/* 计算一行卷积 */
......@@ -308,7 +325,6 @@ DEFINE_FUNC(cnncell_convolution_clktick) {
vput(state, CONV_STRIPLINE);
index = 0;
if (vget(writeline) == pobj->output_h - 1) {
vput(line, 0);
vput(writeline, 0);
vput(readline, 0);
vput(lineindex, 0);
......@@ -326,31 +342,39 @@ DEFINE_FUNC(cnncell_convolution_clktick) {
vput(index, index);
}
else if (state == CONV_STRIPLINE) {
int readline = vget(readline);
int lineindex = vget(lineindex);
int line;
int index = vget(index);
int line = vget(line);
if (index == pobj->input_c * pobj->input_w - 1) {
if (index >= pobj->input_c * pobj->input_w * (pobj->input_h - readline)) {
line = index / (pobj->input_w * pobj->input_c);
for (; line < pobj->sh; line++) {
memset(&pobj->line_in[((lineindex + line) % pobj->kh) * pobj->linesize + pobj->pw * pobj->input_c],
0,
pobj->linesize * sizeof(float));
}
index = 0;
line = line + 1;
if (line == pobj->sh) {
line = 0;
vput(state, CONV_CALCLINE);
vput(readline, readline + pobj->sh);
vput(lineindex, (lineindex + pobj->sh) % pobj->kh);
vput(index, index);
}
else if (vget(wDataReadValid)) {
int ind;
line = index / (pobj->input_w * pobj->input_c);
ind = index - line * pobj->input_w * pobj->input_c;
*(unsigned int*)(&pobj->line_in[((lineindex + line) % pobj->kh) * pobj->linesize + pobj->pw * pobj->input_c + ind]) = vget(bDataReadData);
if (index >= pobj->input_c * pobj->input_w * pobj->sh - 1) {
index = 0;
vput(state, CONV_CALCLINE);
vput(readline, vget(readline) + pobj->sh);
vput(lineindex, (vget(lineindex) + pobj->sh) % pobj->kh);
vput(readline, readline + pobj->sh);
vput(lineindex, (lineindex + pobj->sh) % pobj->kh);
}
}
else if (line + vget(readline) >= pobj->input_h) {
index = pobj->input_c * pobj->input_w - 1;
memset(&pobj->line_in[((vget(lineindex) + line) % pobj->kh) * pobj->linesize], 0, pobj->linesize * sizeof(float));
}
else {
if (vget(wDataReadValid)) {
//wDataRead=1
*(unsigned int*)(&pobj->line_in[((vget(lineindex) + line) % pobj->kh) * pobj->linesize + pobj->pw * pobj->input_c + index]) = vget(bDataReadData);
else {
index = index + 1;
}
vput(index, index);
}
vput(index, index);
vput(line, line);
}
}
} END_DEFINE_FUNC
......@@ -416,7 +440,6 @@ MODULE_INIT(cnncell_convolution)
REG(state, 4);
REG(index, 16);
REG(line, 16);
REG(writeline, 16);
REG(readline, 16);
REG(lineindex, 16);
......
......@@ -122,9 +122,6 @@ DEFINE_FUNC(cnncell_datasource_gen_cur_whc) {
}
} END_DEFINE_FUNC
DEFINE_FUNC(cnncell_datasource_setup) {
pobj->w = 224;
} END_DEFINE_FUNC
MODULE_INIT(cnncell_datasource)
pobj->w = (int)MODULE_PARAM(0);
......@@ -141,7 +138,6 @@ MODULE_INIT(cnncell_datasource)
GREG(cur_c, 32, cnncell_datasource_gen_cur_whc);
GREG(index, 32, cnncell_datasource_gen_cur_whc);
GREG(complete, 1, cnncell_datasource_gen_cur_whc);
SETUP_FUNC(cnncell_datasource_setup);
END_MODULE_INIT(cnncell_datasource)
#else
......
......@@ -89,6 +89,13 @@ int main(int argc, char* argv[])
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "writeline");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "readline");
objectCall2(vcdfile, AddSignal, "/top", "wSrcDataRead");
objectCall2(vcdfile, AddSignal, "/top", "wSrcDataReadValid");
objectCall2(vcdfile, AddSignal, "/top", "bSrcDataReadData");
objectCall2(vcdfile, AddSignal, "/top", "wSrcDataWriteEnable");
objectCall2(vcdfile, AddSignal, "/top", "wSrcDataWrite");
objectCall2(vcdfile, AddSignal, "/top", "bSrcDataWriteData");
objectCall1(vcdfile, SetTopModule, topmodule);
objectCall0(vcdfile, StartRecord);
#endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册