[Bug][Relax][TOPI] rms_norm fails when normalization covers every input axis

未关闭
#20,849 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
56/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

Read include/tvm/topi/nn/rms_norm.h, especially the reduction and indexing described in the issue, then run the supplied reproducer in an LLVM-enabled TVM environment. The all-axis ONNX case and direct TOPI construction should succeed while the leading-unit-batch control continues to match the NumPy result.

由索引模型根据 Issue 内容生成。

描述

Problem

Normalizing a FLOAT tensor of shape [3, 4] over axes [0, 1], with a matching [3, 4] weight, fails with Tensor dimension mismatch in read ndim = 1, indices.size=0. Both ONNX RMSNormalization(axis=0) and a direct TOPI construction reproduce the same failure. Adding a leading unit batch dimension and reducing axes [1, 2] gives the same mathematical operation and executes correctly after removing that dimension.

ONNX RMSNormalization-23 normalizes from axis through the final dimension. Axis zero is valid. The NumPy oracle evaluates x / sqrt(mean(x*x) + epsilon) * weight; unoptimized ORT and ONNX ReferenceEvaluator agree.

Minimal reproduction

Use an LLVM-enabled TVM environment with onnx==1.22.0, numpy==2.5.4 and onnxruntime==1.29.0. Save the following as repro.py and run:

OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 python repro.py

No downloaded model, private package or external input file is required. The script constructs and checks the models, saves the generated models/inputs under artifacts/, and asserts the observed failure as well as its controls. It exits successfully only when those checks reproduce the reported behavior.

from pathlib import Path
import hashlib
import numpy as np
import onnx
import onnxruntime as ort
import tvm
from onnx import TensorProto as T, helper as h, numpy_helper as nh
from onnx.reference import ReferenceEvaluator
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx


def make(nodes, inputs, outputs, initializers=(), opset=18):
    model = h.make_model(h.make_graph(nodes, "repro", inputs, outputs, list(initializers)),
                         opset_imports=[h.make_opsetid("", opset)], ir_version=10)
    onnx.checker.check_model(model, full_check=True)
    return model


def sources(model, feeds, expected):
    artifacts = Path("artifacts")
    artifacts.mkdir(exist_ok=True)
    identity = hashlib.sha256(model.SerializeToString()).hexdigest()
    onnx.save(model, artifacts / (identity + ".onnx"))
    np.savez(artifacts / (identity + "-inputs.npz"), **feeds)
    options = ort.SessionOptions()
    options.intra_op_num_threads = options.inter_op_num_threads = 1
    options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_DISABLE_ALL
    actual = ort.InferenceSession(model.SerializeToString(), options,
                                 providers=["CPUExecutionProvider"]).run(None, feeds)
    reference = ReferenceEvaluator(model).run(None, feeds)
    for values in (actual, reference):
        for a, b in zip(values, expected, strict=True):
            np.testing.assert_allclose(a, b, rtol=1e-5, atol=1e-6)


def tvm_run(model, feeds):
    mod = from_onnx(model, keep_params_in_input=False)
    names = [value.name for value in model.graph.input]
    executable = relax.build(mod, target="llvm", relax_pipeline="default")
    result = relax.VirtualMachine(executable, tvm.cpu())["main"](
        *[tvm.runtime.tensor(feeds[name]) for name in names])
    return [result.numpy()] if hasattr(result, "numpy") else [v.numpy() for v in result]


x = np.linspace(0.25, 2.0, 12, dtype="float32").reshape(3, 4)
w = np.linspace(0.5, 1.5, 12, dtype="float32").reshape(3, 4)
expected = x / np.sqrt(np.mean(x * x) + np.float32(1e-5)) * w
for add_batch in (False, True):
    data = x[None, ...] if add_batch else x
    axis = 1 if add_batch else 0
    model = make([h.make_node("RMSNormalization", ["x", "w"], ["y"],
                              axis=axis, epsilon=1e-5)],
                 [h.make_tensor_value_info("x", T.FLOAT, list(data.shape)),
                  h.make_tensor_value_info("w", T.FLOAT, [3, 4])],
                 [h.make_tensor_value_info("y", T.FLOAT, list(data.shape))], opset=23)
    oracle = expected[None, ...] if add_batch else expected
    sources(model, {"x": data, "w": w}, [oracle])
    try:
        actual = tvm_run(model, {"x": data, "w": w})[0]
    except Exception as error:
        assert not add_batch and "Tensor dimension mismatch" in str(error), str(error)
        print("all-axis ONNX", str(error).splitlines()[-1])
    else:
        assert add_batch, "Reported failure did not reproduce"
        np.testing.assert_allclose(actual, oracle, rtol=1e-5, atol=1e-6)
        print("equivalent leading-unit-batch control passed")

# The same failure occurs directly in TOPI, without ONNX conversion.
from tvm import te, topi
vx = te.placeholder((3, 4), name="x", dtype="float32")
vw = te.placeholder((3, 4), name="w", dtype="float32")
try:
    topi.nn.rms_norm(vx, vw, axis=[0, 1], epsilon=1e-5)
except Exception as error:
    assert "Tensor dimension mismatch" in str(error), str(error)
    print("direct TOPI construction", str(error).splitlines()[-1])
else:
    raise AssertionError("Direct TOPI failure did not reproduce")

Key output

all-axis ONNX Check failed: shape.size() == indices.size() (1 vs. 0) : Tensor dimension mismatch in read ndim = 1, indices.size=0
equivalent leading-unit-batch control passed
direct TOPI construction Check failed: shape.size() == indices.size() (1 vs. 0) : Tensor dimension mismatch in read ndim = 1, indices.size=0

Causal analysis

The TOPI rms_norm implementation constructs its squared sum with keepdims=false and atleast1d=true. When every axis is reduced, that result retains rank one. Its subsequent indexing gathers the non-reduced indices, which are empty, and uses them to read the rank-one sum. The reduction shape and indexing must agree in this all-axis case. The direct TOPI probe isolates this independently of ONNX conversion.

Related work and discovery scope

Targeted searches for rms_norm, all-axis/scalar normalization and dimension failures did not find a covering report. This case uses nonempty input data and valid full-axis normalization; it is not a zero-rank input.

The original observation came from a graph-testing workflow. The self-contained script above is a subsequently constructed minimal diagnostic, not a byte-identical replay of a historical generated model. No global novelty claim is made.

Environment

Linux x86_64 CPU, LLVM target; Python 3.12.3, TVM 0.28.dev0, ONNX 1.22.0, NumPy 2.5.4, ORT 1.29.0, tvm-ffi 0.1.14.post2.dev22+gdf463f9bd.

The exact source revision used to build the installed TVM binaries is unknown. The current-source links below are a separate static inspection at 2bf0532998d4dc7879cb342e873608b90c1d4497; that revision was not built or executed for this report. The observed runtime results refer to these loaded binary identities:

  • libtvm_compiler.so SHA256 574a573fe994e5eda7352ddd8d685e64e45c7934bdeeaab57a5a272a77792ab1
  • libtvm_runtime.so SHA256 4175916fc0ce6266c912629d2255094b94087425c6ab53ffd5e004d4c292c828
  • libtvm_runtime_extra.so SHA256 79c93351fca12dd7851964890cea7da65dd2b2e0e64e5c28bfed56c878b70817
  • libtvm_ffi.so SHA256 12072d0ccd9d5cb2c6dac80d42f4217ab50dbf795db03197000614a7a770a816

Reproducer SHA256: 8ece9b58b1db26fef13f7a0a10ce1c3be69f38882d84f4fc7cdcb67ee2912dd2. Model and input hashes can be computed from the files saved by the script. No compiler/library rebuild or environment upgrade was used for the reported checks.

主要语言
Python
星标
13.9k
派生
4k
平均合并
8 小时 2 分钟
30 天内合并 PR
180

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。