Skip to content

Commit

Permalink
[RFC] [Contrib] Minimal runtime (~12kb .text on ARMv7/x86) for subset…
Browse files Browse the repository at this point in the history
… 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`).
  • Loading branch information
ajtulloch committed Jul 22, 2019
1 parent 443b5b4 commit ab32dfa
Show file tree
Hide file tree
Showing 12 changed files with 2,219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tvm_option(USE_ROCBLAS "Build with ROCM:RoCBLAS" OFF)
tvm_option(USE_SORT "Build with sort support" OFF)
tvm_option(USE_NNPACK "Build with nnpack support" OFF)
tvm_option(USE_RANDOM "Build with random support" OFF)
tvm_option(USE_MICRO_STANDALONE_RUNTIME "Build with micro.standalone_runtime support" OFF)
tvm_option(USE_ANTLR "Build with ANTLR for Relay parsing" OFF)

# include directories
Expand Down Expand Up @@ -210,6 +211,7 @@ include(cmake/modules/LLVM.cmake)
include(cmake/modules/ANTLR.cmake)
include(cmake/modules/contrib/BLAS.cmake)
include(cmake/modules/contrib/Random.cmake)
include(cmake/modules/contrib/MicroStandaloneRuntime.cmake)
include(cmake/modules/contrib/Sort.cmake)
include(cmake/modules/contrib/NNPack.cmake)
include(cmake/modules/contrib/HybridDump.cmake)
Expand Down
23 changes: 23 additions & 0 deletions cmake/modules/contrib/MicroStandaloneRuntime.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.

if(USE_MICRO_STANDALONE_RUNTIME)
message(STATUS "Build with micro.standalone_runtime")
file(GLOB MICRO_STANDALONE_RUNTIME_SRC src/runtime/micro/standalone/*.cc)
list(APPEND RUNTIME_SRCS ${MICRO_STANDALONE_RUNTIME_SRC})
add_definitions(-DUSE_MICRO_STANDALONE_RUNTIME=1)
endif(USE_MICRO_STANDALONE_RUNTIME)
46 changes: 46 additions & 0 deletions include/tvm/runtime/micro/standalone/minimalruntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

#pragma once

#include <stddef.h>
#include <stdint.h>

#define TVM_MICRO_STANDALONE_RUNTIME_API extern "C" __attribute__((visibility("default")))

TVM_MICRO_STANDALONE_RUNTIME_API void* TVMMicroStandaloneRuntimeCreate(const char* json,
size_t json_len,
void* module);

TVM_MICRO_STANDALONE_RUNTIME_API void TVMMicroStandaloneRuntimeDestroy(void* handle);

TVM_MICRO_STANDALONE_RUNTIME_API void TVMMicroStandaloneRuntimeSetInput(void* handle, int index,
void* tensor);

TVM_MICRO_STANDALONE_RUNTIME_API void TVMMicroStandaloneRuntimeRun(void* handle);

TVM_MICRO_STANDALONE_RUNTIME_API void TVMMicroStandaloneRuntimeGetOutput(void* handle, int index,
void* tensor);

TVM_MICRO_STANDALONE_RUNTIME_API void* TVMMicroStandaloneRuntimeDSOModuleCreate(const char* so,
size_t so_len);

TVM_MICRO_STANDALONE_RUNTIME_API void TVMMicroStandaloneRuntimeDSOModuleDestroy(void* module);

#undef TVM_MICRO_STANDALONE_RUNTIME_API
22 changes: 22 additions & 0 deletions src/runtime/micro/standalone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--- 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. -->

## A replacement implementation of the TVM runtime, focused on a minimal subset of the overall runtime.

## Notes

`picojson.h` is derived from https://github.com/kazuho/picojson
Loading

0 comments on commit ab32dfa

Please sign in to comment.