Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,31 @@ class PRelu(OnnxOpConverter):
def _impl_v1(cls, bb, inputs, attr, params):
x = inputs[0]
slope = inputs[1]
return relax.op.nn.prelu(x, slope)

x_shape = x.struct_info.shape
slope_shape = slope.struct_info.shape

ndim = len(x_shape)
s_ndim = len(slope_shape)

if all(ss == 1 for ss in slope_shape) or s_ndim == 1:
slope = relax.op.reshape(slope, (slope_shape[0],))
return relax.op.nn.prelu(x, slope, ndim - 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic for handling 1D or scalar-like slopes might not align with the ONNX PReLU operator specification. This implementation defaults to applying the slope on the last dimension (axis = ndim - 1).

While this is a valid interpretation of general ONNX broadcasting rules, the PReLU operator has specific semantics. For instance, older versions of the ONNX PReLU specification (e.g., opset 7) explicitly state that for a scalar slope (shape (1,)) or a 1D per-channel slope (shape (C,)), the slope should be applied along the channel axis, which is typically axis=1 for NCHW data formats.

Using ndim - 1 could lead to incorrect behavior when the channel axis is not the last dimension. For example, with an input of shape (N, C, H, W) and a slope of shape (C,), this implementation would incorrectly try to apply the slope along the W axis.

To be more robust and compliant with the common use of PReLU, I recommend using axis=1 for these cases. This would align with the operator's typical per-channel behavior.

Suggested change
return relax.op.nn.prelu(x, slope, ndim - 1)
return relax.op.nn.prelu(x, slope, 1)


if s_ndim == ndim:
non_one_axes = [i for i, ss in enumerate(slope_shape) if ss != 1]

# Must have only ONE non-broadcast axis
if len(non_one_axes) != 1:
raise ValueError(
f"Invalid PRelu slope shape (multiple non-broadcast dims): {slope_shape}"
)
axis = non_one_axes[0]

slope = relax.op.reshape(slope, (slope_shape[axis],))
return relax.op.nn.prelu(x, slope, axis)

raise ValueError(f"Unsupported PRelu slope shape: {slope_shape}")


class ThresholdedRelu(OnnxOpConverter):
Expand Down
7 changes: 4 additions & 3 deletions python/tvm/topi/nn/elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ def prelu(x, slope, axis=1):

assert len(slope.shape) == 1
assert axis < len(x.shape)
slope = te.compute(
(get_const_int(x.shape[axis]),), lambda c: slope[0], name="slope_broadcasted"
)
if slope.shape[0] == 1:
slope = te.compute(
(get_const_int(x.shape[axis]),), lambda c: slope[0], name="slope_broadcasted"
)
assert get_const_int(slope.shape[0]) == get_const_int(x.shape[axis])

def _compute_channelwise(*indices):
Expand Down
3 changes: 3 additions & 0 deletions tests/python/relax/test_frontend_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,9 @@ def test_mish():

def test_prelu():
verify_binary("PRelu", [3, 32, 32], [1], [3, 32, 32])
verify_binary("PRelu", [3, 32, 32], [1, 1], [3, 32, 32])
verify_binary("PRelu", [3, 32, 32], [32], [3, 32, 32])
verify_binary("PRelu", [3, 32, 32], [3, 1, 1], [3, 32, 32])


def test_thresholded_relu():
Expand Down