Skip to content

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

Open

Description

@Yuhx141

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

Activity

  1. Nanmur commented on Oct 3, 2026

    @Nanmur
    Contributor

    Thanks for the clear reproduction and diagnosis. I opened #20537 with a focused fix that constructs epsilon using the matched variance dtype, along with a float16 regression test that verifies the rewritten module is well formed and BatchNorm is folded. The targeted test and repository pre-commit checks pass locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions