[Bug][Relax] FoldBatchnormToConv2D crashes on float16 BatchNorm constants

オープン 初心者向け
#20,533 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

2026年10月3日 から。

  • #20537 @Nanmur による — オープン

評価

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

調査の方向性

まず提供された float16 の再現を実行し、次に epsilon 定数が作成されて BatchNorm の分散に追加されている fold_batch_norm_to_conv2d_for_inference.py を調べます。float16 グラフで dtype エラーが発生しなくなり、該当する場合はサポートされていない dtypes が変更されず、既存の float32 の folding 動作が引き続き成功すれば完了です。

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

説明

Description

FoldBatchnormToConv2D constructs BatchNorm epsilon as float32 unconditionally.
Applying the pass to a well-formed, executable float16 graph therefore raises a
dtype error instead of producing a target module or leaving the graph unchanged.

Environment

  • TVM 0.26.dev0, executed at e269315c90e3a061c9e1c77b370ce883b1b223f4
  • Ubuntu 24.04 x86-64, LLVM 18.1.3 CPU target
  • Current upstream main checked at 043b57fbe9b84aba6c0525fc77b624ef8b7bbe2f
  • The relevant Python source is byte-identical at both revisions (SHA-256 c591c953ce9de0f8b539149ab466692572ae608978bbb7ca4875fe919f3c588e)
  • I did not have a binary built from current main, so the runtime reproduction below is from the executed revision

Minimal reproduction

import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R

@I.ir_module
class Module:
    @R.function
    def main(
        x: R.Tensor((1, 1, 2, 2), "float16"),
        weight: R.Tensor((2, 1, 1, 1), "float16"),
        gamma: R.Tensor((2,), "float16"),
        beta: R.Tensor((2,), "float16"),
        mean: R.Tensor((2,), "float16"),
        variance: R.Tensor((2,), "float16"),
    ):
        conv = R.nn.conv2d(x, weight, data_layout="NCHW", kernel_layout="OIHW", out_layout="NCHW", out_dtype="float16")
        return R.nn.batch_norm(conv, gamma, beta, mean, variance, axis=1, training=False)[0]

def tensor(values):
    return tvm.runtime.tensor(np.asarray(values, dtype="float16"))

params = {
    "weight": tensor([[[[1.5]]], [[[-2.0]]]]),
    "gamma": tensor([2.0, 3.0]),
    "beta": tensor([5.0, 7.0]),
    "mean": tensor([1.0, -1.0]),
    "variance": tensor([4.0, 9.0]),
}
source = relax.transform.BindParams("main", params)(Module)
assert relax.analysis.check_well_formed(source, check_ty=True)
relax.transform.FoldBatchnormToConv2D()(source)

Expected behavior

The pass should construct epsilon in the variance dtype, or leave unsupported dtypes unchanged without throwing.

Actual behavior

The pass raises:

TypeError: Binary operators must have the same datatype for both operands.
R.add(<float16 variance>, T.float32(1e-05)) uses float16 and float32.

The source is well formed and executes on LLVM. Channels 1 through 6 all reproduced the same error. Changing only the graph dtype to float32 gave six successful, well-formed folds whose VM outputs matched the sources.

Likely cause

fold_batch_norm_to_conv2d_for_inference.py unconditionally creates tirx.FloatImm("float32", bn_attrs["epsilon"]) and adds it to bn_variance.

Duplicate check

I searched open and closed TVM issues and PRs for this pass, float16, epsilon dtype, and the exception, and did not find the same cause. #19977/#19979 concern ONNX frontend importing, and #3260 concerns the older Relay stack.

I can send a fix PR and help with follow-up testing if this diagnosis looks right.

Triage

  • needs-triage
  • type: bug
主要言語
Python
スター
13.9k
フォーク
4k
平均マージ
8時間 10分
マージ済み PR(30日)
186

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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