Skip to content

Commit e17700a

Browse files
authored
Sym not prim op (pytorch#20641)
## Summary Register `torch.sym_not` in ExecuTorch's prim ops registry so that models shapes) can be exported and executed on-device. This was the root cause of ~53% of HuggingFace exporter test failures when using `Dim.AUTO` dynamic shapes — `sym_not` appeared in the exported graph but had no ET registration or runtime kernel. - Python op definition + mapping in `executorch_prim_ops_registry.py` - C++ kernel in `register_prim_ops.cpp` - Python e2e test in `test_emit.py` - C++ unit test in `prim_ops_test.cpp` ## Test plan - [ ] `python -m pytest exir/emit/test/test_emit.py::TestEmit::test_emit_sym_not -xvs` - [ ] C++ prim_ops_test `SymNotReturnsCorrectValue` and `OpRegistered` tests - [ ] HuggingFace exporter test suite — sym_not failures should be resolved
1 parent f95486d commit e17700a

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎exir/emit/test/test_emit.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,53 @@ def forward(self, x):
24052405
self.assertTrue(expected.shape == et_result.shape)
24062406
self.assertTrue(torch.allclose(expected, et_result))
24072407

2408+
def test_emit_sym_not(self) -> None:
2409+
class SymNotModel(nn.Module):
2410+
def forward(self, x):
2411+
n = x.shape[0]
2412+
flag = n > 5
2413+
neg = torch.sym_not(flag)
2414+
val = torch.sym_float(neg)
2415+
return x + val
2416+
2417+
model = SymNotModel()
2418+
model.eval()
2419+
test_inputs = [
2420+
torch.randn(3, 4), # n<=5: sym_not(False)=True, float(True)=1.0
2421+
torch.randn(8, 4), # n>5: sym_not(True)=False, float(False)=0.0
2422+
]
2423+
reference_outputs = []
2424+
with torch.no_grad():
2425+
for inp in test_inputs:
2426+
reference_outputs.append(model(inp))
2427+
2428+
batch_dim = Dim("batch", min=1, max=20)
2429+
dynamic_shapes = {"x": {0: batch_dim}}
2430+
exported_program = torch.export.export(
2431+
model, (test_inputs[0],), dynamic_shapes=dynamic_shapes
2432+
)
2433+
sym_not_nodes = [
2434+
n
2435+
for n in exported_program.graph.nodes
2436+
if n.op == "call_function" and n.target is torch.sym_not
2437+
]
2438+
self.assertGreater(
2439+
len(sym_not_nodes), 0, "sym_not should appear in exported graph"
2440+
)
2441+
2442+
edge_program = to_edge(
2443+
exported_program,
2444+
compile_config=exir.EdgeCompileConfig(_check_ir_validity=False),
2445+
)
2446+
et_program = edge_program.to_executorch()
2447+
program_buffer = et_program.buffer
2448+
et_module = _load_for_executorch_from_buffer(program_buffer)
2449+
for inp, expected in zip(test_inputs, reference_outputs):
2450+
et_output = et_module.forward([inp])
2451+
et_result = et_output[0]
2452+
self.assertTrue(expected.shape == et_result.shape)
2453+
self.assertTrue(torch.allclose(expected, et_result))
2454+
24082455
def test_emit_channels_last_constant(self) -> None:
24092456
"""Test that channels-last constant tensors are emitted correctly.
24102457

‎exir/passes/executorch_prim_ops_registry.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def sym_min(a: _SymScalar, b: _SymScalar) -> bool:
124124
return min(a, b) # pyre-ignore
125125

126126

127+
@bind_pattern_to_op(executorch_prims_lib, "sym_not.Scalar(Scalar a) -> bool")
128+
def sym_not(a: _SymScalar) -> bool:
129+
return not a # pyre-ignore
130+
131+
127132
_PYTHON_SYM_OPS_TO_EXECUTORCH_SYM_OPS: Dict[Any, OpOverload] = {
128133
builtins.round: ops.backend.executorch_prim.round.Scalar,
129134
math.ceil: ops.backend.executorch_prim.ceil.Scalar,
@@ -143,6 +148,7 @@ def sym_min(a: _SymScalar, b: _SymScalar) -> bool:
143148
torch.sym_float: ops.backend.executorch_prim.sym_float.Scalar,
144149
torch.sym_max: ops.backend.executorch_prim.sym_max.Scalar,
145150
torch.sym_min: ops.backend.executorch_prim.sym_min.Scalar,
151+
torch.sym_not: ops.backend.executorch_prim.sym_not.Scalar,
146152
}
147153

148154

‎kernels/prim_ops/register_prim_ops.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ static Kernel prim_ops[] = {
391391
} else if (a.isDouble()) {
392392
// TODO: This should be impossible
393393
out = EValue(a.toDouble());
394+
} else if (a.isBool()) {
395+
out = EValue(static_cast<double>(a.toBool()));
394396
} else {
395397
ET_KERNEL_CHECK_MSG(
396398
context, false, InvalidType, /* void */, "%zu", (size_t)a.tag);
@@ -475,6 +477,36 @@ static Kernel prim_ops[] = {
475477
}),
476478
#endif
477479

480+
#if !defined(EXECUTORCH_ENABLE_PRIM_OPS_SELECTIVE_BUILD) || \
481+
defined(INCLUDE_EXECUTORCH_PRIM_SYM_NOT_SCALAR)
482+
// executorch_prim::sym_not.Scalar(bool a) -> bool
483+
Kernel(
484+
"executorch_prim::sym_not.Scalar",
485+
[](KernelRuntimeContext& context, Span<EValue*> stack) {
486+
ET_KERNEL_CHECK_MSG(
487+
context,
488+
stack.size() == 2,
489+
InvalidProgram,
490+
/* void */,
491+
"Expected %zu args, got %zu",
492+
(size_t)2,
493+
stack.size());
494+
EValue& a = *stack[0];
495+
EValue& out = *stack[1];
496+
if (a.isBool()) {
497+
out = EValue(!a.toBool());
498+
} else {
499+
ET_KERNEL_CHECK_MSG(
500+
context,
501+
false,
502+
InvalidType,
503+
/* void */,
504+
"sym_not only supports bool inputs, got %zu",
505+
(size_t)a.tag);
506+
}
507+
}),
508+
#endif
509+
478510
#if !defined(EXECUTORCH_ENABLE_PRIM_OPS_SELECTIVE_BUILD) || \
479511
defined(INCLUDE_EXECUTORCH_PRIM_FLOORDIV_INT)
480512
// executorch_prim::floordiv.int(int, int) -> int

‎kernels/prim_ops/test/prim_ops_test.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ TEST_F(RegisterPrimOpsTest, OpRegistered) {
4040
EXPECT_TRUE(hasOpsFn("aten::sym_numel"));
4141
EXPECT_TRUE(hasOpsFn("executorch_prim::sym_max.Scalar"));
4242
EXPECT_TRUE(hasOpsFn("executorch_prim::sym_min.Scalar"));
43+
EXPECT_TRUE(hasOpsFn("executorch_prim::sym_not.Scalar"));
4344
}
4445

4546
TEST_F(RegisterPrimOpsTest, SymSizeReturnsCorrectValue) {
@@ -166,6 +167,43 @@ TEST_F(RegisterPrimOpsTest, SymMinReturnsCorrectValue) {
166167
EXPECT_EQ(stack[2]->toInt(), -5);
167168
}
168169

170+
TEST_F(RegisterPrimOpsTest, SymNotReturnsCorrectValue) {
171+
EValue values[2];
172+
values[0] = EValue(true);
173+
values[1] = EValue(false);
174+
175+
EValue* stack[2];
176+
for (size_t i = 0; i < 2; i++) {
177+
stack[i] = &values[i];
178+
}
179+
180+
getOpsFn("executorch_prim::sym_not.Scalar")(context_, Span<EValue*>(stack));
181+
EXPECT_EQ(stack[1]->toBool(), false);
182+
183+
// Test not(false) -> true
184+
values[0] = EValue(false);
185+
values[1] = EValue(false);
186+
getOpsFn("executorch_prim::sym_not.Scalar")(context_, Span<EValue*>(stack));
187+
EXPECT_EQ(stack[1]->toBool(), true);
188+
}
189+
190+
TEST_F(RegisterPrimOpsTest, SymFloatHandlesBoolInput) {
191+
EValue values[2];
192+
values[0] = EValue(true);
193+
values[1] = EValue(0.0);
194+
EValue* stack[2];
195+
for (size_t i = 0; i < 2; i++) {
196+
stack[i] = &values[i];
197+
}
198+
getOpsFn("executorch_prim::sym_float.Scalar")(context_, Span<EValue*>(stack));
199+
EXPECT_FLOAT_EQ(stack[1]->toDouble(), 1.0);
200+
201+
values[0] = EValue(false);
202+
values[1] = EValue(0.0);
203+
getOpsFn("executorch_prim::sym_float.Scalar")(context_, Span<EValue*>(stack));
204+
EXPECT_FLOAT_EQ(stack[1]->toDouble(), 0.0);
205+
}
206+
169207
TEST_F(RegisterPrimOpsTest, TestAlgebraOps) {
170208
EValue values[3];
171209
int64_t a = 3;

0 commit comments

Comments
 (0)