[Performance] `image.resize2d` nearest (asymmetric, non-floor rounding) is ~2x slower on CUDA since v0.24.0 (int64 index math from #18838)
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
Read the _resize_2d implementation and get_closest_index, then inspect test_transform_legalize_ops_image.py and test_op_image.py; the issue says test_image_resize2d covers the affected case. Check the proposed integer rounding path against the existing float behavior for power-of-two upscales and all rounding modes. Done means correctness tests pass and CUDA measurements confirm the slowdown is reduced; the author says a fix is ready and plans to open a PR.
由索引模型根据 Issue 内容生成。
描述
Thanks for participating in the TVM community! We use https://discuss.tvm.ai for any general usage questions and discussions. The issue tracker is used for actionable items such as feature proposals discussion, roadmaps, and bug tracking. You are always welcomed to post on the forum first 😸
Issues that are inactive for a period of time may get closed. We adopt this policy so that we won't lose track of actionable issues that may fall at the bottom of the pile. Feel free to reopen a new one if you feel there is an additional problem that needs attention when an old one gets closed.
Expected behavior
image.resize2d with method="nearest_neighbor", coordinate_transformation_mode="asymmetric" and an integer
upscale factor should run about as fast for round / round_prefer_floor / round_prefer_ceil / ceil as it does
for floor: the source index is still x / factor rounded, which is cheap integer math.
Actual behavior
Since v0.24.0, the non-floor rounding modes run ~2x slower on CUDA than floor for this case, and ~1.9x slower
than v0.23.0. The cause is #18838 ("[BugFix][TOPI] Fix resize accuracy issue with non-floor rounding"). That fix is
correct and should stay: before it, an integer upscale used the integer-division index path for every rounding mode,
so round, round_prefer_ceil and ceil silently computed floor (the repro below shows this on v0.23.0). After it,
only floor keeps the integer path. The other modes go through
closest_x_index = te.round(in_x).astype("int32") # and floor/ceil(in_x ± 0.5) etc.
on a float coordinate, and in the generated kernel the whole index expression becomes int64_t with a
float conversion, nearbyintf/roundf/floorf, and a clamp per output element (int64_t appears 65 times in the
kernel on main, 4 times for floor). resize2d nearest is a pure copy kernel, so the index math is most of its time.
Kernel time on CUDA (RTX 4060 Ti, sm_89, CUDA 12; ncu gpu__time_duration.sum, median of 10 fresh processes,
x float32[1,8,56,56,8] NCHW8c → 112×112, rounding_method="round"):
| build | kernel time (µs) | ratio |
|---|---|---|
| v0.23.0 | 14.78 | — |
| v0.24.0 | 28.70 | 1.94x of v0.23.0 |
#18838 parent → #18838 (c1d32438a) |
14.78 → 28.99 | 1.96x |
main 1fa4b8627d |
28.70 | 1.94x of v0.23.0 |
| main + proposed fix | 16.54 | 0.58x of main |
The repro script below shows the same in wall-clock time_evaluator on the same GPU:
v0.23.0 v0.24.0 main main + fix
round 10.32 us 24.68 us 24.69 us 11.90 us
round_prefer_floor 10.31 us 22.06 us 22.06 us 10.08 us
round_prefer_ceil 10.31 us 22.18 us 22.18 us 11.20 us
ceil 10.31 us 22.18 us 22.18 us 11.20 us
floor 10.31 us 9.41 us 9.41 us 10.37 us
v0.23.0: every mode returns the floor output. main + fix: bit-identical to main for all modes.
Proposed fix
When the upscale factor k = target_size / image_size is a power of two, in_x = x / k is exact in float32, so the
rounded index can be computed with one integer floordiv, with the same tie rules as get_closest_index:
def _is_pow2_upscale(origin_size, scaled_size):
"""Whether scaled_size == origin_size * 2**k (k >= 1) and the int-div path applies."""
if not can_convert_multiply_to_intdiv(origin_size, scaled_size):
return False
factor = int(scaled_size) // int(origin_size)
return factor > 1 and factor & (factor - 1) == 0
def _round_div(x, factor, rounding_method):
"""Round x / factor for a non-negative integer x and a power-of-two factor, with the same
tie rules as get_closest_index: "round" is half-to-even (te.round), "round_prefer_floor"
rounds ties down, "round_prefer_ceil" rounds ties up."""
half = factor // 2
if rounding_method == "round":
# ties go up when x // factor is odd, down when it is even
offset = half - 1 + te.floormod(te.floordiv(x, factor), 2)
elif rounding_method == "round_prefer_floor":
offset = half - 1
elif rounding_method == "round_prefer_ceil":
offset = half
else: # ceil
offset = factor - 1
return te.floordiv(x + offset, factor).astype("int32")
and in _resize_2d, for nearest_neighbor + asymmetric with one of these four rounding modes and a power-of-two
upscale, closest_x_index = _round_div(x, target_width // image_width, rounding_method) (same for y) instead of
get_closest_index(...). Everything else, including the existing edge clamp, is unchanged, and other factors keep the
float path.
Correctness: on main the patched outputs are bit-identical to unpatched main for all five rounding modes on the
benchmark input (GPU), and on an LLVM sweep of 90 settings (5 rounding modes × 6 shape pairs × 3 layouts) that also
checks the result against a float32 model of the rounding rule. test_transform_legalize_ops_image.py and
test_op_image.py pass (27/27); test_image_resize2d covers exactly this case, so its expected TIR is regenerated.
I restricted the integer path to power-of-two factors on purpose: an integer formula for every integer factor would
differ from the current float path at ties (5,279 of 64,168 settings checked), because there x / k is not exact in
float32.
The remaining ~12% over v0.23.0 is the edge clamp and one extra floormod for ties-to-even, which correct rounding needs
(v0.23.0 was faster because it computed floor for every mode).
I have the patch ready and I'll open a PR with the fix — filing this issue first to track it.
Environment
- TVM: the slowdown first appears in v0.24.0 (#18838) and is unchanged on main (
1fa4b8627d). - Target: CUDA, GPU sm_89 (RTX 4060 Ti), CUDA 12.
- OS: Linux x86_64.
Steps to reproduce
Minimal script. For each rounding mode it builds a single image.resize2d relax function, compiles it with the relax
CUDA pipeline, counts int64_t and float rounding calls in the generated kernel, checks whether the output equals the
floor output, and times it. It runs standalone on one TVM version (v0.2x releases and current main) and prints
tvm.__version__, so the output is self-labeling.
repro_resize2d.py
# repro_resize2d.py
import os
import re
import numpy as np
import tvm
from tvm import relax
from tvm.relax.backend.cuda.pipeline import get_default_pipeline
ROUNDING = os.environ.get("ROUNDING", "round,round_prefer_floor,round_prefer_ceil,ceil,floor").split(",")
SHAPE = (1, 8, 56, 56, 8) # NCHW8c, upsampled 56x56 -> 112x112
print(f"tvm.__version__ = {tvm.__version__}")
print(f"tvm.__file__ = {tvm.__file__}")
try: # current main: target / runtime live in tvm_tirx
import tvm_tirx
# main compiles with NVRTC by default; TVM_CUDA_COMPILER=nvcc selects nvcc instead
cc = os.environ.get("TVM_CUDA_COMPILER")
target = tvm_tirx.target.Target(
{"kind": "cuda", "backend_config": {"cuda": {"compiler": cc}}} if cc else "cuda")
dev = tvm_tirx.runtime.cuda(0)
to_nd = tvm_tirx.runtime.tensor
except ImportError: # v0.2x releases
target = tvm.target.Target("cuda")
dev = tvm.cuda(0)
to_nd = getattr(tvm.runtime, "tensor", None) or tvm.nd.array
rng = np.random.default_rng(0) # fixed seed: same input across versions/patches
data = to_nd(rng.standard_normal(SHAPE).astype("float32"), dev)
def cuda_source(mod):
# best effort: the generated CUDA source, searched through the module import tree
try:
src = mod.inspect_source() if hasattr(mod, "inspect_source") else mod.get_source()
except Exception:
src = ""
if "__global__" in src:
return src
for m in getattr(mod, "imports", None) or getattr(mod, "imported_modules", None) or []:
src = cuda_source(m)
if src:
return src
return ""
def bench(rounding_method):
bb = relax.BlockBuilder()
tensor_type = getattr(relax, "TensorStructInfo", None) or relax.TensorType # renamed on main
x = relax.Var("x", tensor_type(SHAPE, "float32"))
with bb.function("main", [x]):
with bb.dataflow():
out = bb.emit(relax.op.image.resize2d(
x, (112, 112), layout="NCHW8c", method="nearest_neighbor",
coordinate_transformation_mode="asymmetric", rounding_method=rounding_method))
gv = bb.emit_output(out)
bb.emit_func_output(gv)
with target:
ex = tvm.compile(get_default_pipeline(target)(bb.get()), target=target)
src = cuda_source(ex.mod)
vm = relax.VirtualMachine(ex, dev)
out = vm["main"](data) # warmup
res = vm.time_evaluator("main", dev, number=200, repeat=20)(data)
return res.mean * 1e6, out.numpy(), src.count("int64_t"), bool(re.search(r"roundf|nearbyintf|rintf|floorf|ceilf", src))
results = {r: bench(r) for r in ROUNDING}
print("\n===== resize2d nearest, asymmetric, NCHW8c 56x56 -> 112x112 =====")
floor_out = results["floor"][1] if "floor" in results else None
print(f"{'rounding':<20} {'time (us)':>10} {'int64_t in kernel':>18} {'float rounding':>15} {'same output as floor':>21}")
for r, (us, o, n64, fround) in results.items():
same = "-" if floor_out is None or r == "floor" else str(bool(np.array_equal(o, floor_out)))
print(f"{r:<20} {us:10.2f} {n64:18d} {str(fround):>15} {same:>21}")
# Save outputs so a stock-vs-patched run can verify bit-identical results.
out_path = os.environ.get("SAVE_OUT")
if out_path:
np.savez(out_path, **{r: o for r, (_, o, _, _) in results.items()})
print(f"\nsaved outputs -> {out_path}")
Run it once per TVM build, e.g.:
for V in v0.23.0 v0.24.0; do
PYTHONPATH=/path/to/tvm-$V/python python repro_resize2d.py
done
# current main (NVRTC by default; TVM_CUDA_COMPILER=nvcc to use nvcc instead)
SAVE_OUT=main.npz python repro_resize2d.py
For clock-invariant numbers, wrap the same run in ncu --metrics gpu__time_duration.sum.
Triage
- needs-triage
- topi
- performance
- 主要语言
- Python
- 星标
- 13.9k
- 派生
- 4.1k
- 平均合并
- 4 天 22 小时
- 30 天内合并 PR
- 22
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/tvm 的其他 Issue
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is None可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 3 天内回复
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensors未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 3 天内回复
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradients可能已有人在做 @pierceluu10 于 5 天前认领。 未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 3 天内回复
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputs可能已有人在做 @tandede 于 9 天前认领。 未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 3 天内回复
-
needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 3 天内回复
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
Techiebutler/freeframe#687 ·
维护者通常 1 天内回复
-
bug verbose GC
难度 2/5 1-3 小时 新手友好度 74/100
IBM/javacore-analyser#677 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 2 天内回复
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
难度 2/5 1-3 小时 新手友好度 75/100
NousResearch/hermes-agent#163932 · 1 条评论 ·
维护者通常 1 天内回复
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's file可能已有人在做 @Beehtrix 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#907 · 1 条评论 ·
维护者通常 1 天内回复