Skip to content

Commit 7be3181

Browse files
mutinifniweberlo
authored andcommitted
uTVM interfaces (apache#14)
1 parent 90eee08 commit 7be3181

19 files changed

+862
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF)
3636
tvm_option(USE_SGX "Build with SGX" OFF)
3737
tvm_option(USE_RTTI "Build with RTTI" ON)
3838
tvm_option(USE_MSVC_MT "Build with MT" OFF)
39+
tvm_option(USE_MICRO "Build with Micro" OFF)
3940
tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF)
4041
tvm_option(HIDE_PRIVATE_SYMBOLS "Compile with -fvisibility=hidden." OFF)
4142

@@ -207,6 +208,7 @@ include(cmake/modules/Metal.cmake)
207208
include(cmake/modules/ROCM.cmake)
208209
include(cmake/modules/SGX.cmake)
209210
include(cmake/modules/LLVM.cmake)
211+
include(cmake/modules/Micro.cmake)
210212
include(cmake/modules/ANTLR.cmake)
211213
include(cmake/modules/contrib/BLAS.cmake)
212214
include(cmake/modules/contrib/Random.cmake)

cmake/config.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ set(USE_VULKAN OFF)
6262
# Whether enable OpenGL runtime
6363
set(USE_OPENGL OFF)
6464

65+
# Whether enable Micro runtime
66+
set(USE_MICRO OFF)
67+
6568
# Whether to enable SGX runtime
6669
#
6770
# Possible values for USE_SGX:

cmake/modules/Micro.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if(USE_MICRO)
2+
message(STATUS "Build with Micro support")
3+
file(GLOB RUNTIME_MICRO_SRCS src/runtime/micro/*.cc)
4+
list(APPEND RUNTIME_SRCS ${RUNTIME_MICRO_SRCS})
5+
endif(USE_MICRO)

include/tvm/runtime/c_runtime_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef enum {
8181
kDLAOCL = 5,
8282
kDLSDAccel = 6,
8383
kOpenGL = 11,
84+
kDLMicroDev = 13,
8485
// AddExtraTVMType which is not in DLPack here
8586
} TVMDeviceExtType;
8687

python/tvm/_ffi/runtime_ctypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class TVMContext(ctypes.Structure):
143143
10: 'rocm',
144144
11: 'opengl',
145145
12: 'ext_dev',
146+
13: 'micro_dev',
146147
}
147148
STR2MASK = {
148149
'llvm': 1,
@@ -163,6 +164,7 @@ class TVMContext(ctypes.Structure):
163164
'rocm': 10,
164165
'opengl': 11,
165166
'ext_dev': 12,
167+
'micro_dev': 13,
166168
}
167169
def __init__(self, device_type, device_id):
168170
super(TVMContext, self).__init__()
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*!
2+
* Copyright (c) 2019 by Contributors
3+
* \file allocator_stream.h
4+
* \brief allocator stream utility
5+
*/
6+
#ifndef TVM_RUNTIME_MICRO_ALLOCATOR_STREAM_H_
7+
#define TVM_RUNTIME_MICRO_ALLOCATOR_STREAM_H_
8+
9+
#include <cstring>
10+
#include <string>
11+
#include <algorithm>
12+
#include <vector>
13+
#include <dmlc/memory_io.h>
14+
15+
namespace tvm {
16+
namespace runtime {
17+
/*!
18+
* \brief allocation-based stream with bounded buffer size for uTVM args allocation
19+
* \note based on dmlc::MemoryStringStream
20+
*/
21+
struct AllocatorStream : public dmlc::SeekStream {
22+
public:
23+
/*!
24+
* \brief constructor
25+
* \param p_buffer the pointer to the string.
26+
*/
27+
explicit AllocatorStream(std::string *p_buffer)
28+
: p_buffer_(p_buffer) {
29+
curr_ptr_ = 0;
30+
max_ptr_ = 0;
31+
}
32+
33+
/*!
34+
* \brief reads size bytes of data starting at ptr
35+
* \param ptr address to begin read
36+
* \param size number of bytes to be read
37+
* \return number of bytes read
38+
*/
39+
size_t Read(void *ptr, size_t size) {
40+
CHECK(curr_ptr_ <= p_buffer_->length());
41+
CHECK(curr_ptr_ + size <= max_ptr_);
42+
size_t nread = std::min(p_buffer_->length() - curr_ptr_, size);
43+
if (nread != 0) std::memcpy(ptr, &(*p_buffer_)[0] + curr_ptr_, nread);
44+
curr_ptr_ += nread;
45+
return nread;
46+
}
47+
48+
/*!
49+
* \brief writes size bytes of data starting at ptr
50+
* \param ptr address of the buffer to be written
51+
* \param size number of bytes to be written
52+
*/
53+
void Write(const void *ptr, size_t size) {
54+
if (size == 0) return;
55+
CHECK(curr_ptr_ + size <= max_ptr_);
56+
if (curr_ptr_ + size > p_buffer_->length()) {
57+
p_buffer_->resize(curr_ptr_+size);
58+
}
59+
std::memcpy(&(*p_buffer_)[0] + curr_ptr_, ptr, size);
60+
curr_ptr_ += size;
61+
}
62+
63+
/*!
64+
* \brief seek to specified location within internal buffer
65+
* \param pos seek position from start in bytes
66+
*/
67+
void Seek(size_t pos) {
68+
curr_ptr_ = static_cast<size_t>(pos);
69+
}
70+
71+
/*!
72+
* \brief get seek pointer location
73+
* \return current seek pointer location from start in bytes
74+
*/
75+
size_t Tell(void) {
76+
return curr_ptr_;
77+
}
78+
79+
/*!
80+
* \brief allocates an empty region within the stream buffer
81+
* \param size size of the allocated region
82+
* \return offset bytes of the allocated region from start of the buffer
83+
*/
84+
size_t Allocate(size_t size) {
85+
size_t ret = max_ptr_;
86+
max_ptr_ += size;
87+
return ret;
88+
}
89+
90+
/*!
91+
* \brief returns current size of the stream buffer
92+
* \return buffer size
93+
*/
94+
size_t GetBufferSize() {
95+
return max_ptr_;
96+
}
97+
98+
private:
99+
/*! \brief in memory buffer */
100+
std::string *p_buffer_;
101+
/*! \brief current pointer */
102+
size_t curr_ptr_;
103+
/*! \brief maximum pointer */
104+
size_t max_ptr_;
105+
};
106+
} // namespace runtime
107+
} // namespace tvm
108+
#endif // TVM_RUNTIME_MICRO_ALLOCATOR_STREAM_H_
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*!
2+
* Copyright (c) 2019 by Contributors
3+
* \file utvm_runtime.cc
4+
* \brief micro device init stub
5+
*/
6+
#include "utvm_runtime.h"
7+
8+
// task pointers must be patched before calling a function
9+
UTVMTask task;
10+
11+
// dummy function to signal execution is finished
12+
void UTVMDone() {}
13+
14+
// init stub
15+
int UTVMMain()
16+
{
17+
task.func(task.args, task.arg_type_ids, *task.num_args);
18+
UTVMDone();
19+
return 0;
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*!
2+
* Copyright (c) 2019 by Contributors
3+
* \file utvm_runtime.h
4+
* \brief utvm runtime headers
5+
*/
6+
#ifndef UTVM_RUNTIME_H_
7+
#define UTVM_RUNTIME_H_
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
#include <stdint.h>
13+
14+
/*!
15+
* \brief task structure for uTVM
16+
*/
17+
typedef struct {
18+
int (*func)(void*, void*, int32_t);
19+
void* args;
20+
void* arg_type_ids;
21+
int32_t* num_args;
22+
} UTVMTask;
23+
24+
#ifdef __cplusplus
25+
} // TVM_EXTERN_C
26+
#endif
27+
#endif // UTVM_RUNTIME_H_
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*!
2+
* Copyright (c) 2019 by Contributors
3+
* \file host_low_level_device.cc
4+
* \brief emulated low-level micro device implementation on host machine
5+
*/
6+
7+
#include "low_level_device.h"
8+
9+
namespace tvm {
10+
namespace runtime {
11+
/*!
12+
* \brief emulated low-level device on host machine
13+
*/
14+
class HostLowLevelDevice final : public LowLevelDevice {
15+
public:
16+
/*!
17+
* \brief constructor to initialize on-host memory region to act as device
18+
* \param num_bytes size of the emulated on-device memory region
19+
*/
20+
HostLowLevelDevice(size_t num_bytes);
21+
22+
/*!
23+
* \brief destructor to deallocate on-host device region
24+
*/
25+
~HostLowLevelDevice();
26+
27+
void Write(void* offset,
28+
void* buf,
29+
size_t num_bytes) final;
30+
31+
void Read(void* offset,
32+
void* buf,
33+
size_t num_bytes) final;
34+
35+
void Execute(void* func_addr, void* breakpoint) final;
36+
37+
const void* base_addr() const final;
38+
39+
private:
40+
/*! \brief base address of the micro device memory region */
41+
void* base_addr_;
42+
/*! \brief size of memory region */
43+
size_t size_;
44+
};
45+
46+
const std::shared_ptr<LowLevelDevice> HostLowLevelDeviceCreate(size_t num_bytes) {
47+
return nullptr;
48+
}
49+
} // namespace runtime
50+
} // namespace tvm
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*!
2+
* Copyright (c) 2019 by Contributors
3+
* \file low_level_device.h
4+
* \brief Abstract low-level micro device management
5+
*/
6+
#ifndef TVM_RUNTIME_LOW_LEVEL_DEVICE_H_
7+
#define TVM_RUNTIME_LOW_LEVEL_DEVICE_H_
8+
9+
#include <cstddef>
10+
#include <memory>
11+
12+
namespace tvm {
13+
namespace runtime {
14+
/*!
15+
* \brief virtual interface for low-level micro device management
16+
*/
17+
class LowLevelDevice {
18+
public:
19+
/*! \brief virtual destructor */
20+
virtual ~LowLevelDevice() {}
21+
22+
/*!
23+
* \brief writes num_bytes from buffer to device memory at base_addr + offset
24+
* \param offset on-device memory offset pointer to be written to
25+
* \param buffer on-host buffer to be written
26+
* \param num_bytes number of bytes to be written
27+
*/
28+
virtual void Write(void* offset,
29+
void* buffer,
30+
size_t num_bytes) = 0;
31+
32+
/*!
33+
* \brief reads num_bytes from device memory at base_addr + offset into buffer
34+
* \param offset on-device memory offset pointer to be read from
35+
* \param buffer on-host buffer to be read into
36+
* \param num_bytes number of bytes to be read
37+
*/
38+
virtual void Read(void* offset,
39+
void* buffer,
40+
size_t num_bytes) = 0;
41+
42+
/*!
43+
* \brief starts execution of device at offset
44+
* \param func_addr address of the init stub function
45+
* \param breakpoint breakpoint at which to stop function execution
46+
*/
47+
virtual void Execute(void* func_addr, void* breakpoint) = 0;
48+
49+
/*!
50+
* \brief getter function for base_addr
51+
* \return the base address of the device memory region
52+
*/
53+
virtual const void* base_addr() const = 0;
54+
};
55+
56+
/*!
57+
* \brief create a host low-level device
58+
* \param num_bytes size of the memory region
59+
*/
60+
const std::shared_ptr<LowLevelDevice> HostLowLevelDeviceCreate(size_t num_bytes);
61+
62+
/*!
63+
* \brief connect to OpenOCD and create an OpenOCD low-level device
64+
* \param port port of the OpenOCD server to connect to
65+
*/
66+
const std::shared_ptr<LowLevelDevice> OpenOCDLowLevelDeviceCreate(int port);
67+
} // namespace runtime
68+
} // namespace tvm
69+
#endif // TVM_RUNTIME_LOW_LEVEL_DEVICE_H_

0 commit comments

Comments
 (0)