From 5b8667749640f4315971e7758e1bbb8e2e77e18a Mon Sep 17 00:00:00 2001 From: Logan Weber Date: Sun, 10 Nov 2019 22:11:50 -0800 Subject: [PATCH] Lint --- python/tvm/contrib/binutil.py | 23 +++++------ python/tvm/micro/base.py | 10 ++--- python/tvm/micro/device/arm/stm32f746xx.py | 3 +- python/tvm/micro/device/base.py | 47 +++++++++++----------- python/tvm/micro/device/host.py | 3 +- python/tvm/micro/device/riscv_spike.py | 12 ++++-- python/tvm/micro/rpc_server.py | 3 +- 7 files changed, 52 insertions(+), 49 deletions(-) diff --git a/python/tvm/contrib/binutil.py b/python/tvm/contrib/binutil.py index cd6556658212b..545314df237c4 100644 --- a/python/tvm/contrib/binutil.py +++ b/python/tvm/contrib/binutil.py @@ -19,7 +19,6 @@ import os import subprocess from . import util -from .._ffi.base import py_str from ..api import register_func RELOCATION_LD_SCRIPT_TEMPLATE = """ @@ -84,9 +83,9 @@ def run_cmd(cmd): resulting stdout capture from the subprocess """ proc = subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) (output, _) = proc.communicate() output = output.decode('utf-8') if proc.returncode != 0: @@ -140,8 +139,8 @@ def tvm_callback_get_section_size(binary_path, section_name, toolchain_prefix): continue entry_name = tokens[0] entry_size = int(tokens[1]) - for section_name in sections_to_sum: - if entry_name.startswith(section_name): + for section in sections_to_sum: + if entry_name.startswith(section): section_size += entry_size break @@ -212,12 +211,12 @@ def tvm_callback_relocate_binary( 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, - rodata_start=rodata_start, - data_start=data_start, - bss_start=bss_start, - stack_pointer_init=stack_pointer_init) + word_size=word_size, + text_start=text_start, + rodata_start=rodata_start, + data_start=data_start, + bss_start=bss_start, + stack_pointer_init=stack_pointer_init) tmp_dir = util.tempdir() rel_obj_path = tmp_dir.relpath('relocated.obj') diff --git a/python/tvm/micro/base.py b/python/tvm/micro/base.py index 5c3366096b0e5..0e8cd09bdbc1d 100644 --- a/python/tvm/micro/base.py +++ b/python/tvm/micro/base.py @@ -18,17 +18,14 @@ from __future__ import absolute_import -import logging import os import sys from enum import Enum -from pathlib import Path import tvm from tvm.contrib import util as _util from tvm.contrib import cc as _cc from .._ffi.function import _init_api -from .._ffi.libinfo import find_include_path class LibType(Enum): """Enumeration of library types that can be compiled and loaded onto a device""" @@ -62,7 +59,8 @@ def __init__(self, config): # TODO(weberlo): add config validation # grab a binutil instance from the ID in the config - self.create_micro_lib = tvm.micro.device.get_device_funcs(config['device_id'])['create_micro_lib'] + 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'] @@ -129,8 +127,8 @@ def create_micro_mod(self, c_mod): temp_dir = _util.tempdir() 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)) + lib_obj_path, + fcompile=cross_compiler(self.create_micro_lib, LibType.OPERATOR)) micro_mod = tvm.module.load(lib_obj_path) return micro_mod diff --git a/python/tvm/micro/device/arm/stm32f746xx.py b/python/tvm/micro/device/arm/stm32f746xx.py index 049832abc2960..950d5da423555 100644 --- a/python/tvm/micro/device/arm/stm32f746xx.py +++ b/python/tvm/micro/device/arm/stm32f746xx.py @@ -47,7 +47,8 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None): '-mthumb', '-gdwarf-5', ] - create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options) + create_micro_lib_base( + obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options) def default_config(server_addr, server_port): diff --git a/python/tvm/micro/device/base.py b/python/tvm/micro/device/base.py index 46e61f1e8799a..aa02064497e97 100644 --- a/python/tvm/micro/device/base.py +++ b/python/tvm/micro/device/base.py @@ -17,8 +17,6 @@ """Base definitions for MicroTVM config""" import glob import os -import sys -from enum import Enum from pathlib import Path from tvm.contrib import util as _util @@ -63,15 +61,15 @@ def get_device_funcs(device_id): return device_funcs -def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_type, options=None): +def create_micro_lib_base(out_obj_path, in_src_path, toolchain_prefix, device_id, lib_type, options=None): """Compiles code into a binary for the target micro device. Parameters ---------- - obj_path : str + out_obj_path : str path to generated object file - src_path : str + in_src_path : str path to source file toolchain_prefix : str @@ -90,27 +88,28 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t additional options to pass to GCC """ base_compile_cmd = [ - f'{toolchain_prefix}gcc', - '-std=c11', - '-Wall', - '-Wextra', - '--pedantic', - '-c', - '-O0', - '-g', - '-nostartfiles', - '-nodefaultlibs', - '-nostdlib', - '-fdata-sections', - '-ffunction-sections', - ] + f'{toolchain_prefix}gcc', + '-std=c11', + '-Wall', + '-Wextra', + '--pedantic', + '-c', + '-O0', + '-g', + '-nostartfiles', + '-nodefaultlibs', + '-nostdlib', + '-fdata-sections', + '-ffunction-sections', + ] if options is not None: base_compile_cmd += options src_paths = [] include_paths = find_include_path() + [get_micro_host_driven_dir()] - ld_script_path = None tmp_dir = _util.tempdir() + # we might transform the src path in one of the branches below + new_in_src_path = in_src_path if lib_type == LibType.RUNTIME: dev_dir = _get_device_source_dir(device_id) dev_src_paths = glob.glob(f'{dev_dir}/*.[csS]') @@ -122,17 +121,17 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t # create a temporary copy of the source, so we can inject the dev lib # header without modifying the original. temp_src_path = tmp_dir.relpath('temp.c') - with open(src_path, 'r') as f: + with open(in_src_path, 'r') as f: src_lines = f.read().splitlines() src_lines.insert(0, '#include "utvm_device_dylib_redirect.c"') with open(temp_src_path, 'w') as f: f.write('\n'.join(src_lines)) - src_path = temp_src_path + new_in_src_path = temp_src_path base_compile_cmd += ['-c'] else: raise RuntimeError('unknown lib type') - src_paths += [src_path] + src_paths += [new_in_src_path] for path in include_paths: base_compile_cmd += ['-I', path] @@ -147,7 +146,7 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t ld_cmd = [f'{toolchain_prefix}ld', '-relocatable'] ld_cmd += prereq_obj_paths - ld_cmd += ['-o', obj_path] + ld_cmd += ['-o', out_obj_path] run_cmd(ld_cmd) diff --git a/python/tvm/micro/device/host.py b/python/tvm/micro/device/host.py index 33fffadea3c49..0131ea78e5784 100644 --- a/python/tvm/micro/device/host.py +++ b/python/tvm/micro/device/host.py @@ -43,7 +43,8 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None): options = [] if sys.maxsize > 2**32 and sys.platform.startswith('linux'): options += ['-mcmodel=large'] - create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options) + create_micro_lib_base( + obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options) def default_config(): diff --git a/python/tvm/micro/device/riscv_spike.py b/python/tvm/micro/device/riscv_spike.py index 0df564ac1b80c..0ebd624ddfa64 100644 --- a/python/tvm/micro/device/riscv_spike.py +++ b/python/tvm/micro/device/riscv_spike.py @@ -39,7 +39,13 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None): options : Optional[List[str]] additional options to pass to GCC """ - create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options) + create_micro_lib_base( + obj_path, + src_path, + TOOLCHAIN_PREFIX, + DEVICE_ID, + lib_type, + options=options) def default_config(base_addr, server_addr, server_port): @@ -100,8 +106,8 @@ def default_config(base_addr, server_addr, server_port): curr_offset = 0 mem_layout = res['mem_layout'] for section_name, region_dict in mem_layout: - mem_layout[section_name]['start'] = base_addr + curr_offset - curr_offset += mem_layout[section_name]['size'] + region_dict['start'] = base_addr + curr_offset + curr_offset += region_dict['size'] return res diff --git a/python/tvm/micro/rpc_server.py b/python/tvm/micro/rpc_server.py index bf779f46bb3e8..6d3c7fadc98e3 100644 --- a/python/tvm/micro/rpc_server.py +++ b/python/tvm/micro/rpc_server.py @@ -18,10 +18,8 @@ import argparse import ast -import ctypes import json import logging -import os import tvm from tvm import rpc from tvm import micro @@ -71,6 +69,7 @@ def main(): @tvm.register_func('tvm.rpc.server.start', override=True) def server_start(): + # pylint: disable=unused-variable session = micro.Session(dev_config) session._enter()