-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Runtime][MISRA-C][Bundle] Bundle deployment with static linking #5158
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
216f4c7
test file for static link added
mehrdadh e09271d
rename files
mehrdadh 2e72463
Fixed static linking issue
mehrdadh e0f39ba
cleanup
mehrdadh 3b57517
changed to dynamic and static demo
mehrdadh 6aaadb3
MISRA-C static and dynamic test
mehrdadh d140fb1
cleanup
mehrdadh 173aa0f
cleanup
mehrdadh 8f3f99b
Update README.md
mehrdadh fc5337e
cleanup headers
mehrdadh 956a756
Merge branch 'app_bundle' of github.com:mehrdadhe/incubator-tvm into …
mehrdadh caa7d1a
update readme
mehrdadh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,22 +33,36 @@ PKG_LDFLAGS = -pthread | |
|
||
build_dir := build | ||
|
||
demo: $(build_dir)/demo $(build_dir)/bundle.so $(build_dir)/bundle_c.so $(build_dir)/cat.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/demo $(build_dir)/bundle.so $(build_dir)/cat.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/demo $(build_dir)/bundle_c.so $(build_dir)/cat.bin | ||
demo_dynamic: $(build_dir)/demo_dynamic $(build_dir)/bundle.so $(build_dir)/bundle_c.so $(build_dir)/cat.bin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apps/bundle_deploy/README.md needs to be updated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the README file. Please let me know if you have any suggestions. |
||
TVM_NUM_THREADS=1 $(build_dir)/demo_dynamic $(build_dir)/bundle.so $(build_dir)/cat.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/demo_dynamic $(build_dir)/bundle_c.so $(build_dir)/cat.bin | ||
|
||
test: $(build_dir)/test $(build_dir)/test_bundle.so $(build_dir)/test_bundle_c.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/test $(build_dir)/test_bundle.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin $(build_dir)/test_graph.json $(build_dir)/test_params.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/test $(build_dir)/test_bundle_c.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin $(build_dir)/test_graph.json $(build_dir)/test_params.bin | ||
test_dynamic: $(build_dir)/test_dynamic $(build_dir)/test_bundle.so $(build_dir)/test_bundle_c.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/test_dynamic $(build_dir)/test_bundle.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin $(build_dir)/test_graph.json $(build_dir)/test_params.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/test_dynamic $(build_dir)/test_bundle_c.so $(build_dir)/test_data.bin $(build_dir)/test_output.bin $(build_dir)/test_graph.json $(build_dir)/test_params.bin | ||
|
||
$(build_dir)/demo: demo.cc ${build_dir}/graph.json.c ${build_dir}/params.bin.c | ||
demo_static: $(build_dir)/demo_static $(build_dir)/cat.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/demo_static $(build_dir)/cat.bin | ||
|
||
test_static: $(build_dir)/test_static $(build_dir)/test_data.bin $(build_dir)/test_output.bin | ||
TVM_NUM_THREADS=1 $(build_dir)/test_static $(build_dir)/test_data.bin $(build_dir)/test_output.bin $(build_dir)/test_graph.json $(build_dir)/test_params.bin | ||
|
||
$(build_dir)/demo_dynamic: demo.cc ${build_dir}/graph.json.c ${build_dir}/params.bin.c | ||
@mkdir -p $(@D) | ||
g++ $(PKG_CXXFLAGS) -o $@ demo.cc -ldl | ||
|
||
$(build_dir)/test: test.cc ${build_dir}/test_graph.json ${build_dir}/test_params.bin | ||
$(build_dir)/test_dynamic: test.cc ${build_dir}/test_graph.json ${build_dir}/test_params.bin | ||
@mkdir -p $(@D) | ||
g++ $(PKG_CXXFLAGS) -o $@ test.cc -ldl | ||
|
||
$(build_dir)/demo_static: demo_static.c ${build_dir}/bundle_static.o ${build_dir}/model.o ${build_dir}/graph.json.c ${build_dir}/params.bin.c | ||
@mkdir -p $(@D) | ||
gcc $(PKG_CXXFLAGS) -o $@ demo_static.c ${build_dir}/bundle_static.o ${build_dir}/model.o -lm | ||
|
||
$(build_dir)/test_static: test_static.c ${build_dir}/bundle_static.o ${build_dir}/test_model.o | ||
@mkdir -p $(@D) | ||
gcc $(PKG_CXXFLAGS) -o $@ $^ | ||
|
||
# Serialize our graph.json file. | ||
$(build_dir)/graph.json.c: $(build_dir)/graph.json | ||
xxd -i $^ > $@ | ||
|
@@ -89,6 +103,10 @@ $(build_dir)/test_bundle_c.so: bundle.c runtime.c $(build_dir)/test_model.o | |
@mkdir -p $(@D) | ||
gcc -shared $(PKG_CFLAGS) -fvisibility=hidden -o $@ $^ $(PKG_LDFLAGS) | ||
|
||
$(build_dir)/bundle_static.o: bundle_static.c | ||
@mkdir -p $(@D) | ||
gcc -c $(PKG_CFLAGS) -o $@ $^ | ||
|
||
clean: | ||
rm -rf $(build_dir)/bundle.so $(build_dir)/bundle_c.so $(build_dir)/test_bundle.so $(build_dir)/test_bundle_c.so | ||
|
||
|
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,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef TVM_APPS_BUNDLE_H | ||
mehrdadh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#define TVM_APPS_BUNDLE_H | ||
|
||
#include <tvm/runtime/c_runtime_api.h> | ||
|
||
TVM_DLL void * tvm_runtime_create(const char * json_data, | ||
const char * params_data, | ||
const uint64_t params_size); | ||
|
||
TVM_DLL void tvm_runtime_destroy(void * runtime); | ||
|
||
TVM_DLL void tvm_runtime_set_input(void * runtime, | ||
const char * name, | ||
DLTensor * tensor); | ||
|
||
TVM_DLL void tvm_runtime_run(void * runtime); | ||
|
||
TVM_DLL void tvm_runtime_get_output(void * runtime, | ||
int32_t index, | ||
DLTensor * tensor); | ||
|
||
#endif /* TVM_APPS_BUNDLE_H */ | ||
mehrdadh marked this conversation as resolved.
Show resolved
Hide resolved
|
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,94 @@ | ||
/* | ||
* 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 "bundle.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
mehrdadh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "runtime.c" | ||
|
||
|
||
// /*! \brief macro to do C API call */ | ||
tmoreau89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// #define TVM_CCALL(func) \ | ||
// do { \ | ||
// int ret = (func); \ | ||
// if (ret != 0) { \ | ||
// fprintf(stderr, "%s: %d: error: %s\n", __FILE__, __LINE__, TVMGetLastError()); \ | ||
// exit(ret); \ | ||
// } \ | ||
// else { \ | ||
// fprintf(stdout, "Function load success!\n"); \ | ||
// } \ | ||
// } 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 | ||
void * (*SystemLibraryCreate)(); | ||
TVMGraphRuntime * (*TVMGraphRuntimeCreate)(const char *, const TVMModuleHandle, const TVMContext *); | ||
int (*TVMGraphRuntime_LoadParams)(TVMModuleHandle, const char *, const uint32_t); | ||
|
||
// get pointers | ||
TVMFuncGetGlobal("runtime.SystemLib", (TVMFunctionHandle*)&SystemLibraryCreate); | ||
TVMFuncGetGlobal("tvm.graph_runtime.create", (TVMFunctionHandle*)&TVMGraphRuntimeCreate); | ||
|
||
// run modules | ||
TVMModuleHandle mod_syslib = SystemLibraryCreate(); | ||
TVMModuleHandle mod = TVMGraphRuntimeCreate(json_data, mod_syslib, &ctx); | ||
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 *); | ||
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*); | ||
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); | ||
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 *); | ||
TVMFuncGetGlobal("tvm.graph_runtime.get_output", (TVMFunctionHandle*)&TVMGraphRuntime_GetOutput); | ||
TVMGraphRuntime_GetOutput(runtime, index, tensor); | ||
} | ||
|
||
mehrdadh marked this conversation as resolved.
Show resolved
Hide resolved
|
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,106 @@ | ||
/* | ||
* 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 <assert.h> | ||
#include <stdio.h> | ||
#include <sys/time.h> | ||
#include <float.h> | ||
|
||
#include "bundle.h" | ||
#include "build/graph.json.c" | ||
#include "build/params.bin.c" | ||
|
||
|
||
int main(int argc, char **argv) { | ||
assert(argc == 2 && "Usage: demo_static <cat.bin>"); | ||
|
||
char * json_data = (char *)(build_graph_json); | ||
char * params_data = (char *)(build_params_bin); | ||
uint64_t params_size = build_params_bin_len; | ||
|
||
struct timeval t0, t1, t2, t3, t4, t5; | ||
gettimeofday(&t0, 0); | ||
|
||
auto *handle = tvm_runtime_create(json_data, params_data, params_size); | ||
gettimeofday(&t1, 0); | ||
|
||
float input_storage[1 * 3 * 224 * 224]; | ||
FILE * fp = fopen(argv[1], "rb"); | ||
fread(input_storage, 3 * 224 * 224, 4, fp); | ||
fclose(fp); | ||
|
||
DLTensor input; | ||
input.data = input_storage; | ||
DLContext ctx = {kDLCPU, 0}; | ||
input.ctx = ctx; | ||
input.ndim = 4; | ||
DLDataType dtype = {kDLFloat, 32, 1}; | ||
input.dtype = dtype; | ||
int64_t shape [4] = {1, 3, 224, 224}; | ||
input.shape = &shape; | ||
input.strides = NULL; | ||
input.byte_offset = 0; | ||
|
||
tvm_runtime_set_input(handle, "data", &input); | ||
gettimeofday(&t2, 0); | ||
|
||
tvm_runtime_run(handle); | ||
gettimeofday(&t3, 0); | ||
|
||
float output_storage[1000]; | ||
DLTensor output; | ||
output.data = output_storage; | ||
DLContext out_ctx = {kDLCPU, 0}; | ||
output.ctx = out_ctx; | ||
output.ndim = 2; | ||
DLDataType out_dtype = {kDLFloat, 32, 1}; | ||
output.dtype = out_dtype; | ||
int64_t out_shape [2] = {1, 1000}; | ||
output.shape = &out_shape; | ||
output.strides = NULL; | ||
output.byte_offset = 0; | ||
|
||
tvm_runtime_get_output(handle, 0, &output); | ||
gettimeofday(&t4, 0); | ||
|
||
float max_iter = -FLT_MAX; | ||
int32_t max_index = -1; | ||
for (auto i = 0; i < 1000; ++i) { | ||
mehrdadh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (output_storage[i] > max_iter) { | ||
max_iter = output_storage[i]; | ||
max_index = i; | ||
} | ||
} | ||
|
||
tvm_runtime_destroy(handle); | ||
gettimeofday(&t5, 0); | ||
|
||
printf("The maximum position in output vector is: %d, with max-value %f.\n", | ||
max_index, max_iter); | ||
printf("timing: %.2f ms (create), %.2f ms (set_input), %.2f ms (run), " | ||
"%.2f ms (get_output), %.2f ms (destroy)\n", | ||
(t1.tv_sec-t0.tv_sec)*1000000 + (t1.tv_usec-t0.tv_usec)/1000.f, | ||
(t2.tv_sec-t1.tv_sec)*1000000 + (t2.tv_usec-t1.tv_usec)/1000.f, | ||
(t3.tv_sec-t2.tv_sec)*1000000 + (t3.tv_usec-t2.tv_usec)/1000.f, | ||
(t4.tv_sec-t3.tv_sec)*1000000 + (t4.tv_usec-t3.tv_usec)/1000.f, | ||
(t5.tv_sec-t4.tv_sec)*1000000 + (t5.tv_usec-t4.tv_usec)/1000.f); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update README.md to reflect this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!