From 28d9cc7fe3c1b320795f6703c2cb2208c508bb85 Mon Sep 17 00:00:00 2001 From: Liangfu Chen Date: Mon, 23 Mar 2020 14:16:13 +0800 Subject: [PATCH] allocate from stack memory for all of the variables Change-Id: I32dba85ac1660c77f51c2d0d8ab6436ed0c01c74 --- apps/bundle_deploy/runtime.c | 5 ++--- src/runtime/crt/crt_backend_api.c | 1 + src/runtime/crt/graph_runtime.c | 29 ++++++++++++++++++++--------- src/runtime/crt/graph_runtime.h | 10 ---------- src/runtime/crt/module.h | 2 +- src/runtime/crt/packed_func.h | 10 ++++------ 6 files changed, 28 insertions(+), 29 deletions(-) diff --git a/apps/bundle_deploy/runtime.c b/apps/bundle_deploy/runtime.c index b979fb41ac05a..a7ffea9bbf912 100644 --- a/apps/bundle_deploy/runtime.c +++ b/apps/bundle_deploy/runtime.c @@ -32,9 +32,8 @@ #define TVM_CRT_MAX_ARGS 10 /*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */ #define TVM_CRT_STRLEN_DLTYPE 10 - -/*! Maximum supported nodes in a GraphRuntime */ -#define GRAPH_RUNTIME_MAX_NODES 100 +/*! Maximum supported string length in function names */ +#define TVM_CRT_STRLEN_NAME 80 /*! * \brief Log memory pool size for virtual memory allocation diff --git a/src/runtime/crt/crt_backend_api.c b/src/runtime/crt/crt_backend_api.c index 45dd913b51990..52cefafe39805 100644 --- a/src/runtime/crt/crt_backend_api.c +++ b/src/runtime/crt/crt_backend_api.c @@ -47,6 +47,7 @@ int TVMBackendParallelLaunch(FTVMParallelLambda flambda, void* cdata, int num_ta } int TVMBackendRegisterSystemLibSymbol(const char* name, void* ptr) { + g_fexecs = vrealloc(g_fexecs, sizeof(TVMPackedFunc) * (g_fexecs_count + 1)); snprintf(g_fexecs[g_fexecs_count].name, sizeof(g_fexecs[g_fexecs_count].name), name); g_fexecs[g_fexecs_count].fexec = ptr; g_fexecs_count++; diff --git a/src/runtime/crt/graph_runtime.c b/src/runtime/crt/graph_runtime.c index 46bf87444144f..4b10af962e883 100644 --- a/src/runtime/crt/graph_runtime.c +++ b/src/runtime/crt/graph_runtime.c @@ -446,8 +446,8 @@ int TVMGraphRuntime_LoadParams(TVMGraphRuntime * runtime, const char * param_blo bptr += sizeof(reserved); // read names - char names[GRAPH_RUNTIME_MAX_NODES][80]; - memset(names, 0, sizeof(names)); + char * names = vmalloc(TVM_CRT_STRLEN_NAME * runtime->nodes_count); + memset(names, 0, TVM_CRT_STRLEN_NAME * runtime->nodes_count); uint64_t names_count; int idx; names_count = ((uint64_t*)bptr)[0]; // NOLINT(*) @@ -459,7 +459,7 @@ int TVMGraphRuntime_LoadParams(TVMGraphRuntime * runtime, const char * param_blo if (name_length >= 80) { fprintf(stderr, "Error: function name longer than expected.\n"); } - memcpy(names[idx], bptr, name_length); + memcpy(names + TVM_CRT_STRLEN_NAME * idx, bptr, name_length); bptr += name_length; } @@ -474,9 +474,9 @@ int TVMGraphRuntime_LoadParams(TVMGraphRuntime * runtime, const char * param_blo } for (idx = 0; idx < size; idx++) { - int32_t in_idx = runtime->GetInputIndex(runtime, names[idx]); + int32_t in_idx = runtime->GetInputIndex(runtime, names + TVM_CRT_STRLEN_NAME * idx); if (!(in_idx >= 0)) { - fprintf(stderr, "Found param for non-existent input: %s\n", names[idx]); + fprintf(stderr, "Found param for non-existent input: %s\n", names + TVM_CRT_STRLEN_NAME * idx); status = -1; } uint32_t eid = runtime->GetEntryId(runtime, runtime->input_nodes[in_idx], 0); @@ -498,11 +498,14 @@ int TVMGraphRuntime_LoadParams(TVMGraphRuntime * runtime, const char * param_blo #if TVM_CRT_DEBUG TVMNDArray * entry = &(runtime->data_entry[eid]); printf("loading: param %s loaded, in_idx=%d, eid=%d, ndim=%d, data[0]=%f\n", - names[idx], in_idx, eid, entry->dl_tensor.ndim, + names + TVM_CRT_STRLEN_NAME * idx, in_idx, eid, entry->dl_tensor.ndim, ((float*)entry->dl_tensor.data)[0]); // NOLINT(*) #endif // TVM_CRT_DEBUG } + // Release memory + vfree(names); + return status; } @@ -550,8 +553,9 @@ void TVMGraphRuntime_SetupStorage(TVMGraphRuntime * runtime) { } // Size and device type of each storage pool entry. - TVMGraphRuntimePoolEntry pool_entry[GRAPH_RUNTIME_MAX_NODES]; - memset(pool_entry, 0, sizeof(pool_entry)); + TVMGraphRuntimePoolEntry * pool_entry = + vmalloc(sizeof(TVMGraphRuntimePoolEntry) * runtime->nodes_count); + memset(pool_entry, 0, sizeof(TVMGraphRuntimePoolEntry) * runtime->nodes_count); uint32_t pool_entry_count = 0; // Find the maximum space size. for (idx = 0; idx < attrs->shape_count; idx++) { @@ -603,6 +607,7 @@ void TVMGraphRuntime_SetupStorage(TVMGraphRuntime * runtime) { // Release memory vfree(vtype); + vfree(pool_entry); } int TVMGraphRuntime_SetupOpExecs(TVMGraphRuntime * runtime) { @@ -689,7 +694,7 @@ int32_t TVMGraphRuntime_CreateTVMOp(TVMGraphRuntime * runtime, const TVMOpParam fprintf(stderr, "%s function is not yet supported.", param->func_name); } - runtime->module.GetFunction(param->func_name, pf); + runtime->module.GetFunction(&(runtime->module), param->func_name, pf); TVMArgs targs = TVMArgs_Create(arg_ptr.arg_values, arg_ptr.arg_tcodes, arg_ptr.arg_values_count); pf->SetArgs(pf, &targs); @@ -756,5 +761,11 @@ void TVMGraphRuntimeRelease(TVMGraphRuntime ** pptr) { vfree((*pptr)->data_entry); vfree((*pptr)->op_execs); vfree(*pptr); + + if (g_fexecs) { + vfree(g_fexecs); + g_fexecs = 0; + } + CHECK_EQ(vleak_size, 0, "found memory leak, leak size=%d", vleak_size); } diff --git a/src/runtime/crt/graph_runtime.h b/src/runtime/crt/graph_runtime.h index 282c41edb55e6..e4ceea512afd4 100644 --- a/src/runtime/crt/graph_runtime.h +++ b/src/runtime/crt/graph_runtime.h @@ -63,7 +63,6 @@ typedef struct TVMGraphRuntimeNode { // parameters TVMOpParam param; // inputs - // TVMGraphRuntimeNodeEntry inputs[GRAPH_RUNTIME_NODE_MAX_INPUTS]; // TODO: remove TVMGraphRuntimeNodeEntry * inputs; // number of inputs size_t inputs_count; @@ -78,14 +77,10 @@ typedef struct TVMGraphRuntimeNode { // Graph attribute typedef struct TVMGraphRuntimeGraphAttr { uint32_t storage_num_not_alloctaed; - // uint32_t storage_id[GRAPH_RUNTIME_MAX_NODES]; // TODO: remove uint32_t * storage_id; - // uint32_t device_index[GRAPH_RUNTIME_MAX_NODES]; // TODO: remove uint32_t * device_index; char * dltype; // "int8", "int16", "float32" uint32_t dltype_count; - // int64_t shape[GRAPH_RUNTIME_MAX_NODES][TVM_CRT_MAX_NDIM]; - // uint32_t ndim[GRAPH_RUNTIME_MAX_NODES]; int64_t * shape; uint32_t * ndim; uint32_t shape_count; @@ -165,20 +160,16 @@ typedef struct TVMGraphRuntime { uint32_t (*GetEntryId)(struct TVMGraphRuntime * runtime, uint32_t nid, uint32_t index); /*! \brief The graph nodes. */ - // TVMGraphRuntimeNode nodes[GRAPH_RUNTIME_MAX_NODES]; TVMGraphRuntimeNode * nodes; /*! \brief The graph nodes counter. */ uint32_t nodes_count; /*! \brief The argument nodes. */ - // uint32_t input_nodes[GRAPH_RUNTIME_MAX_INPUT_NODES]; uint32_t * input_nodes; uint32_t input_nodes_count; /*! \brief Used for quick entry indexing. */ - // uint32_t node_row_ptr[GRAPH_RUNTIME_MAX_NODE_ROW_PTR]; uint32_t * node_row_ptr; uint32_t node_row_ptr_count; /*! \brief Output entries. */ - // TVMGraphRuntimeNodeEntry outputs[GRAPH_RUNTIME_MAX_OUTPUTS]; TVMGraphRuntimeNodeEntry * outputs; /*! \brief Output entries counter. */ uint32_t outputs_count; @@ -190,7 +181,6 @@ typedef struct TVMGraphRuntime { TVMContext ctxs[1]; uint32_t ctxs_count; /*! \brief Common storage pool for all devices. */ - // TVMNDArray storage_pool[GRAPH_RUNTIME_MAX_NODES]; TVMNDArray * storage_pool; uint32_t storage_pool_count; /*! \brief Data entry of each node. */ diff --git a/src/runtime/crt/module.h b/src/runtime/crt/module.h index ed63c1428f475..e527ec01e5d30 100644 --- a/src/runtime/crt/module.h +++ b/src/runtime/crt/module.h @@ -42,7 +42,7 @@ typedef struct TVMModule { * * This function will return PackedFunc(nullptr) if function do not exist. */ - void (*GetFunction)(const char * name, TVMPackedFunc * pf); + void (*GetFunction)(struct TVMModule * mod, const char * name, TVMPackedFunc * pf); } TVMModule; #endif // TVM_RUNTIME_CRT_MODULE_H_ diff --git a/src/runtime/crt/packed_func.h b/src/runtime/crt/packed_func.h index 21370b69c8c08..93898a436c885 100644 --- a/src/runtime/crt/packed_func.h +++ b/src/runtime/crt/packed_func.h @@ -112,14 +112,12 @@ static inline void TVMPackedFunc_SetArgs(TVMPackedFunc * pf, const TVMArgs * arg memcpy(&(pf->args), args, sizeof(TVMArgs)); } -TVMPackedFunc g_fexecs[GRAPH_RUNTIME_MAX_NODES]; +TVMPackedFunc * g_fexecs = 0; uint32_t g_fexecs_count = 0; -void TVMPackedFunc_SetupExecs(); - // Implement TVMModule::GetFunction // Put implementation in this file so we have seen the TVMPackedFunc -static inline void TVMModule_GetFunction(const char * name, TVMPackedFunc * pf) { +static inline void TVMModule_GetFunction(TVMModule * mod, const char * name, TVMPackedFunc * pf) { int idx; memset(pf, 0, sizeof(TVMPackedFunc)); assert(strlen(name) <= sizeof(pf->name)); @@ -127,13 +125,13 @@ static inline void TVMModule_GetFunction(const char * name, TVMPackedFunc * pf) pf->Call = TVMPackedFunc_Call; pf->SetArgs = TVMPackedFunc_SetArgs; pf->fexec = &TVMNoOperation; - for (idx = 0; idx < GRAPH_RUNTIME_MAX_NODES; idx++) { + for (idx = 0; idx < g_fexecs_count; idx++) { if (!strcmp(g_fexecs[idx].name, name)) { pf->fexec = g_fexecs[idx].fexec; break; } } - if (idx == GRAPH_RUNTIME_MAX_NODES) { + if (idx == g_fexecs_count) { fprintf(stderr, "function handle for %s not found\n", name); } }