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

add insert_op for block #9765

Merged
merged 1 commit into from
Apr 9, 2018
Merged
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
15 changes: 11 additions & 4 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ class Block(object):
def __init__(self, program, idx):
self.desc = program.desc.block(idx)
self.vars = dict() # var_name --> var
self.ops = collections.deque() # operator list
self.ops = list() # operator list
self.program = program
self.removed_vars = dict()

Expand Down Expand Up @@ -831,6 +831,13 @@ def append_op(self, *args, **kwargs):
self.ops.append(op)
return op

def insert_op(self, index, *args, **kwargs):
self.sync_with_cpp()
op_desc = self.desc.insert_op(index)
op = Operator(block=self, desc=op_desc, *args, **kwargs)
self.ops.insert(index, op)
return op

def delete_ops(self, ops):
# remove from cpp
# FIXME(typhoonzero): remove only the first occurrence.
Expand All @@ -842,12 +849,12 @@ def delete_ops(self, ops):
self.desc.remove_op(start, end + 1)

def slice_ops(self, start, end):
return list(self.ops)[start:end]
return self.ops[start:end]

def prepend_op(self, *args, **kwargs):
op_desc = self.desc.prepend_op()
op = Operator(self, op_desc, *args, **kwargs)
self.ops.appendleft(op)
self.ops.insert(0, op)
return op

def sync_with_cpp(self):
Expand Down Expand Up @@ -892,7 +899,7 @@ def sync_with_cpp(self):
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)
self.ops.insert(0, op)

# sync ops append to the end of cpp_ops
for index in range((end_index + 1), len(ops_in_cpp)):
Expand Down