You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "cchung100m (via GitHub)" <gi...@apache.org> on 2026/04/29 13:04:00 UTC
[PR] [Relax][ONNX] Fix CumSum axis handling: support runtime axis tensor, error on multi-element axis [tvm]
cchung100m opened a new pull request, #19467:
URL: https://github.com/apache/tvm/pull/19467
Hi Committers,
This PR is trying to fix issues https://github.com/apache/tvm/issues/19437. Any suggestions would be appreciated if you are available.
### Root Cause
The original CumSum converter always defaulted to axis=0 when the axis input was a relax.Var (i.e., a runtime tensor), ignoring the actual runtime value. This led to incorrect behavior and did not comply with the ONNX specification.
### Solutions
Update CumSum._impl_v14 to:
- Check if the axis input is a Constant: require it to have exactly one element, otherwise raise an error.
- If the axis input is a relax.Var, require its shape to be [1]; otherwise, raise an error. Extract the scalar axis at runtime using squeeze.
- Remove the default axis=0 fallback to ensure correct handling of dynamic axis inputs
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][ONNX] Fix CumSum axis handling: support runtime axis tensor, error on multi-element axis [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19467:
URL: https://github.com/apache/tvm/pull/19467
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][ONNX] Fix CumSum axis handling: support runtime axis tensor, error on multi-element axis [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19467:
URL: https://github.com/apache/tvm/pull/19467#discussion_r3161190208
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -1748,22 +1748,31 @@ class CumSum(OnnxOpConverter):
@classmethod
def _impl_v14(cls, bb, inputs, attr, params):
data = inputs[0]
- axis = get_constant(inputs[1], params)
+ axis_input = get_constant(inputs[1], params)
assert not attr.get("exclusive", False), "Exclusive option not yet supported."
- if isinstance(axis, relax.Constant):
- axis = int(axis.data.numpy())
- elif isinstance(axis, relax.Var):
- axis = 0
-
+ if isinstance(axis_input, relax.Constant):
+ axis_np = int(axis_input.data.numpy())
+ if axis_np.size != 1:
+ raise ValueError("CumSum axis input tensor must have exactly on element, got shape {}".format(axis_np.shape))
+ axis = int(axis_np.item())
+ elif isinstance(axis_input, relax.Var):
+ axis_shape = axis_input.struct_info.shape if hasattr(axis_input.struct_info, 'shape') else None
+ if axis_shape is not None and hasattr(axis_shape, '__len__') and len(axis_shape) == 1:
+ axis = bb.normalize(relax.op.squeeze(axis_input, [0]))
+ else:
+ raise ValueError("CumSum axis input must be a single-element tensor (shape [1]), got {}".format(axis_shape))
Review Comment:

This logic has two issues:
1. **Rank-0 Support**: According to the ONNX specification, the `axis` input for `CumSum` can be a 0-D (scalar) or 1-D tensor. The current check `len(axis_shape) == 1` will incorrectly raise a `ValueError` for scalar tensors (rank 0).
2. **Dynamic Axis Limitation**: While this PR aims to support runtime axis tensors, both `topi.flip` (used on lines 1769 and 1775) and the `relax.op.cumsum` operator currently require the `axis` argument to be a constant integer during compilation. Passing a `relax.Expr` (like a `Var` or the result of `squeeze`) will cause a failure when emitting the TE operation or normalizing the Relax call, as these operators do not yet support dynamic axes in the Relax backend.
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -1748,22 +1748,31 @@ class CumSum(OnnxOpConverter):
@classmethod
def _impl_v14(cls, bb, inputs, attr, params):
data = inputs[0]
- axis = get_constant(inputs[1], params)
+ axis_input = get_constant(inputs[1], params)
assert not attr.get("exclusive", False), "Exclusive option not yet supported."
- if isinstance(axis, relax.Constant):
- axis = int(axis.data.numpy())
- elif isinstance(axis, relax.Var):
- axis = 0
-
+ if isinstance(axis_input, relax.Constant):
+ axis_np = int(axis_input.data.numpy())
+ if axis_np.size != 1:
+ raise ValueError("CumSum axis input tensor must have exactly on element, got shape {}".format(axis_np.shape))
+ axis = int(axis_np.item())
Review Comment:

There are several issues in this block:
1. `axis_np` is assigned the result of `int(...)`, making it a Python `int`. Python integers do not have `.size`, `.shape`, or `.item()` attributes, so lines 1756-1758 will raise an `AttributeError` at runtime.
2. The `int()` conversion on line 1755 will fail with a `TypeError` if the numpy array has more than one element, making the subsequent size check redundant if it worked.
3. There is a typo in the error message: `exactly on element` should be `exactly one element`.
You should perform the size check on the numpy array before converting it to a scalar integer.
```suggestion
axis_data = axis_input.data.numpy()
if axis_data.size != 1:
raise ValueError("CumSum axis input tensor must have exactly one element, got shape {}".format(axis_data.shape))
axis = int(axis_data.item())
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org