Note: This should only used in Paddle/python/paddle/nn/layer/layers.py to record the call path for the operators in Static Graph of AutoParallel. Args: prefix(str, optional): prefix. Default is none.
(prefix=None)
| 1341 | |
| 1342 | @signature_safe_contextmanager |
| 1343 | def name_struct(prefix=None): |
| 1344 | """ |
| 1345 | Note: This should only used in Paddle/python/paddle/nn/layer/layers.py |
| 1346 | to record the call path for the operators in Static Graph of AutoParallel. |
| 1347 | |
| 1348 | Args: |
| 1349 | prefix(str, optional): prefix. Default is none. |
| 1350 | """ |
| 1351 | # TODO(panyx0718): Only [0-9a-z]. |
| 1352 | # in dygraph we don't need namescope since it will cause mem leak |
| 1353 | if in_dygraph_mode(): |
| 1354 | yield |
| 1355 | else: |
| 1356 | assert prefix, "namescope prefix can not be empty." |
| 1357 | global _name_struct |
| 1358 | _name_struct = _name_struct.child(prefix) |
| 1359 | if in_pir_mode(): |
| 1360 | op_num_before = len( |
| 1361 | paddle.static.default_main_program().global_block().ops |
| 1362 | ) |
| 1363 | try: |
| 1364 | yield |
| 1365 | finally: |
| 1366 | if in_pir_mode(): |
| 1367 | all_ops = ( |
| 1368 | paddle.static.default_main_program().global_block().ops |
| 1369 | ) |
| 1370 | op_num = len(all_ops) |
| 1371 | |
| 1372 | for idx in reversed(range(op_num_before, op_num)): |
| 1373 | op = all_ops[idx] |
| 1374 | if op.has_attr("struct_name"): |
| 1375 | continue |
| 1376 | op.set_str_attr("struct_name", _full_name_struct()) |
| 1377 | |
| 1378 | _name_struct = _name_struct.parent() |
| 1379 | |
| 1380 | |
| 1381 | def _full_name_struct(): |
no test coverage detected