Skip to content

[Relax][Frontend][ONNX] Add LpNormalization operator support - #20973

Open
vamsi8213 wants to merge 1 commit into
apache:mainfrom
vamsi8213:feat/lp-normalizaton-impl
Open

vamsi8213 wants to merge 1 commit into
apache:mainfrom
vamsi8213:feat/lp-normalizaton-impl

Conversation

@vamsi8213

Copy link
Copy Markdown

This PR adds support for the ONNX LpNormalization operator in the Relax ONNX frontend.

LpNormalization normalizes the input along axis by its L1 or L2 norm:

p = 1:  y = x / sum(|x|, axis, keepdims=True)
p = 2:  y = x / sqrt(sum(x * x, axis, keepdims=True))

Changes

  • Added an LpNormalization converter (_impl_v1) in python/tvm/relax/frontend/onnx/onnx_frontend.py, supporting the axis (default -1) and p (default 2) attributes, and registered it in the converter map.
    • Only p=1 and p=2 are accepted, as defined by the ONNX spec (onnxruntime also rejects other values); other values raise a ValueError.
    • axis is normalized and range-checked against the input rank.
    • Slices whose norm is zero produce zeros instead of NaN, matching onnxruntime. This is done with where(norm == 0, 1, norm) rather than clamping with an epsilon, so non-zero norms are never altered and float16 inputs don't underflow to 0 / 0.
  • Added test_lp_norm in tests/python/relax/test_frontend_onnx.py:
    • structural IR checks for the L2 (default) and L1 lowerings,
    • numerical correctness against onnxruntime for p in {1, 2} across positive and negative axes,
    • zero-norm slices (float32 and float16),
    • invalid p and out-of-range axis.
  • Enabled the ONNX backend node test (test_lpnormalization_default) by adding lpnormalization to _INCLUDE_OPS in tests/python/relax/test_frontend_onnx_backend.py.

@vamsi8213

Copy link
Copy Markdown
Author

Hi @tqchen, @mshr-h

Could you please review this PR?

@vamsi8213 vamsi8213 changed the title [Relax][ONNX] Add LpNormalization operator support [Relax][Frontend][ONNX] Add LpNormalization operator support Nov 11, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant