Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/tvm/rpc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def random_key(prefix, delimiter=":", cmap=None):
The generated random key
"""
while True:
key = "{}{}{}".format(prefix, delimiter, random.random())
key = f"{prefix}{delimiter}{random.random()}"
if not cmap or key not in cmap:
break
return key
Expand Down Expand Up @@ -192,8 +192,8 @@ def connect_with_retry(addr, timeout=60, retry_period=5):
raise sock_err
period = time.time() - tstart
if period > timeout:
raise RuntimeError("Failed to connect to server %s" % str(addr))
raise RuntimeError(f"Failed to connect to server {str(addr)}")
logger.warning(
"Cannot connect to tracker %s, retry in %g secs...", str(addr), retry_period
f"Cannot connect to tracker {str(addr)}, retry in {retry_period:g} secs..."
)
time.sleep(retry_period)
23 changes: 9 additions & 14 deletions python/tvm/rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _connect(self):
self._sock.sendall(struct.pack("<i", base.RPC_TRACKER_MAGIC))
magic = struct.unpack("<i", base.recvall(self._sock, 4))[0]
if magic != base.RPC_TRACKER_MAGIC:
raise RuntimeError("%s is not RPC Tracker" % str(self._addr))
raise RuntimeError(f"{str(self._addr)} is not RPC Tracker")

def close(self):
"""Close the tracker connection."""
Expand All @@ -315,7 +315,7 @@ def summary(self):
base.sendjson(self._sock, [base.TrackerCode.SUMMARY])
value = base.recvjson(self._sock)
if value[0] != base.TrackerCode.SUCCESS:
raise RuntimeError("Invalid return value %s" % str(value))
raise RuntimeError(f"Invalid return value {str(value)}")
return value[1]

def text_summary(self):
Expand Down Expand Up @@ -351,19 +351,14 @@ def text_summary(self):
max_key_len = 0

res += "Queue Status\n"
title = ("%%-%ds" % max_key_len + " total free pending\n") % "key"
title = f"{'key':<{max_key_len}s} total free pending\n"
separate_line = "-" * len(title) + "\n"
res += separate_line + title + separate_line
for k in keys:
total = total_ct.get(k, 0)
free, pending = queue_info[k]["free"], queue_info[k]["pending"]
if total or pending:
res += ("%%-%ds" % max_key_len + " %-5d %-4d %-7d\n") % (
k,
total,
free,
pending,
)
res += f"{k:<{max_key_len}} {total:<5d} {free:<4d} {pending:<7d}\n"
res += separate_line
return res

Expand Down Expand Up @@ -401,7 +396,7 @@ def request(
base.sendjson(self._sock, [base.TrackerCode.REQUEST, key, "", priority])
value = base.recvjson(self._sock)
if value[0] != base.TrackerCode.SUCCESS:
raise RuntimeError("Invalid return value %s" % str(value))
raise RuntimeError(f"Invalid return value {str(value)}")
url, port, matchkey = value[1]
return connect(
url,
Expand All @@ -416,7 +411,7 @@ def request(
except TVMError as err:
last_err = err
raise RuntimeError(
"Cannot request %s after %d retry, last_error:%s" % (key, max_retry, str(last_err))
f"Cannot request {key} after {max_retry} retry, last_error:{str(last_err)}"
)

def request_and_run(self, key, func, priority=1, session_timeout=0, max_retry=2):
Expand Down Expand Up @@ -454,10 +449,10 @@ def request_and_run(self, key, func, priority=1, session_timeout=0, max_retry=2)
duration = time.time() - tstart
# roughly estimate if the error is due to timeout termination
if session_timeout and duration >= session_timeout * 0.95:
raise RuntimeError("Session timeout when running %s" % func.__name__)
raise RuntimeError(f"Session timeout when running {func.__name__}")
last_err = err
raise RuntimeError(
"Failed to run on %s after %d retry, last_error:%s" % (key, max_retry, str(last_err))
f"Failed to run on {key} after {max_retry} retry, last_error:{str(last_err)}"
)


Expand Down Expand Up @@ -517,7 +512,7 @@ def connect(
"""
try:
if session_timeout:
key += " -timeout=%s" % str(session_timeout)
key += f" -timeout={session_timeout}"
session_constructor_args = session_constructor_args if session_constructor_args else []
if not isinstance(session_constructor_args, (list, tuple)):
raise TypeError("Expect the session constructor to be a list or tuple")
Expand Down
4 changes: 2 additions & 2 deletions python/tvm/rpc/minrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def find_minrpc_server_libpath(server="posix_popen_server"):
curr_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
source_dir = os.path.abspath(os.path.join(curr_dir, "..", "..", ".."))
minrpc_dir = os.path.join(source_dir, "src", "runtime", "minrpc")
path = os.path.join(minrpc_dir, server, ("%s.cc" % server))
path = os.path.join(minrpc_dir, server, f"{server}.cc")

candidates = [path]
if not os.path.isfile(path):
raise RuntimeError("Cannot find minserver %s, in candidates %s" % (server, candidates))
raise RuntimeError(f"Cannot find minserver {server}, in candidates {candidates}")
return minrpc_dir, path


Expand Down
22 changes: 10 additions & 12 deletions python/tvm/rpc/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from . import tornado_util
except ImportError as error_msg:
raise ImportError(
"RPCProxy module requires tornado package %s. Try 'pip install tornado'." % error_msg
f"RPCProxy module requires tornado package {error_msg}. Try 'pip install tornado'."
)

from tvm.contrib.popen_pool import PopenWorker
Expand Down Expand Up @@ -152,7 +152,7 @@ def __init__(self, sock, addr):
self.addr = addr

def name(self):
return "TCPSocketProxy:%s:%s" % (str(self.addr[0]), self.rpc_key)
return f"TCPSocketProxy:{str(self.addr[0])}:{self.rpc_key}"

def send_data(self, message, binary=True):
self.write_message(message, True)
Expand All @@ -178,7 +178,7 @@ def __init__(self, *args, **kwargs):
self._init_handler()

def name(self):
return "WebSocketProxy:%s" % (self.rpc_key)
return f"WebSocketProxy:{self.rpc_key}"

def on_message(self, message):
self.on_data(message)
Expand Down Expand Up @@ -213,7 +213,7 @@ def __init__(self, *args, **kwargs):
web_port = kwargs.pop("rpc_web_port", None)
if web_port:
self.page = self.page.replace(
"ws://localhost:9190/ws", "ws://localhost:%d/ws" % web_port
"ws://localhost:9190/ws", f"ws://localhost:{web_port}/ws"
)
else:
self.page = open(file_path, "rb").read()
Expand Down Expand Up @@ -245,9 +245,7 @@ def __init__(
assert ProxyServerHandler.current is None
ProxyServerHandler.current = self
if web_port:
handlers = [
(r"/ws", WebSocketHandler),
]
handlers = [(r"/ws", WebSocketHandler)]
if index_page:
handlers.append(
(r"/", RequestHandler, {"file_path": index_page, "rpc_web_port": web_port})
Expand All @@ -256,7 +254,7 @@ def __init__(
resource_files = resource_files if resource_files else []
for fname in resource_files:
basename = os.path.basename(fname)
pair = (r"/%s" % basename, RequestHandler, {"file_path": fname})
pair = (rf"/{basename}", RequestHandler, {"file_path": fname})
handlers.append(pair)
logging.info(pair)
self.app = tornado.web.Application(handlers)
Expand Down Expand Up @@ -340,7 +338,7 @@ def _update_tracker(self, period_update=False):
magic = struct.unpack("<i", base.recvall(self._tracker_conn, 4))[0]
if magic != base.RPC_TRACKER_MAGIC:
self.loop.stop()
raise RuntimeError("%s is not RPC Tracker" % str(self._tracker_addr))
raise RuntimeError(f"{self._tracker_addr} is not RPC Tracker")
# just connect to tracker, need to update all keys
self._tracker_pending_puts = self._server_pool.keys()

Expand Down Expand Up @@ -516,7 +514,7 @@ def __init__(
continue
raise sock_err
if not self.port:
raise ValueError("cannot bind to any port in [%d, %d)" % (port, port_end))
raise ValueError(f"cannot bind to any port in [{port}, {port_end})")
logging.info("RPCProxy: client port bind to %s:%d", host, self.port)
sock.listen(1)
self.thread = threading.Thread(
Expand Down Expand Up @@ -684,11 +682,11 @@ def _connect(key):
assert len(msg) >= 4
magic = struct.unpack("<i", msg[:4])[0]
if magic == base.RPC_CODE_DUPLICATE:
raise RuntimeError("key: %s has already been used in proxy" % key)
raise RuntimeError(f"key: {key} has already been used in proxy")
if magic == base.RPC_CODE_MISMATCH:
logging.info("RPCProxy do not have matching client key %s", key)
elif magic != base.RPC_CODE_SUCCESS:
raise RuntimeError("%s is not RPC Proxy" % url)
raise RuntimeError(f"{url} is not RPC Proxy")
msg = msg[4:]

logging.info("Connection established with remote")
Expand Down
12 changes: 6 additions & 6 deletions python/tvm/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def download_linked_module(file_name):
elif path.endswith(".dylib") or path.endswith(".so"):
pass
else:
raise RuntimeError("Do not know how to link %s" % file_name)
raise RuntimeError(f"Do not know how to link {file_name}")
logger.info("Send linked module %s to client", path)
return bytearray(open(path, "rb").read())

Expand Down Expand Up @@ -219,7 +219,7 @@ def _accept_conn(listen_sock, tracker_conn, ping_period=2):
tracker_conn.sendall(struct.pack("<i", base.RPC_TRACKER_MAGIC))
magic = struct.unpack("<i", base.recvall(tracker_conn, 4))[0]
if magic != base.RPC_TRACKER_MAGIC:
raise RuntimeError("%s is not RPC Tracker" % str(tracker_addr))
raise RuntimeError(f"{str(tracker_addr)} is not RPC Tracker")
# report status of current queue
cinfo = {"key": "server:" + rpc_key, "addr": (custom_addr, port)}
base.sendjson(tracker_conn, [TrackerCode.UPDATE_INFO, cinfo])
Expand Down Expand Up @@ -277,12 +277,12 @@ def _connect_proxy_loop(addr, key, load_library):
sock.sendall(key.encode("utf-8"))
magic = struct.unpack("<i", base.recvall(sock, 4))[0]
if magic == base.RPC_CODE_DUPLICATE:
raise RuntimeError("key: %s has already been used in proxy" % key)
raise RuntimeError(f"key: {key} has already been used in proxy")

if magic == base.RPC_CODE_MISMATCH:
logger.warning("RPCProxy do not have matching client key %s", key)
elif magic != base.RPC_CODE_SUCCESS:
raise RuntimeError("%s is not RPC Proxy" % str(addr))
raise RuntimeError(f"{str(addr)} is not RPC Proxy")
keylen = struct.unpack("<i", base.recvall(sock, 4))[0]
remote_key = py_str(base.recvall(sock, keylen))
opts = _parse_server_opt(remote_key.split()[1:])
Expand All @@ -299,7 +299,7 @@ def _connect_proxy_loop(addr, key, load_library):
retry_count += 1
logger.warning("Error encountered %s, retry in %g sec", str(err), retry_period)
if retry_count > max_retry:
raise RuntimeError("Maximum retry error: last error: %s" % str(err))
raise RuntimeError(f"Maximum retry error: last error: {str(err)}")
time.sleep(retry_period)


Expand Down Expand Up @@ -349,7 +349,7 @@ def __init__(
continue
raise sock_err
if not self.port:
raise ValueError("cannot bind to any port in [%d, %d)" % (port, port_end))
raise ValueError(f"cannot bind to any port in [{port}, {port_end})")
logger.info("bind to %s:%d", host, self.port)
sock.listen(1)
self.sock = sock
Expand Down
4 changes: 2 additions & 2 deletions python/tvm/rpc/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def _addone(x):

@tvm.register_func("rpc.test.strcat")
def _strcat(name, x):
return "%s:%d" % (name, x)
return f"{name}:{x}"


@tvm.register_func("rpc.test.except")
def _remotethrow(name):
raise ValueError("%s" % name)
raise ValueError(f"{name}")


@tvm.register_func("rpc.test.runtime_str_concat")
Expand Down
16 changes: 4 additions & 12 deletions python/tvm/rpc/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from . import tornado_util
except ImportError as error_msg:
raise ImportError(
"RPCTracker module requires tornado package %s. Try 'pip install tornado'." % error_msg
f"RPCTracker module requires tornado package {error_msg}. Try 'pip install tornado'."
)

from .._ffi.base import py_str
Expand Down Expand Up @@ -184,7 +184,7 @@ def __init__(self, tracker, sock, addr):

def name(self):
"""name of connection"""
return "TCPSocket: %s" % str(self._addr)
return f"TCPSocket: {str(self._addr)}"

def summary(self):
"""Summary of this connection"""
Expand Down Expand Up @@ -408,7 +408,7 @@ def __init__(self, host, port=9190, port_end=9199, silent=False, reuse_addr=True
continue
raise sock_err
if not self.port:
raise ValueError("cannot bind to any port in [%d, %d)" % (port, port_end))
raise ValueError(f"cannot bind to any port in [{port}, {port_end})")
logger.info("bind to %s:%d", host, self.port)
sock.listen(1)
self.thread = threading.Thread(target=_tracker_server, args=(sock, self.stop_key))
Expand Down Expand Up @@ -463,15 +463,7 @@ def __init__(
self.proc = PopenWorker()
# send the function
self.proc.send(
_popen_start_tracker_server,
[
host,
port,
port_end,
silent,
reuse_addr,
timeout,
],
_popen_start_tracker_server, [host, port, port_end, silent, reuse_addr, timeout]
)
# receive the port
self.port, self.stop_key = self.proc.recv()
Expand Down
14 changes: 7 additions & 7 deletions python/tvm/runtime/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def getitem_helper(obj, elem_getter, length, idx):
return [elem_getter(obj, i) for i in range(start, stop, step)]

if idx < -length or idx >= length:
raise IndexError("Index out of range. size: {}, got index {}".format(length, idx))
raise IndexError(f"Index out of range. size: {length}, got index {idx}")
if idx < 0:
idx += length
return elem_getter(obj, idx)
Expand All @@ -77,7 +77,7 @@ def __init__(self, tag, fields):
for f in fields:
assert isinstance(
f, ObjectTypes
), "Expect object or " "tvm NDArray type, but received : {0}".format(type(f))
), f"Expect object or tvm NDArray type, but received : {type(f)}"
self.__init_handle_by_constructor__(_ffi_api.ADT, tag, *fields)

@property
Expand Down Expand Up @@ -108,7 +108,7 @@ def tuple_object(fields=None):
for f in fields:
assert isinstance(
f, ObjectTypes
), "Expect object or tvm " "NDArray type, but received : {0}".format(type(f))
), f"Expect object or tvm NDArray type, but received : {type(f)}"
return _ffi_api.Tuple(*fields)


Expand Down Expand Up @@ -149,11 +149,11 @@ class ShapeTuple(Object):
"""

def __init__(self, shape):
assert isinstance(shape, (list, tuple)), "Expect list of tuple, but received : {0}".format(
type(shape)
)
assert isinstance(
shape, (list, tuple)
), f"Expect list of tuple, but received : {type(shape)}"
for x in shape:
assert isinstance(x, int), "Expect int type, but received : {0}".format(type(x))
assert isinstance(x, int), f"Expect int type, but received : {type(x)}"
self.__init_handle_by_constructor__(_ffi_api.ShapeTuple, *shape)

def __len__(self):
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/runtime/executor/aot_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def set_input(self, key=None, value=None, **params):
if key is not None:
v = self._get_input(key)
if v is None:
raise RuntimeError("Could not find '%s' in model's inputs" % key)
raise RuntimeError(f"Could not find '{key}' in model's inputs")
v.copyfrom(value)

if params:
Expand Down
Loading