`torch.export` runs `aten.hardtanh` through FakeTensor/Meta dispatch. Without a
Meta kernel, that path fell back to the `_refs.nn.functional.hardtanh`
decomposition, which intentionally follows the Python frontend and rejects
`min_val > max_val`. Native `aten.hardtanh` has legacy behavior that allows
inverted bounds and delegates the result to clamp, so export failed for a model
that eager ATen accepted.
Add dedicated Meta kernels for `aten.hardtanh`, `aten.hardtanh.out`, and
`aten.hardtanh_` so export preserves the ATen operator semantics without
changing eager behavior or the Python frontend. The Meta implementation mirrors
native validation order for bool/complex inputs, integer scalar conversion,
unsigned negative limits, out device/dtype errors, scalar range checks, and
resized out strides.
The alternative of adding validation to native `hardtanh` was rejected because
it would be BC-breaking. Changing the functional/ref frontend would also blur
the existing distinction between `torch.nn.functional.hardtanh` and
`torch.ops.aten.hardtanh`. A dedicated Meta implementation keeps the fix scoped
to the path that was diverging.
Fixes #161081
Generated by my agent
Test Plan:
- python test/test_meta.py -k hardtanh
- python test/export/test_export.py -k test_export_allows_aten_hardtanh_with_inverted_bounds
- lintrunner -a
Pull Request resolved: https://github.com/pytorch/pytorch/pull/185298
Approved by: https://github.com/yushangdi