Skip to content

Commit b52a8be

Browse files
Revert "[dynamo][guards][serialization] Dont use ID_MATCH guard for bool and None (pytorch#149228)"
This reverts commit 5905bbe. Reverted pytorch#149228 on behalf of https://github.com/malfet due to I wonder if this will fix the pr-time-benchmark regressions ([comment](pytorch#149228 (comment)))
1 parent 46226a9 commit b52a8be

File tree

2 files changed

+1
-97
lines changed

2 files changed

+1
-97
lines changed

torch/_dynamo/guards.py

+1-34
Original file line numberDiff line numberDiff line change
@@ -1464,35 +1464,6 @@ def DICT_CONTAINS(self, guard: Guard, key: str, invert: bool):
14641464
not invert, key, get_verbose_code_parts(code, guard)
14651465
)
14661466

1467-
def BOOL_MATCH(self, guard: Guard):
1468-
# checks val == True or val == False
1469-
ref = self.arg_ref(guard)
1470-
val = self.get(guard.name)
1471-
assert istype(val, bool)
1472-
code = [f"{ref} == {val!r}"]
1473-
self._set_guard_export_info(guard, code)
1474-
1475-
if val:
1476-
self.get_guard_manager(guard).add_true_match_guard(
1477-
get_verbose_code_parts(code, guard)
1478-
)
1479-
else:
1480-
self.get_guard_manager(guard).add_false_match_guard(
1481-
get_verbose_code_parts(code, guard)
1482-
)
1483-
1484-
def NONE_MATCH(self, guard: Guard):
1485-
# checks `val is None`
1486-
ref = self.arg_ref(guard)
1487-
val = self.get(guard.name)
1488-
assert val is None
1489-
code = [f"{ref} is None"]
1490-
self._set_guard_export_info(guard, code)
1491-
1492-
self.get_guard_manager(guard).add_none_match_guard(
1493-
get_verbose_code_parts(code, guard)
1494-
)
1495-
14961467
def ID_MATCH(self, guard: Guard):
14971468
# ___check_obj_id is same as `id(x) == y`
14981469
if isinstance(guard.originating_source, TypeSource):
@@ -1711,11 +1682,7 @@ def EQUALS_MATCH(self, guard: Guard):
17111682

17121683
def CONSTANT_MATCH(self, guard: Guard):
17131684
val = self.get(guard.name)
1714-
if istype(val, bool):
1715-
self.BOOL_MATCH(guard)
1716-
elif val is None:
1717-
self.NONE_MATCH(guard)
1718-
elif istype(val, types.CodeType):
1685+
if istype(val, (bool, type(None), types.CodeType)):
17191686
self.ID_MATCH(guard)
17201687
else:
17211688
self.EQUALS_MATCH(guard)

torch/csrc/dynamo/guards.cpp

-63
Original file line numberDiff line numberDiff line change
@@ -1486,36 +1486,6 @@ class ID_MATCH : public LeafGuard {
14861486
intptr_t _expected;
14871487
};
14881488

1489-
class NONE_MATCH : public LeafGuard {
1490-
public:
1491-
NONE_MATCH(py::object verbose_code_parts)
1492-
: LeafGuard(std::move(verbose_code_parts)) {}
1493-
1494-
bool check_nopybind(PyObject* value) override { // borrowed ref
1495-
return value == Py_None;
1496-
}
1497-
};
1498-
1499-
class TRUE_MATCH : public LeafGuard {
1500-
public:
1501-
TRUE_MATCH(py::object verbose_code_parts)
1502-
: LeafGuard(std::move(verbose_code_parts)) {}
1503-
1504-
bool check_nopybind(PyObject* value) override { // borrowed ref
1505-
return value == Py_True;
1506-
}
1507-
};
1508-
1509-
class FALSE_MATCH : public LeafGuard {
1510-
public:
1511-
FALSE_MATCH(py::object verbose_code_parts)
1512-
: LeafGuard(std::move(verbose_code_parts)) {}
1513-
1514-
bool check_nopybind(PyObject* value) override { // borrowed ref
1515-
return value == Py_False;
1516-
}
1517-
};
1518-
15191489
class EQUALS_MATCH : public LeafGuard {
15201490
public:
15211491
EQUALS_MATCH(py::object value, py::object verbose_code_parts)
@@ -5263,18 +5233,6 @@ PyObject* torch_c_dynamo_guards_init() {
52635233
py::class_<ID_MATCH, LeafGuard, std::shared_ptr<ID_MATCH>>(py_m, "ID_MATCH")
52645234
.def(py::init<py::object, py::list>())
52655235
.def("__call__", &ID_MATCH::check);
5266-
py::class_<NONE_MATCH, LeafGuard, std::shared_ptr<NONE_MATCH>>(
5267-
py_m, "NONE_MATCH")
5268-
.def(py::init<py::list>())
5269-
.def("__call__", &NONE_MATCH::check);
5270-
py::class_<TRUE_MATCH, LeafGuard, std::shared_ptr<TRUE_MATCH>>(
5271-
py_m, "TRUE_MATCH")
5272-
.def(py::init<py::list>())
5273-
.def("__call__", &TRUE_MATCH::check);
5274-
py::class_<FALSE_MATCH, LeafGuard, std::shared_ptr<FALSE_MATCH>>(
5275-
py_m, "FALSE_MATCH")
5276-
.def(py::init<py::list>())
5277-
.def("__call__", &FALSE_MATCH::check);
52785236
py::class_<EQUALS_MATCH, LeafGuard, std::shared_ptr<EQUALS_MATCH>>(
52795237
py_m, "EQUALS_MATCH")
52805238
.def(py::init<py::object, py::list>())
@@ -5520,27 +5478,6 @@ PyObject* torch_c_dynamo_guards_init() {
55205478
self.add_leaf_guard(std::make_shared<ID_MATCH>(
55215479
std::move(value), std::move(verbose_code_parts)));
55225480
})
5523-
.def(
5524-
"add_none_match_guard",
5525-
[](GuardManager& self, py::object verbose_code_parts) -> void {
5526-
SKIP_IF_GUARD_ALREADY_PRESENT("NONE_MATCH");
5527-
self.add_leaf_guard(
5528-
std::make_shared<NONE_MATCH>(std::move(verbose_code_parts)));
5529-
})
5530-
.def(
5531-
"add_true_match_guard",
5532-
[](GuardManager& self, py::object verbose_code_parts) -> void {
5533-
SKIP_IF_GUARD_ALREADY_PRESENT("TRUE_MATCH");
5534-
self.add_leaf_guard(
5535-
std::make_shared<TRUE_MATCH>(std::move(verbose_code_parts)));
5536-
})
5537-
.def(
5538-
"add_false_match_guard",
5539-
[](GuardManager& self, py::object verbose_code_parts) -> void {
5540-
SKIP_IF_GUARD_ALREADY_PRESENT("FALSE_MATCH");
5541-
self.add_leaf_guard(
5542-
std::make_shared<FALSE_MATCH>(std::move(verbose_code_parts)));
5543-
})
55445481
.def(
55455482
"add_equals_match_guard",
55465483
[](GuardManager& self,

0 commit comments

Comments
 (0)