Skip to content

Commit 94957f0

Browse files
cyber-pioneerco63oc
authored andcommitted
[Prim]polish prim FLAGS_prim_skip_dynamic (PaddlePaddle#64106)
* polish prim FLAGS_prim_skip_dynamic * add backward blacklist * fix bug * fix special case * fix test case
1 parent 6e58998 commit 94957f0

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

paddle/common/flags.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ PHI_DEFINE_EXPORTED_bool(pir_debug,
15281528
false,
15291529
"Whether print more pir debug info.");
15301530
PHI_DEFINE_EXPORTED_bool(prim_skip_dynamic,
1531-
false,
1531+
true,
15321532
"Whether to skip decomposing op with dynamic shape.");
15331533
PHI_DEFINE_EXPORTED_bool(prim_check_ops,
15341534
false,

paddle/fluid/primitive/base/decomp_trans.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "paddle/pir/include/core/builtin_dialect.h"
2525
#include "paddle/pir/include/core/program.h"
2626

27-
COMMON_DECLARE_bool(prim_skip_dynamic);
2827
COMMON_DECLARE_bool(prim_check_ops);
2928
COMMON_DECLARE_string(prim_forward_blacklist);
3029

@@ -44,7 +43,15 @@ std::unordered_set<std::string> decomp_op_contain_none = {"pd_op.squeeze",
4443
"pd_op.batch_norm_"};
4544
//
4645
std::unordered_set<std::string> dynamic_shape_blacklist = {
47-
"pd_op.squeeze", "pd_op.unsqueeze", "pd_op.flatten"};
46+
"pd_op.squeeze",
47+
"pd_op.unsqueeze",
48+
"pd_op.batch_norm",
49+
"pd_op.batch_norm_",
50+
"pd_op.bmm",
51+
"pd_op.elu",
52+
"pd_op.flatten",
53+
"pd_op.instance_norm",
54+
"pd_op.one_hot"};
4855

4956
namespace {
5057
std::set<std::string> StringSplit(const std::string& str) {
@@ -422,10 +429,6 @@ void DecompProgram::decomp_block(
422429
}
423430
bool enable_prim =
424431
has_decomp_rule(*op) && enable_decomp_by_filter(op->name());
425-
if (enable_prim && FLAGS_prim_skip_dynamic &&
426-
check_decomp_dynamic_shape(op)) {
427-
enable_prim = false;
428-
}
429432
if (enable_prim && check_decomp_dynamic_shape(op) &&
430433
dynamic_shape_blacklist.find(op->name()) !=
431434
dynamic_shape_blacklist.end()) {

python/paddle/autograd/backward_utils.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@
9494
]
9595

9696

97+
# TODO(CZ): to be removed when we support dynamic shape by default.
98+
ALLOW_DYNAMIC_SHAPE_VJP_OPS = [
99+
"pd_op.abs",
100+
"pd_op.assign",
101+
"pd_op.sin",
102+
"pd_op.cos",
103+
"pd_op.tanh",
104+
"pd_op.cast",
105+
"pd_op.log",
106+
"pd_op.exp",
107+
"pd_op.sqrt",
108+
"pd_op.rsqrt",
109+
"pd_op.sigmoid",
110+
"pd_op.silu",
111+
]
112+
113+
97114
class ValueWrapper:
98115
def __init__(self, value) -> None:
99116
if isinstance(value, ValueWrapper):
@@ -314,17 +331,23 @@ def _check_vjp_dynamic_shape(op, inputs):
314331
# Prim currently does not support dynamic shape, when dynamic shape exits in shape of op inputs, prim will be skipped its vjp op.
315332
@signature_safe_contextmanager
316333
def dynamic_shape_prim_vjp_guard(op, inputs):
317-
skip_prim = (
318-
core._is_bwd_prim_enabled()
319-
and core._enable_prim_skip_dynamic_shape()
320-
and _check_vjp_dynamic_shape(op, inputs)
321-
)
334+
origin_prim = core._is_bwd_prim_enabled()
335+
if op.name() == "cf.tuple_push":
336+
skip_prim = True
337+
else:
338+
skip_prim = (
339+
origin_prim
340+
and core._enable_prim_skip_dynamic_shape()
341+
and _check_vjp_dynamic_shape(op, inputs)
342+
and op.name() not in ALLOW_DYNAMIC_SHAPE_VJP_OPS
343+
)
344+
322345
try:
323-
if skip_prim:
346+
if origin_prim and skip_prim:
324347
core._set_prim_backward_enabled(False)
325348
yield
326349
finally:
327-
if skip_prim:
350+
if origin_prim:
328351
core._set_prim_backward_enabled(True)
329352

330353

python/paddle/base/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def _get_batch_norm_none_var(op):
541541

542542
# This api is used for development for dynamic shape in prim, and will be removed in future.
543543
def _enable_prim_skip_dynamic_shape():
544-
flag = os.getenv("FLAGS_prim_skip_dynamic")
544+
flag = os.getenv("FLAGS_prim_skip_dynamic", "1")
545545
if flag and flag.lower() in ("1", "true"):
546546
return True
547547
else:

test/ir/pir/cinn/sub_graphs/test_sub_graph_73.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def train(self, net, to_static, with_prim=False, with_cinn=False):
9999
def test_ast_prim_cinn(self):
100100
st_out = self.train(self.net, to_static=True)
101101
cinn_out = self.train(
102-
self.net, to_static=True, with_prim=True, with_cinn=True
102+
self.net, to_static=True, with_prim=True, with_cinn=False
103103
)
104104
for st, cinn in zip(
105105
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out)

0 commit comments

Comments
 (0)