[torchlib] log2 and log10 lose float64 precision

クローズ 初心者向け
#3,143 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

@Syedmudassir1604 がすでに取り組んでいます。

2026年11月2日 から。

  • #3148 @Syedmudassir1604 による — オープン
  • #3151 @usmanmateen による — マージ済み

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

torchlib の aten_log2 と aten_log10 の実装から始めてください。これらは float32 の定数 Log(2.0) または Log(10.0) の CastLike で除算しています。issue の再現コードを実行し、float64 でのエクスポート結果が PyTorch の出力からずれる様子を確認してください。n = 8 と n = 1024 のエクスポート値が float64 で PyTorch と一致し、torch.floor(torch.log2(n)) が 3 と 10 を返せば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
457
フォーク
149
平均マージ
3日 16時間
マージ済み PR(30日)
24

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/onnxscript のほかの issue

microsoft/onnxscript の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。