forked from nudt_dsp/netrans
210 lines
5.0 KiB
Plaintext
210 lines
5.0 KiB
Plaintext
/****************************************************************************
|
|
* Generated by NETRANS #NETRANS_VERSION#
|
|
* Match ovxlib #OVXLIB_VERSION#
|
|
*
|
|
* Neural Network appliction network definition source file
|
|
****************************************************************************/
|
|
/*-------------------------------------------
|
|
Includes
|
|
-------------------------------------------*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "vsi_nn_pub.h"
|
|
|
|
#include "vnn_global.h"
|
|
#include "vnn_#NETWORK_NAME_LOWER#.h"
|
|
/*-------------------------------------------
|
|
Macros
|
|
-------------------------------------------*/
|
|
|
|
#define NEW_VXNODE(_node, _type, _in, _out, _uid) do {\
|
|
_node = vsi_nn_AddNode( graph, _type, _in, _out, NULL );\
|
|
if( NULL == _node ) {\
|
|
goto error;\
|
|
}\
|
|
_node->uid = (uint32_t)_uid;\
|
|
} while(0)
|
|
|
|
#define NEW_TENSOR(_id, _attr) do {\
|
|
if( _attr.sz != 0 ) {\
|
|
data = load_data( fp, _attr.ofst, _attr.sz );\
|
|
}\
|
|
if( NULL == data ) {\
|
|
goto error;\
|
|
}\
|
|
_id = vsi_nn_AddTensor( graph, VSI_NN_TENSOR_ID_AUTO,\
|
|
(vsi_nn_tensor_attr_t *)&_attr.attr, data );\
|
|
if( data ) {\
|
|
free( data );\
|
|
data = NULL;\
|
|
}\
|
|
if( VSI_NN_TENSOR_ID_NA == _id ) {\
|
|
goto error;\
|
|
}\
|
|
} while(0)
|
|
|
|
/*-------------------------------------------
|
|
Local Variables
|
|
-------------------------------------------*/
|
|
|
|
/*-------------------------------------------
|
|
Functions
|
|
-------------------------------------------*/
|
|
static uint8_t* load_data
|
|
(
|
|
FILE * fp,
|
|
size_t ofst,
|
|
size_t sz
|
|
)
|
|
{
|
|
uint8_t* data;
|
|
ssize_t ret;
|
|
size_t size;
|
|
data = NULL;
|
|
if(NULL == fp)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
ret = VSI_FSEEK(fp, ofst, SEEK_SET);
|
|
if (ret != 0)
|
|
{
|
|
VSILOGE("blob seek failure.");
|
|
return NULL;
|
|
}
|
|
|
|
data = (uint8_t *)malloc(sz);
|
|
if (data == NULL)
|
|
{
|
|
VSILOGE("buffer malloc failure.");
|
|
return NULL;
|
|
}
|
|
size = fread(data, 1, sz, fp);
|
|
if (size != sz || size == 0)
|
|
{
|
|
free(data);
|
|
data = NULL;
|
|
VSILOGE("Read file to buffer failed.");
|
|
}
|
|
return data;
|
|
} /* load_data() */
|
|
|
|
vsi_nn_graph_t * vnn_Create#NETWORK_NAME#
|
|
(
|
|
const char * data_file_name,
|
|
vsi_nn_context_t in_ctx,
|
|
const vnn_attr_t * attr
|
|
)
|
|
{
|
|
uint32_t _infinity = VSI_NN_FLOAT32_INF;
|
|
vsi_status status;
|
|
vsi_bool release_ctx;
|
|
vsi_nn_context_t ctx;
|
|
vsi_nn_graph_t * graph;
|
|
vsi_nn_node_t * node[#NETWORK_NAME_UPPER#_NODE_NUM];
|
|
vsi_nn_tensor_id_t tensor[#NETWORK_NAME_UPPER#_TOTAL_TENSOR_NUM];
|
|
FILE * fp;
|
|
uint8_t * data;
|
|
vsi_bool sort = FALSE;
|
|
|
|
#NET_ARRAY_PARAMETERS#
|
|
|
|
(void)(_infinity);
|
|
ctx = NULL;
|
|
graph = NULL;
|
|
data = NULL;
|
|
status = VSI_FAILURE;
|
|
memset(&node, 0, sizeof( vsi_nn_node_t * ) * #NETWORK_NAME_UPPER#_NODE_NUM);
|
|
memset(&tensor, 0, sizeof( vsi_nn_tensor_id_t) * #NETWORK_NAME_UPPER#_TOTAL_TENSOR_NUM);
|
|
|
|
fp = fopen(data_file_name, "rb");
|
|
if( NULL == fp )
|
|
{
|
|
VSILOGE( "Open file %s failed.", data_file_name );
|
|
goto error;
|
|
}
|
|
|
|
if(NULL == in_ctx)
|
|
{
|
|
ctx = vsi_nn_CreateContext();
|
|
}
|
|
else
|
|
{
|
|
ctx = in_ctx;
|
|
}
|
|
|
|
graph = vsi_nn_CreateGraph(ctx, #NETWORK_NAME_UPPER#_TOTAL_TENSOR_NUM, #NETWORK_NAME_UPPER#_NODE_NUM);
|
|
if(NULL == graph)
|
|
{
|
|
VSILOGE("Create graph fail.");
|
|
goto error;
|
|
}
|
|
vsi_nn_SetGraphVersion(graph, VNN_VERSION_MAJOR, VNN_VERSION_MINOR, VNN_VERSION_PATCH);
|
|
vsi_nn_SetGraphInputs(graph, NULL, #NET_INPUT_NUM#);
|
|
vsi_nn_SetGraphOutputs(graph, NULL, #NET_OUTPUT_NUM#);
|
|
|
|
/*-----------------------------------------
|
|
Node definitions
|
|
-----------------------------------------*/
|
|
#NODE_INITIALIZER#
|
|
|
|
/*-----------------------------------------
|
|
Tensor initialize
|
|
-----------------------------------------*/
|
|
#TENSOR_INITIALIZER#
|
|
|
|
/*-----------------------------------------
|
|
Connection initialize
|
|
-----------------------------------------*/
|
|
#NODE_OUTPUT_TENSORS#
|
|
|
|
#CONNECTIONS#
|
|
|
|
#GRAPH_IO#
|
|
|
|
status = vsi_nn_SetupGraph(graph, sort);
|
|
TEST_CHECK_STATUS(status, error);
|
|
|
|
if(VSI_FAILURE == status)
|
|
{
|
|
goto error;
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return graph;
|
|
|
|
error:
|
|
if(NULL != fp)
|
|
{
|
|
fclose(fp);
|
|
}
|
|
|
|
release_ctx = (NULL == in_ctx);
|
|
vsi_nn_DumpGraphToJson(graph);
|
|
vnn_Release#NETWORK_NAME#(graph, release_ctx);
|
|
|
|
return NULL;
|
|
} /* vsi_nn_Create#NETWORK_NAME#() */
|
|
|
|
void vnn_Release#NETWORK_NAME#
|
|
(
|
|
vsi_nn_graph_t * graph,
|
|
vsi_bool release_ctx
|
|
)
|
|
{
|
|
vsi_nn_context_t ctx;
|
|
if(NULL != graph)
|
|
{
|
|
ctx = graph->ctx;
|
|
vsi_nn_ReleaseGraph(&graph);
|
|
|
|
if(release_ctx)
|
|
{
|
|
vsi_nn_ReleaseContext(&ctx);
|
|
}
|
|
}
|
|
} /* vsi_nn_Release#NETWORK_NAME#() */
|
|
|