Prune operators and variables which are not needed to generate :code:`targets`. Prune operators and variables which are needed to generate feeded_var Notes: This is a very low level API. Users should not use this API directly. This API is in flux and not sta
(self, feeded_var_names, targets)
| 6803 | return self._prune_with_input([], targets) |
| 6804 | |
| 6805 | def _prune_with_input(self, feeded_var_names, targets): |
| 6806 | """ |
| 6807 | Prune operators and variables which are not needed to generate |
| 6808 | :code:`targets`. Prune operators and variables which are needed |
| 6809 | to generate feeded_var |
| 6810 | |
| 6811 | Notes: This is a very low level API. Users should not use this API |
| 6812 | directly. This API is in flux and not stable. |
| 6813 | |
| 6814 | Args: |
| 6815 | feeded_var_names(list|str): A list of variable names from where |
| 6816 | pruning start. If it is set as [], this API works just like _prune() |
| 6817 | targets(list|Variable|Operator): A list of variables, operators, or variable names |
| 6818 | need to be pruned |
| 6819 | |
| 6820 | Returns: |
| 6821 | Program: A new, pruned program. |
| 6822 | """ |
| 6823 | |
| 6824 | # NOTE(zhiqiu): we sync the original program first, since its program may diff with |
| 6825 | # its desc due to modifying desc in c++ space. E.g. save op will add kLookupTablePath in desc. |
| 6826 | self._sync_with_cpp() |
| 6827 | |
| 6828 | if not isinstance(feeded_var_names, list): |
| 6829 | feeded_var_names = [feeded_var_names] |
| 6830 | if not isinstance(targets, list): |
| 6831 | targets = [targets] |
| 6832 | |
| 6833 | for var in feeded_var_names: |
| 6834 | if not isinstance(var, str): |
| 6835 | raise ValueError( |
| 6836 | "All feeded_var_names of Program._prune_with_input() can only be " |
| 6837 | f"str, but received {type(var)}." |
| 6838 | ) |
| 6839 | |
| 6840 | # find out all variables that can be generated or updated with given feed |
| 6841 | generatable_vars = set() |
| 6842 | |
| 6843 | for idx, op in enumerate(self.global_block().ops): |
| 6844 | runnable_op = True |
| 6845 | for name in op.input_arg_names: |
| 6846 | if not self.global_block().has_var(name): |
| 6847 | continue |
| 6848 | if self.global_block().var(name).persistable: |
| 6849 | continue |
| 6850 | if name not in generatable_vars.union(feeded_var_names): |
| 6851 | runnable_op = False |
| 6852 | break |
| 6853 | if runnable_op: |
| 6854 | generatable_vars = generatable_vars.union(op.output_arg_names) |
| 6855 | |
| 6856 | targets_idx = [] |
| 6857 | for t in targets: |
| 6858 | if not isinstance(t, Operator): |
| 6859 | if isinstance(t, Variable): |
| 6860 | name = t.name |
| 6861 | elif isinstance(t, str): |
| 6862 | name = str(t) |