-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Design of if else op #3828
Merged
Merged
Design of if else op #3828
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ff3ec3c
switch_op.h
zchen0211 f075141
switch_op.cc
zchen0211 2f2dd23
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zchen0211 d156dfa
if else design doc
zchen0211 e39a2c9
update if else
zchen0211 32244b5
if else modified
zchen0211 d76c60b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zchen0211 523eb23
modify if else
zchen0211 e0c27a9
if else slight modify
zchen0211 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 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
IfOp should have only one branch. An IfOp operator takes a `cond` variable whose value must be a vector of N boolean elements. Its return value has M (M<=N) instances, each corresponds to a true element in `cond`. | ||
|
||
```python | ||
import paddle as pd | ||
|
||
x = var() | ||
y = var() | ||
cond = var() | ||
|
||
b = pd.create_ifop(inputs=[x], output_num=1) | ||
with b.true_block(): | ||
x = b.inputs(0) | ||
z = operator.add(x, y) | ||
b.set_output(0, operator.softmax(z)) | ||
|
||
out = b(cond) | ||
``` | ||
|
||
If we want the output still has N instances, we can use IfElseOp with a default value, whose minibatch size must be N: | ||
|
||
```python | ||
import paddle as pd | ||
|
||
x = var() | ||
y = var() | ||
cond = var() | ||
default_value = var() | ||
b = pd.create_ifelseop(inputs=[x], output_num=1) | ||
with b.true_block(): | ||
x = b.inputs(0) | ||
z = operator.add(x, y) | ||
b.set_output(0, operator.softmax(z)) | ||
|
||
with b.false_block(): | ||
x = b.inputs(0) | ||
z = layer.fc(x) | ||
b.set_output(0, operator.softmax(z)) | ||
|
||
out = b(cond) | ||
``` | ||
|
||
If only true_block is set in an IfElseOp, we can have a default value for false as: | ||
```python | ||
import paddle as pd | ||
|
||
x = var() | ||
y = var() | ||
cond = var() | ||
default_value = var() | ||
b = pd.create_ifelseop(inputs=[x], output_num=1, default_value) | ||
|
||
with b.true_block(): | ||
x = b.inputs(0) | ||
z = operator.add(x, y) | ||
b.set_output(0, operator.softmax(z)) | ||
|
||
out = b(cond) | ||
``` | ||
where default_value is a list of vars for `cond` == False. |
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.
should we add a static IfElseOp just like TF, just run one branch.
This design describe a DynamicIfElseOp ? am I right?
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.
Let's make this IfElseOp. If you are going to add a static conditional branching structure later, we can name it StaticIfElseOp.