Skip to content

Comments

Add block.fwd_block_id#8489

Merged
tonyyang-svail merged 6 commits intoPaddlePaddle:developfrom
reyoung:feature/add_fwd_block_id
Feb 23, 2018
Merged

Add block.fwd_block_id#8489
tonyyang-svail merged 6 commits intoPaddlePaddle:developfrom
reyoung:feature/add_fwd_block_id

Conversation

@reyoung
Copy link
Collaborator

@reyoung reyoung commented Feb 22, 2018

Fix #8367


int32_t Parent() const { return desc_->parent_idx(); }

int32_t ForwardBlockID() const { return desc_->forward_block_idx(); }
Copy link

@tonyyang-svail tonyyang-svail Feb 23, 2018

Choose a reason for hiding this comment

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

looks like this function is unused

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is used in pybind to expose the forward block id to Python side.

}
return it->second.get();

BlockDesc *tmp = ParentBlock();

Choose a reason for hiding this comment

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

In issue #8367, the search is described as BFS. But the code here is DFS. Please confirm.

if fwd_block is not None:
return fwd_block.var_recursive(name)
else:
raise

Choose a reason for hiding this comment

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

I am not familiar with the python syntax. What is being raised here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This statement will rethrow the exception.

try:
parent_block = self.program.block(self.parent_idx)
return parent_block.var_recursive(name)
except ValueError:

Choose a reason for hiding this comment

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

I feel the logic is not very straightforward. Probably change to

if self.forward_block_idx == -1:
    raise
else
    self.program.block(self.forward_block_idx).var_recursive(name)

else:
parent_block = self.program.block(self.parent_idx)
return parent_block.var_recursive(name)
# DFS

Choose a reason for hiding this comment

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

In issue #8367, the search is described as BFS. But the code here is DFS. Please confirm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. I think the BFS should be the correct logic, however, DFS can be implemented easily. I will change the logic to BFS in following commits.

# If the op has its own sub-block, deal with the sub-block first
if op.has_attr("sub_block"):
sub_block = program.block(op.block_attr("sub_block"))
grad_sub_block = program.create_block(parent_idx=sub_block.idx)

Choose a reason for hiding this comment

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

Was this a bug?
grad_sub_block = program.create_block(parent_idx=sub_block.idx)
=>
grad_sub_block = program.create_block(parent_idx=current_block.idx)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We use parent to specify the forward block of the grad block before.
Since we separate the forward block and parent block, here we change the parent of grad block to its actual parent.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent!

Choose a reason for hiding this comment

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

Cool.

if (block_ins.find(input_name) != block_ins.end() ||
fwd_block->FindVar(input_name) != nullptr) {
fwd_block->FindVar(input_name) != nullptr ||
parent_block->FindVar(input_name) != nullptr) {

Choose a reason for hiding this comment

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

Why would while_op handle this separately? All the logic has been implemented in FindVar, so we only need grad_bock->FindVar(input_name) != nullptr?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This logic is to calculate for each operator, which inputs are the OG that calculated in outside and pass into while_grad_op by its inputs. The grad_block always contains all variables that operators needed.

}

BlockDesc *BlockDesc::ParentBlock() const {
if (this->desc_->parent_idx() == kNoneBlockIndex) {

Choose a reason for hiding this comment

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

This logic has been implemented in MutableBlock. Excellent.

@reyoung reyoung changed the title Add block.fwd_block_id [WIP] Add block.fwd_block_id Feb 23, 2018
@reyoung reyoung force-pushed the feature/add_fwd_block_id branch from 7218c87 to 574bcda Compare February 23, 2018 05:13
@reyoung reyoung changed the title [WIP] Add block.fwd_block_id Add block.fwd_block_id Feb 23, 2018

prog = self.program

while len(frontier) != 0: # BFS
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we reuse BlockDesc::FindVarRecursive here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need to find Variable in python, not in C++, so we cannot reuse that code.

Copy link
Collaborator

@JiayiFeng JiayiFeng left a comment

Choose a reason for hiding this comment

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

LGTM

@tonyyang-svail tonyyang-svail merged commit 7a9098a into PaddlePaddle:develop Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants