[Bug] [Relax] FLOAT16 quantize wraps positive INT16 saturation to -32768

Đang mở
#20,891 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/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
compilers

Hướng nghiên cứu

The payload names only the Relax relax.op.quantize call with float16 input and int16 output, plus the repro script. Run the snippet first to confirm the float16 path returns -32768 where the float32 path saturates to 32767. Then find where quantize's float-to-int16 clamp is lowered and check whether the clip bound is applied after float16 rounding. Done means both dtypes match the expected clipped output and the float32 assertion still passes.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

needs-triage type: bug
Expected behavior

With scale 1 and zero point 0, quantizing finite positive values 65504 and 32768 to INT16 should saturate to 32767.

Actual behavior

Direct Relax execution returns:

FLOAT32: [32767, 32767, 32752, -32768]
FLOAT16: [-32768, -32768, 32752, -32768]

The inputs represent exactly the same finite values. The INT16 upper bound 32767 rounds up to 32768 in FLOAT16, so the final cast wraps instead of saturating. The FLOAT32 control matches the independent wide-domain clipping formula.

Environment

TVM 0.28.dev0 (local CPU build; build commit unavailable), LLVM 18.1.3, Python 3.12.3, NumPy 2.5.4, Linux x86_64.

Steps to reproduce

Run with python repro.py:

import numpy as np
import tvm
from tvm import relax

values = np.array([65504, 32768, 32752, -65504], dtype="float32")
expected = np.clip(np.rint(values.astype("float64")), -32768, 32767).astype("int16")
for dtype in ["float32", "float16"]:
    x = relax.Var("x", relax.TensorType((4,), dtype))
    builder = relax.BlockBuilder()
    with builder.function("main", params=[x]):
        result = builder.emit(relax.op.quantize(
            x, relax.const(1, dtype), relax.const(0, "int16"), axis=0, out_dtype="int16"))
        builder.emit_func_output(result)
    vm = relax.VirtualMachine(relax.build(builder.get(), target="llvm"), tvm.cpu())
    actual = vm["main"](tvm.runtime.tensor(values.astype(dtype))).numpy()
    print(dtype, "expected", expected.tolist(), "actual", actual.tolist())
    if dtype == "float32":
        np.testing.assert_array_equal(actual, expected)
Triage
  • needs-triage
Ngôn ngữ chính
Python
Star
13.9k
Fork
4k
Merge trung bình
8 giờ 6 phút
Pull request đã merge (30 ngày)
147

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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/tvm

Tất cả issue của apache/tvm

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.