Skip to content

Commit

Permalink
Make quotes thicker
Browse files Browse the repository at this point in the history
  • Loading branch information
weberlo committed Nov 24, 2019
1 parent 27897d6 commit 013795e
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 222 deletions.
78 changes: 39 additions & 39 deletions python/tvm/contrib/binutil.py
Expand Up @@ -87,10 +87,10 @@ def run_cmd(cmd):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
(output, _) = proc.communicate()
output = output.decode('utf-8')
output = output.decode("utf-8")
if proc.returncode != 0:
cmd_str = ' '.join(cmd)
msg = f'error while running command \"{cmd_str}\":\n{output}'
cmd_str = " ".join(cmd)
msg = f"error while running command \"{cmd_str}\":\n{output}"
raise RuntimeError(msg)
return output

Expand All @@ -117,18 +117,18 @@ def tvm_callback_get_section_size(binary_path, section_name, toolchain_prefix):
size of the section in bytes
"""
if not os.path.isfile(binary_path):
raise RuntimeError('no such file \"{}\"'.format(binary_path))
raise RuntimeError("no such file \"{}\"".format(binary_path))
# We use the "-A" flag here to get the ".rodata" section's size, which is
# not included by default.
size_output = run_cmd(['{}size'.format(toolchain_prefix), '-A', binary_path])
size_output = run_cmd(["{}size".format(toolchain_prefix), "-A", binary_path])

# TODO(weberlo): Refactor this method and `*relocate_binary` so they are
# both aware of [".bss", ".sbss", ".sdata"] being relocated to ".bss".
section_mapping = {
'.text': ['.text'],
'.rodata': ['.rodata'],
'.data': ['.data', '.sdata'],
'.bss': ['.bss', '.sbss'],
".text": [".text"],
".rodata": [".rodata"],
".data": [".data", ".sdata"],
".bss": [".bss", ".sbss"],
}
sections_to_sum = section_mapping["." + section_name]
section_size = 0
Expand All @@ -147,7 +147,7 @@ def tvm_callback_get_section_size(binary_path, section_name, toolchain_prefix):
# NOTE: For some reason, the size of the BSS section on the RISC-V
# GCC is sometimes reported to be smaller than it is, so we need to adjust
# for this.
if 'riscv' in toolchain_prefix and section_name == 'bss':
if "riscv" in toolchain_prefix and section_name == "bss":
# TODO(weberlo): Figure out why 32 is the minimum constant that works.
#
# The current hypothesis is that the last symbols in the ".bss" and
Expand All @@ -162,7 +162,7 @@ def tvm_callback_get_section_size(binary_path, section_name, toolchain_prefix):
return section_size


@register_func('tvm_callback_relocate_binary')
@register_func("tvm_callback_relocate_binary")
def tvm_callback_relocate_binary(
binary_path,
word_size,
Expand Down Expand Up @@ -206,10 +206,10 @@ def tvm_callback_relocate_binary(
the relocated binary
"""
stack_pointer_init = stack_end - word_size
ld_script_contents = ''
ld_script_contents = ""
# TODO(weberlo): There should be a better way to configure this for different archs.
if 'riscv' in toolchain_prefix:
ld_script_contents += 'OUTPUT_ARCH( "riscv" )\n\n'
if "riscv" in toolchain_prefix:
ld_script_contents += "OUTPUT_ARCH( \"riscv\" )\n\n"
ld_script_contents += RELOCATION_LD_SCRIPT_TEMPLATE.format(
word_size=word_size,
text_start=text_start,
Expand All @@ -219,21 +219,21 @@ def tvm_callback_relocate_binary(
stack_pointer_init=stack_pointer_init)

tmp_dir = util.tempdir()
rel_obj_path = tmp_dir.relpath('relocated.obj')
rel_ld_script_path = tmp_dir.relpath('relocated.lds')
with open(rel_ld_script_path, 'w') as f:
rel_obj_path = tmp_dir.relpath("relocated.obj")
rel_ld_script_path = tmp_dir.relpath("relocated.lds")
with open(rel_ld_script_path, "w") as f:
f.write(ld_script_contents)
run_cmd([
'{}ld'.format(toolchain_prefix),
"{}ld".format(toolchain_prefix),
binary_path,
'-T', rel_ld_script_path,
'-o', rel_obj_path])
with open(rel_obj_path, 'rb') as f:
"-T", rel_ld_script_path,
"-o", rel_obj_path])
with open(rel_obj_path, "rb") as f:
rel_bin = bytearray(f.read())
return rel_bin


@register_func('tvm_callback_read_binary_section')
@register_func("tvm_callback_read_binary_section")
def tvm_callback_read_binary_section(binary, section, toolchain_prefix):
"""Returns the contents of the specified section in the binary byte array
Expand All @@ -254,26 +254,26 @@ def tvm_callback_read_binary_section(binary, section, toolchain_prefix):
contents of the read section
"""
tmp_dir = util.tempdir()
tmp_bin = tmp_dir.relpath('temp.bin')
tmp_section = tmp_dir.relpath('tmp_section.bin')
with open(tmp_bin, 'wb') as out_file:
tmp_bin = tmp_dir.relpath("temp.bin")
tmp_section = tmp_dir.relpath("tmp_section.bin")
with open(tmp_bin, "wb") as out_file:
out_file.write(bytes(binary))
run_cmd([
'{}objcopy'.format(toolchain_prefix),
'--dump-section',
'.{}={}'.format(section, tmp_section),
"{}objcopy".format(toolchain_prefix),
"--dump-section",
".{}={}".format(section, tmp_section),
tmp_bin])
if os.path.isfile(tmp_section):
# Get section content if it exists.
with open(tmp_section, 'rb') as f:
with open(tmp_section, "rb") as f:
section_bin = bytearray(f.read())
else:
# Return empty bytearray if the section does not exist.
section_bin = bytearray('', 'utf-8')
section_bin = bytearray("", "utf-8")
return section_bin


@register_func('tvm_callback_get_symbol_map')
@register_func("tvm_callback_get_symbol_map")
def tvm_callback_get_symbol_map(binary, toolchain_prefix):
"""Obtains a map of symbols to addresses in the passed binary
Expand All @@ -292,18 +292,18 @@ def tvm_callback_get_symbol_map(binary, toolchain_prefix):
alternating newline-separated keys and values
"""
tmp_dir = util.tempdir()
tmp_obj = tmp_dir.relpath('tmp_obj.bin')
with open(tmp_obj, 'wb') as out_file:
tmp_obj = tmp_dir.relpath("tmp_obj.bin")
with open(tmp_obj, "wb") as out_file:
out_file.write(bytes(binary))
nm_output = run_cmd([
'{}nm'.format(toolchain_prefix),
'-C',
'--defined-only',
"{}nm".format(toolchain_prefix),
"-C",
"--defined-only",
tmp_obj])
nm_output = nm_output.splitlines()
map_str = ''
map_str = ""
for line in nm_output:
line = line.split()
map_str += line[2] + '\n'
map_str += line[0] + '\n'
map_str += line[2] + "\n"
map_str += line[0] + "\n"
return map_str
102 changes: 51 additions & 51 deletions python/tvm/micro/base.py
Expand Up @@ -49,7 +49,7 @@ class Session:
.. code-block:: python
c_mod = ... # some module generated with "c" as the target
dev_config = micro.device.arm.stm32f746xx.default_config('127.0.0.1', 6666)
dev_config = micro.device.arm.stm32f746xx.default_config("127.0.0.1", 6666)
with tvm.micro.Session(dev_config) as sess:
micro_mod = sess.create_micro_mod(c_mod)
"""
Expand All @@ -59,57 +59,57 @@ def __init__(self, config):
# TODO(weberlo): add config validation

# grab a binutil instance from the ID in the config
dev_funcs = tvm.micro.device.get_device_funcs(config['device_id'])
self.create_micro_lib = dev_funcs['create_micro_lib']
self.toolchain_prefix = config['toolchain_prefix']
self.mem_layout = config['mem_layout']
self.word_size = config['word_size']
self.thumb_mode = config['thumb_mode']
self.comms_method = config['comms_method']
dev_funcs = tvm.micro.device.get_device_funcs(config["device_id"])
self.create_micro_lib = dev_funcs["create_micro_lib"]
self.toolchain_prefix = config["toolchain_prefix"]
self.mem_layout = config["mem_layout"]
self.word_size = config["word_size"]
self.thumb_mode = config["thumb_mode"]
self.comms_method = config["comms_method"]

# First, find and compile runtime library.
runtime_src_path = os.path.join(get_micro_host_driven_dir(), 'utvm_runtime.c')
runtime_src_path = os.path.join(get_micro_host_driven_dir(), "utvm_runtime.c")
tmp_dir = _util.tempdir()
runtime_obj_path = tmp_dir.relpath('utvm_runtime.obj')
runtime_obj_path = tmp_dir.relpath("utvm_runtime.obj")
self.create_micro_lib(runtime_obj_path, runtime_src_path, LibType.RUNTIME)
#input(f'check {runtime_obj_path}: ')

comms_method = config['comms_method']
if comms_method == 'openocd':
server_addr = config['server_addr']
server_port = config['server_port']
elif comms_method == 'host':
server_addr = ''
#input(f"check {runtime_obj_path}: ")

comms_method = config["comms_method"]
if comms_method == "openocd":
server_addr = config["server_addr"]
server_port = config["server_port"]
elif comms_method == "host":
server_addr = ""
server_port = 0
else:
raise RuntimeError(f'unknown communication method: f{self.comms_method}')
raise RuntimeError(f"unknown communication method: f{self.comms_method}")

self.module = _CreateSession(
comms_method,
runtime_obj_path,
self.toolchain_prefix,
self.mem_layout['text'].get('start', 0),
self.mem_layout['text']['size'],
self.mem_layout['rodata'].get('start', 0),
self.mem_layout['rodata']['size'],
self.mem_layout['data'].get('start', 0),
self.mem_layout['data']['size'],
self.mem_layout['bss'].get('start', 0),
self.mem_layout['bss']['size'],
self.mem_layout['args'].get('start', 0),
self.mem_layout['args']['size'],
self.mem_layout['heap'].get('start', 0),
self.mem_layout['heap']['size'],
self.mem_layout['workspace'].get('start', 0),
self.mem_layout['workspace']['size'],
self.mem_layout['stack'].get('start', 0),
self.mem_layout['stack']['size'],
self.mem_layout["text"].get("start", 0),
self.mem_layout["text"]["size"],
self.mem_layout["rodata"].get("start", 0),
self.mem_layout["rodata"]["size"],
self.mem_layout["data"].get("start", 0),
self.mem_layout["data"]["size"],
self.mem_layout["bss"].get("start", 0),
self.mem_layout["bss"]["size"],
self.mem_layout["args"].get("start", 0),
self.mem_layout["args"]["size"],
self.mem_layout["heap"].get("start", 0),
self.mem_layout["heap"]["size"],
self.mem_layout["workspace"].get("start", 0),
self.mem_layout["workspace"]["size"],
self.mem_layout["stack"].get("start", 0),
self.mem_layout["stack"]["size"],
self.word_size,
self.thumb_mode,
server_addr,
server_port)
self._enter = self.module['enter']
self._exit = self.module['exit']
self._enter = self.module["enter"]
self._exit = self.module["exit"]

def create_micro_mod(self, c_mod):
"""Produces a micro module from a given module.
Expand All @@ -125,7 +125,7 @@ def create_micro_mod(self, c_mod):
micro module for the target device
"""
temp_dir = _util.tempdir()
lib_obj_path = temp_dir.relpath('dev_lib.obj')
lib_obj_path = temp_dir.relpath("dev_lib.obj")
c_mod.export_library(
lib_obj_path,
fcompile=cross_compiler(self.create_micro_lib, LibType.OPERATOR))
Expand All @@ -137,12 +137,12 @@ def _check_system(self):
Raises error if not supported.
"""
if not sys.platform.startswith('linux'):
raise RuntimeError('MicroTVM is currently only supported on Linux')
if not sys.platform.startswith("linux"):
raise RuntimeError("MicroTVM is currently only supported on Linux")
# TODO(weberlo): Add 32-bit support.
# It's primarily the compilation pipeline that isn't compatible.
if sys.maxsize <= 2**32:
raise RuntimeError('MicroTVM is currently only supported on 64-bit platforms')
raise RuntimeError("MicroTVM is currently only supported on 64-bit platforms")

def __enter__(self):
self._enter()
Expand All @@ -161,7 +161,7 @@ def cross_compiler(create_micro_lib, lib_type):
----------
create_micro_lib : func
function for creating MicroTVM libraries for a specific device (e.g.,
`tvm.micro.device.get_device_funcs('arm.stm32f746xx')['create_micro_lib']`)
`tvm.micro.device.get_device_funcs("arm.stm32f746xx")["create_micro_lib"]`)
lib_type : micro.LibType
whether to compile a MicroTVM runtime or operator library
Expand All @@ -177,16 +177,16 @@ def cross_compiler(create_micro_lib, lib_type):
.. code-block:: python
c_mod = ... # some module generated with "c" as the target
fcompile = tvm.micro.cross_compiler('arm.stm32f746xx', LibType.OPERATOR)
c_mod.export_library('dev_lib.obj', fcompile=fcompile)
fcompile = tvm.micro.cross_compiler("arm.stm32f746xx", LibType.OPERATOR)
c_mod.export_library("dev_lib.obj", fcompile=fcompile)
"""
def compile_func(obj_path, src_path, **kwargs):
if isinstance(obj_path, list):
obj_path = obj_path[0]
if isinstance(src_path, list):
src_path = src_path[0]
create_micro_lib(obj_path, src_path, lib_type, kwargs.get('options', None))
return _cc.cross_compiler(compile_func, output_format='obj')
create_micro_lib(obj_path, src_path, lib_type, kwargs.get("options", None))
return _cc.cross_compiler(compile_func, output_format="obj")


def get_micro_host_driven_dir():
Expand All @@ -198,8 +198,8 @@ def get_micro_host_driven_dir():
directory path
"""
micro_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
micro_host_driven_dir = os.path.join(micro_dir, '..', '..', '..',
'src', 'runtime', 'micro', 'host_driven')
micro_host_driven_dir = os.path.join(micro_dir, "..", "..", "..",
"src", "runtime", "micro", "host_driven")
return micro_host_driven_dir


Expand All @@ -212,9 +212,9 @@ def get_micro_device_dir():
directory path
"""
micro_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
micro_device_dir = os.path.join(micro_dir, '..', '..', '..',
'src', 'runtime', 'micro', 'device')
micro_device_dir = os.path.join(micro_dir, "..", "..", "..",
"src", "runtime", "micro", "device")
return micro_device_dir


_init_api('tvm.micro', 'tvm.micro.base')
_init_api("tvm.micro", "tvm.micro.base")

0 comments on commit 013795e

Please sign in to comment.