Sync from the desc on the c++ end. This method is used to synchronize the c++ desc instance generated by backward.
(self)
| 4923 | return op |
| 4924 | |
| 4925 | def _sync_with_cpp(self): |
| 4926 | """ |
| 4927 | Sync from the desc on the c++ end. This method is used to synchronize |
| 4928 | the c++ desc instance generated by backward. |
| 4929 | """ |
| 4930 | # sync variables from cpp |
| 4931 | for var in self.desc.all_vars(): |
| 4932 | if not self.has_var(var.name()): |
| 4933 | is_stop_gradient = False |
| 4934 | if var.has_stop_gradient(): |
| 4935 | is_stop_gradient = var.stop_gradient() |
| 4936 | if var.has_is_parameter() and var.is_parameter(): |
| 4937 | self.create_parameter( |
| 4938 | name=var.name(), |
| 4939 | desc=var, |
| 4940 | type=var.type(), |
| 4941 | shape=var.shape(), |
| 4942 | dtype=var.dtype(), |
| 4943 | stop_gradient=is_stop_gradient, |
| 4944 | ) |
| 4945 | else: |
| 4946 | self.create_var( |
| 4947 | name=var.name(), |
| 4948 | desc=var, |
| 4949 | type=var.type(), |
| 4950 | stop_gradient=is_stop_gradient, |
| 4951 | ) |
| 4952 | |
| 4953 | # sync variables removed from c++ end |
| 4954 | for var in list(self.vars.keys()): |
| 4955 | if not self.desc.find_var(var.encode()): |
| 4956 | self.vars.pop(var) |
| 4957 | |
| 4958 | # sync operators from cpp |
| 4959 | ops_in_cpp = [] |
| 4960 | for op_idx in range(0, self.desc.op_size()): |
| 4961 | ops_in_cpp.append(self.desc.op(op_idx)) |
| 4962 | |
| 4963 | if len(self.ops) != 0: |
| 4964 | first_op_in_python = self.ops[0].desc |
| 4965 | last_op_in_python = self.ops[len(self.ops) - 1].desc |
| 4966 | start_index = None |
| 4967 | end_index = None |
| 4968 | for index in range(len(ops_in_cpp)): |
| 4969 | if first_op_in_python == ops_in_cpp[index]: |
| 4970 | start_index = index |
| 4971 | if last_op_in_python == ops_in_cpp[index]: |
| 4972 | end_index = index |
| 4973 | assert start_index is not None |
| 4974 | assert end_index is not None |
| 4975 | assert start_index <= end_index |
| 4976 | else: |
| 4977 | start_index = 0 |
| 4978 | end_index = -1 |
| 4979 | |
| 4980 | # sync ops append to the head of cpp_ops |
| 4981 | for index in range((start_index - 1 - 1), -1, -1): |
| 4982 | op_desc = ops_in_cpp[index] |
no test coverage detected