-
Notifications
You must be signed in to change notification settings - Fork 750
Open
Labels
bugUnexpected behaviour that should be corrected (type)Unexpected behaviour that should be corrected (type)triagedReviewed and examined, release as been assigned if applicable (status)Reviewed and examined, release as been assigned if applicable (status)
Description
🐞Describing the bug
mb.pow(x=2.0, y=0.0) != 1.0, a discrepancy that isn't mentioned in the API reference.
To Reproduce
import numpy as np
import coremltools as ct
from coremltools.converters.mil.mil import Builder as mb
from coremltools.converters.mil.mil import types
def mb_pow_issue():
@mb.program(input_specs=[mb.TensorSpec(shape=(1,), dtype=types.fp32)])
def prog(e_raw):
# Problematic approach: mb.pow(2, e_raw)
# We cast 2 to the same type as e_raw (float32)
two = mb.cast(x=2.0, dtype="fp32")
pow_2_e_raw_unstable = mb.pow(x=two, y=e_raw, name="unstable_output")
# Workaround: exp(e_raw * ln(2))
ln_2 = np.log(2.0).astype(np.float32)
pow_2_e_raw_stable = mb.exp(x=mb.mul(x=e_raw, y=ln_2), name="stable_output")
return pow_2_e_raw_unstable, pow_2_e_raw_stable
# Compile the model
model = ct.convert(prog, source='milinternal', convert_to="mlprogram", compute_precision=ct.precision.FLOAT32)
# Test with 0
e_raw_val = np.array([0.0], dtype=np.float32)
prediction = model.predict({"e_raw": e_raw_val})
print(f"Input e_raw: {e_raw_val}")
print(f"Unstable output (mb.pow): {prediction['unstable_output']}")
print(f"Stable output (exp trick): {prediction['stable_output']}")
if __name__ == "__main__":
mb_pow_issue()which prints
Input e_raw: [0.]
Unstable output (mb.pow): [0.]
Stable output (exp trick): [1.]
System environment (please complete the following information):
- coremltools version: 9.0
- OS (e.g. MacOS version or Linux type): macOS 26.1
Metadata
Metadata
Assignees
Labels
bugUnexpected behaviour that should be corrected (type)Unexpected behaviour that should be corrected (type)triagedReviewed and examined, release as been assigned if applicable (status)Reviewed and examined, release as been assigned if applicable (status)