18 #ifdef JEVOIS_PLATFORM_PRO
23 #include <vsi_nn_version.h>
25 #define VNN_APP_DEBUG (FALSE)
31 #define NEW_VXNODE(_node, _type, _in, _out, _uid) do { \
32 _node = vsi_nn_AddNode(itsGraph, _type, _in, _out, NULL); \
33 _node->uid = (uint32_t)_uid; \
34 if (NULL == _node) LFATAL("NEW_VXNODE failed"); \
37 #define NEW_VIRTUAL_TENSOR(_id, _attr, _dtype) do { \
38 memset(_attr.size, 0, VSI_NN_MAX_DIM_NUM * sizeof(vsi_size_t)); \
39 _attr.dim_num = VSI_NN_DIM_AUTO; \
40 _attr.vtl = !VNN_APP_DEBUG; \
41 _attr.is_const = FALSE; \
42 _attr.dtype.vx_type = _dtype; \
43 _id = vsi_nn_AddTensor(itsGraph, VSI_NN_TENSOR_ID_AUTO, & _attr, NULL); \
44 if (VSI_NN_TENSOR_ID_NA == _id) LFATAL("NEW_VIRTUAL_TENSOR failed"); \
48 #define NEW_CONST_TENSOR(_id, _attr, _dtype, _ofst, _size) do { \
49 data = load_data(fp, _ofst, _size); \
51 _attr.is_const = TRUE; \
52 _attr.dtype.vx_type = _dtype; \
53 _id = vsi_nn_AddTensor(itsGraph, VSI_NN_TENSOR_ID_AUTO, & _attr, data); \
55 if (VSI_NN_TENSOR_ID_NA == _id) LFATAL("NEW_CONST_TENSOR failed"); \
59 #define NEW_NORM_TENSOR(_id, _attr, _dtype) do { \
61 _attr.is_const = FALSE; \
62 _attr.dtype.vx_type = _dtype; \
63 _id = vsi_nn_AddTensor(itsGraph, VSI_NN_TENSOR_ID_AUTO, & _attr, NULL); \
64 if (VSI_NN_TENSOR_ID_NA == _id) LFATAL("NEW_NORM_TENSOR failed"); \
68 #define NEW_NORM_TENSOR_FROM_HANDLE(_id, _attr, _dtype) do { \
70 _attr.is_const = FALSE; \
71 _attr.dtype.vx_type = _dtype; \
72 _id = vsi_nn_AddTensorFromHandle(itsGraph, VSI_NN_TENSOR_ID_AUTO, & _attr, NULL); \
73 if (VSI_NN_TENSOR_ID_NA == _id) LFATAL("NEW_NORM_TENSOR_FROM_HANDLE failed"); \
89 void jevois::dnn::NetworkNPU::create_tensors(std::vector<vsi_nn_tensor_attr_t> & attrs, vsi_nn_node_t * node,
bool isin)
91 if (attrs.empty())
LFATAL(
"Invalid empty " << (isin ?
"in" :
"out") <<
"tensors specification");
93 for (
int tnum = 0; vsi_nn_tensor_attr_t & attr : attrs)
96 vsi_nn_tensor_id_t id;
102 node->input.tensors[tnum] = id;
103 itsGraph->input.tensors[tnum] = id;
108 node->output.tensors[tnum] = id;
109 itsGraph->output.tensors[tnum] = id;
120 if (itsGraph) vsi_nn_ReleaseGraph(&itsGraph);
121 if (itsCtx) vsi_nn_ReleaseContext(&itsCtx);
127 dataroot::freeze(doit);
129 intensors::freeze(doit);
130 outtensors::freeze(doit);
131 ovxver::freeze(doit);
139 if (itsGraph) { vsi_nn_ReleaseGraph(&itsGraph); itsGraph =
nullptr; }
142 if (itsCtx == 0) itsCtx = vsi_nn_CreateContext();
147 size_t const numin = iattrs.size();
148 size_t const numout = oattrs.size();
151 itsGraph = vsi_nn_CreateGraph(itsCtx, numin + numout * 2, 1);
152 if (itsGraph == NULL)
LFATAL(
"Graph creation failed");
157 if (tok.size() != 3)
LFATAL(
"Malformed ovxver version [" <<
ovxver::get() <<
"] -- should be x.y.z");
158 vsi_nn_SetGraphVersion(itsGraph, std::stoi(tok[0]), std::stoi(tok[1]), std::stoi(tok[2]));
161 vsi_nn_SetGraphVersion(itsGraph, VSI_NN_VERSION_MAJOR, VSI_NN_VERSION_MINOR, VSI_NN_VERSION_PATCH);
163 vsi_nn_SetGraphInputs(itsGraph, NULL, numin);
164 vsi_nn_SetGraphOutputs(itsGraph, NULL, numout);
166 LINFO(
"Created graph with " << numin <<
" inputs and " << numout <<
" outputs");
172 vsi_nn_node_t * node[1];
173 NEW_VXNODE(node[0], VSI_NN_OP_NBG, numin, numout, 0);
174 node[0]->nn_param.nbg.type = VSI_NN_NBG_FILE;
175 node[0]->nn_param.nbg.url = m.c_str();
178 create_tensors(oattrs, node[0],
false);
179 create_tensors(iattrs, node[0],
true);
182 auto status = vsi_nn_SetupGraph(itsGraph, FALSE);
183 if (status != VSI_SUCCESS)
184 LFATAL(
"Failed to setup graph -- Possible causes:\n"
185 "- Your source network uses unsupported layer types?\n"
186 "- Wrong NPU model? Check --optimize VIPNANOQI_PID0X88\n"
187 "- Wrong NPU SDK version? Running ovxlib " <<
188 VSI_NN_VERSION_MAJOR <<
'.' << VSI_NN_VERSION_MINOR <<
'.' << VSI_NN_VERSION_PATCH);
189 LINFO(
"Graph ready.");
193 status = vsi_nn_VerifyGraph(itsGraph);
194 if (status != VSI_SUCCESS)
LFATAL(
"Graph verification failed");
195 else LINFO(
"Graph verification ok");
201 std::vector<std::string> & info)
203 if (blobs.size() != itsGraph->input.num)
204 LFATAL(
"Received " << blobs.size() <<
" blobs, but network has " << itsGraph->input.num <<
" inputs");
206 for (
size_t b = 0; b < blobs.size(); ++b)
208 cv::Mat
const & blob = blobs[b];
211 vsi_nn_tensor_t * tensor = vsi_nn_GetTensor(itsGraph, itsGraph->input.tensors[b]);
212 if (tensor ==
nullptr)
LFATAL(
"Network does not have input tensor " << b);
213 auto const & iattr = tensor->attr;
221 auto status = vsi_nn_CopyDataToTensor(itsGraph, tensor, (uint8_t *)blob.data);
222 if (status != VSI_SUCCESS)
LFATAL(
"Error setting input tensor: " << status);
228 auto status = vsi_nn_RunGraph(itsGraph);
229 if (status != VSI_SUCCESS)
LFATAL(
"Error running graph: " << status);
230 info.emplace_back(
"- Network forward pass ok");
233 std::vector<cv::Mat> outs;
234 for (uint32_t i = 0; i < itsGraph->output.num; ++i)
236 vsi_nn_tensor_t * ot = vsi_nn_GetTensor(itsGraph, itsGraph->output.tensors[i]);
237 vsi_nn_tensor_attr_t
const & oattr = ot->attr;
238 uint8_t * tensor_data = (uint8_t *)vsi_nn_ConvertTensorToData(itsGraph, ot);
251 outs.emplace_back(rawout.clone());
256 vsi_nn_Free(tensor_data);
262 #endif // JEVOIS_PLATFORM_PRO