Skip to content

Commit

Permalink
[Relay/Op] Use f-strings for string formatting, NFC (#14831)
Browse files Browse the repository at this point in the history
* [Relay/Op] Use f-strings for string formatting, NFC

Replace uses of % and .format() with f-strings.

* Reformat modified files

* No f-strings in hybrid script...
  • Loading branch information
Krzysztof Parzyszek authored May 12, 2023
1 parent 42aaae1 commit a3991b3
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 188 deletions.
2 changes: 1 addition & 1 deletion python/tvm/relay/op/annotation/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _make_virtual_device(device):
return target.VirtualDevice(_nd.device(device))
if isinstance(device, target.VirtualDevice):
return device
raise ValueError("expecting a Device or device name, but received a %s" % (type(device)))
raise ValueError(f"expecting a Device or device name, but received a {type(device)}")


def on_device(body, device, constrain_result=False, constrain_body=True):
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/relay/op/contrib/cutlass.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def make_conv2d_pattern(with_bias=False, with_act=None):
)
return is_op("multiply")(conv2d_out, rhs)

raise ValueError("Unknown activation %s." % with_act)
raise ValueError(f"Unknown activation {with_act}.")

return conv2d_out

Expand Down
19 changes: 6 additions & 13 deletions python/tvm/relay/op/contrib/dnnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@
from tvm.relay.expr_functor import ExprMutator, ExprVisitor

from ... import _ffi_api
from ...dataflow_pattern import (
DFPatternCallback,
is_constant,
is_expr,
is_op,
rewrite,
wildcard,
)
from ...dataflow_pattern import DFPatternCallback, is_constant, is_expr, is_op, rewrite, wildcard
from .register import register_pattern_table

logger = logging.getLogger("DNNL")
Expand Down Expand Up @@ -172,7 +165,7 @@ def make_conv_pattern(conv_name, with_bias=True, with_eltwise=None):
Call node sequence.
"""
if with_eltwise not in supported_post_elts:
raise ValueError("Unsupported eltwise post-op: %s" % with_eltwise)
raise ValueError(f"Unsupported eltwise post-op: {with_eltwise}")
data = wildcard()
weight = wildcard()
bias = wildcard()
Expand Down Expand Up @@ -335,7 +328,7 @@ def make_dense_pattern(with_bias=True, with_eltwise=None):
Call node sequence.
"""
if with_eltwise not in supported_post_elts:
raise ValueError("Unsupported eltwise post-op: %s" % with_eltwise)
raise ValueError(f"Unsupported eltwise post-op: {with_eltwise}")
data = wildcard()
weight = wildcard()
bias = wildcard()
Expand Down Expand Up @@ -579,7 +572,7 @@ def get_shape(tensor):
if tensor.op.name == "multiply":
return tensor.type_args[0].shape
return tensor.checked_type.shape
raise TypeError("Unsupport data type: %s" % type(tensor))
raise TypeError(f"Unsupport data type: {type(tensor)}")


def get_dtype(tensor):
Expand All @@ -596,7 +589,7 @@ def get_dtype(tensor):
if tensor.op.name == "multiply":
return tensor.type_args[0].dtype
return tensor.checked_type.dtype
raise TypeError("Unsupport data type: %s" % type(tensor))
raise TypeError(f"Unsupport data type: {type(tensor)}")


def tag2layout(input_data, is_weight=False, conv_type="Conv1D"):
Expand Down Expand Up @@ -627,7 +620,7 @@ def tag2layout(input_data, is_weight=False, conv_type="Conv1D"):
elif i.isdigit():
res += i
else:
raise ValueError("Unsupport layout format: %s" % input_data)
raise ValueError(f"Unsupport layout format: {input_data}")

return res

Expand Down
15 changes: 6 additions & 9 deletions python/tvm/relay/op/nn/_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def convert_conv2d(attrs, inputs, tinfos, desired_layouts):
new_attrs["kernel_layout"] = desired_kernel_layout
return relay.nn.contrib_conv2d_nchwc(data, weight, **new_attrs)

raise ValueError("Layout %s is not yet supported." % desired_data_layout)
raise ValueError(f"Layout {desired_data_layout} is not yet supported.")


# conv2d_transpose
Expand Down Expand Up @@ -375,7 +375,7 @@ def convert_conv2d_transpose(attrs, inputs, tinfos, desired_layouts):
new_attrs["kernel_layout"] = "HWIO"
return relay.nn.conv2d_transpose(data, weight, **new_attrs)

raise ValueError("Layout %s is not yet supported." % desired_data_layout)
raise ValueError(f"Layout {desired_data_layout} is not yet supported.")


# conv3d_transpose
Expand Down Expand Up @@ -424,7 +424,7 @@ def convert_conv3d_transpose(attrs, inputs, tinfos, desired_layouts):
new_attrs["kernel_layout"] = "DHWOI"
return relay.nn.conv3d_transpose(data, weight, **new_attrs)

raise ValueError("Layout %s is not yet supported" % desired_data_layout)
raise ValueError(f"Layout {desired_data_layout} is not yet supported")


@reg.register_legalize("nn.conv3d_transpose")
Expand Down Expand Up @@ -498,7 +498,7 @@ def convert_conv3d(attrs, inputs, tinfos, desired_layouts):
new_attrs["kernel_layout"] = "DHWIO"
return relay.nn.conv3d(data, weight, **new_attrs)

raise ValueError("Layout %s is not yet supported" % desired_data_layout)
raise ValueError(f"Layout {desired_data_layout} is not yet supported")


# conv3d_winograd related operators
Expand Down Expand Up @@ -917,7 +917,7 @@ def convert_deformable_conv2d(attrs, inputs, tinfos, desired_layouts):
elif desired_data_layout == "NHWC":
new_attrs["kernel_layout"] = "HWIO"
else:
raise ValueError("Layout %s is not yet supported." % desired_data_layout)
raise ValueError(f"Layout {desired_data_layout} is not yet supported.")

return relay.nn.deformable_conv2d(data, offset, weight, **new_attrs)

Expand Down Expand Up @@ -1457,10 +1457,7 @@ def dense_shape_func(attrs, inputs, _):
"""
ret = [
_matmul_shape_func(
inputs[0],
inputs[1],
expr.IntImm("bool", False),
expr.IntImm("bool", True),
inputs[0], inputs[1], expr.IntImm("bool", False), expr.IntImm("bool", True)
)
]
return ret
Expand Down
6 changes: 3 additions & 3 deletions python/tvm/relay/op/nn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_pad_tuple1d(padding):
elif isinstance(padding, int):
pad_w = padding * 2
else:
raise ValueError("Unknown padding option %s" % padding)
raise ValueError(f"Unknown padding option {padding}")
pad_left = (pad_w + 1) // 2
return pad_left, pad_w - pad_left

Expand Down Expand Up @@ -81,7 +81,7 @@ def get_pad_tuple2d(padding):
elif isinstance(padding, int):
pad_h = pad_w = padding * 2
else:
raise ValueError("Unknown padding option %s" % padding)
raise ValueError(f"Unknown padding option {padding}")
pad_top = (pad_h + 1) // 2
pad_left = (pad_w + 1) // 2
return pad_top, pad_left, pad_h - pad_top, pad_w - pad_left
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_pad_tuple3d(padding):
elif isinstance(padding, int):
pad_d = pad_h = pad_w = padding * 2
else:
raise ValueError("Unknown padding option %s" % padding)
raise ValueError(f"Unknown padding option {padding}")
pad_front = (pad_d + 1) // 2
pad_top = (pad_h + 1) // 2
pad_left = (pad_w + 1) // 2
Expand Down
10 changes: 5 additions & 5 deletions python/tvm/relay/op/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ def _fstrategy(attrs, inputs, out_type, target):
def _create_fstrategy_from_schedule(op_name, schedule):
assert hasattr(schedule, "dispatch_dict")
compute = get(op_name).get_attr("FTVMCompute")
assert compute is not None, "FTVMCompute is not registered for op %s" % op_name
fstrategy = get_native_generic_func("{}_strategy".format(op_name))
assert compute is not None, f"FTVMCompute is not registered for op {op_name}"
fstrategy = get_native_generic_func(f"{op_name}_strategy")
name_pfx = schedule.__name__
name_pfx = name_pfx[name_pfx.index("_") + 1 :]
fstrategy.set_default(
_wrap_default_fstrategy(compute, schedule.fdefault, "%s.generic" % name_pfx)
_wrap_default_fstrategy(compute, schedule.fdefault, f"{name_pfx}.generic")
)
for key, sch in schedule.dispatch_dict.items():
fstrategy.register(_wrap_default_fstrategy(compute, sch, "%s.%s" % (name_pfx, key)), [key])
fstrategy.register(_wrap_default_fstrategy(compute, sch, f"{name_pfx}.{key}"), [key])
return fstrategy


Expand Down Expand Up @@ -522,7 +522,7 @@ def debug(expr, debug_func=None):
global __DEBUG_COUNTER__

if debug_func:
name = "debugger_func{}".format(__DEBUG_COUNTER__)
name = f"debugger_func{__DEBUG_COUNTER__}"
tvm._ffi.register_func(name, debug_func)
__DEBUG_COUNTER__ += 1
else:
Expand Down
10 changes: 3 additions & 7 deletions python/tvm/relay/op/strategy/adreno.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def conv2d_strategy_adreno(attrs, inputs, out_type, target):
elif data_layout == "NHWC4c":
ic = data.shape[3] * data.shape[4]
else:
raise RuntimeError("Unsupported depthwise_conv2d data layout {}".format(data_layout))
raise RuntimeError(f"Unsupported depthwise_conv2d data layout {data_layout}")
if kernel_layout == "OIHW":
oc = kernel.shape[0]
elif kernel_layout == "OIHW4o":
Expand All @@ -119,9 +119,7 @@ def conv2d_strategy_adreno(attrs, inputs, out_type, target):
elif kernel_layout == "HWOI4o":
oc = kernel.shape[2] * kernel.shape[4]
else:
raise RuntimeError(
"Unsupported depthwise_conv2d kernel layout {}".format(kernel_layout)
)
raise RuntimeError(f"Unsupported depthwise_conv2d kernel layout {kernel_layout}")

if ic == oc == groups:
if (data_layout == "NCHW" and kernel_layout == "OIHW") or (
Expand Down Expand Up @@ -186,9 +184,7 @@ def conv2d_winograd_without_weight_transform_strategy_adreno(attrs, inputs, out_
plevel=5,
)
else:
raise RuntimeError(
"Unsupported conv2d_winograd_without_weight_transform layout {}".format(layout)
)
raise RuntimeError(f"Unsupported conv2d_winograd_without_weight_transform layout {layout}")
return strategy


Expand Down
32 changes: 12 additions & 20 deletions python/tvm/relay/op/strategy/arm_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ def conv2d_strategy_arm_cpu(attrs, inputs, out_type, target):
name="conv2d_nchw_spatial_pack.arm_cpu",
)
else:
raise RuntimeError(
"Unsupported weight layout {} for conv2d NCHW".format(kernel_layout)
)
raise RuntimeError(f"Unsupported weight layout {kernel_layout} for conv2d NCHW")
elif layout == "HWCN":
assert kernel_layout == "HWIO"
logger.warning("conv2d_hwcn is not optimized for arm cpu.")
Expand Down Expand Up @@ -237,12 +235,10 @@ def conv2d_strategy_arm_cpu(attrs, inputs, out_type, target):
name="conv2d_nhwc_spatial_pack.arm_cpu",
)
else:
raise RuntimeError(
"Unsupported kernel layout {} for conv2d NHWC".format(kernel_layout)
)
raise RuntimeError(f"Unsupported kernel layout {kernel_layout} for conv2d NHWC")

else:
raise RuntimeError("Unsupported conv2d layout {} for arm cpu".format(layout))
raise RuntimeError(f"Unsupported conv2d layout {layout} for arm cpu")
elif is_depthwise_conv2d(data.shape, layout, kernel.shape, kernel_layout, groups):
if layout == "NCHW":
assert kernel_layout == "OIHW" or re.match(r"OIHW\d*o", kernel_layout)
Expand Down Expand Up @@ -329,7 +325,7 @@ def conv2d_strategy_arm_cpu(attrs, inputs, out_type, target):
name="depthwise_conv2d_nhwc.generic",
)
else:
raise RuntimeError("Unsupported depthwise_conv2d layout {} for arm cpu".format(layout))
raise RuntimeError(f"Unsupported depthwise_conv2d layout {layout} for arm cpu")
else: # group_conv2d
if layout == "NCHW":
assert kernel_layout == "OIHW"
Expand All @@ -347,7 +343,7 @@ def conv2d_strategy_arm_cpu(attrs, inputs, out_type, target):
name="group_conv2d_nhwc.generic",
)
else:
raise RuntimeError("Unsupported group_conv2d layout {} for arm cpu".format(layout))
raise RuntimeError(f"Unsupported group_conv2d layout {layout} for arm cpu")
return strategy


Expand Down Expand Up @@ -439,11 +435,9 @@ def conv2d_winograd_without_weight_transform_strategy_arm_cpu(attrs, inputs, out
plevel=15,
)
else:
raise RuntimeError("Unsupported kernel shape: {}".format(kernel.shape))
raise RuntimeError(f"Unsupported kernel shape: {kernel.shape}")
else:
raise RuntimeError(
"Unsupported conv2d_winograd_without_weight_transform layout {}".format(layout)
)
raise RuntimeError(f"Unsupported conv2d_winograd_without_weight_transform layout {layout}")
return strategy


Expand Down Expand Up @@ -493,8 +487,8 @@ def conv2d_gemm_without_weight_transform_strategy_arm_cpu(attrs, inputs, out_typ
)
else:
raise RuntimeError(
"Unsupported conv2d_NHWC_quantized_without_transform layout {0}"
"with datatype {1}".format(layout, data.dtype)
f"Unsupported conv2d_NHWC_quantized_without_transform layout {layout}"
f"with datatype {data.dtype}"
)
return strategy

Expand Down Expand Up @@ -535,7 +529,7 @@ def bitserial_conv2d_strategy_arm_cpu(attrs, inputs, out_type, target):
name="bitserial_conv2d_nhwc.arm_cpu",
)
else:
raise ValueError("Data layout {} not supported.".format(layout))
raise ValueError(f"Data layout {layout} not supported.")
return strategy


Expand Down Expand Up @@ -612,9 +606,7 @@ def conv1d_strategy_arm_cpu(attrs, inputs, out_type, target):
)
else:
raise RuntimeError(
"Unsupported kernel layout {} for conv1d {} for arm cpu.".format(
kernel_layout, layout
)
f"Unsupported kernel layout {kernel_layout} for conv1d {layout} for arm cpu."
)
elif layout == "NCW":
logger.warning("conv1d with layout %s is not optimized for arm cpu.", layout)
Expand All @@ -632,6 +624,6 @@ def conv1d_strategy_arm_cpu(attrs, inputs, out_type, target):
)
else:
raise RuntimeError(
"Unsupported kernel layout {} for conv1d {} for arm cpu.".format(kernel_layout, layout)
f"Unsupported kernel layout {kernel_layout} for conv1d {layout} for arm cpu."
)
return strategy
10 changes: 3 additions & 7 deletions python/tvm/relay/op/strategy/bifrost.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def conv2d_strategy_bifrost(attrs, inputs, out_type, target):
name="conv2d_nhwc_spatial_pack.bifrost",
)
else:
raise RuntimeError("Unsupported conv2d layout {} for Mali(Bifrost)".format(layout))
raise RuntimeError(f"Unsupported conv2d layout {layout} for Mali(Bifrost)")
elif is_depthwise_conv2d(data.shape, layout, kernel.shape, kernel_layout, groups):
if layout == "NCHW":
assert kernel_layout == "OIHW"
Expand All @@ -92,9 +92,7 @@ def conv2d_strategy_bifrost(attrs, inputs, out_type, target):
name="depthwise_conv2d_nchw.bifrost",
)
else:
raise RuntimeError(
"Unsupported depthwise_conv2d layout {} for Mali(Bifrost)".format(layout)
)
raise RuntimeError(f"Unsupported depthwise_conv2d layout {layout} for Mali(Bifrost)")
else: # group_conv2d
raise RuntimeError("group_conv2d is not supported for Mali(Bifrost)")
return strategy
Expand All @@ -118,9 +116,7 @@ def conv2d_winograd_without_weight_transform_strategy_bifrost(attrs, inputs, out
name="conv2d_nchw_winograd.bifrost",
)
else:
raise RuntimeError(
"Unsupported conv2d_winograd_without_weight_transform layout {}".format(layout)
)
raise RuntimeError(f"Unsupported conv2d_winograd_without_weight_transform layout {layout}")
return strategy


Expand Down
Loading

0 comments on commit a3991b3

Please sign in to comment.