Skip to content
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

refine sync_with_cpp for insert_op #9747

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,15 @@ def sync_with_cpp(self):
ops_in_cpp_index += 1
ops_in_python_index += 1

# sync ops inserted from c++ end
if len(self.ops) != len(ops_in_cpp) and start_index == 0 and len(
self.ops) == end_index:
self.ops.clear()
for index in range(len(ops_in_cpp)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity, can we only keep this and remove all above sync operations? It's slower but very easy to understand.

Copy link
Contributor Author

@luotao1 luotao1 Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I couldn't remove all above sync operators. I tried it, but many unit tests fail. The reason I guess is:

# sync ops append to the head of cpp_ops
for index in range((start_index - 1 - 1), -1, -1):
    op_desc = ops_in_cpp[index]
    op = Operator(self, op_desc)
    self.ops.appendleft(op)

The reverse order of range((start_index - 1 - 1), -1, -1) and appendleft make the order of python_ops different from cpp_ops.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's wired. The above code still tries to make the order of self.ops the same with cpp_ops. We can merge this if it's currently a blocking feature and add some comments here.

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]
Expand Down
11 changes: 9 additions & 2 deletions python/paddle/fluid/tests/unittests/test_protobuf_descs.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,20 @@ def test_add_var(self):
self.assertEqual(var2_re, var2)

def test_add_op(self):
program_desc = core.ProgramDesc()
program = Program()
program_desc = program.desc
self.assertIsNotNone(program_desc)
block = program_desc.block(0)
self.assertIsNotNone(block)
op1 = block.append_op()
op2 = block.append_op()
op0 = block.prepend_op()
op1 = block.insert_op(1)
op0.set_type("test")
op1.set_type("test")
op2.set_type("test")

program.sync_with_cpp()

all_ops = []
for idx in xrange(0, block.op_size()):
all_ops.append(block.op(idx))
Expand Down