This is an automated email from the ASF dual-hosted git repository.

mshr-h pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 62a5528bb5 [Relax][ONNX] Add LpNormalization converter (#20818)
62a5528bb5 is described below

commit 62a5528bb58b4110ccf455a2f287ca063c560225
Author: Chen Yufan <[email protected]>
AuthorDate: Tue Nov 17 18:34:08 2026 +0800

    [Relax][ONNX] Add LpNormalization converter (#20818)
    
    The Relax ONNX frontend rejects checker-valid `LpNormalization` models
    because no converter is registered.
    
    Compose existing Relax operators for L1 and L2 normalization. Honor the
    default `axis=-1` and `p=2`, use absolute values for L1, and return zero
    for zero-norm slices. Float16/bfloat16 inputs accumulate and divide in
    float32 before casting back, avoiding float16 overflow. Other input
    dtypes are preserved. Reject unsupported values of `p`.
    
    Related to #20124 (LpNormalization only).
    
    Validation against main `f0267a0a3e`, Python 3.11, LLVM 21.1.8, and the
    CI-pinned ONNX 1.20.1 / ONNX Runtime 1.23.2:
    
    - 150 passed across LpNormalization, InstanceNormalization and
    MeanVarianceNormalization.
    - The matrix covers opsets 13/22, explicit/default p and axes,
    float16/float32/float64, static/symbolic shapes, negative values and
    zero-norm slices. All TVM cases check values and output dtypes against
    NumPy. ONNX Runtime checks use a separate, equivalent opset 13 model
    because CI's runtime lacks the opset 22 kernel; TVM's opset 22 coverage
    remains enabled.
    - Two additional bfloat16 VM cases use an independent NumPy reference;
    two cases check invalid p.
    - File-scoped pre-commit checks passed.
    - Review follow-up: explicit intermediate `bb.emit` bindings; 16 local
    L1/L2 imports across four dtypes and static/symbolic shapes remain well
    formed and structurally equal to the earlier version after
    `CanonicalizeBindings`.
    
    The original CPU Jenkins failures were in the opset 22 ONNX Runtime
    oracle. The reference model now uses its supported opset 13 kernel
    without changing the model tested by TVM or skipping cases.
    
    Prepared with OpenAI Codex assistance. The LLVM build and validation
    above were executed locally using the pinned TIRx/FFI sources.
    
    The previous revision passed all eight CI checks. Fresh CI results for
    the review follow-up are pending.
---
 python/tvm/relax/frontend/onnx/onnx_frontend.py | 26 ++++++++
 tests/python/relax/test_frontend_onnx.py        | 81 +++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py 
b/python/tvm/relax/frontend/onnx/onnx_frontend.py
index d786f9d653..ba684062d7 100644
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -4258,6 +4258,31 @@ class Range(OnnxOpConverter):
         return relax.op.add(relax.op.multiply(positions, delta_value), 
start_value)
 
 
+class LpNormalization(OnnxOpConverter):
+    """Converts an onnx LpNormalization node into an equivalent Relax 
expression."""
+
+    @classmethod
+    def _impl_v1(cls, bb, inputs, attr, params):
+        data = inputs[0]
+        axis = attr.get("axis", -1)
+        p = attr.get("p", 2)
+        if p not in (1, 2):
+            raise ValueError(f"LpNormalization only supports p=1 or 2, got 
{p}")
+
+        dtype = data.ty.dtype
+        compute_dtype = "float32" if dtype in ("float16", "bfloat16") else 
dtype
+        if compute_dtype != dtype:
+            data = bb.emit(relax.op.astype(data, compute_dtype))
+        terms = bb.emit(relax.op.abs(data) if p == 1 else 
relax.op.multiply(data, data))
+        norm = bb.emit(relax.op.sum(terms, axis=[axis], keepdims=True))
+        if p == 2:
+            norm = bb.emit(relax.op.sqrt(norm))
+        is_zero = bb.emit(relax.op.equal(norm, relax.const(0, compute_dtype)))
+        safe_norm = bb.emit(relax.op.where(is_zero, relax.const(1, 
compute_dtype), norm))
+        result = bb.emit(relax.op.divide(data, safe_norm))
+        return relax.op.astype(result, dtype) if compute_dtype != dtype else 
result
+
+
 class InstanceNormalization(OnnxOpConverter):
     """Converts an onnx InstanceNormalization node into an equivalent Relax 
expression."""
 
@@ -6458,6 +6483,7 @@ def _get_convert_map():
         "Size": Size,
         "EyeLike": EyeLike,
         # Normalization
+        "LpNormalization": LpNormalization,
         "BatchNormalization": BatchNormalization,
         "LayerNormalization": LayerNormalization,
         "RMSNormalization": RMSNormalization,
diff --git a/tests/python/relax/test_frontend_onnx.py 
b/tests/python/relax/test_frontend_onnx.py
index a6be0b132b..ca88a9259b 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -5920,6 +5920,87 @@ def test_const():
     tvm_tirx.ir.assert_structural_equal(tvm_model, Expected)
 
 
[email protected]("opset", [13, 22])
[email protected]("symbolic", [False, True])
[email protected]("dtype", ["float16", "float32", "float64"])
[email protected]("axis", [0, 1, -1, None])
[email protected]("p", [1, 2, None])
+def test_lp_normalization(opset, symbolic, dtype, axis, p):
+    attrs = {}
+    if axis is not None:
+        attrs["axis"] = axis
+    if p is not None:
+        attrs["p"] = p
+    tensor_type = helper.np_dtype_to_tensor_dtype(np.dtype(dtype))
+    model = make_unary_model(
+        "LpNormalization",
+        ["N", "C", "W"] if symbolic else [2, 3, 4],
+        attrs=attrs,
+        input_dtype=tensor_type,
+        output_dtype=tensor_type,
+    )
+    model.ir_version = 8
+    model.opset_import[0].version = opset
+    onnx.checker.check_model(model, full_check=True)
+
+    values = (np.arange(24).reshape(2, 3, 4) % 7 - 3).astype(dtype)
+    values[0, 0, :] = 0
+    values[:, 0, 0] = 0
+    values[0, :, 0] = 0
+    if dtype == "float16":
+        values *= 256
+    reference_values = values.astype("float64")
+    norm = np.linalg.norm(
+        reference_values,
+        ord=2 if p is None else p,
+        axis=-1 if axis is None else axis,
+        keepdims=True,
+    )
+    expected = np.divide(
+        reference_values, norm, out=np.zeros_like(reference_values), 
where=norm != 0
+    ).astype(dtype)
+    actual = run_in_tvm(model, inputs={"x": values}, opset=opset).numpy()
+    assert actual.dtype == values.dtype
+    tolerance = 2e-3 if dtype == "float16" else 1e-6
+    tvm_tirx.testing.assert_allclose(actual, expected, rtol=tolerance, 
atol=1e-7)
+    # The CI ONNX Runtime supports the equivalent opset 13 kernel for these 
dtypes.
+    reference_model = ModelProto()
+    reference_model.CopyFrom(model)
+    reference_model.opset_import[0].version = 13
+    ort_output = onnxruntime.InferenceSession(
+        reference_model.SerializeToString(), providers=["CPUExecutionProvider"]
+    ).run(None, {"x": values})[0]
+    tvm_tirx.testing.assert_allclose(actual, ort_output, rtol=tolerance, 
atol=1e-7)
+
+
[email protected]("p", [0, 3])
+def test_lp_normalization_invalid_p(p):
+    model = make_unary_model("LpNormalization", [2, 3], attrs={"p": p})
+    model.opset_import[0].version = 13
+    with pytest.raises(ValueError, match="LpNormalization.*p.*1 or 2"):
+        from_onnx(model, opset=13)
+
+
[email protected]("p", [1, 2])
+def test_lp_normalization_bfloat16(p):
+    dtype = helper.tensor_dtype_to_np_dtype(TensorProto.BFLOAT16)
+    model = make_unary_model(
+        "LpNormalization",
+        [2, 3],
+        attrs={"p": p},
+        input_dtype=TensorProto.BFLOAT16,
+        output_dtype=TensorProto.BFLOAT16,
+    )
+    values = np.array([[1, -1, 0], [0, 0, 0]], dtype=dtype)
+    norm = np.linalg.norm(values.astype("float32"), ord=p, axis=-1, 
keepdims=True)
+    expected = (values.astype("float32") / np.where(norm == 0, 1, 
norm)).astype(dtype)
+    actual = run_in_tvm(model, inputs={"x": values}, opset=22).numpy()
+    assert actual.dtype == values.dtype
+    tvm_tirx.testing.assert_allclose(
+        actual.astype("float32"), expected.astype("float32"), rtol=1e-2, 
atol=1e-7
+    )
+
+
 def test_instance_norm():
     def verify_instance_norm(input_shape, scale_shape, bias_shape, expected):
         node = helper.make_node("InstanceNormalization", ["a", "b", "c"], 
["d"], epsilon=1e-12)

Reply via email to