Skip to content

[Fix][Relax][PyTorch] Handle one-sided clamp on integer tensors - #20885

Open
JakkulaVeerababu wants to merge 1 commit into
apache:mainfrom
JakkulaVeerababu:fix-clamp-int-one-sided-20881
Open

JakkulaVeerababu wants to merge 1 commit into
apache:mainfrom
JakkulaVeerababu:fix-clamp-int-one-sided-20881

Conversation

@JakkulaVeerababu

Copy link
Copy Markdown

Fixes #20881

TVM was handling PyTorch clamp operations incorrectly for integer tensors. When one limit was missing, it used infinity, which relax.op.clip cannot represent for an integer dtype, so the output was garbage. Also, clamp_max was reading the maximum value from the wrong argument (args[2] instead of args[1]), so a positional value was ignored.

This PR adds a common helper, _emit_clip, used by _clamp, _clamp_min and _clamp_max. For integer tensors it now uses the actual integer limits of the dtype instead of infinity. It also fixes the clamp_max argument, and adds test_clamp_one_sided_integer, which checks clamp(min=...), clamp(max=...), clamp_min and clamp_max against PyTorch for int32, int64 and uint8.

Testing:

  • The new test fails without this change (garbage output) and passes with it.
  • tests/python/relax/test_frontend_from_exported_program.py: 257 passed, 2 skipped. test_norm fails with and without this change (amax.default is unsupported with the newer PyTorch I used locally).
  • tests/python/relax/test_frontend_from_fx.py -k "clamp or unary": 27 passed.

AI disclosure: I used Claude Code to help find the cause, write the fix and the test, and run the tests. I reviewed the change and can explain it.

The clamp, clamp_min and clamp_max converters fill a missing bound
with +/-inf and pass it to relax.op.clip, which cannot represent an
infinite bound for an integer dtype, so the result is garbage. Use the
integer type limits for a missing bound instead.

clamp_max also read its bound from args[2], so a positional max was
ignored. Read it from args[1].

Fixes apache#20881

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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][PyTorch] One-sided clamp on an integer tensor returns garbage after from_exported_program

1 participant