Skip to content

Commit

Permalink
[Relay] Use f-strings for string formatting, NFC
Browse files Browse the repository at this point in the history
Replace uses of % and .format() with f-strings.

Reformat modified files.
  • Loading branch information
Krzysztof Parzyszek committed May 12, 2023
1 parent ae9209b commit cf473b5
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 346 deletions.
11 changes: 5 additions & 6 deletions python/tvm/relay/backend/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _convert_args(self, expr, args, kwargs):

if kwargs and not isinstance(expr, Function):
raise Exception(
"can only supply keyword parameters for a " "relay.Function, found {0}".format(expr)
f"can only supply keyword parameters for a relay.Function, found {expr}"
)

params = expr.params
Expand All @@ -111,17 +111,16 @@ def _convert_args(self, expr, args, kwargs):
if i < num_of_args:
if kwargs.get(name):
raise Exception(
"duplicate argument supplied in "
"both positional args (at position: {0}), "
"and keyword argument (with name: {1})".format(i, name)
f"duplicate argument supplied in "
f"both positional args (at position: {i}), "
f"and keyword argument (with name: {name})"
)
else:
cargs.append(kwargs[name])

if len(cargs) != len(params):
raise Exception(
"insufficient arguments, expected "
"{0}, provided {1}".format(len(cargs), len(params))
f"insufficient arguments, expected " f"{len(cargs)}, provided {len(params)}"
)

return tuple(cargs)
Expand Down
4 changes: 2 additions & 2 deletions python/tvm/relay/backend/te_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def get_valid_implementations(op, attrs, inputs, out_type, target):
"""
fstrategy = op.get_attr("FTVMStrategy")
assert fstrategy is not None, (
"%s doesn't have an FTVMStrategy registered. You can register "
"one in python with `tvm.relay.op.register_strategy`." % op.name
f"{op.name} doesn't have an FTVMStrategy registered. You can register "
f"one in python with `tvm.relay.op.register_strategy`."
)
with target:
strategy = fstrategy(attrs, inputs, out_type, target)
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/relay/build_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,4 @@ def create_executor(kind="debug", mod=None, device=None, target="llvm", params=N
return VMExecutor(mod, device, raw_targets)
if kind == "aot":
return AotExecutor(mod, device, raw_targets)
raise RuntimeError("unknown execution strategy: {0}".format(kind))
raise RuntimeError(f"unknown execution strategy: {kind}")
69 changes: 24 additions & 45 deletions python/tvm/relay/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,41 +97,41 @@ def __lt__(self, other):
if isinstance(other, Expr):
return _op_make.less(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __gt__(self, other):
if isinstance(other, Expr):
return _op_make.greater(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __ge__(self, other):
if isinstance(other, Expr):
return _op_make.greater_equal(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __le__(self, other):
if isinstance(other, Expr):
return _op_make.less_equal(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __add__(self, other):
if isinstance(other, Expr):
return _op_make.add(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __radd__(self, other):
return self.__add__(other)
Expand All @@ -140,22 +140,22 @@ def __sub__(self, other):
if isinstance(other, Expr):
return _op_make.subtract(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __rsub__(self, other):
if isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f'convert "{str(other)}" with `const` first')
raise TypeError(f"type {type(other)} not supported")

def __mul__(self, other):
if isinstance(other, Expr):
return _op_make.multiply(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __rmul__(self, other):
return self.__mul__(other)
Expand All @@ -164,14 +164,14 @@ def __div__(self, other):
if isinstance(other, Expr):
return _op_make.divide(self, other)
elif isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError(f'convert "{str(other)}" with `const` first')
else:
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f"type {type(other)} not supported")

def __rdiv__(self, other):
if isinstance(other, _Number):
raise TypeError('convert "%s" with `const` first' % str(other))
raise TypeError("type %s not supported" % str(type(other)))
raise TypeError(f'convert "{str(other)}" with `const` first')
raise TypeError(f"type {type(other)} not supported")

def __truediv__(self, other):
return self.__div__(other)
Expand Down Expand Up @@ -213,12 +213,7 @@ def __init__(self, data, span=None):


@tvm._ffi.register_func("relay.ConstantWithFields")
def ConstantWithFields(
constant,
data=None,
virtual_device=None,
span=None,
):
def ConstantWithFields(constant, data=None, virtual_device=None, span=None):
"""
Returns constant with the given properties. A None property denotes 'no change'.
Returns constant if all properties are unchanged. Otherwise, returns a copy with the new
Expand Down Expand Up @@ -467,12 +462,7 @@ def __init__(self, value, span=None):


@tvm._ffi.register_func("relay.RefCreateWithFields")
def RefCreateWithFields(
ref_create,
value=None,
virtual_device=None,
span=None,
):
def RefCreateWithFields(ref_create, value=None, virtual_device=None, span=None):
"""
Returns ref_create with the given properties. A None property denotes 'no change'.
Returns ref_create if all properties are unchanged. Otherwise, returns a copy with the new
Expand All @@ -498,12 +488,7 @@ def __init__(self, ref, span=None):


@tvm._ffi.register_func("relay.RefReadWithFields")
def RefReadWithFields(
ref_read,
ref=None,
virtual_device=None,
span=None,
):
def RefReadWithFields(ref_read, ref=None, virtual_device=None, span=None):
"""
Returns ref_read with the given properties. A None property denotes 'no change'.
Returns ref_read if all properties are unchanged. Otherwise, returns a copy with the new
Expand Down Expand Up @@ -534,13 +519,7 @@ def __init__(self, ref, value, span=None):


@tvm._ffi.register_func("relay.RefWriteWithFields")
def RefWriteWithFields(
ref_write,
ref=None,
value=None,
virtual_device=None,
span=None,
):
def RefWriteWithFields(ref_write, ref=None, value=None, virtual_device=None, span=None):
"""
Returns ref_write with the given properties. A None property denotes 'no change'.
Returns ref_write if all properties are unchanged. Otherwise, returns a copy with the new
Expand Down
8 changes: 2 additions & 6 deletions python/tvm/relay/expr_functor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def visit(self, expr):
elif isinstance(expr, Match):
res = self.visit_match(expr)
else:
raise Exception("warning unhandled case: {0}".format(type(expr)))
raise Exception(f"warning unhandled case: {type(expr)}")

self.memo_map[expr] = res

Expand Down Expand Up @@ -204,11 +204,7 @@ class ExprMutator(ExprFunctor):
def visit_function(self, fn):
new_params = [self.visit(x) for x in fn.params]
new_body = self.visit(fn.body)
return FunctionWithFields(
fn,
list(new_params),
new_body,
)
return FunctionWithFields(fn, list(new_params), new_body)

def visit_let(self, let):
new_var = self.visit(let.var)
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/relay/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def while_loop(cond, loop_vars, loop_bodies):
fresh_vars = []

for i, loop_var in enumerate(loop_vars):
name = loop_var.name_hint if isinstance(loop_var, _expr.Var) else "arg{}".format(i)
name = loop_var.name_hint if isinstance(loop_var, _expr.Var) else f"arg{i}"
new_var = _expr.var(name, type_annotation=sb.type_of(loop_var), span=loop_var.span)
fresh_vars.append(new_var)

Expand Down
Loading

0 comments on commit cf473b5

Please sign in to comment.