Skip to content

Fix pow with a constant base of 2 squaring the exponent#2742

Open
eyupcanakman wants to merge 1 commit into
apple:mainfrom
eyupcanakman:fix/pow-const-base-2628
Open

Fix pow with a constant base of 2 squaring the exponent#2742
eyupcanakman wants to merge 1 commit into
apple:mainfrom
eyupcanakman:fix/pow-const-base-2628

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

pow(x, y) with a constant base of 2 returns y ** 2 instead of 2 ** y. Only an exact constant base of 2 is affected. Other bases, and a constant exponent of 2 (pow(x, 2)), are computed correctly. The behavior matches a pow(_, 2) square fast path in the runtime that keys on a constant operand of 2 regardless of which side it is on.

decompose_const_base2_pow rewrites pow(2, y) into exp(y * log(2)), which is equal and avoids the constant-2 base. It runs in the backend pipeline, so it applies to every frontend and to milinternal input. Floating-point pow only.

test_decompose_predict_values converts pow(2, y) and checks the predicted output against 2 ** y in fp32 and fp16. It fails on main without the fix.

Fixes #2628.

The behavior matches a pow(x, 2) square fast path in the Core ML runtime keyed on a constant operand of 2 regardless of side, so a constant base of 2 returns the exponent squared.
A new backend pass rewrites pow(2, y) into exp(y * log(2)), which avoids the constant-2 base and is computed correctly.

Fixes apple#2628.
@TobyRoseman

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIL pow op for 2.0 to 0.0 is 0.0

2 participants