Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions hello_tvm/c_runtime/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
all: double

TVM_ROOT=/home/sunway/source/tvm
CRT_ROOT=${TVM_ROOT}/build/standalone_crt

DMLC_CORE=${TVM_ROOT}/3rdparty/dmlc-core
PKG_COMPILE_OPTS = -g -Wall -O2 -fPIC
CFLAGS = ${PKG_COMPILE_OPTS} \
-I${TVM_ROOT}/include \
-I${DMLC_CORE}/include \
-I${TVM_ROOT}/3rdparty/dlpack/include \
-I. \
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\> \
-ffunction-sections -fdata-sections

LDFLAGS = -static -Wl,-gc-sections

CRT_SRCS = $(shell find $(CRT_ROOT))

MODEL_OBJ = devc.o lib0.o lib1.o

${MODEL_OBJ}:libdouble.py
python ./libdouble.py
tar xvf /tmp/libdouble.tar

crt/libcommon.a: $(CRT_SRCS)
cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath .)/crt CRT_CONFIG=$(abspath crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" common

crt/libmemory.a: $(CRT_SRCS)
cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath .)/crt CRT_CONFIG=$(abspath crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" memory

OBJS=double.o tvm_runtime.o
double: ${OBJS} $(MODEL_OBJ) crt/libmemory.a crt/libcommon.a

run:double
./double

clean:
-rm ${OBJS}
-rm ${MODEL_OBJ}
-rm double
-rm -rf crt
51 changes: 51 additions & 0 deletions hello_tvm/c_runtime/crt_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

/*!
* \file apps/bundle_deploy/crt_config.h
* \brief CRT configuration for bundle_deploy app.
*/
#ifndef TVM_RUNTIME_CRT_CONFIG_H_
#define TVM_RUNTIME_CRT_CONFIG_H_

/*! Log level of the CRT runtime */
#define TVM_CRT_LOG_LEVEL TVM_CRT_LOG_LEVEL_DEBUG

/*! Support low-level debugging in MISRA-C runtime */
#define TVM_CRT_DEBUG 0

/*! Maximum supported dimension in NDArray */
#define TVM_CRT_MAX_NDIM 6
/*! Maximum supported arguments in generated functions */
#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 string length in function names */
#define TVM_CRT_STRLEN_NAME 80

/*! Maximum number of registered modules. */
#define TVM_CRT_MAX_REGISTERED_MODULES 2

/*! Size of the global function registry, in bytes. */
#define TVM_CRT_GLOBAL_FUNC_REGISTRY_SIZE_BYTES 200

/*! Maximum packet size, in bytes, including the length header. */
#define TVM_CRT_MAX_PACKET_SIZE_BYTES 512

#endif // TVM_RUNTIME_CRT_CONFIG_H_
51 changes: 51 additions & 0 deletions hello_tvm/c_runtime/double.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <float.h>
#include <stdio.h>
#include <stdlib.h>

#include "tvm_runtime.h"

int main(int argc, char** argv) {
TVMModuleHandle syslib = tvm_runtime_create();
int ndim = 1;
int dtype_code = kDLFloat;
int dtype_bits = 32;
int dtype_lanes = 1;
int device_type = kDLCPU;
int device_id = 0;
int64_t shape[1] = {10};

DLTensor x;
TVMArrayHandle input = &x;
TVMArrayAlloc(
shape, ndim, dtype_code, dtype_bits, dtype_lanes, device_type,
device_id, &input);

DLTensor y;
TVMArrayHandle output = &y;
TVMArrayAlloc(
shape, ndim, dtype_code, dtype_bits, dtype_lanes, device_type,
device_id, &output);

for (int i = 0; i < shape[0]; ++i) {
((float*)(input->data))[i] = i;
}

TVMArgs args = TVMArgs_Create(
(TVMValue[]){{.v_handle = input}, {.v_handle = output}},
(uint32_t[]){kTVMNDArrayHandle, kTVMNDArrayHandle}, 2);

TVMPackedFunc func;
TVMPackedFunc_InitModuleFunc(
&func, syslib, "tvmgen_default_fused_add", &args);

TVMPackedFunc_Call(&func);

for (int i = 0; i < shape[0]; ++i) {
printf(
"%.0f*2=%.0f\n", ((float*)input->data)[i],
((float*)output->data)[i]);
}

TVMArrayFree(input);
TVMArrayFree(output);
}
34 changes: 34 additions & 0 deletions hello_tvm/c_runtime/libdouble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import tvm
import numpy as np
from tvm import te
from tvm import relay
import os


def prepare_relay_libs():
x = relay.var("x", shape=(10,), dtype="float32")
y = relay.add(x, x)
func = relay.Function([x], y)
mod = tvm.IRModule.from_expr(func)

with tvm.transform.PassContext(opt_level=1):
mod = relay.build(mod, target="llvm --system-lib --runtime=c", params=None)

mod.lib.export_library("/tmp/libdouble.tar")


def prepare_te_libs():
A = te.placeholder((10,), name="A")
B = te.compute(A.shape, lambda *i: A(*i) * 2.0, name="B")
s = te.create_schedule(B.op)

fadd_syslib = tvm.build(
s, [A, B], "llvm --system-lib --runtime=c", name="tvmgen_default_fused_add"
)
fadd_syslib.export_library("/tmp/libdouble.tar")


if __name__ == "__main__":
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
prepare_relay_libs()
# prepare_te_libs()
58 changes: 58 additions & 0 deletions hello_tvm/c_runtime/tvm_runtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 2021-08-09 11:02
#include "tvm_runtime.h"

#define CRT_MEMORY_NUM_PAGES 16384
#define CRT_MEMORY_PAGE_SIZE_LOG2 10

static uint8_t
g_crt_memory[CRT_MEMORY_NUM_PAGES * (1 << CRT_MEMORY_PAGE_SIZE_LOG2)];
static MemoryManagerInterface* g_memory_manager;

void TVMLogf(const char* msg, ...) {
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
}

void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t error_code) {
fprintf(stderr, "TVMPlatformAbort: %d\n", error_code);
exit(-1);
}

tvm_crt_error_t TVMPlatformMemoryAllocate(
size_t num_bytes, DLDevice dev, void** out_ptr) {
return g_memory_manager->Allocate(
g_memory_manager, num_bytes, dev, out_ptr);
}

tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
return g_memory_manager->Free(g_memory_manager, ptr, dev);
}

tvm_crt_error_t TVMPlatformTimerStart() {
return kTvmErrorFunctionCallNotImplemented;
}

tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {
return kTvmErrorFunctionCallNotImplemented;
}

TVMModuleHandle tvm_runtime_create() {
DLDevice dev;
dev.device_type = (DLDeviceType)kDLCPU;
dev.device_id = 0;

// get pointers
PageMemoryManagerCreate(
&g_memory_manager, g_crt_memory, sizeof(g_crt_memory),
CRT_MEMORY_PAGE_SIZE_LOG2);
TVMInitializeRuntime();
TVMPackedFunc pf;
TVMArgs args = TVMArgs_Create(NULL, NULL, 0);
TVMPackedFunc_InitGlobalFunc(&pf, "runtime.SystemLib", &args);
TVMPackedFunc_Call(&pf);

TVMModuleHandle mod_syslib = TVMArgs_AsModuleHandle(&pf.ret_value, 0);
return mod_syslib;
}
10 changes: 10 additions & 0 deletions hello_tvm/c_runtime/tvm_runtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 2021-08-09 11:03
#ifndef TVM_RUNTIME_H
#define TVM_RUNTIME_H

#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/crt/crt.h>
#include <tvm/runtime/crt/packed_func.h>
#include <tvm/runtime/crt/page_allocator.h>

#endif // TVM_RUNTIME_H
57 changes: 57 additions & 0 deletions hello_tvm/c_runtime_graph_runner/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
all: kws

TVM_ROOT=~/source/tvm
CRT_ROOT=${TVM_ROOT}/build/standalone_crt

DMLC_CORE=${TVM_ROOT}/3rdparty/dmlc-core
PKG_COMPILE_OPTS = -g -Wall -O2 -fPIC
CFLAGS = ${PKG_COMPILE_OPTS} \
-I${TVM_ROOT}/include \
-I${DMLC_CORE}/include \
-I${TVM_ROOT}/3rdparty/dlpack/include \
-I. \
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\> \
-ffunction-sections -fdata-sections

LDFLAGS = -static -Wl,-gc-sections
LDLIBS = -lm

CRT_SRCS = $(shell find $(CRT_ROOT))

MODEL_OBJ = devc.o lib0.o lib1.o

kws_graph.c:kws_graph.json
xxd -i $^ > $@

kws_params.c:kws_params.bin
xxd -i $^ > $@

${MODEL_OBJ} kws_graph.json kws_params.bin:libkws.py
python ./libkws.py
tar xvf /tmp/libkws.tar

crt/libcommon.a: $(CRT_SRCS)
cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath .)/crt CRT_CONFIG=$(abspath crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" common

crt/libmemory.a: $(CRT_SRCS)
cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath .)/crt CRT_CONFIG=$(abspath crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" memory

crt/libgraph_executor.a: $(CRT_SRCS)
$(QUIET)cd $(CRT_ROOT) && make QUIET= BUILD_DIR=$(abspath .)/crt CRT_CONFIG=$(abspath crt_config.h) "EXTRA_CFLAGS=$(PKG_COMPILE_OPTS)" graph_executor

OBJ=kws.o tvm_runtime.o kws_graph.o kws_params.o test/test_xiaoai.o test/test_unknown.o

kws: ${OBJ} $(MODEL_OBJ) crt/libmemory.a crt/libcommon.a crt/libgraph_executor.a

run:kws
./kws 2>/dev/null

clean:
-rm ${OBJ}
-rm ${MODEL_OBJ}
-rm kws
-rm -rf crt
-rm kws_graph.json
-rm kws_params.bin
-rm kws_graph.c
-rm kws_params.c
Binary file added hello_tvm/c_runtime_graph_runner/calib.npy
Binary file not shown.
51 changes: 51 additions & 0 deletions hello_tvm/c_runtime_graph_runner/crt_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

/*!
* \file apps/bundle_deploy/crt_config.h
* \brief CRT configuration for bundle_deploy app.
*/
#ifndef TVM_RUNTIME_CRT_CONFIG_H_
#define TVM_RUNTIME_CRT_CONFIG_H_

/*! Log level of the CRT runtime */
#define TVM_CRT_LOG_LEVEL TVM_CRT_LOG_LEVEL_DEBUG

/*! Support low-level debugging in MISRA-C runtime */
#define TVM_CRT_DEBUG 0

/*! Maximum supported dimension in NDArray */
#define TVM_CRT_MAX_NDIM 6
/*! Maximum supported arguments in generated functions */
#define TVM_CRT_MAX_ARGS 12
/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */
#define TVM_CRT_STRLEN_DLTYPE 10
/*! Maximum supported string length in function names */
#define TVM_CRT_STRLEN_NAME 80

/*! Maximum number of registered modules. */
#define TVM_CRT_MAX_REGISTERED_MODULES 2

/*! Size of the global function registry, in bytes. */
#define TVM_CRT_GLOBAL_FUNC_REGISTRY_SIZE_BYTES 200

/*! Maximum packet size, in bytes, including the length header. */
#define TVM_CRT_MAX_PACKET_SIZE_BYTES 512

#endif // TVM_RUNTIME_CRT_CONFIG_H_
Binary file added hello_tvm/c_runtime_graph_runner/kws
Binary file not shown.
Loading