Skip to content

[Fix][Relax][PyTorch] Emit squeeze for unbatched AdaptiveAvgPool1d/2d/3d - #21129

Open
GoyalIshaan wants to merge 1 commit into
apache:mainfrom
GoyalIshaan:fix/adaptive-avg-pool-unbatched-emit
Open

GoyalIshaan wants to merge 1 commit into
apache:mainfrom
GoyalIshaan:fix/adaptive-avg-pool-unbatched-emit

Conversation

@GoyalIshaan

Copy link
Copy Markdown

Fixes #20943.

When translating an unbatched AdaptiveAvgPool1d/2d/3d input, the PyTorch Relax frontend adds a temporary batch dimension, emits the pooling op, and then removes the batch dimension with relax.op.squeeze(result, axis=[0]) without calling self.block_builder.emit(...). Returning that un-emitted relax.Call instead of a typed relax.Var causes downstream ops such as _binary_op to miss the Var/TensorType check and fall through to intrinsic_op, failing on expressions like pool(x) * 2.0 (TypeError: Please convert 2.0 with const first) or torch.maximum(pool(x), pool(y)) (must be Tensor, not Call).

This PR wraps relax.op.squeeze(result, axis=[0]) in self.block_builder.emit(...) across all six _adaptive_avg_pool{1,2,3}d and _adaptive_avg_pool{1,2,3}d_module translators so both from_exported_program and from_fx always return a typed relax.Var, and adds unbatched 1D/2D/3D regression tests (with batched controls) for scalar multiplication and elementwise max.

Note: The same un-emitted squeeze exists in _avg_pool*_impl and _max_pool*_impl; I'll send a follow-up PR.

Tests:

  • Confirmed pre-fix failures on main for unbatched 2D/3D (from_exported_program) and unbatched 1D/2D/3D (from_fx), with batched controls passing.
  • pytest tests/python/relax/test_frontend_from_exported_program.py tests/python/relax/test_frontend_from_fx.py -k "adaptive_avgpool" -v (15 passed, including numerical LLVM CPU verification against PyTorch eager).
  • pre-commit run --files python/tvm/relax/frontend/torch/base_fx_graph_translator.py python/tvm/relax/frontend/torch/fx_translator.py tests/python/relax/test_frontend_from_exported_program.py tests/python/relax/test_frontend_from_fx.py (all hooks passed).

Wrap the post-pool relax.op.squeeze(result, axis=[0]) in
self.block_builder.emit(...) across _adaptive_avg_pool{1,2,3}d and
_adaptive_avg_pool{1,2,3}d_module so unbatched AdaptiveAvgPool returns a
typed relax.Var instead of an un-emitted relax.Call.

Closes apache#20943

Note: The same un-emitted squeeze exists in _avg_pool*_impl and
_max_pool*_impl; I'll send a follow-up PR.

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.

[Bug][Relax] Arithmetic on an unbatched AdaptiveAvgPool2d output fails in from_exported_program: "Please convert 2.0 with const first"

1 participant