[BUG] MSE histogram calibration: wrong amax for INT, TypeError for FP8 (args passed in the bias slot)

Đang mở Phù hợp với người mới
#3,261 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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/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, pytorch

Hướng nghiên cứu

Bắt đầu tại modelopt/torch/quantization/calib/histogram.py và kiểm tra _compute_amax_mse cùng với các chữ ký của fake_tensor_quant và scaled_e4m3. Tái hiện các ví dụ CPU trong issue cho INT8 và FP8, sau đó bổ sung kiểm thử hồi quy cho cả hai định dạng. Công việc hoàn tất khi cả hai trả về giá trị amax hợp lý mà không phát sinh ngoại lệ, kể cả khi đi qua đường dẫn hiệu chuẩn MSE dùng chung.

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

Mô tả

Describe the bug

HistogramCalibrator.compute_amax("mse") returns an amax that is far too small for INT formats. For FP8 (E4M3) it raises a TypeError. calibrate_weights(method="mse") goes through the same code, so it has the same problem.

The cause is in _compute_amax_mse in modelopt/torch/quantization/calib/histogram.py:

quant_centers = fake_tensor_quant(centers, amax, num_bits, unsigned)
...
quant_centers = scaled_e4m3(centers, amax, num_bits[0], num_bits[1])

Both functions now take bias as their third argument (fake_tensor_quant(inputs, amax, bias=None, num_bits=8, unsigned=False, ...), scaled_e4m3(inputs, amax, bias=None, E=4, M=3, ...)).

  • INT path: the call passes bias=8 and num_bits=False, so every candidate amax gives the same constant output and the same MSE. argmin then returns the first candidate, centers[start_bin].
  • FP8 path: M is missing, so the call raises.

Steps to reproduce

Main at 86bd9b78425a23a45b329c7c9b86ea772d7a9e77, CPU.

import torch
from modelopt.torch.quantization import calib

torch.manual_seed(0)
x = torch.randn(100_000)
print("max|x|:", x.abs().max().item())

for num_bits in (8, (4, 3)):
    cal = calib.HistogramCalibrator(num_bits, None, False, num_bins=2048)
    cal.collect(x)
    try:
        print(num_bits, "mse amax:", cal.compute_amax("mse").item())
    except Exception as e:
        print(num_bits, type(e).__name__, e)

Actual:

max|x|: 4.562695503234863
8 mse amax: 0.28628242015838623
(4, 3) TypeError ScaledE4M3Function.forward() missing 1 required positional argument: 'M'

Expected: an amax a bit below max|x| for both formats. With the arguments passed correctly the same script prints 4.0603 (INT8) and 4.3811 (FP8).

With CUDA available the histogram moves to the GPU and the quantizers go through the CUDA extension. The same script on B300 prints 4.5616 for INT8 (the amax lands at max|x|, not at the MSE optimum 4.0603) and the same TypeError for FP8.

Same effect through the public API on CPU: calibrate_weights(model, method="mse", perchannel=True) on an INT8 per-channel Linear(256, 4) sets every channel's amax to 0.0039 while max|w| is about 0.0625. The relative weight error after quantization is 0.908. With the fix it is 0.0039.

The existing TestMSECalibrator tests still pass on main. They only check that amax is closer to the bulk value than to the outlier, and the first candidate bin also satisfies that.

Proposed fix

Pass None for bias in both calls, plus a Gaussian regression test for INT8 and FP8.

Could a maintainer confirm this is a bug and not already fixed internally? I have the fix and tests ready and can open a PR after your reply.

Ngôn ngữ chính
Python
Star
5.5k
Fork
800
Merge trung bình
3 ngày 6 giờ
Pull request đã merge (30 ngày)
267

Chuẩn bị môi trường

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 NVIDIA/Model-Optimizer

Tất cả issue của NVIDIA/Model-Optimizer

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.