forked from neo-ai/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Runtime] MISRA-C compliant TVM runtime (apache#3934)
* implement of MISRA-C compliant TVM runtime; * working on bundle_deploy_c demo * move header files into include dir * fix compatibility issues * fix compatibility issues * resolve most of the warnings and errros * implement c_backend_api * introduce bridge * working well * move to header files and bundle.c into src/runtime/crt * clean up * satisfy linter * clean up * test with the cat image * remove synset * refactoring * refactoring * refactoring * initial crt_runtime_api.c * improved compatibility with g++ * using exposed API in c_runtime_api.h * call from c_runtime_api.h * clean up * lint * merge into apps/bundle_deploy directory Change-Id: I51904db81b8589e65d107d8ca77b47452e3812b5 * make the demo runs in ci Change-Id: I2c24f8b592508833d3555311c2b24d1931f19385 * address review comments Change-Id: I027ddff15c31fb4da0bd0e461427dce619de1f93 * release Change-Id: I5ad5bb8426468aac9fc8d074e56ddea358a7fd91 * fix ci testing Change-Id: Ic2e82fb3051b6c254ef32a964f976b61e3e5fe4d * add test case for misra c runtime Change-Id: Ie0dfd0ade6be4665b4384db7d260a6c69b35010f * fread files in testing to avoid calling xxd Change-Id: Ie7fbc16b4b0b9509918d986a841f443900813bef
- Loading branch information
Showing
19 changed files
with
2,328 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include <tvm/runtime/c_runtime_api.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/*! \brief macro to do C API call */ | ||
#define TVM_CCALL(func) \ | ||
do { \ | ||
int ret = (func); \ | ||
if (ret != 0) { \ | ||
fprintf(stderr, "%s: %d: error: %s\n", __FILE__, __LINE__, TVMGetLastError()); \ | ||
exit(ret); \ | ||
} \ | ||
} while (0) | ||
|
||
TVM_DLL void * tvm_runtime_create(const char * json_data, | ||
const char * params_data, | ||
const uint64_t params_size) { | ||
int64_t device_type = kDLCPU; | ||
int64_t device_id = 0; | ||
|
||
TVMByteArray params; | ||
params.data = params_data; | ||
params.size = params_size; | ||
|
||
TVMContext ctx; | ||
ctx.device_type = (DLDeviceType)device_type; | ||
ctx.device_id = device_id; | ||
|
||
// declare pointers | ||
TVMModuleHandle (*SystemLibraryCreate)(); | ||
TVMModuleHandle (*TVMGraphRuntimeCreate)(const char *, const TVMModuleHandle, const TVMContext *); | ||
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char *, const uint32_t); | ||
|
||
// get pointers | ||
TVM_CCALL(TVMFuncGetGlobal("runtime.SystemLib", (TVMFunctionHandle*)&SystemLibraryCreate)); | ||
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.create", (TVMFunctionHandle*)&TVMGraphRuntimeCreate)); | ||
|
||
// run modules | ||
TVMModuleHandle mod_syslib = SystemLibraryCreate(); | ||
TVMModuleHandle mod = TVMGraphRuntimeCreate(json_data, mod_syslib, &ctx); | ||
TVM_CCALL(TVMModGetFunction(mod, "load_params", 0, (TVMFunctionHandle*)&TVMGraphRuntime_LoadParams)); | ||
TVMGraphRuntime_LoadParams(mod, params.data, params.size); | ||
|
||
return mod; | ||
} | ||
|
||
TVM_DLL void tvm_runtime_destroy(void * runtime) { | ||
void (*TVMGraphRuntimeRelease)(TVMModuleHandle *); | ||
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.release", (TVMFunctionHandle*)&TVMGraphRuntimeRelease)); | ||
TVMGraphRuntimeRelease(&runtime); | ||
} | ||
|
||
TVM_DLL void tvm_runtime_set_input(void * runtime, const char * name, DLTensor * tensor) { | ||
void (*TVMGraphRuntime_SetInput)(TVMModuleHandle, const char *, DLTensor*); | ||
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.set_input", (TVMFunctionHandle*)&TVMGraphRuntime_SetInput)); | ||
TVMGraphRuntime_SetInput(runtime, name, tensor); | ||
} | ||
|
||
TVM_DLL void tvm_runtime_run(void * runtime) { | ||
void (*TVMGraphRuntime_Run)(TVMModuleHandle runtime); | ||
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.run", (TVMFunctionHandle*)&TVMGraphRuntime_Run)); | ||
TVMGraphRuntime_Run(runtime); | ||
} | ||
|
||
TVM_DLL void tvm_runtime_get_output(void * runtime, int32_t index, DLTensor * tensor) { | ||
int (*TVMGraphRuntime_GetOutput)(TVMModuleHandle, const int32_t, DLTensor *); | ||
TVM_CCALL(TVMFuncGetGlobal("tvm.graph_runtime.get_output", (TVMFunctionHandle*)&TVMGraphRuntime_GetOutput)); | ||
TVMGraphRuntime_GetOutput(runtime, index, tensor); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.