Skip to content

Commit 3efca52

Browse files
committed
[RFC] [Contrib] Minimal runtime (~12kb .text on ARMv7/x86) for subset of TVM models
This is an alternative implementation of a subset of the TVM runtime API (and graph runtime) that focuses entirely on reducing code size, at the expense of functionality (no tvm.extern(..) calls via PackedFunc, CPU only, etc). It might be worth incrementally expanding the surface area if there's interest. The motivation for this work was seeing what the minimal useful subset of the TVM runtime is. This is relevant for e.g. super code-size constrained applications in e.g. embedded/mobile. The current runtime is more like O(100KiB) or so, so this might be compelling for some users. The smaller surface area for auditing might make this relevant for #3159, or the usecases I was thinking about in #2523 (comment) re: the Rust runtime. The symbols in the tvm::minimalruntime space (i.e. excluding std:: and picojson::) are about 5KiB, so I think there's a bunch of room here (i.e. we could replace picojson:: with [`jsmn`](https://zserge.com/jsmn.html) or something, and we could replace more of the `std::unordered_map` usage, etc with custom primitives as well (similar to the `DynArray`).
1 parent ce363d6 commit 3efca52

File tree

12 files changed

+2210
-0
lines changed

12 files changed

+2210
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ tvm_option(USE_ROCBLAS "Build with ROCM:RoCBLAS" OFF)
5555
tvm_option(USE_SORT "Build with sort support" OFF)
5656
tvm_option(USE_NNPACK "Build with nnpack support" OFF)
5757
tvm_option(USE_RANDOM "Build with random support" OFF)
58+
tvm_option(USE_MINIMAL_RUNTIME "Build with minimalruntime support" OFF)
5859
tvm_option(USE_ANTLR "Build with ANTLR for Relay parsing" OFF)
5960

6061
# include directories
@@ -210,6 +211,7 @@ include(cmake/modules/LLVM.cmake)
210211
include(cmake/modules/ANTLR.cmake)
211212
include(cmake/modules/contrib/BLAS.cmake)
212213
include(cmake/modules/contrib/Random.cmake)
214+
include(cmake/modules/contrib/MinimalRuntime.cmake)
213215
include(cmake/modules/contrib/Sort.cmake)
214216
include(cmake/modules/contrib/NNPack.cmake)
215217
include(cmake/modules/contrib/HybridDump.cmake)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
if(USE_MINIMAL_RUNTIME)
19+
message(STATUS "Build with contrib.minimal_runtime")
20+
file(GLOB MINIMAL_RUNTIME_CONTRIB_SRC src/contrib/minimalruntime/*.cc)
21+
list(APPEND RUNTIME_SRCS ${MINIMAL_RUNTIME_CONTRIB_SRC})
22+
add_definitions(-DUSE_MINIMAL_RUNTIME=1)
23+
endif(USE_MINIMAL_RUNTIME)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <stddef.h>
23+
#include <stdint.h>
24+
25+
#define TVM_MINIMALRUNTIME_API extern "C" __attribute__((visibility("default")))
26+
27+
TVM_MINIMALRUNTIME_API void* TVMMinimalRuntimeCreate(const char* json, size_t json_len,
28+
void* module);
29+
30+
TVM_MINIMALRUNTIME_API void TVMMinimalRuntimeDestroy(void* handle);
31+
32+
TVM_MINIMALRUNTIME_API void TVMMinimalRuntimeSetInput(void* handle, int index, void* tensor);
33+
34+
TVM_MINIMALRUNTIME_API void TVMMinimalRuntimeRun(void* handle);
35+
36+
TVM_MINIMALRUNTIME_API void TVMMinimalRuntimeGetOutput(void* handle, int index, void* tensor);
37+
38+
TVM_MINIMALRUNTIME_API void* TVMMinimalRuntimeDSOModuleCreate(const char* so, size_t so_len);
39+
40+
TVM_MINIMALRUNTIME_API void TVMMinimalRuntimeDSOModuleDestroy(void* module);
41+
42+
#undef TVM_MINIMALRUNTIME_API
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2+
<!--- or more contributor license agreements. See the NOTICE file -->
3+
<!--- distributed with this work for additional information -->
4+
<!--- regarding copyright ownership. The ASF licenses this file -->
5+
<!--- to you under the Apache License, Version 2.0 (the -->
6+
<!--- "License"); you may not use this file except in compliance -->
7+
<!--- with the License. You may obtain a copy of the License at -->
8+
9+
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10+
11+
<!--- Unless required by applicable law or agreed to in writing, -->
12+
<!--- software distributed under the License is distributed on an -->
13+
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14+
<!--- KIND, either express or implied. See the License for the -->
15+
<!--- specific language governing permissions and limitations -->
16+
<!--- under the License. -->
17+
18+
## A replacement implementation of the TVM runtime, focused on a minimal subset of the overall runtime.
19+
20+
## Notes
21+
22+
`picojson.h` is derived from https://github.com/kazuho/picojson

0 commit comments

Comments
 (0)