Skip to content

Commit

Permalink
added micro_common implementation and python interfaces (apache#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutinifni authored and weberlo committed Jul 24, 2019
1 parent 0ca1d97 commit 059fdc2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
9 changes: 5 additions & 4 deletions python/tvm/contrib/binutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..api import register_func, convert


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


@register_func("tvm_relocate_binary")
@register_func("tvm_callback_relocate_binary")
def tvm_callback_relocate_binary(binary_path, text, data, bss):
"""Relocates sections in the binary to new addresses
Expand Down Expand Up @@ -80,7 +80,7 @@ def tvm_callback_relocate_binary(binary_path, text, data, bss):
return rel_bin


@register_func("tvm_read_binary_section")
@register_func("tvm_callback_read_binary_section")
def tvm_callback_read_binary_section(binary_path, section):
"""Returns the contents of the specified section in the binary file
Expand Down Expand Up @@ -118,7 +118,7 @@ def tvm_callback_read_binary_section(binary_path, section):
return section_bin


@register_func("tvm_get_symbol_map")
@register_func("tvm_callback_get_symbol_map")
def tvm_callback_get_symbol_map(binary):
"""Obtains a map of symbols to addresses in the passed binary
Expand Down Expand Up @@ -152,6 +152,7 @@ def tvm_callback_get_symbol_map(binary):
map_str += line[0] + "\n"
return map_str


@register_func("tvm_callback_compile_micro")
def tvm_callback_compile_binary(code_path="reasonable_default", cc="gcc"):
"""Compiles code into a binary
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/micro/micro_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::string ReadSection(std::string binary_name, SectionKind section) {
const auto* f = Registry::Get("tvm_callback_read_binary_section");
CHECK(f != nullptr)
<< "Require tvm_callback_read_binary_section to exist in registry";
std::string section_contents = (*f)(binary_name, SectionToString(section));
std::string section_contents = (*f)(binary, SectionToString(section));
return section_contents;
}

Expand Down
7 changes: 3 additions & 4 deletions src/runtime/micro/micro_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace runtime {
*/
class MicroDeviceAPI final : public DeviceAPI {
public:
/*! \brief constructor */
MicroDeviceAPI() {
session_ = MicroSession::Global();
}
Expand Down Expand Up @@ -64,6 +65,7 @@ class MicroDeviceAPI final : public DeviceAPI {
to_lld->Write(
const_cast<uint8_t*>(static_cast<const uint8_t*>(to)) + to_offset,
const_cast<char*>(&buffer[0]), size);

} else if (type_from_to == std::make_tuple(micro_devtype, kDLCPU)) {
const std::shared_ptr<LowLevelDevice>& from_lld = session_->low_level_device();
from_lld->Read(
Expand All @@ -82,17 +84,14 @@ class MicroDeviceAPI final : public DeviceAPI {
}
}

// TODO(): ignore this?
void StreamSync(TVMContext ctx, TVMStreamHandle stream) final {
}

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

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

private:
/*! \brief pointer to global session */
MicroSession* session_;
std::shared_ptr<MicroSession>& session_;
};

// register device that can be obtained from Python frontend
Expand Down
40 changes: 18 additions & 22 deletions tests/python/contrib/test_binutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ def make_binary():
return prog_bin


def test_tvm_get_section_size(binary):
def test_tvm_callback_get_section_size(binary):
tmp_dir = util.tempdir()
tmp_bin = tmp_dir.relpath("obj.bin")
with open(tmp_bin, "wb") as f:
f.write(binary)
def verify():
print("Text section size: %d" % tvm_get_section_size(tmp_bin, "text"))
print("Data section size: %d" % tvm_get_section_size(tmp_bin, "data"))
print("Bss section size: %d" % tvm_get_section_size(tmp_bin, "bss"))
print("Text section size: %d" % tvm_callback_get_section_size(tmp_bin, "text"))
print("Data section size: %d" % tvm_callback_get_section_size(tmp_bin, "data"))
print("Bss section size: %d" % tvm_callback_get_section_size(tmp_bin, "bss"))
print
verify()


def test_tvm_relocate_binary(binary):
def test_tvm_callback_relocate_binary(binary):
tmp_dir = util.tempdir()
tmp_bin = tmp_dir.relpath("obj.bin")
with open(tmp_bin, "wb") as f:
f.write(binary)
def verify():
rel_bin = tvm_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
rel_bin = tvm_callback_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
print("Relocated binary section sizes")
test_tvm_get_section_size(rel_bin)
test_tvm_callback_get_section_size(rel_bin)
relf = tmp_dir.relpath("rel.bin")
with open(relf, "wb") as f:
f.write(rel_bin)
Expand All @@ -59,38 +59,34 @@ def verify():
verify()


def test_tvm_read_binary_section(binary):
tmp_dir = util.tempdir()
tmp_bin = tmp_dir.relpath("obj.bin")
with open(tmp_bin, "wb") as f:
f.write(binary)
def test_tvm_callback_read_binary_section(binary):
def verify():
text_bin = tvm_read_binary_section(tmp_bin, "text")
data_bin = tvm_read_binary_section(tmp_bin, "data")
bss_bin = tvm_read_binary_section(tmp_bin, "bss")
text_bin = tvm_callback_read_binary_section(binary, "text")
data_bin = tvm_callback_read_binary_section(binary, "data")
bss_bin = tvm_callback_read_binary_section(binary, "bss")
print("Read text section part of binary? %r" % (text_bin in binary))
print("Read data section part of binary? %r" % (data_bin in binary))
print("Read bss section part of binary? %r" % (bss_bin in binary))
print
verify()


def test_tvm_get_symbol_map(binary):
def test_tvm_callback_get_symbol_map(binary):
tmp_dir = util.tempdir()
tmp_bin = tmp_dir.relpath("obj.bin")
with open(tmp_bin, "wb") as f:
f.write(binary)
def verify():
rel_bin = tvm_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
symbol_map = tvm_get_symbol_map(rel_bin)
rel_bin = tvm_callback_relocate_binary(tmp_bin, "0x0", "0x10000", "0x20000")
symbol_map = tvm_callback_get_symbol_map(rel_bin)
print("Obtained symbol map")
print(symbol_map)
verify()


if __name__ == "__main__":
prog_bin = make_binary()
test_tvm_get_section_size(prog_bin)
test_tvm_relocate_binary(prog_bin)
test_tvm_read_binary_section(prog_bin)
test_tvm_get_symbol_map(prog_bin)
test_tvm_callback_get_section_size(prog_bin)
test_tvm_callback_relocate_binary(prog_bin)
test_tvm_callback_read_binary_section(prog_bin)
test_tvm_callback_get_symbol_map(prog_bin)

0 comments on commit 059fdc2

Please sign in to comment.