This method will create a new program and do following adjustments on it: 1. Remove all reader variables and their creator ops if exist. 2. Remove the :code:`read_op` if exists. 3. change the :code:`is_test` attribute of operators to :code:`True`. All the :
(self, prune_read_op=True)
| 6911 | return res |
| 6912 | |
| 6913 | def _inference_optimize(self, prune_read_op=True): |
| 6914 | """ |
| 6915 | This method will create a new program and do following adjustments on it: |
| 6916 | 1. Remove all reader variables and their creator ops if exist. |
| 6917 | |
| 6918 | 2. Remove the :code:`read_op` if exists. |
| 6919 | |
| 6920 | 3. change the :code:`is_test` |
| 6921 | attribute of operators to :code:`True`. All the :code:`Parameter` |
| 6922 | information will be lost. |
| 6923 | |
| 6924 | Args: |
| 6925 | prune_read_op(bool): remove the read ops that are added by py_reader |
| 6926 | for cpp inference library |
| 6927 | |
| 6928 | Notes: This API is a very low level API. Use |
| 6929 | :code:`Program.clone(for_test=True)` instead. |
| 6930 | |
| 6931 | Returns: |
| 6932 | Program: The new program. |
| 6933 | """ |
| 6934 | res = Program() |
| 6935 | res.desc = core.ProgramDesc(self.desc) |
| 6936 | |
| 6937 | # remove all readers and the read_op if exist |
| 6938 | read_op_idx = 0 |
| 6939 | root_block = res.desc.block(0) |
| 6940 | if prune_read_op: |
| 6941 | while True: |
| 6942 | if ( |
| 6943 | read_op_idx >= root_block.op_size() |
| 6944 | or root_block.op(read_op_idx).type() == "read" |
| 6945 | ): |
| 6946 | break |
| 6947 | read_op_idx += 1 |
| 6948 | if read_op_idx < root_block.op_size(): |
| 6949 | root_block._remove_op(0, read_op_idx + 1) |
| 6950 | for var in root_block.all_vars(): |
| 6951 | if var.type() == core.VarDesc.VarType.READER: |
| 6952 | root_block._remove_var(var.name().encode()) |
| 6953 | |
| 6954 | # change all `is_test` attributes to True |
| 6955 | for i in range(res.desc.num_blocks()): |
| 6956 | block = res.desc.block(i) |
| 6957 | for j in range(block.op_size()): |
| 6958 | op = block.op(j) |
| 6959 | if op.has_attr("is_test"): |
| 6960 | op._set_bool_attr("is_test", True) |
| 6961 | if op.type() == "batch_norm": |
| 6962 | # Remove the output ReserveSpace of batch_norm if exists. |
| 6963 | op.remove_output("ReserveSpace") |
| 6964 | res.blocks = [Block(res, i) for i in range(res.desc.num_blocks())] |
| 6965 | res._sync_with_cpp() |
| 6966 | return res |
| 6967 | |
| 6968 | def _remove_training_info(self, clip_extra=True): |
| 6969 | """ |