Skip to content

Commit

Permalink
allocate from stack memory for all of the variables
Browse files Browse the repository at this point in the history
Change-Id: I32dba85ac1660c77f51c2d0d8ab6436ed0c01c74
  • Loading branch information
liangfu committed Mar 23, 2020
1 parent c904a29 commit 28d9cc7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
5 changes: 2 additions & 3 deletions apps/bundle_deploy/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/runtime/crt/crt_backend_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
29 changes: 20 additions & 9 deletions src/runtime/crt/graph_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(*)
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -603,6 +607,7 @@ void TVMGraphRuntime_SetupStorage(TVMGraphRuntime * runtime) {

// Release memory
vfree(vtype);
vfree(pool_entry);
}

int TVMGraphRuntime_SetupOpExecs(TVMGraphRuntime * runtime) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
10 changes: 0 additions & 10 deletions src/runtime/crt/graph_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/crt/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
10 changes: 4 additions & 6 deletions src/runtime/crt/packed_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,26 @@ 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));
snprintf(pf->name, strlen(name), "%s", name);
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);
}
}
Expand Down

0 comments on commit 28d9cc7

Please sign in to comment.