Repository navigation
[exir] Add pow to executorch_prim - #25768
themanoj-025 wants to merge 5 commits into
Conversation
…ToBackendOpsPass When exporting models with dynamic shapes where a SymInt is raised to an integer power (e.g. SeamlessM4T's relative position bias calculation x.shape[0] ** 2), to_executorch() fails with: executorch.exir.error.InternalError: Failed with error: invalid target for call_function <built-in function pow> Because executorch_prim does not have a pow operator, this commit rewrites operator.pow(base, exp) for non-negative integer exponents into a scalar multiplication chain using _PYTHON_SYM_OPS_TO_EXECUTORCH_SYM_OPS[operator.mul]. Tracked in huggingface/transformers#51303 (Item 1).
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25768
Note: Links to docs will display an error until the docs builds have been completed. ❌ 5 New Failures, 5 Pending, 1 Unrelated Failure, 3 Unclassified FailuresAs of commit 3cf8ea8 with merge base ddb19ca ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @themanoj-025! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
@pytorchbot label "release notes: none" |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
JacobSzwejbka
left a comment
There was a problem hiding this comment.
Thanks for looking into this. I would rather just define the pow op though.
https://github.com/pytorch/executorch/blob/main/exir/passes/executorch_prim_ops_registry.py
https://github.com/pytorch/executorch/blob/main/kernels/prim_ops/register_prim_ops.cpp
JakeStevens
left a comment
There was a problem hiding this comment.
please also update the PR title/summary to match the new approach, we are not using a mul chain
| return a / b # pyre-ignore | ||
|
|
||
|
|
||
| @bind_pattern_to_op(executorch_prims_lib, "pow.Scalar(Scalar a, Scalar b) -> Scalar") |
There was a problem hiding this comment.
Could you also add operator.pow: ops.backend.executorch_prim.pow.Scalar to _PYTHON_SYM_OPS_TO_EXECUTORCH_SYM_OPS? EdgeToBackendOpsPass uses that map to rewrite FX targets. On the pinned PyTorch nightly, the new test still raises invalid target for call_function <built-in function pow>. Adding that entry made the same model emit executorch_prim::pow.Scalar.
| Kernel( | ||
| "executorch_prim::pow.Scalar", | ||
| [](KernelRuntimeContext& context, Span<EValue*> stack) { | ||
| ET_UNUSED(context); |
There was a problem hiding this comment.
Please replace this line with __ET_PRIM_OP_NUM_ARGS_CHECK_IMPL(stack, context) before indexing stack. ET_UNUSED expands to [[maybe_unused]], which Clang rejects here. The existing arity check also returns InvalidProgram if the call is missing an operand or the output slot.
| int64_t base = a.toInt(); | ||
| int64_t exp = b.toInt(); | ||
| if (exp >= 0) { | ||
| out = EValue(static_cast<int64_t>(std::pow(base, exp))); |
There was a problem hiding this comment.
Please use checked integer exponentiation for nonnegative integer exponents. 3 ** 34 fits in int64_t and equals 16677181699666569. This expression returned 16677181699666570 locally because std::pow rounds through double. A kernel test with (3, 34) would catch the precision loss.
| } | ||
| } else { | ||
| double base = a.isInt() ? static_cast<double>(a.toInt()) : a.toDouble(); | ||
| double exp = b.isInt() ? static_cast<double>(b.toInt()) : b.toDouble(); |
There was a problem hiding this comment.
x.shape[0] ** True emits a Bool exponent once the lowering map is wired up. This branch calls toDouble() on that value, which aborts on the tag check. Please handle Boolean operands as integers on either side and return InvalidType through context for unsupported tags.
| } else { | ||
| double base = a.isInt() ? static_cast<double>(a.toInt()) : a.toDouble(); | ||
| double exp = b.isInt() ? static_cast<double>(b.toInt()) : b.toDouble(); | ||
| out = EValue(std::pow(base, exp)); |
There was a problem hiding this comment.
At input length 3, (x.shape[0] - 3) ** -1 raises ZeroDivisionError in the exported PyTorch model. Here std::pow(0.0, -1.0) returns infinity and the kernel reports success. Please return InvalidArgument for a zero base and negative exponent in both floating-result paths.
| M(), (torch.randn(4),), dynamic_shapes=({0: torch.export.Dim.AUTO},) | ||
| ) | ||
| new_prog = to_edge(ep).to_executorch() | ||
| self.assertIsNotNone(new_prog) |
There was a problem hiding this comment.
Could you run the emitted program at a second input length? For example, bound the dynamic dimension at 4, export at length 4, then run the portable runtime at length 3 and assert the output shape is (9,). That exercises the C++ registration and catches a result frozen to the example shape.
Check the accumulator product and repeated squaring so an overflowing symbolic shape calculation returns InvalidArgument. Cover each multiplication path in the kernel test. Authored with assistance from OpenAI Codex.
executorch_prim
Authored with assistance from OpenAI Codex.
Summary
Fixes #25767
Part of huggingface/transformers#51303
When exporting models with dynamic shapes where a SymInt is raised to an integer power (e.g.
x.shape[0] ** 2in SeamlessM4T's relative positional bias calculation), lowering fails with:Because
executorch_primdoes not define apowkernel,operator.powremained an invalid target during emission. This PR adds it.Test plan
test_edge_to_backend_sym_powinexir/tests/test_passes.py.cc @nil-is-all