-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[Prim][PIR] dropout forward sink #59176
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
cyber-pioneer
merged 13 commits into
PaddlePaddle:develop
from
kevincheng2:dropout_prim_pir
Nov 29, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9bdaedd
dropout op sink
kevincheng2 6e388bd
update code
kevincheng2 b9c5f0e
dropout sink
kevincheng2 9ae7cc6
Merge branch 'develop' into dropout_prim_pir
kevincheng2 67c279b
test dropout op
kevincheng2 682afa7
prim dropout sink
kevincheng2 6d8f015
pirm dropout sink
kevincheng2 caefe00
remove dropout in python
kevincheng2 18e149f
merge code
kevincheng2 78028cc
merge code
kevincheng2 77424b5
Merge branch 'develop' into dropout_prim_pir
kevincheng2 1c63254
merge code
kevincheng2 930608f
merge code
kevincheng2 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
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 |
|---|---|---|
|
|
@@ -384,6 +384,65 @@ std::tuple<Tensor, Tensor, Tensor> layer_norm_decomp( | |
| return std::make_tuple(out, mean_, variance); | ||
| } | ||
|
|
||
| template <typename T> | ||
| std::tuple<Tensor, Tensor> dropout_decomp( | ||
| const Tensor& x, | ||
| const paddle::optional<Tensor>& seed_tensor, | ||
| const paddle::Scalar& p, | ||
| bool is_test, | ||
| const std::string& mode, | ||
| const int seed, | ||
| bool fix_seed) { | ||
| auto org_dtype = x.dtype(); | ||
| bool upscale_in_train = false; | ||
| if (mode.compare("upscale_in_train") == 0) { | ||
| upscale_in_train = true; | ||
| } | ||
|
|
||
| int seed_tmp = 0; | ||
| if (fix_seed) { | ||
| seed_tmp = seed; | ||
| } | ||
|
|
||
| auto dtype_tmp = org_dtype; | ||
| if (is_half_dtype(org_dtype)) { | ||
| dtype_tmp = phi::DataType::FLOAT32; | ||
| } | ||
|
|
||
| auto uniform_tensor = | ||
| uniform<T>(phi::vectorize(x.dims()), dtype_tmp, 0.0, 1.0, seed_tmp); | ||
| auto mask = | ||
| cast<T>(greater_equal<T>(uniform_tensor, | ||
| full<T>(phi::vectorize(x.dims()), p, dtype_tmp)), | ||
| org_dtype); | ||
| auto ones_p = | ||
| full<T>(phi::vectorize(x.dims()), 1.0 - p.to<float>(), org_dtype); | ||
| if (upscale_in_train) { | ||
| if (is_test) { | ||
| // inference: out = input | ||
| return std::make_tuple(x, cast<T>(mask, phi::DataType::UINT8)); | ||
| } else { | ||
| // train: out = input * mask / ( 1.0 - p ) | ||
| if (p.to<float>() == 1.0) { | ||
| // Process p=1. for avoid devide zero error (x*mask/(1.0-p)) | ||
| auto zero = full<T>(phi::vectorize(x.dims()), 0.0, org_dtype); | ||
| return std::make_tuple(x * zero, cast<T>(zero, phi::DataType::UINT8)); | ||
| } else { | ||
| auto ans = divide<T>(x * mask, ones_p); | ||
|
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. divide -> / 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. 好的 |
||
| return std::make_tuple(ans, cast<T>(mask, phi::DataType::UINT8)); | ||
| } | ||
| } | ||
| } else { | ||
| if (is_test) { | ||
| // inference: out = input * (1.0 - p) | ||
| return std::make_tuple(x * ones_p, cast<T>(mask, phi::DataType::UINT8)); | ||
| } else { | ||
| // train: out = input * mask | ||
| return std::make_tuple(x * mask, cast<T>(mask, phi::DataType::UINT8)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <typename T> | ||
| Tensor sqrt_decomp(const Tensor& x) { | ||
| auto org_dtype = x.dtype(); | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -52,3 +52,4 @@ | |
| - cast | ||
| - sign | ||
| - slice | ||
| - uniform | ||
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.
直接用 == 判断吧,这里是区分了大小写的,api侧已经check过了

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.
好的