-   Notifications  You must be signed in to change notification settings 
- Fork 5.9k
Python cpp sync #4816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
  jacquesqiao merged 5 commits into PaddlePaddle:develop from jacquesqiao:python-cpp-sync        Oct 15, 2017  
    Merged  
 Python cpp sync #4816
Changes from all commits
 Commits 
  Show all changes 
  5 commits   Select commit Hold shift + click to select a range 
 787b8ad  add sync_with_cpp to Python Program and Block 
  jacquesqiao ad0d9aa  Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into… 
  jacquesqiao ae34692  sync vars and ops in block from cpp 
  jacquesqiao 1ab717a  optimize code and add some comment 
  jacquesqiao 0f1f96d  add more check for sync 
  jacquesqiao File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -308,6 +308,9 @@ def idx(self): | |
| def create_var(self, *args, **kwargs): | ||
| return Variable(self, *args, **kwargs) | ||
|  | ||
| def has_var(self, name): | ||
| return name in self.vars | ||
|  | ||
| def create_parameter(self, *args, **kwargs): | ||
| global_block = self.program.global_block() | ||
| return Parameter(global_block, *args, **kwargs) | ||
|  | @@ -324,6 +327,43 @@ def prepend_op(self, *args, **kwargs): | |
| self.ops.appendleft(op) | ||
| return op | ||
|  | ||
| def sync_with_cpp(self): | ||
| # sync variables from cpp | ||
| for var in self.desc.all_vars(): | ||
| if not self.has_var(var.name()): | ||
| self.create_var(name=var.name(), desc=var, type=var.type()) | ||
|  | ||
| # sync operators from cpp | ||
| ops_in_cpp = self.desc.all_ops() | ||
| first_op_in_python = self.ops[0].desc | ||
| last_op_in_python = self.ops[len(self.ops) - 1].desc | ||
| start_index = None | ||
| end_index = None | ||
| for index in range(len(ops_in_cpp)): | ||
| if first_op_in_python == ops_in_cpp[index]: | ||
| start_index = index | ||
| if last_op_in_python == ops_in_cpp[index]: | ||
| end_index = index | ||
| assert start_index is not None | ||
| assert end_index is not None | ||
| assert start_index <= end_index | ||
|  | ||
| # sync ops append to the head of cpp_ops | ||
| for index in range((start_index - 1 - 1), -1, -1): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why  | ||
| op_desc = ops_in_cpp[index] | ||
| op = Operator(self, op_desc) | ||
| self.ops.appendleft(op) | ||
|  | ||
| # sync ops append to the end of cpp_ops | ||
| for index in range((end_index + 1), len(ops_in_cpp)): | ||
| op_desc = ops_in_cpp[index] | ||
| op = Operator(self, op_desc) | ||
| self.ops.append(op) | ||
|  | ||
| assert len(self.ops) == len(ops_in_cpp) | ||
| for index in range(len(self.ops)): | ||
| assert self.ops[index].desc == ops_in_cpp[index] | ||
|  | ||
|  | ||
| class Program(object): | ||
| @classmethod | ||
|  | @@ -354,6 +394,12 @@ def global_block(self): | |
| def current_block(self): | ||
| return self.blocks[self.current_block_idx] | ||
|  | ||
| def append_backward(self, target, no_grad_set): | ||
| assert isinstance(target, Variable) | ||
| param_to_grad_info = self.desc.append_backward(target.desc, no_grad_set) | ||
| self.sync_with_cpp() | ||
| return param_to_grad_info | ||
|  | ||
| def create_block(self): | ||
| new_block_idx = len(self.blocks) | ||
| self.desc.append_block(self.current_block().desc) | ||
|  | @@ -364,6 +410,12 @@ def create_block(self): | |
| def rollback(self): | ||
| self.current_block_idx = self.current_block().parent_idx | ||
|  | ||
| def sync_with_cpp(self): | ||
| for block_idx in range(len(self.blocks), self.desc.num_blocks()): | ||
| self.blocks.append(Block(self, block_idx)) | ||
| for block in self.blocks: | ||
| block.sync_with_cpp() | ||
|  | ||
|  | ||
| class Parameter(Variable): | ||
| def __init__(self, block, shape, dtype, **kwargs): | ||
|  | ||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
      Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like append from cpp_ops, not to cpp_ops?