-
Notifications
You must be signed in to change notification settings - Fork 758
Comments
**Fix reciprocal op crash on int32 input by raising ValueError (Fixes #2579)**#2580
**Fix reciprocal op crash on int32 input by raising ValueError (Fixes #2579)** #2580Pranaykarvi wants to merge 1 commit intoapple:main from
Conversation
Pranaykarvi
commented
Aug 5, 2025
junpeiz
commented
Aug 5, 2025
Thank you @Pranaykarvi for contributing! I will let this week's oncaller (@abhishek-patel6) to take a look.
abhishek-patel6
commented
Aug 5, 2025
Hi, @Pranaykarvi . Thank you for the patch! I think that the conversion already throws an error. Original issue expects the torch compiler to implicitly convert to float. Probably something like
coremltools/coremltools/converters/mil/frontend/torch/ops.py
Lines 7103 to 7108 in d4b83ad
Pranaykarvi
commented
Aug 6, 2025
Thanks @abhishek-patel6!
I checked ops.py, and unlike log1p, there’s no frontend handler for inverse or reciprocal. The op is introduced internally during MIL lowering (e.g., for 16 / x.shape[0]), so casting can't happen in the frontend.
That’s why I added the check directly in the MIL op to raise a clear ValueError — it’s the only reliable place to catch this issue. Let me know if you’d prefer an alternate approach!
abhishek-patel6
commented
Aug 11, 2025
Thanks @abhishek-patel6!
I checked
ops.py, and unlikelog1p, there’s no frontend handler forinverseorreciprocal. The op is introduced internally during MIL lowering (e.g., for16 / x.shape[0]), so casting can't happen in the frontend.That’s why I added the check directly in the MIL op to raise a clear
ValueError— it’s the only reliable place to catch this issue. Let me know if you’d prefer an alternate approach!
I think there is a handler for inverse.
coremltools/coremltools/converters/mil/frontend/torch/ops.py
Lines 7087 to 7090 in d4b83ad
Anyways, the linked issue requests for implicit conversion to float IIUC. Throwing an error message earlier wouldn't fix their original issue still.
Uh oh!
There was an error while loading. Please reload this page.
📝 Description
This PR addresses Issue #2579, where calling the
reciprocalMIL op withint32inputs causes an unexpected crash during SSA conversion.🔧 Problem
Using
mb.reciprocal(x=int32_tensor)leads to a runtime crash because reciprocal operations are invalid forint32types. The crash occurred deep within the MIL SSA conversion without a clear error message, making it hard to debug.✅ Fix
ValueErrorin thetype_inferencemethod of thereciprocalop forint32inputs.🧪 Tests
test_inverse_conversion.pyto verify theValueErroris raised correctly.pytest coremltools/test/test_inverse_conversion.py.🧠 Files Changed
coremltools/converters/mil/mil/ops/defs/elementwise.py➤ Added type check and exception for
int32inputs inreciprocal.type_inference.coremltools/test/test_inverse_conversion.py➤ Added test case to ensure invalid
int32input raisesValueError.🔗 Related Issue
Closes #2579