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

[Prim][PIR] dropout forward sink #59176

Merged
merged 13 commits into from
Nov 29, 2023

Conversation

kevincheng2
Copy link
Contributor

@kevincheng2 kevincheng2 commented Nov 20, 2023

PR types

New features

PR changes

Others

Description

prim dropout sink c++ and add uniform in primitive.h

Comparison of Python and C++ Program:
prim in Python

{
 (%0) = "pd_op.data" () {dtype:(pd_op.DataType)float32,name:"x",place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[8,16,32,64],stop_gradient:[false]} : () -> pd_op.tensor<8x16x32x64xf32>
 (%1) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)8,(Int64)16,(Int64)32,(Int64)64]} : () -> pd_op.tensor<4xi64>
 (%2) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)0} : () -> pd_op.tensor<1xf32>
 (%3) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)1} : () -> pd_op.tensor<1xf32>
 (%4) = "pd_op.uniform" (%1, %2, %3) {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),seed:(Int32)0,stop_gradient:[true]} : (pd_op.tensor<4xi64>, pd_op.tensor<1xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%5) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)0.5} : () -> pd_op.tensor<1xf32>
 (%6) = "pd_op.greater_equal" (%4, %5) {stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<8x16x32x64xb>
 (%7) = "pd_op.cast" (%6) {dtype:(pd_op.DataType)float32,stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xb>) -> pd_op.tensor<8x16x32x64xf32>
 (%8) = "pd_op.multiply" (%0, %7) {stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%9) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[8,16,32,64],stop_gradient:[true],value:(Float)0.5} : () -> pd_op.tensor<8x16x32x64xf32>
 (%10) = "pd_op.divide" (%8, %9) {stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%11) = "pd_op.cast" (%7) {dtype:(pd_op.DataType)uint8,stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xu8>
 (%12) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)1} : () -> pd_op.tensor<1xf32>
 (%13) = "pd_op.full_like" (%10, %12) {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%14, %15) = "pd_op.divide_grad" (%8, %9, %10, %13) {axis:(Int32)-1,stop_gradient:[false,false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>, <<NULL TYPE>>
 (%16, %17) = "pd_op.multiply_grad" (%0, %7, %14) {axis:(Int32)-1,stop_gradient:[false,false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>, <<NULL TYPE>>
 (%18) = "pd_op.fetch" (%10) {col:(Int32)0,name:"fetch0",stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%19) = "pd_op.fetch" (%16) {col:(Int32)1,name:"fetch1",stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
}

prim in C++

{
 (%0) = "pd_op.data" () {dtype:(pd_op.DataType)float32,name:"x",place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[8,16,32,64],stop_gradient:[false]} : () -> pd_op.tensor<8x16x32x64xf32>
 (%1) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],value:[(Int64)8,(Int64)16,(Int64)32,(Int64)64]} : () -> pd_op.tensor<4xi64>
 (%2) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)0} : () -> pd_op.tensor<1xf32>
 (%3) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)1} : () -> pd_op.tensor<1xf32>
 (%4) = "pd_op.uniform" (%1, %2, %3) {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),seed:(Int32)0,stop_gradient:[true]} : (pd_op.tensor<4xi64>, pd_op.tensor<1xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%5) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[8,16,32,64],stop_gradient:[true],value:(Float)0.5} : () -> pd_op.tensor<8x16x32x64xf32>
 (%6) = "pd_op.greater_equal" (%4, %5) {stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xb>
 (%7) = "pd_op.cast" (%6) {dtype:(pd_op.DataType)float32,stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xb>) -> pd_op.tensor<8x16x32x64xf32>
 (%8) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[8,16,32,64],stop_gradient:[true],value:(Float)0.5} : () -> pd_op.tensor<8x16x32x64xf32>
 (%9) = "pd_op.multiply" (%0, %7) {stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%10) = "pd_op.divide" (%9, %8) {stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%11) = "pd_op.cast" (%7) {dtype:(pd_op.DataType)uint8,stop_gradient:[true]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xu8>
 (%12) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],value:(Float)1} : () -> pd_op.tensor<1xf32>
 (%13) = "pd_op.full_like" (%10, %12) {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%14, %15) = "pd_op.divide_grad" (%9, %8, %10, %13) {axis:(Int32)-1,stop_gradient:[false,false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>, <<NULL TYPE>>
 (%16, %17) = "pd_op.multiply_grad" (%0, %7, %14) {axis:(Int32)-1,stop_gradient:[false,false]} : (pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>, pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>, <<NULL TYPE>>
 (%18) = "pd_op.fetch" (%10) {col:(Int32)0,name:"fetch0",stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
 (%19) = "pd_op.fetch" (%16) {col:(Int32)1,name:"fetch1",stop_gradient:[false]} : (pd_op.tensor<8x16x32x64xf32>) -> pd_op.tensor<8x16x32x64xf32>
}

Copy link

paddle-bot bot commented Nov 20, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Nov 20, 2023
bool fix_seed) {
auto org_dtype = x.dtype();
bool upscale_in_train = false;
if (mode.compare("upscale_in_train") == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

直接用 == 判断吧,这里是区分了大小写的,api侧已经check过了
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

divide -> /

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

@cyber-pioneer cyber-pioneer merged commit ee63c32 into PaddlePaddle:develop Nov 29, 2023
30 checks passed
@kevincheng2 kevincheng2 deleted the dropout_prim_pir branch January 5, 2024 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants