Skip to content

Enhance symbolic arithmetics - #2779

Merged
KaelanDt merged 22 commits into
mainfrom
symbolic-arith
Dec 12, 2025
Merged

Enhance symbolic arithmetics#2779
KaelanDt merged 22 commits into
mainfrom
symbolic-arith

Conversation

@shino16

@shino16 shino16 commented Nov 30, 2025

Copy link
Copy Markdown
Collaborator

Closes #2736. The main purpose is to implement math.ceil, which is needed for supporting clang.arange (ref).

To test it against both int and float, I added a logic to test the type of NumberProxy's underlying values in prologue.

I took a quick side step to implement all ops tested in test_elementwise.py. This involved the following patches.

Rounding ops: ceil, floor, trunc, round

  • Registered their NumberProxy versions to pythonex, backed with math.ceil/floor/trunc, builtins.round.
  • Patched them to compute in the promoted type (not in int).

Bitwise ops: bitwise_not, bitwise_left_shift, bitwise_right_shift

  • Registered their NumberProxy version to pythonex, backed with operator.inv/lshift/rshift
  • Routed e.g. NumberProxy.__lshift__ to the corresponding clangop.

floor_divide

  • Patched where, which floor_divide depends on, to support all-NumberProxy inputs

pos (as opposed to neg)

  • Thunder has no implementation of pos. I changed NumberProxy.__pos__ and TensorProxy.__pos__ to just return self, which aligns with Python and PyTorch (try for x in (1000, torch.randn(3)): assert +x is x).

Comment thread thunder/core/interpreter.py Outdated
@shino16
shino16 marked this pull request as draft December 2, 2025 15:58
@shino16
shino16 marked this pull request as ready for review December 2, 2025 16:27
Comment thread thunder/executors/nvfuserex_impl.py

@kiya00 kiya00 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thank you @shino16

Comment thread thunder/core/interpreter.py Outdated
Comment thread thunder/core/prims.py Outdated
Comment thread thunder/core/prims.py Outdated
Comment thread thunder/executors/nvfuserex_impl.py
Comment thread thunder/tests/test_elementwise.py Outdated
Comment thread thunder/tests/test_elementwise.py
@kshitij12345
kshitij12345 requested a review from Copilot December 3, 2025 11:12

Copilot AI left a comment

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.

Pull request overview

This PR enhances symbolic arithmetic support by implementing rounding operations (ceil, floor, trunc, round), bitwise shift operations, and improving where to support all-NumberProxy inputs. The main motivation is to enable math.ceil support for clang.arange. The implementation includes proper type promotion handling to ensure rounding operations return integers for number-only inputs while preserving dtypes for tensor inputs.

Key changes:

  • Added rounding and bitwise operations with proper type promotion semantics
  • Extended prologue to test both int and float types of NumberProxy values via symbolic value caching
  • Changed __pos__ methods to return self for both NumberProxy and TensorProxy, aligning with Python/PyTorch behavior

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
thunder/tests/test_elementwise.py Refactored tests to use parametrization for binary/unary ops and added symbolic value caching tests to verify type-based cache behavior
thunder/executors/pythonex.py Registered rounding and bitwise operations with appropriate backing functions, added where implementation for all-NumberProxy inputs
thunder/executors/nvfuserex_impl.py Added decorator to ensure rounding operations return int type for number-only inputs in nvFuser execution
thunder/core/utils.py Changed type promotion for NUMBER_TO_INT kind to use promoted type instead of always returning int
thunder/core/proxies.py Updated NumberProxy and TensorProxy __pos__ to return self, implemented bitwise shift operations, fixed type annotations
thunder/core/prims.py Updated rounding operations to use INT_FOR_NUMBER output dtype kind, enabled where for all-NumberProxy inputs
thunder/core/jit_ext.py Added type constraint check for symbolic value caching
thunder/clang/init.py Updated rounding operations to use NUMBER_TO_INT type promotion, added method_name to bitwise shift operations
Comments suppressed due to low confidence (1)

thunder/core/prims.py:1

  • Lines 1950-1952 duplicate the description of 'operations, like abs, map complex numbers to floats (COMPLEX_TO_FLOAT)'. Remove the duplicate sentence on lines 1951-1952 and update line 1953 to reference 'four behaviors' without the redundant explanation.
from enum import auto, Enum

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread thunder/tests/test_elementwise.py Outdated
Comment thread thunder/tests/test_elementwise.py Outdated
Comment thread thunder/tests/test_nvfuser.py
Comment thread thunder/core/proxies.py

@kshitij12345 kshitij12345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, just few minor comments, thanks @shino16

Comment thread thunder/core/proxies.py
Comment thread thunder/core/interpreter.py

@KaelanDt KaelanDt left a comment

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.

looks great, thanks @shino16

@KaelanDt
KaelanDt merged commit bb9e150 into main Dec 12, 2025
48 of 49 checks passed
@KaelanDt
KaelanDt deleted the symbolic-arith branch December 12, 2025 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support symbolic arithmetic operations

6 participants