Skip to content

python: Widen int scalars that do not fit in int64 to uint64 - #4365

Closed
vraj00222 wants to merge 2 commits into
ml-explore:mainfrom
vraj00222:vraj/uint64-python-int
Closed

python: Widen int scalars that do not fit in int64 to uint64#4365
vraj00222 wants to merge 2 commits into
ml-explore:mainfrom
vraj00222:vraj/uint64-python-int

Conversation

@vraj00222

Copy link
Copy Markdown
Contributor

Issue

uint64 is a fully supported MLX dtype, but (2^632^64−1) is unreachable from Python ints. so every path crashes for value >= 2^63 even when explicit dtype can hold the value :

mx.array(2^63)                          -> RuntimeError: std::bad_cast
mx.array(2^63, dtype=mx.uint64)         -> RuntimeError: std::bad_cast
mx.full((2,), 2^63)                     -> RuntimeError: std::bad_cast
mx.zeros((3,), dtype=mx.uint64) + 2^63  -> RuntimeError: std::bad_cast
mx.array(np.uint64(2^63))               -> works fine (dtype supports it!)

Solution (+63/−3, 4 files)

One shared inline helper in utils.h (to_int64_or_uint64): nb::try_cast<int64_t> → fallback PyLong_AsUnsignedLongLong → clean error naming the value. The three call sites use it; every current and future scalar consumer by default inherits the fix.

question for maintainers: lists do NOT get uint64 widening ,NumPy promotes [1, 2^63] to float64, silently losing precision above 2^53, which seems wrong to copy. let me know if that needs a follow up PR with same implementation

After this change (all verified on this machine, cpu+gpu)

mx.array(2**63)                      -> uint64, matches np.array(2**63)
mx.array(2**64 - 1)                  -> uint64
mx.full((2,), 2**63)                 -> uint64 fill works; explicit dtype wins
mx.zeros((2,), mx.uint64) + 2**63    -> uint64 promotion, matches numpy
mx.array(2**64)                      -> ValueError: Python int 18446744073709551616 does not fit in int64 or uint64.
mx.full((2,), 2**63, dtype=mx.int32) -> ValueError: Converting ... to int32 would result in overflow.  (existing message style)
mx.array([2**63])                    -> ValueError: Python ints larger than int64 are not supported in list initialization.

AI usage disclosure:

I used Claude Code to help me but every number and error string above is from a run on my machine, and I reviewed and understand every line in this diff.

☑️ I understand it is strictly prohibited to use AI to write PR description

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While we do value-dependent semantics and we don't plan to change that any time soon, it is still possible that we would drop it some day (see discussion in #4334 (comment)), supporting uint64 would make it significant more difficult to move away from value-dependent semantics, and as far as I see there is no practical usage for this yet, so I'm closing this as won't fix.

@zcbenz zcbenz added the wontfix label Aug 23, 2026
@zcbenz zcbenz closed this Aug 23, 2026
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.

2 participants