[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradients
Maintainer thường phản hồi trong vòng 3 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- backend, machine-learning
Hướng nghiên cứu
Bắt đầu tại python/tvm/relax/transform/legalize_ops/grad.py, trong _grad_take_backward, và kiểm tra cách thuộc tính axis được dùng để tính các kích thước gradient đầu ra hợp nhất. Tái hiện bằng ví dụ (3, 4) được cung cấp, sau đó so sánh gradient với axis=-1 và gradient với axis=1. Hoàn tất khi cả hai gradient khớp với mảng mong đợi và các trục âm được chuẩn hóa trước khi tính các kích thước đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Expected behavior
R.take(x, idx, axis=-1) is a valid Relax operation (axis=-1 selects along the last dimension, equivalent to axis=1 for a 2D tensor). relax.transform.Gradient + LegalizeOps should produce the exact same gradient as axis=1.
For x of shape (3, 4), idx = [1, 3], w = full((3, 2), 2.0), L = sum(R.take(x, idx, axis=-1) * w), the exact gradient dL/dx must be [[0, 2, 0, 2], [0, 2, 0, 2], [0, 2, 0, 2]].
Actual behavior
In python/tvm/relax/transform/legalize_ops/grad.py (_grad_take_backward, lines 160–205):
axis = call.attrs.axis
if axis is not None:
axis = int(axis) # <-- missing negative axis normalization: if axis < 0: axis += len(x.shape)
...
for i in range(len(output_grad_ptr.shape)):
if i < axis:
fused_output_grad_shape_pre *= output_grad_ptr.shape[i]
elif i > axis:
fused_output_grad_shape_nxt *= output_grad_ptr.shape[i]
When axis = -1 (or any negative axis):
i < -1is always False (fused_output_grad_shape_pre = 1).i > -1is always True for every dimension (including the gathered axis itself!), inflatingfused_output_grad_shape_nxtto the entire size ofoutput_grad(3 * 2 = 6instead of1).- The accumulation loop then writes to
out_idx = idx[loop_l] * 6 + j. Foridx[1] = 3andj = 5,out_idx = 23, which writes 11float64elements (88 bytes) past the end of the 12-elementout_bufon the heap, silently corruptingdL/dx(and crashing withSIGSEGVon larger shapes such as(64, 512)).
Environment
- TVM version:
apache-tvm==0.27.0.post1 - OS: Linux (x86_64)
- Python: 3.12.x
- Target:
llvm(backend-independent — the bug is inlegalize_ops/grad.py)
Steps to reproduce
import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I, relax as R
# 1. Silent wrong gradient + heap OOB write on (3, 4)
@I.ir_module
class Module:
@R.function
def main(
x: R.Tensor((3, 4), "float64"),
idx: R.Tensor((2,), "int64"),
w: R.Tensor((3, 2), "float64"),
) -> R.Tensor((), "float64"):
with R.dataflow():
t = R.take(x, idx, axis=-1)
s = R.sum(R.multiply(t, w))
R.output(s)
return s
dev = tvm.cpu(0)
gmod = relax.transform.Gradient("main")(Module)
lgmod = relax.transform.LegalizeOps()(gmod)
vm = relax.VirtualMachine(tvm.compile(lgmod, target="llvm"), dev)
x = np.arange(12, dtype="float64").reshape(3, 4)
idx = np.array([1, 3], dtype="int64")
w = np.ones((3, 2), dtype="float64") * 2.0
_, (grad_x, _, _) = vm["main_adjoint"](
tvm.runtime.tensor(x, dev), tvm.runtime.tensor(idx, dev), tvm.runtime.tensor(w, dev)
)
print("Expected grad_x:\n", np.array([[0., 2., 0., 2.]] * 3))
print("Actual TVM grad_x (axis=-1):\n", grad_x.numpy())
Observed output on apache-tvm==0.27.0.post1:
Expected grad_x:
[[0. 2. 0. 2.]
[0. 2. 0. 2.]
[0. 2. 0. 2.]]
Actual TVM grad_x (axis=-1):
[[0. 0. 0. 0.]
[0. 0. 2. 2.]
[2. 2. 2. 2.]]
Changing the shape to x: (64, 512), idx = [0, 511], w: (64, 2) immediately triggers a heap buffer overflow SIGSEGV (exit code -11).
Suggested Fix
In _grad_take_backward, normalize negative axis against len(x_ptr.shape) before computing fused_output_grad_shape_pre and fused_output_grad_shape_nxt:
if axis is not None:
axis = int(axis)
if axis < 0:
axis += len(x_ptr.shape)
Triage
- needs-triage
- relax
- Ngôn ngữ chính
- Python
- Star
- 13.9k
- Fork
- 4.1k
- Merge trung bình
- 4 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 22
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NoneCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensorsĐang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsCó thể đã có người làm @tandede đã nhận 9 ngày trước. Đang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 3 ngày
-
needs-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
apache/tvm#21043 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
[Bug][Relax][TOPI] RoiAlign mode=max returns the average of the sample pointsCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởneeds-triage type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 3 ngày
Issue tương tự
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
web-push-libs/pywebpush#216 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
AgriciDaniel/claude-seo#572 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
area:print-dispatch
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area: docs domain: technical-writing good first issue type: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
Deepak3699/Ai_Mentor#919 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày