PyTorch PR #186043 fixes symbolic bool negation with torch.sym_not
Dynamo no longer constant-folds torch._check(not expr), preventing dropped guard assertions
PyTorch's Dynamo compiler had a subtle bug when handling Python's `not` operator on symbolic booleans. During bytecode tracing, Dynamo normally maps `not` to `operator.not_` in the resulting FX graph. For `SymNodeVariable` values representing symbolic booleans, this old lowering forced fake evaluation to use the example hint value, which allowed `torch._check(not expr)` to be constant-folded away — silently dropping the symbolic assertion. Later uses of `expr` would then trigger data-dependent guard errors. The fix, submitted in PR #186043, lowers symbolic boolean negation to `torch.sym_not` instead, keeping the negated symbolic expression in the graph so `torch._check` can preserve the assertion. Non-boolean symbolic values continue using the existing `operator.not_` path for normal Python truthiness.
For non-strict export, user code runs directly rather than through Dynamo tracing, so Python `not` still hits `SymBool.__bool__` before `torch._check` can see it. To address this, the PR adds a descriptive error hint pointing users to `torch.sym_not()` or an equivalent symbolic comparison. The change was tested with added cases in `test/dynamo/test_misc.py` and `test/export/test_export.py`, and was approved by reviewers anijain2305 and mlazos. This is a correctness fix that matters for anyone relying on PyTorch's export and symbolic shape guards, an area increasingly critical for inference optimization and compilation workflows.
- Dynamo now lowers symbolic boolean negation to torch.sym_not instead of operator.not_ to avoid constant-folding in fake evaluation
- Fixes issue #143157 where torch._check(not expr) silently dropped assertions, causing data-dependent guard failures
- Non-strict export gets a new error hint directing users to torch.sym_not() or an explicit symbolic comparison
Why It Matters
PyTorch export users get reliable symbolic guards, preventing silent assertion drops and data-dependent errors in compiled graphs.