[torchlib] log2 and log10 lose float64 precision

Đã đóng Phù hợp với người mới
#3,143 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 4 ngày

@Syedmudassir1604 đang làm issue này rồi.

Từ ngày 2/11/2026.

  • #3148 của @Syedmudassir1604 — đang mở
  • #3151 của @usmanmateen — đã merge

Đá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
72/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
machine-learning

Hướng nghiên cứu

Bắt đầu từ các triển khai torchlib của aten_log2 và aten_log10, chúng chia cho một CastLike của hằng số float32 Log(2.0) hoặc Log(10.0). Chạy reproducer trong issue để thấy sai lệch của các giá trị xuất ở float64 so với đầu ra của PyTorch. Hoàn thành khi các giá trị xuất cho n = 8 và n = 1024 khớp với PyTorch ở float64 và torch.floor(torch.log2(n)) cho ra 3 và 10.

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

Mô tả

In a float64 model, torch.log2 and torch.log10 export with only about float32 accuracy, so whole-number results come out just below the whole number:

import torch

class Model(torch.nn.Module):
    def forward(self, x):
        return torch.log2(x), torch.log10(x)

x = torch.tensor([8.0, 1000.0], dtype=torch.float64)
onnx_program = torch.onnx.export(Model(), (x,), dynamo=True)
torch.set_printoptions(precision=17)
print(*onnx_program(x))  # [2.99999999175649368, 9.96578425727775219]  [0.90308997445095363, 2.99999995833973321]
print(*Model()(x))       # [3.00000000000000000, 9.96578428466208699]  [0.90308998699194354, 3.00000000000000000]

aten_log2 and aten_log10 divide by op.CastLike(op.Log(2.0), self). Log runs on a float32 constant and only the result is cast, so the graph holds 0.6931471824645996 instead of log(2) = 0.6931471805599453 as a float64 initializer, and every result is off by about 3e-9 relative. That's within the 1e-7 float64 tolerance of assert_onnx_program, which is why the op tests pass.

It matters wherever the result is floored or compared, for example torch.floor(torch.log2(n)) for a bit length or a number of levels: the exported model gives 2 for n = 8 and 9 for n = 1024, where PyTorch gives 3 and 10.

Versions: onnxscript 0.7.2 and main (682b59de), torch 2.14.1, onnxruntime 1.31.0, Linux, CPU.

Ngôn ngữ chính
Python
Star
457
Fork
149
Merge trung bình
3 ngày 16 giờ
Pull request đã merge (30 ngày)
24

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 microsoft/onnxscript

Tất cả issue của microsoft/onnxscript

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.