apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.46k stars 647 forks source link

Fix a bug when fusing non-scalar pow-operations #2395

Open kasper0406 opened 6 days ago

kasper0406 commented 6 days ago

When attempting to convert a GPT-2 model through StableHLO, I hit an issue with the fuse optimization for the power operation.

In the current form, it attempts to check if the exponent is 2, and if so, applies some optimizations. However, the check assumes the provided array is a scalar, which may not be the case.

I have added two tests illustrating the problem, and proposed a fix.

Without the fix, the new tests fail with the following errors:

FAILED coremltools/converters/mil/backend/mil/passes/test_passes.py::TestPassFusePow2Sqrt::test_fuse_multiple_exponents - ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
FAILED coremltools/converters/mil/backend/mil/passes/test_passes.py::TestPassFusePow2Sqrt::test_no_fuse_multiple_exponents - ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
jakesabathia2 commented 4 days ago

CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/1555435197