提交 3b657fae 编写于 作者: 饶先宏's avatar 饶先宏

202108021805

上级 b0980fd1
......@@ -126,7 +126,7 @@ static int cnn_layer_setup_convolution(cnn_convolution_layer_node * node)
#define CONVDEBUG 0
#if CONVDEBUG
FILE* pDumpFile = NULL;
static FILE* pDumpFile = NULL;
#endif
......
......@@ -98,6 +98,13 @@ static int cnn_layer_setup_lrn(cnn_lrn_layer_node * node)
return -1;
}
#define LRNDEBUG 1
#if LRNDEBUG
static FILE* pDumpFile = NULL;
#endif
static int cnn_layer_pushdata_lrn(cnn_lrn_layer_node * node, int port, CNNData * data, int isthelast)
{
const cnn_lrn_layer * player;
......@@ -106,20 +113,27 @@ static int cnn_layer_pushdata_lrn(cnn_lrn_layer_node * node, int port, CNNData *
/* LRN_NormRegion_ACROSS_CHANNELS */
if (player->norm_region == LRN_NormRegion_ACROSS_CHANNELS) {
int hh, ww, cc;
#if LRNDEBUG
if (player->layer_base.layer_index == 5) {
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/lrndebug_cnn.txt", "wt");
}
}
#endif
for (hh = 0;hh<node->base.inputsize[0][0];hh++) {
for (ww = 0;ww<node->base.inputsize[0][1];ww++) {
for (ww = 0; ww < node->base.inputsize[0][1]; ww++) {
/* 计算平方 */
CNNData scale, sum;
CNNData * output;
CNNData * input = data + (hh * node->base.outputsize[0][1] + ww) * node->base.outputsize[0][2];
CNNData* output;
CNNData* input = data + (hh * node->base.outputsize[0][1] + ww) * node->base.outputsize[0][2];
CNNData* square = node->squarevalue;
for (i = 0;i<player->local_size/2;i++)
for (i = 0; i < player->local_size / 2; i++)
*square++ = (CNNData)0;
for (cc = 0;cc<node->base.inputsize[0][2];cc++) {
for (cc = 0; cc < node->base.inputsize[0][2]; cc++) {
CNNData value = *input++;
*square++ = value * value;
}
for (i = 0;i<player->local_size/2;i++)
for (i = 0; i < player->local_size / 2; i++)
*square++ = (CNNData)0;
/* 计算scale */
input = data + (hh * node->base.outputsize[0][1] + ww) * node->base.outputsize[0][2];
......@@ -127,18 +141,32 @@ static int cnn_layer_pushdata_lrn(cnn_lrn_layer_node * node, int port, CNNData *
square = node->squarevalue;
/* first one */
sum = (CNNData)0;
for (i = 0;i<player->local_size;i++) {
for (i = 0; i < player->local_size; i++) {
sum += square[i];
}
for (j = 0;j<node->base.outputsize[0][2];j++) {
for (j = 0; j < node->base.outputsize[0][2]; j++) {
scale = powf(player->k + sum * player->alpha, -player->beta);
*output++ = *input++ * scale;
sum += square[player->local_size] - square[0];
square++;
}
#if LRNDEBUG
output = node->base.data_buffer_output + (hh * node->base.outputsize[0][1] + ww) * node->base.outputsize[0][2];
if (pDumpFile != NULL) {
for (j = 0; j < node->base.outputsize[0][2]; j++) {
fprintf(pDumpFile, "%d, %f\n", j, output[j]);
}
fflush(pDumpFile);
}
#endif
}
}
}
#if LRNDEBUG
if (pDumpFile != NULL)
fclose(pDumpFile);
pDumpFile = NULL;
#endif
node->base.ready = 1;
return 0;
}
......
......@@ -116,7 +116,7 @@ static int cnn_layer_setup_pooling(cnn_pooling_layer_node * node)
#define POOLINGDEBUG 0
#if POOLINGDEBUG
FILE* pDumpFile = NULL;
static FILE* pDumpFile = NULL;
#endif
static int cnn_layer_pushdata_pooling(cnn_pooling_layer_node * node, int port, CNNData * data)
......@@ -132,8 +132,10 @@ static int cnn_layer_pushdata_pooling(cnn_pooling_layer_node * node, int port, C
node->base.inputsize[0][1] * node->base.inputsize[0][2] * sizeof(CNNData));
}
#if POOLINGDEBUG
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/poolingdebug_cnn.txt", "wt");
if (layer_info->layer_base.layer_index == 11) {
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/poolingdebug_11_cnn.txt", "wt");
}
}
#endif
......@@ -167,7 +169,7 @@ static int cnn_layer_pushdata_pooling(cnn_pooling_layer_node * node, int port, C
/* store it to output buffer */
line_out[ww * node->base.outputsize[0][2] + cc] = data_max;
#if POOLINGDEBUG
if (pDumpFile != NULL)
if (pDumpFile != NULL && (layer_info->layer_base.layer_index == 11))
fprintf(pDumpFile, "%d, %d, %d, %f\n", hh, ww, cc, data_max);
#endif
......@@ -204,7 +206,8 @@ static int cnn_layer_pushdata_pooling(cnn_pooling_layer_node * node, int port, C
node->base.ready = 1;
#if POOLINGDEBUG
if (pDumpFile != NULL)
fflush(pDumpFile);
fclose(pDumpFile);
pDumpFile = NULL;
#endif
return 0;
}
......
......@@ -44,8 +44,6 @@
#include "hdl4secell.h"
#include "threadlock.h"
#if 1
#define M_ID(id) cnncell_buf_r4##id
#define cnncell_buf_r4_MODULE_VERSION_STRING "0.4.0-20210718.0955 CNN buf r4 cell"
#define cnncell_buf_r4_MODULE_CLSID CLSID_CNN_BUF_R4
......@@ -53,351 +51,94 @@
IDLIST
VID(wClk),
VID(nwReset),
VID(wDataReadEnable_0),
VID(wDataRead_0),
VID(wDataReadValid_0),
VID(bDataReadAddr_0),
VID(bDataReadData_0),
VID(wDataReadComplete_0),
VID(wDataReadEnable_1),
VID(wDataRead_1),
VID(wDataReadValid_1),
VID(bDataReadAddr_1),
VID(bDataReadData_1),
VID(wDataReadComplete_1),
VID(wDataReadEnable_2),
VID(wDataRead_2),
VID(wDataReadValid_2),
VID(bDataReadAddr_2),
VID(bDataReadData_2),
VID(wDataReadComplete_2),
VID(wDataReadEnable_3),
VID(wDataRead_3),
VID(wDataReadValid_3),
VID(bDataReadAddr_3),
VID(bDataReadData_3),
VID(wDataReadComplete_3),
VID(wDataWriteEnable),
VID(wDataWrite),
VID(bDataWriteAddr),
VID(bDataWriteData),
VID(wDataWriteComplete),
VID(wDataWriteEnable_0),
VID(wDataWriteEnable_1),
VID(wDataWriteEnable_2),
VID(wDataWriteEnable_3),
END_IDLIST
MODULE_DECLARE(cnncell_buf_r4)
int wordsize, wordcount;
END_MODULE_DECLARE(cnncell_buf_r4)
DEFINE_FUNC(cnncell_buf_r4_gen_write) {
vput(wDataWriteEnable,
vget(wDataWriteEnable_0)
&& vget(wDataWriteEnable_1)
&& vget(wDataWriteEnable_2)
&& vget(wDataWriteEnable_3)
);
} END_DEFINE_FUNC
MODULE_INIT(cnncell_buf_r4)
char param[32];
pobj->wordsize = (int)MODULE_PARAM(0);
pobj->wordcount = (int)MODULE_PARAM(1);
PORT_IN(wClk, 1);
PORT_IN(nwReset, 1);
PORT_OUT(wDataReadEnable_0, 1);
PORT_IN(wDataRead_0, 1);
PORT_OUT(wDataReadValid_0, 1);
PORT_IN(bDataReadAddr_0, 32);
PORT_OUT(bDataReadData_0, 32);
PORT_IN(wDataReadComplete_0, 1);
PORT_OUT(wDataReadEnable_1, 1);
PORT_IN(wDataRead_1, 1);
PORT_OUT(wDataReadValid_1, 1);
PORT_IN(bDataReadAddr_1, 32);
PORT_OUT(bDataReadData_1, 32);
PORT_IN(wDataReadComplete_1, 1);
PORT_OUT(wDataReadEnable_2, 1);
PORT_IN(wDataRead_2, 1);
PORT_OUT(wDataReadValid_2, 1);
PORT_IN(bDataReadAddr_2, 32);
PORT_OUT(bDataReadData_2, 32);
PORT_IN(wDataReadComplete_2, 1);
PORT_OUT(wDataReadEnable_3, 1);
PORT_IN(wDataRead_3, 1);
PORT_OUT(wDataReadValid_3, 1);
PORT_IN(bDataReadAddr_3, 32);
PORT_OUT(bDataReadData_3, 32);
PORT_IN(wDataReadComplete_3, 1);
PORT_OUT(wDataWriteEnable, 1);
GPORT_OUT(wDataWriteEnable, 1, cnncell_buf_r4_gen_write);
PORT_IN(wDataWrite, 1);
PORT_IN(bDataWriteAddr, 32);
PORT_IN(bDataWriteData, 32);
PORT_IN(wDataWriteComplete, 1);
END_MODULE_INIT(cnncell_buf_r4)
//MODULE_DEINIT(cnncell_buf_r4)
//END_MODULE_DEINIT(cnncell_buf_r4)
#else
#define HDL4SEDEBUG 0
/*
(*
HDL4SE="LCOM",
CLSID="9AA0D743-5FFB-4649-ACEC-4B4675AE2A57",
softmodule="hdl4se"
*)
module hdl4se_cnn_buf_r4
#(parameter wordsize=32, wordcount=1024)
(
0 input wClk,
1 input nwReset,
2 output wDataReadEnable_0,
3 input wDataRead_0,
4 output wDataReadValid_0,
5 input [31:0] bDataReadAddr_0,
6 output [31:0]bDataReadData_0,
7 input wDataReadComplete_0,
8 output wDataReadEnable_1,
9 input wDataRead_1,
10 output wDataReadValid_1,
11 input [31:0] bDataReadAddr_1,
12 output [31:0]bDataReadData_1,
13 input wDataReadComplete_1,
14 output wDataReadEnable_2,
15 input wDataRead_2,
16 output wDataReadValid_2,
17 input [31:0] bDataReadAddr_2,
18 output [31:0]bDataReadData_2,
19 input wDataReadComplete_2,
20 output wDataReadEnable_3,
21 input wDataRead_3,
22 output wDataReadValid_3,
23 input [31:0] bDataReadAddr_3,
24 output [31:0]bDataReadData_3,
25 input wDataReadComplete_3,
26 output wDataWriteEnable,
27 input wDataWrite,
28 input [31:0] bDataWriteAddr,
29 input [31:0] bDataWriteData,
30 input wDataWriteComplete
);
endmodule
*/
/* wClk不算 */
#define INPUTPORTCOUNT 17
#define PARAMCOUNT 2
#define READPORT 4
typedef struct _sCNNCellBufR4 {
OBJECT_HEADER
INTERFACE_DECLARE(IHDL4SEUnit)
HDL4SEUNIT_VARDECLARE
DLIST_VARDECLARE
IHDL4SEModuleVar * parent;
char* name;
IBigNumber** inputdata;
IHDL4SEUnit** input_unit[INPUTPORTCOUNT];
int input_index[INPUTPORTCOUNT];
unsigned char* buf;
union {
struct {
int wordsize;
int wordcount;
};
int __param[PARAMCOUNT];
};
struct sReadPortSingal {
/* input */
int readcmd, read_reg;
int readaddr, readaddr_reg;
int readcomplete, readcomplete_reg;
/* output */
int datareadenable, datareadenable_reg;
}readport[READPORT];
int writeenable, writeenable_reg;
}sCNNCellBufR4;
OBJECT_FUNCDECLARE(cnncell_buf_r4_r4, CLSID_CNN_BUF_R4);
HDL4SEUNIT_FUNCDECLARE(cnncell_buf_r4_r4, CLSID_CNN_BUF_R4, sCNNCellBufR4);
DLIST_FUNCIMPL(cnncell_buf_r4_r4, CLSID_CNN_BUF_R4, sCNNCellBufR4);
OBJECT_FUNCIMPL(cnncell_buf_r4_r4, sCNNCellBufR4, CLSID_CNN_BUF_R4);
QUERYINTERFACE_BEGIN(cnncell_buf_r4_r4, CLSID_CNN_BUF_R4)
QUERYINTERFACE_ITEM(IID_HDL4SEUNIT, IHDL4SEUnit, sCNNCellBufR4)
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sCNNCellBufR4)
QUERYINTERFACE_END
static const char* cnncell_buf_r4_r4ModuleInfo()
{
return "0.3.0-20210706.1436 CNN Buf R4 cell";
}
static int cnncell_buf_r4_r4Create(const PARAMITEM* pParams, int paramcount, HOBJECT* pObject)
{
sCNNCellBufR4* pobj;
int i;
pobj = (sCNNCellBufR4*)mt_malloc(sizeof(sCNNCellBufR4));
if (pobj == NULL)
return -1;
*pObject = 0;
HDL4SEUNIT_VARINIT(pobj, CLSID_CNN_BUF_R4);
INTERFACE_INIT(IHDL4SEUnit, pobj, cnncell_buf_r4_r4, hdl4se_unit);
DLIST_VARINIT(pobj, cnncell_buf_r4_r4);
for (i = 0; i < INPUTPORTCOUNT; i++)
pobj->input_unit[i] = NULL;
pobj->inputdata = bigintegerCreate(64);
pobj->buf = 0;
pobj->name = NULL;
pobj->parent = NULL;
for (i = 0; i < paramcount; i++) {
if (pParams[i].name == PARAMID_HDL4SE_UNIT_NAME) {
if (pobj->name != NULL)
mt_free(pobj->name);
pobj->name = strdup(pParams[i].pvalue);
}
else if (pParams[i].name == PARAMID_HDL4SE_UNIT_PARENT) {
pobj->parent = (IHDL4SEModuleVar *)pParams[i].pvalue;
}
else if (pParams[i].name == PARAMID_HDL4SE_UNIT_INSTANCE_PARAMETERS) {
IBigNumber** temp = bigintegerCreate(32);
if (temp != NULL) {
const char* nstr;
const char* lstr;
int j;
lstr = (const char*)pParams[i].pvalue;
for (j = 0; j < PARAMCOUNT; j++) {
if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) {
objectCall1(temp, GetInt32, &pobj->__param[j]);
lstr = nstr;
}
}
objectRelease(temp);
}
}
}
/* 返回生成的对象 */
OBJECT_RETURN_GEN(cnncell_buf_r4_r4, pobj, pObject, CLSID_CNN_BUF_R4);
return EIID_OK;
}
static void cnncell_buf_r4_r4Destroy(HOBJECT object)
{
sCNNCellBufR4* pobj;
int i;
pobj = (sCNNCellBufR4*)objectThis(object);
if (pobj->name != NULL)
mt_free(pobj->name);
for (i = 0; i < INPUTPORTCOUNT; i++)
objectRelease(pobj->input_unit[i]);
objectRelease(pobj->inputdata);
memset(pobj, 0, sizeof(sCNNCellBufR4));
mt_free(pobj);
}
static int cnncell_buf_r4_r4Valid(HOBJECT object)
{
sCNNCellBufR4* pobj;
pobj = (sCNNCellBufR4*)objectThis(object);
return 1;
}
static int cnncell_buf_r4_r4_hdl4se_unit_GetName(HOBJECT object, const char** pname)
{
sCNNCellBufR4* pobj;
pobj = (sCNNCellBufR4*)objectThis(object);
*pname = pobj->name;
return 0;
}
static int cnncell_buf_r4_r4_hdl4se_unit_ConnectInput(HOBJECT object, int index, HOBJECT from, int fromindex)
{
int i, inputind;
#define CONNECTPORT(ind, innerind) \
if (index == ind) { \
if (0 == objectQueryInterface(from, IID_HDL4SEUNIT, (void**)&unit)) { \
objectRelease(pobj->input_unit[innerind]); \
pobj->input_unit[innerind] = unit; \
pobj->input_index[innerind] = fromindex; \
} \
}
sCNNCellBufR4* pobj;
IHDL4SEUnit** unit = NULL;
pobj = (sCNNCellBufR4*)objectThis(object);
CONNECTPORT(1, 0); /* nwReset */
inputind = 1;
for (i = 0; i < READPORT; i++) {
CONNECTPORT(3 + i * 6, 1 + i * 3); /* wDataRead_i */
CONNECTPORT(5 + i * 6, 2 + i * 3); /* bDataReadAddr_i */
CONNECTPORT(7 + i * 6, 3 + i * 3); /* wDataReadComplete_i */
}
CONNECTPORT(27, 13); /* wDataWrite */
CONNECTPORT(28, 14); /* bDataWriteAddr */
CONNECTPORT(29, 15); /* bDataWriteData */
CONNECTPORT(30, 16); /* wDataWriteComplete */
return 0;
}
static int cnncell_buf_r4_r4_hdl4se_unit_GetValue(HOBJECT object, int index, int width, IBigNumber ** value)
{
sCNNCellBufR4* pobj;
pobj = (sCNNCellBufR4*)objectThis(object);
if (index == 26) {
if (width <= 0) {
objectCall1(value, SetUnsigned, 1);
objectCall1(value, SetWidth, 1);
}
objectCall1(value, AssignInt32, pobj->writeenable_reg);
}
else {
int port;
int offset;
port = (index - 2) / 6;
offset = (index - 2) % 6;
if (offset == 0) { /* wDataReadEnable_i */
}
else {
}
}
return 0;
}
static int cnncell_buf_r4_r4_hdl4se_unit_ClkTick(HOBJECT object)
{
sCNNCellBufR4* pobj;
pobj = (sCNNCellBufR4*)objectThis(object);
return 0;
}
static int cnncell_buf_r4_r4_hdl4se_unit_Setup(HOBJECT object)
{
int i;
sCNNCellBufR4* pobj;
pobj = (sCNNCellBufR4*)objectThis(object);
pobj->writeenable_reg = pobj->writeenable;
for (i = 0; i < READPORT; i++) {
pobj->readport[i].read_reg = pobj->readport[i].readcmd;
pobj->readport[i].readaddr_reg = pobj->readport[i].readaddr;
pobj->readport[i].readcomplete_reg = pobj->readport[i].readcomplete;
}
return 0;
}
WIRE(wDataWriteEnable_0, 1);
WIRE(wDataWriteEnable_1, 1);
WIRE(wDataWriteEnable_2, 1);
WIRE(wDataWriteEnable_3, 1);
sprintf(param, "%d, %d", pobj->wordsize, pobj->wordcount);
CELL_INST("230946EF-3EC1-43EC-A841-CDE8A9E97314", /* hdl4se_fifo */
"cnncell_buf_r4_0",
param,
"wClk, nwReset, wDataRead_0, wDataReadValid_0, bDataReadData_0, "
"wDataWriteEnable_0, wDataWrite, bDataWriteData");
CELL_INST("230946EF-3EC1-43EC-A841-CDE8A9E97314", /* hdl4se_fifo */
"cnncell_buf_r4_1",
param,
"wClk, nwReset, wDataRead_1, wDataReadValid_1, bDataReadData_1, "
"wDataWriteEnable_1, wDataWrite, bDataWriteData");
CELL_INST("230946EF-3EC1-43EC-A841-CDE8A9E97314", /* hdl4se_fifo */
"cnncell_buf_r4_2",
param,
"wClk, nwReset, wDataRead_2, wDataReadValid_2, bDataReadData_2, "
"wDataWriteEnable_2, wDataWrite, bDataWriteData");
CELL_INST("230946EF-3EC1-43EC-A841-CDE8A9E97314", /* hdl4se_fifo */
"cnncell_buf_r4_3",
param,
"wClk, nwReset, wDataRead_3, wDataReadValid_3, bDataReadData_3, "
"wDataWriteEnable_3, wDataWrite, bDataWriteData");
END_MODULE_INIT(cnncell_buf_r4)
#endif
\ No newline at end of file
......@@ -45,9 +45,6 @@
#include "hdl4secell.h"
#include "threadlock.h"
#if 1
#define M_ID(id) cnncell_concat##id
#define cnncell_concat_MODULE_VERSION_STRING "0.4.0-20210718.1026 CNN concat4 cell"
#define cnncell_concat_MODULE_CLSID CLSID_CNN_CONCAT4
......@@ -55,42 +52,86 @@
IDLIST
VID(wClk),
VID(nwReset),
VID(wDataReadEnable_0),
VID(wDataRead_0),
VID(wDataReadValid_0),
VID(bDataReadAddr_0),
VID(bDataReadData_0),
VID(wDataReadComplete_0),
VID(wDataReadEnable_1),
VID(wDataRead_1),
VID(wDataReadValid_1),
VID(bDataReadAddr_1),
VID(bDataReadData_1),
VID(wDataReadComplete_1),
VID(wDataReadEnable_2),
VID(wDataRead_2),
VID(wDataReadValid_2),
VID(bDataReadAddr_2),
VID(bDataReadData_2),
VID(wDataReadComplete_2),
VID(wDataReadEnable_3),
VID(wDataRead_3),
VID(wDataReadValid_3),
VID(bDataReadAddr_3),
VID(bDataReadData_3),
VID(wDataReadComplete_3),
VID(wDataWriteEnable),
VID(wDataWrite),
VID(bDataWriteAddr),
VID(bDataWriteData),
VID(wDataWriteComplete),
VID(index),
END_IDLIST
MODULE_DECLARE(cnncell_concat)
int input_w[4], input_h[4], input_c[4];
int c0, c1, c2, c3;
int output_w, output_h, output_c;
END_MODULE_DECLARE(cnncell_concat)
DEFINE_FUNC(cncell_concat_gen_read_write) {
int index = vget(index);
vput(wDataRead_0, 0);
vput(wDataRead_1, 0);
vput(wDataRead_2, 0);
vput(wDataRead_3, 0);
vput(wDataWrite, 0);
if (vget(nwReset) && vget(wDataWriteEnable)) {
if (((index >= 0) && (index < pobj->c0) && vget(wDataReadValid_0))) {
vput(wDataRead_0, 1);
vput(wDataWrite, 1);
VAssign(bDataWriteData, bDataReadData_0);
}
else if (((index >= pobj->c0) && (index < pobj->c1) && vget(wDataReadValid_1))) {
vput(wDataRead_1, 1);
vput(wDataWrite, 1);
VAssign(bDataWriteData, bDataReadData_1);
}
else if (((index >= pobj->c1) && (index < pobj->c2) && vget(wDataReadValid_2))) {
vput(wDataRead_2, 1);
vput(wDataWrite, 1);
VAssign(bDataWriteData, bDataReadData_2);
}
else if (((index >= pobj->c2) && (index < pobj->c3) && vget(wDataReadValid_3))) {
vput(wDataRead_3, 1);
vput(wDataWrite, 1);
VAssign(bDataWriteData, bDataReadData_3);
}
}
} END_DEFINE_FUNC
DEFINE_FUNC(cncell_concat_gen_clktick) {
int index = vget(index);
if (vget(nwReset) == 0) {
index = 0;
}
else if (vget(wDataWriteEnable)) {
if ( ((index >= 0) && (index < pobj->c0) && vget(wDataReadValid_0))
|| ((index >= pobj->c0) && (index < pobj->c1) && vget(wDataReadValid_1))
|| ((index >= pobj->c1) && (index < pobj->c2) && vget(wDataReadValid_2))
|| ((index >= pobj->c2) && (index < pobj->c3) && vget(wDataReadValid_3))
) {
index = index + 1;
}
}
if (index >= pobj->c3) {
index = 0;
}
vput(index, index);
} END_DEFINE_FUNC
MODULE_INIT(cnncell_concat)
int i;
......@@ -99,315 +140,40 @@ MODULE_INIT(cnncell_concat)
pobj->input_h[i] = (int)MODULE_PARAM(i * 3 + 1);
pobj->input_c[i] = (int)MODULE_PARAM(i * 3 + 2);
}
pobj->c0 = pobj->input_c[0];
pobj->c1 = pobj->c0 + pobj->input_c[1];
pobj->c2 = pobj->c1 + pobj->input_c[2];
pobj->c3 = pobj->c2 + pobj->input_c[3];
pobj->output_w = (int)MODULE_PARAM(i * 3 + 0);
pobj->output_h = (int)MODULE_PARAM(i * 3 + 1);
pobj->output_c = (int)MODULE_PARAM(i * 3 + 2);
PORT_IN(wClk, 1);
PORT_IN(nwReset, 1);
PORT_IN(wDataReadEnable_0, 1);
PORT_OUT(wDataRead_0, 1);
GPORT_OUT(wDataRead_0, 1, cncell_concat_gen_read_write);
PORT_IN(wDataReadValid_0, 1);
PORT_OUT(bDataReadAddr_0, 32);
PORT_IN(bDataReadData_0, 32);
PORT_OUT(wDataReadComplete_0, 1);
PORT_IN(wDataReadEnable_1, 1);
PORT_OUT(wDataRead_1, 1);
GPORT_OUT(wDataRead_1, 1, cncell_concat_gen_read_write);
PORT_IN(wDataReadValid_1, 1);
PORT_OUT(bDataReadAddr_1, 32);
PORT_IN(bDataReadData_1, 32);
PORT_OUT(wDataReadComplete_1, 1);
PORT_IN(wDataReadEnable_2, 1);
PORT_OUT(wDataRead_2, 1);
GPORT_OUT(wDataRead_2, 1, cncell_concat_gen_read_write);
PORT_IN(wDataReadValid_2, 1);
PORT_OUT(bDataReadAddr_2, 32);
PORT_IN(bDataReadData_2, 32);
PORT_OUT(wDataReadComplete_2, 1);
PORT_IN(wDataReadEnable_3, 1);
PORT_OUT(wDataRead_3, 1);
GPORT_OUT(wDataRead_3, 1, cncell_concat_gen_read_write);
PORT_IN(wDataReadValid_3, 1);
PORT_OUT(bDataReadAddr_3, 32);
PORT_IN(bDataReadData_3, 32);
PORT_OUT(wDataReadComplete_3, 1);
PORT_IN(wDataWriteEnable, 1);
PORT_OUT(wDataWrite, 1);
PORT_OUT(bDataWriteAddr, 32);
PORT_OUT(bDataWriteData, 32);
PORT_OUT(wDataWriteComplete, 1);
END_MODULE_INIT(cnncell_concat)
//MODULE_DEINIT(cnncell_concat)
//END_MODULE_DEINIT(cnncell_concat)
#else
#define HDL4SEDEBUG 0
/*
(*
HDL4SE="LCOM",
CLSID="8EC66CDF-C750-46A1-AA1F-C44308E1F001",
softmodule="hdl4se"
*)
module hdl4se_cnn_concat4
#(parameter
INW0=128, INH0=64, INC0=1,
INW1=128, INH1=64, INC1=1,
INW2=128, INH2=64, INC2=1,
INW3=128, INH3=64, INC3=1,
OUTW3=128, OUTH3=64, OUTC3=4
)
(
inputsize[0][0] = HEIGHT;
inputsize[0][1] = WIDTH;
inputsize[0][2] = C0;
inputsize[1][0] = HEIGHT;
inputsize[1][1] = WIDTH;
inputsize[1][2] = C1;
inputsize[2][0] = HEIGHT;
inputsize[2][1] = WIDTH;
inputsize[2][2] = C2;
inputsize[3][0] = HEIGHT;
inputsize[3][1] = WIDTH;
inputsize[3][2] = C3;
outputsize[0] = HEIGHT;
outputsize[1] = WIDTH;
outputsize[2] = C0 + C1 + C2 + C3;
0 input wClk,
1 input nwReset,
2 input wDataReadEnable_0,
3 output wDataRead_0,
4 input wDataReadValid_0,
5 output[31:0] bDataReadAddr_0,
6 input[31:0] bDataReadData_0,
7 output wDataReadComplete_0,
8 input wDataReadEnable_1,
9 output wDataRead_1,
10 input wDataReadValid_1,
11 output[31:0] bDataReadAddr_1,
12 input[31:0] bDataReadData_1,
13 output wDataReadComplete_1,
14 input wDataReadEnable_2,
15 output wDataRead_2,
16 input wDataReadValid_2,
17 output[31:0] bDataReadAddr_2,
18 input[31:0] bDataReadData_2,
19 output wDataReadComplete_2,
20 input wDataReadEnable_3,
21 output wDataRead_3,
22 input wDataReadValid_3,
23 output[31:0] bDataReadAddr_3,
24 input[31:0] bDataReadData_3,
25 output wDataReadComplete_3,
26 input wDataWriteEnable,
27 output wDataWrite,
28 output[31:0] bDataWriteAddr,
29 output[31:0] bDataWriteData,
30 output wDataWriteComplete
);
endmodule
*/
/* wClk不算 */
#define INPUTPORTCOUNT 14
#define PARAMCOUNT 15
typedef struct _sCNNCellInnerConcat {
OBJECT_HEADER
INTERFACE_DECLARE(IHDL4SEUnit)
HDL4SEUNIT_VARDECLARE
DLIST_VARDECLARE
IHDL4SEModuleVar * parent;
char* name;
IBigNumber** inputdata;
IHDL4SEUnit** input_unit[INPUTPORTCOUNT];
int input_index[INPUTPORTCOUNT];
union {
struct {
int input_w0, input_h0, input_c0;
int input_w1, input_h1, input_c1;
int input_w2, input_h2, input_c2;
int input_w3, input_h3, input_c3;
int output_w, output_h, output_c;
};
int __param[PARAMCOUNT];
};
int datavalid;
}sCNNCellInnerConcat;
OBJECT_FUNCDECLARE(cnncell_concat, CLSID_CNN_CONCAT4);
HDL4SEUNIT_FUNCDECLARE(cnncell_concat, CLSID_CNN_CONCAT4, sCNNCellInnerConcat);
DLIST_FUNCIMPL(cnncell_concat, CLSID_CNN_CONCAT4, sCNNCellInnerConcat);
OBJECT_FUNCIMPL(cnncell_concat, sCNNCellInnerConcat, CLSID_CNN_CONCAT4);
QUERYINTERFACE_BEGIN(cnncell_concat, CLSID_CNN_CONCAT4)
QUERYINTERFACE_ITEM(IID_HDL4SEUNIT, IHDL4SEUnit, sCNNCellInnerConcat)
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sCNNCellInnerConcat)
QUERYINTERFACE_END
static const char* cnncell_concatModuleInfo()
{
return "0.3.0-20210704.2057 CNN Concat4 cell";
}
static int cnncell_concatCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* pObject)
{
sCNNCellInnerConcat* pobj;
int i;
pobj = (sCNNCellInnerConcat*)mt_malloc(sizeof(sCNNCellInnerConcat));
if (pobj == NULL)
return -1;
*pObject = 0;
HDL4SEUNIT_VARINIT(pobj, CLSID_CNN_CONCAT4);
INTERFACE_INIT(IHDL4SEUnit, pobj, cnncell_concat, hdl4se_unit);
DLIST_VARINIT(pobj, cnncell_concat);
for (i = 0; i < INPUTPORTCOUNT; i++)
pobj->input_unit[i] = NULL;
pobj->inputdata = bigintegerCreate(64);
pobj->datavalid = 0;
pobj->name = NULL;
pobj->parent = NULL;
for (i = 0; i < paramcount; i++) {
if (pParams[i].name == PARAMID_HDL4SE_UNIT_NAME) {
if (pobj->name != NULL)
mt_free(pobj->name);
pobj->name = strdup(pParams[i].pvalue);
}
else if (pParams[i].name == PARAMID_HDL4SE_UNIT_PARENT) {
pobj->parent = (IHDL4SEModule **)pParams[i].pvalue;
}
else if (pParams[i].name == PARAMID_HDL4SE_UNIT_INSTANCE_PARAMETERS) {
IBigNumber** temp = bigintegerCreate(32);
if (temp != NULL) {
const char* nstr;
const char* lstr;
int j;
lstr = (const char*)pParams[i].pvalue;
for (j = 0; j < PARAMCOUNT; j++) {
if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) {
objectCall1(temp, GetInt32, &pobj->__param[j]);
lstr = nstr;
}
}
objectRelease(temp);
}
}
}
/* 返回生成的对象 */
OBJECT_RETURN_GEN(cnncell_concat, pobj, pObject, CLSID_CNN_CONCAT4);
return EIID_OK;
}
static void cnncell_concatDestroy(HOBJECT object)
{
sCNNCellInnerConcat* pobj;
int i;
pobj = (sCNNCellInnerConcat*)objectThis(object);
if (pobj->name != NULL)
mt_free(pobj->name);
for (i = 0; i < INPUTPORTCOUNT; i++)
objectRelease(pobj->input_unit[i]);
objectRelease(pobj->inputdata);
memset(pobj, 0, sizeof(sCNNCellInnerConcat));
mt_free(pobj);
}
static int cnncell_concatValid(HOBJECT object)
{
sCNNCellInnerConcat* pobj;
pobj = (sCNNCellInnerConcat*)objectThis(object);
return 1;
}
static int cnncell_concat_hdl4se_unit_GetName(HOBJECT object, const char** pname)
{
sCNNCellInnerConcat* pobj;
pobj = (sCNNCellInnerConcat*)objectThis(object);
*pname = pobj->name;
return 0;
}
static int cnncell_concat_hdl4se_unit_ConnectInput(HOBJECT object, int index, HOBJECT from, int fromindex)
{
#define CONNECTPORT(ind, innerind) \
if (index == ind) { \
if (0 == objectQueryInterface(from, IID_HDL4SEUNIT, (void**)&unit)) { \
objectRelease(pobj->input_unit[innerind]); \
pobj->input_unit[innerind] = unit; \
pobj->input_index[innerind] = fromindex; \
} \
}
sCNNCellInnerConcat* pobj;
IHDL4SEUnit** unit = NULL;
pobj = (sCNNCellInnerConcat*)objectThis(object);
CONNECTPORT(1, 0); /* nwReset */
CONNECTPORT(2, 1); /* wDataReadEnable_0 */
CONNECTPORT(4, 2); /* wDataReadValid_0 */
CONNECTPORT(6, 3); /* bDataReadData_0 */
CONNECTPORT(8, 4); /* wDataReadEnable_1 */
CONNECTPORT(10, 5); /* wDataReadValid_1 */
CONNECTPORT(12, 6); /* bDataReadData_1 */
CONNECTPORT(14, 7); /* wDataReadEnable_2 */
CONNECTPORT(16, 8); /* wDataReadValid_2 */
CONNECTPORT(18, 9); /* bDataReadData_2 */
CONNECTPORT(20, 10); /* wDataReadEnable_3 */
CONNECTPORT(22, 11); /* wDataReadValid_3 */
CONNECTPORT(24, 12); /* bDataReadData_3 */
CONNECTPORT(26, 13); /* wDataWriteEnable */
return 0;
}
static int cnncell_concat_hdl4se_unit_GetValue(HOBJECT object, int index, int width, IBigNumber ** value)
{
int i;
int outputwidth = 0;
sCNNCellInnerConcat* pobj;
pobj = (sCNNCellInnerConcat*)objectThis(object);
if (pobj->datavalid == 0) {
pobj->datavalid = 1;
}
if (width <= 0) {
objectCall1(value, SetUnsigned, 1);
objectCall1(value, SetWidth, outputwidth);
}
// objectCall1(value, Assign, pobj->out_data);
return 0;
}
PORT_IN(wDataWriteEnable, 1);
GPORT_OUT(wDataWrite, 1, cncell_concat_gen_read_write);
GPORT_OUT(bDataWriteData, 32, cncell_concat_gen_read_write);
static int cnncell_concat_hdl4se_unit_ClkTick(HOBJECT object)
{
sCNNCellInnerConcat* pobj;
pobj = (sCNNCellInnerConcat*)objectThis(object);
return 0;
}
REG(index, 16);
static int cnncell_concat_hdl4se_unit_Setup(HOBJECT object)
{
sCNNCellInnerConcat* pobj;
pobj = (sCNNCellInnerConcat*)objectThis(object);
pobj->datavalid = 0;
return 0;
}
CLKTICK_FUNC(cncell_concat_gen_clktick);
END_MODULE_INIT(cnncell_concat)
#endif
\ No newline at end of file
......@@ -183,7 +183,7 @@ DEFINE_FUNC(cnncell_convolution_gen_output_write) {
#define CONVDEBUG 0
#if CONVDEBUG
FILE* pDumpFile = NULL;
static FILE* pDumpFile = NULL;
#endif
DEFINE_FUNC(cnncell_convolution_gen_line) {
......
......@@ -37,6 +37,7 @@
*/
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "object.h"
#include "dlist.h"
......@@ -80,30 +81,127 @@ enum cnncell_lrn_state {
};
DEFINE_FUNC(cnncell_lrn_gen_read) {
vput(wDataRead, 1);
vput(wDataRead, 0);
if (vget(nwReset) != 0) {
if (vget(state) == LRN_READ) {
if (vget(wDataReadValid)) {
vput(wDataRead, 1);
}
}
}
} END_DEFINE_FUNC
DEFINE_FUNC(cnncell_lrn_gen_write) {
vput(wDataWrite, 0);
if (vget(state) == LRN_WRITE && vget(wDataWriteEnable)) {
int index = vget(index);
if (index < pobj->c) {
vput(wDataWrite, 1);
vput(bDataWriteData, *(unsigned int*)&pobj->buf[index]);
}
}
} END_DEFINE_FUNC
enum {
LRN_NormRegion_ACROSS_CHANNELS = 0,
LRN_NormRegion_WITHIN_CHANNEL = 1
};
#define LRNDEBUG 0
#if LRNDEBUG
static FILE* pDumpFile = NULL;
#endif
DEFINE_FUNC(cnncell_lrn_gen_line) {
#if LRNDEBUG
if (strcmp(pobj->data.name, "lrn_5") == 0) {
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/lrndebug.txt", "wt");
}
}
#endif
if (pobj->region == LRN_NormRegion_ACROSS_CHANNELS) {
int cc, i, j;
/* 计算平方 */
float scale, sum;
float* output = pobj->buf;
float* input = pobj->buf;
float* square = pobj->square;
for (i = 0; i < pobj->local_size / 2; i++)
*square++ = 0.0f;
for (cc = 0; cc < pobj->c; cc++) {
float value = *input++;
*square++ = value * value;
}
for (i = 0; i < pobj->local_size / 2; i++)
*square++ = 0;
/* 计算scale */
input = pobj->buf;
output = pobj->buf;
square = pobj->square;
sum = 0;
for (i = 0; i < pobj->local_size; i++) {
sum += square[i];
}
for (j = 0; j < pobj->c; j++) {
scale = powf(pobj->k + sum * pobj->alpha, -pobj->beta);
*output++ = *input++ * scale;
sum += square[pobj->local_size] - square[0];
square++;
}
#if LRNDEBUG
if (pDumpFile != NULL && (strcmp(pobj->data.name, "lrn_5") == 0)) {
for (j = 0; j < pobj->c; j++) {
fprintf(pDumpFile, "%d, %f\n", j, pobj->buf[j]);
}
fflush(pDumpFile);
}
#endif
}
} END_DEFINE_FUNC
DEFINE_FUNC(cnncell_lrn_clktick) {
int state = vget(state);
int index = vget(index);
if (vget(nwReset) == 0) {
state = LRN_READ;
vput(state, LRN_READ);
index = 0;
}
else if (state == LRN_READ) {
if (vget(wDataReadValid)) {
*(unsigned*)(&pobj->buf[index]) = vget(bDataReadData);
if (index == pobj->c - 1) {
index = 0;
vput(state, LRN_CALC);
}
else {
index = index + 1;
}
}
}
else if (state == LRN_CALC) {
CALL_FUNC(cnncell_lrn_gen_line);
vput(index, 0);
vput(state, LRN_WRITE);
}
else if (state == LRN_WRITE) {
if (index == pobj->c - 1) {
vput(state, LRN_READ);
index = 0;
}
else {
if (vget(wDataWriteEnable)) {
index = index + 1;
}
}
}
vput(state, state);
vput(index, index);
} END_DEFINE_FUNC
......
......@@ -122,7 +122,7 @@ DEFINE_FUNC(cnncell_pooling_gen_write) {
#define CONVDEBUG 0
#if CONVDEBUG
FILE* pDumpFile = NULL;
static FILE* pDumpFile = NULL;
#endif
enum pooling_method {
......@@ -135,9 +135,9 @@ DEFINE_FUNC(cnncell_pooling_gen_line) {
int ww, cc;
int lineindex = vget(lineindex);
#if CONVDEBUG
if (strcmp(pobj->data.name, "pooling_4") == 0) {
if (strcmp(pobj->data.name, "pooling_11") == 0) {
if (pDumpFile == NULL) {
pDumpFile = fopen("d:/gitwork/poolingdebug.txt", "wt");
pDumpFile = fopen("d:/gitwork/pooling_11.txt", "wt");
}
}
#endif
......@@ -159,7 +159,7 @@ DEFINE_FUNC(cnncell_pooling_gen_line) {
}
}
#if CONVDEBUG
if (pDumpFile != NULL)
if (pDumpFile != NULL && (strcmp(pobj->data.name, "pooling_11") == 0))
fprintf(pDumpFile, "%d, %d, %d, %f\n", vget(writeline), ww, cc, data_max);
#endif
#if 0
......
......@@ -31,7 +31,7 @@
/*
* 由googlenet程序生成
* 生成时间:Mon Aug 2 06:11:30 2021
* 生成时间:Mon Aug 2 16:02:33 2021
* 请不要手工修改。
*/
`include "hdl4secell.v"
......
......@@ -77,24 +77,14 @@ int main(int argc, char* argv[])
#if VCDOUTPUT
vcdfile = hdl4sesimCreateVCDFile("googlenet.vcd");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "wDataRead");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "wDataReadValid");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "bDataReadData");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "wDataWriteEnable");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "wDataWrite");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "bDataWriteData");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "state");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "index");
objectCall2(vcdfile, AddSignal, "/top/net/convolution_2", "line");
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");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "wDataRead");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "wDataReadValid");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "bDataReadData");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "wDataWriteEnable");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "wDataWrite");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "bDataWriteData");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "state");
objectCall2(vcdfile, AddSignal, "/top/net/lrn_5", "index");
objectCall1(vcdfile, SetTopModule, topmodule);
objectCall0(vcdfile, StartRecord);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册