Prune operators and variables which are not needed to generate :code:`fetch_list` and optimize operators. Prune operators and variables which are needed to generate variables to be fed. Notes: This is a very low level API. Users should not use this API
(
cls, program, feed=None, fetch_list=None, optimize_ops=None
)
| 1547 | |
| 1548 | @classmethod |
| 1549 | def _prune_program( |
| 1550 | cls, program, feed=None, fetch_list=None, optimize_ops=None |
| 1551 | ): |
| 1552 | """ |
| 1553 | Prune operators and variables which are not needed to generate |
| 1554 | :code:`fetch_list` and optimize operators. |
| 1555 | Prune operators and variables which are needed |
| 1556 | to generate variables to be fed. |
| 1557 | |
| 1558 | Notes: This is a very low level API. Users should not use this API |
| 1559 | directly. |
| 1560 | |
| 1561 | Args: |
| 1562 | program(Program): the origin program |
| 1563 | feed(list|dict): feed dict or list. |
| 1564 | fetch_list(list|Variable): A list of variables need to be fetched |
| 1565 | optimize_ops(list[Operator]): A list of optimizer operators |
| 1566 | |
| 1567 | Returns: |
| 1568 | Program: A new, pruned program. |
| 1569 | """ |
| 1570 | compiled = isinstance(program, compiler.CompiledProgram) |
| 1571 | if compiled: |
| 1572 | if program._program: |
| 1573 | origin_program = program._program |
| 1574 | else: |
| 1575 | warnings.warn( |
| 1576 | "The program holds no _program, maybe it is constructed by graph, which can't be pruned yet." |
| 1577 | ) |
| 1578 | return |
| 1579 | else: |
| 1580 | origin_program = program |
| 1581 | |
| 1582 | feed_names = [] |
| 1583 | if isinstance(feed, dict): |
| 1584 | feed_names = list(feed.keys()) |
| 1585 | elif isinstance(feed, (list, tuple)): |
| 1586 | for i, each in enumerate(feed): |
| 1587 | feed_names += list(each.keys()) |
| 1588 | |
| 1589 | # if optimize_ops is [], all optimize ops in the program is used. |
| 1590 | if not optimize_ops: |
| 1591 | for block in origin_program.blocks: |
| 1592 | for op in block.ops: |
| 1593 | if op._is_optimize_op(): |
| 1594 | optimize_ops.append(op) |
| 1595 | |
| 1596 | targets = fetch_list + optimize_ops |
| 1597 | pruned_program = origin_program._prune_with_input(feed_names, targets) |
| 1598 | |
| 1599 | if compiled: |
| 1600 | # for compiled program, update the underlying program, re-generate graph, |
| 1601 | # and reset the flag so it can be compiled again. |
| 1602 | program._program = pruned_program |
| 1603 | program._graph = core.Graph(pruned_program.desc) |
| 1604 | program._compiled = False |
| 1605 | else: |
| 1606 | program = pruned_program |
no test coverage detected