Skip to content

Commit ab11dac

Browse files
mutinifniweberlo
authored andcommitted
added micro_common implementation and python interfaces (apache#18)
1 parent e73871c commit ab11dac

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

python/tvm/contrib/binutil.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..api import register_func, convert
77

88

9-
@register_func("tvm_get_section_size")
9+
@register_func("tvm_callback_get_section_size")
1010
def tvm_callback_get_section_size(binary_path, section):
1111
"""Finds size of the section in the binary.
1212
Assumes "size" shell command exists (typically works only on Linux machines)
@@ -39,7 +39,7 @@ def tvm_callback_get_section_size(binary_path, section):
3939
return int(out)
4040

4141

42-
@register_func("tvm_relocate_binary")
42+
@register_func("tvm_callback_relocate_binary")
4343
def tvm_callback_relocate_binary(binary_path, text, data, bss):
4444
"""Relocates sections in the binary to new addresses
4545
@@ -80,7 +80,7 @@ def tvm_callback_relocate_binary(binary_path, text, data, bss):
8080
return rel_bin
8181

8282

83-
@register_func("tvm_read_binary_section")
83+
@register_func("tvm_callback_read_binary_section")
8484
def tvm_callback_read_binary_section(binary_path, section):
8585
"""Returns the contents of the specified section in the binary file
8686
@@ -118,7 +118,7 @@ def tvm_callback_read_binary_section(binary_path, section):
118118
return section_bin
119119

120120

121-
@register_func("tvm_get_symbol_map")
121+
@register_func("tvm_callback_get_symbol_map")
122122
def tvm_callback_get_symbol_map(binary):
123123
"""Obtains a map of symbols to addresses in the passed binary
124124
@@ -152,6 +152,7 @@ def tvm_callback_get_symbol_map(binary):
152152
map_str += line[0] + "\n"
153153
return map_str
154154

155+
155156
@register_func("tvm_callback_compile_micro")
156157
def tvm_callback_compile_binary(code_path="reasonable_default", cc="gcc"):
157158
"""Compiles code into a binary

src/runtime/micro/micro_common.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ std::string ReadSection(std::string binary_name, SectionKind section) {
6666
const auto* f = Registry::Get("tvm_callback_read_binary_section");
6767
CHECK(f != nullptr)
6868
<< "Require tvm_callback_read_binary_section to exist in registry";
69-
std::string section_contents = (*f)(binary_name, SectionToString(section));
69+
std::string section_contents = (*f)(binary, SectionToString(section));
7070
return section_contents;
7171
}
7272

src/runtime/micro/micro_device_api.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace runtime {
1616
*/
1717
class MicroDeviceAPI final : public DeviceAPI {
1818
public:
19+
/*! \brief constructor */
1920
MicroDeviceAPI() {
2021
session_ = MicroSession::Global();
2122
}
@@ -64,6 +65,7 @@ class MicroDeviceAPI final : public DeviceAPI {
6465
to_lld->Write(
6566
const_cast<uint8_t*>(static_cast<const uint8_t*>(to)) + to_offset,
6667
const_cast<char*>(&buffer[0]), size);
68+
6769
} else if (type_from_to == std::make_tuple(micro_devtype, kDLCPU)) {
6870
const std::shared_ptr<LowLevelDevice>& from_lld = session_->low_level_device();
6971
from_lld->Read(
@@ -82,17 +84,14 @@ class MicroDeviceAPI final : public DeviceAPI {
8284
}
8385
}
8486

85-
// TODO(): ignore this?
8687
void StreamSync(TVMContext ctx, TVMStreamHandle stream) final {
8788
}
8889

89-
// TODO: what about ctx?
9090
void* AllocWorkspace(TVMContext ctx, size_t size, TVMType type_hint) final {
9191
void* alloc_ptr = session_->AllocateInSection(kWorkspace, size);
9292
return alloc_ptr;
9393
}
9494

95-
// TODO: what about ctx?
9695
void FreeWorkspace(TVMContext ctx, void* data) final {
9796
session_->FreeInSection(kWorkspace, data);
9897
}
@@ -109,7 +108,7 @@ class MicroDeviceAPI final : public DeviceAPI {
109108

110109
private:
111110
/*! \brief pointer to global session */
112-
MicroSession* session_;
111+
std::shared_ptr<MicroSession>& session_;
113112
};
114113

115114
// register device that can be obtained from Python frontend

tests/python/contrib/test_binutil.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ def make_binary():
2424
return prog_bin
2525

2626

27-
def test_tvm_get_section_size(binary):
27+
def test_tvm_callback_get_section_size(binary):
2828
tmp_dir = util.tempdir()
2929
tmp_bin = tmp_dir.relpath("obj.bin")
3030
with open(tmp_bin, "wb") as f:
3131
f.write(binary)
3232
def verify():
33-
print("Text section size: %d" % tvm_get_section_size(tmp_bin, "text"))
34-
print("Data section size: %d" % tvm_get_section_size(tmp_bin, "data"))
35-
print("Bss section size: %d" % tvm_get_section_size(tmp_bin, "bss"))
33+
print("Text section size: %d" % tvm_callback_get_section_size(tmp_bin, "text"))
34+
print("Data section size: %d" % tvm_callback_get_section_size(tmp_bin, "data"))
35+
print("Bss section size: %d" % tvm_callback_get_section_size(tmp_bin, "bss"))
3636
print
3737
verify()
3838

3939

40-
def test_tvm_relocate_binary(binary):
40+
def test_tvm_callback_relocate_binary(binary):
4141
tmp_dir = util.tempdir()
4242
tmp_bin = tmp_dir.relpath("obj.bin")
4343
with open(tmp_bin, "wb") as f:
4444
f.write(binary)
4545
def verify():
46-
rel_bin = tvm_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
46+
rel_bin = tvm_callback_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
4747
print("Relocated binary section sizes")
48-
test_tvm_get_section_size(rel_bin)
48+
test_tvm_callback_get_section_size(rel_bin)
4949
relf = tmp_dir.relpath("rel.bin")
5050
with open(relf, "wb") as f:
5151
f.write(rel_bin)
@@ -59,38 +59,34 @@ def verify():
5959
verify()
6060

6161

62-
def test_tvm_read_binary_section(binary):
63-
tmp_dir = util.tempdir()
64-
tmp_bin = tmp_dir.relpath("obj.bin")
65-
with open(tmp_bin, "wb") as f:
66-
f.write(binary)
62+
def test_tvm_callback_read_binary_section(binary):
6763
def verify():
68-
text_bin = tvm_read_binary_section(tmp_bin, "text")
69-
data_bin = tvm_read_binary_section(tmp_bin, "data")
70-
bss_bin = tvm_read_binary_section(tmp_bin, "bss")
64+
text_bin = tvm_callback_read_binary_section(binary, "text")
65+
data_bin = tvm_callback_read_binary_section(binary, "data")
66+
bss_bin = tvm_callback_read_binary_section(binary, "bss")
7167
print("Read text section part of binary? %r" % (text_bin in binary))
7268
print("Read data section part of binary? %r" % (data_bin in binary))
7369
print("Read bss section part of binary? %r" % (bss_bin in binary))
7470
print
7571
verify()
7672

7773

78-
def test_tvm_get_symbol_map(binary):
74+
def test_tvm_callback_get_symbol_map(binary):
7975
tmp_dir = util.tempdir()
8076
tmp_bin = tmp_dir.relpath("obj.bin")
8177
with open(tmp_bin, "wb") as f:
8278
f.write(binary)
8379
def verify():
84-
rel_bin = tvm_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
85-
symbol_map = tvm_get_symbol_map(rel_bin)
80+
rel_bin = tvm_callback_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
81+
symbol_map = tvm_callback_get_symbol_map(rel_bin)
8682
print("Obtained symbol map")
8783
print(symbol_map)
8884
verify()
8985

9086

9187
if __name__ == "__main__":
9288
prog_bin = make_binary()
93-
test_tvm_get_section_size(prog_bin)
94-
test_tvm_relocate_binary(prog_bin)
95-
test_tvm_read_binary_section(prog_bin)
96-
test_tvm_get_symbol_map(prog_bin)
89+
test_tvm_callback_get_section_size(prog_bin)
90+
test_tvm_callback_relocate_binary(prog_bin)
91+
test_tvm_callback_read_binary_section(prog_bin)
92+
test_tvm_callback_get_symbol_map(prog_bin)

0 commit comments

Comments
 (0)