Skip to content

to_executorch() fails on !=, & and | between symbolic sizes #25910

Backlog

Description

@AxelNoun

🐛 Describe the bug

torch.export accepts a comparison !=, or a & / | between symbolic booleans, on dynamic sizes (a common shape for torch.cond predicates), but to_executorch() then fails with invalid target for call_function. _PYTHON_SYM_OPS_TO_EXECUTORCH_SYM_OPS in exir/passes/executorch_prim_ops_registry.py maps operator.eq, gt, lt, ge, le and torch.sym_not, but not operator.ne, operator.and_ or operator.or_, and kernels/prim_ops/register_prim_ops.cpp has no matching kernels. I found this while comparing the runtime with eager on small programs with dynamic shapes.

import torch
from executorch.exir import to_edge

class M(torch.nn.Module):
    def forward(self, t):
        return torch.cond(t.shape[0] != 7, lambda y: y + 1, lambda y: y - 1, (t,))

ep = torch.export.export(
    M(),
    (torch.randn(6, 2),),
    dynamic_shapes=({0: torch.export.Dim("n", min=2, max=40)},),
    strict=False,
)
to_edge(ep).to_executorch()
executorch.exir.error.InternalError: Failed with error: invalid target for call_function <built-in function ne>

(t.shape[0] > 3) & (t.shape[0] < 10) fails the same way with <built-in function and_>, and | with <built-in function or_>. The same program with == lowers and runs.

This is the same gap as #25767 (operator.pow) and #25772 (math.floor), for different operators. I have a fix that adds executorch_prim::ne.Scalar, sym_and.Scalar and sym_or.Scalar on the model of eq and sym_not, with tests, and will open a PR linked to this issue.

Versions

ExecuTorch built from main at 035df43, PyTorch 2.15.0+cpu, Python 3.12.3, Ubuntu 24.04 on WSL2, x86_64.

This issue was written with the help of Claude Code (Anthropic); I verified the reproduction.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions