Skip to content

Conversation

@zyfncg
Copy link
Contributor

@zyfncg zyfncg commented Feb 22, 2024

PR types

Others

PR changes

Others

Description

Pcard-67164

修复动态shape下 if 和 while 子图单测。

主要工作:

  1. 前端Pass执行流程重构,支持 If 和 While 等复杂嵌套Op的处理
  2. 前端支持将元素计算个数较小的Op fallback 到 PHI 体系执行

测试脚本:

FLAGS_pd_unittest_use_cinn=1 ctest -R test_if_dy -V

FLAGS_pd_unittest_use_cinn=1 ctest -R test_while_dy -V
if 子图:
{
    (%0) = "pd_op.data" () {dtype:(pd_op.DataType)float32,name:"x",place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[-1,-1],stop_gradient:[false],sym_shape_str:"shape[S0, S1], data[NULL]"} : () -> pd_op.tensor<-1x-1xf32>
    (%1) = "pd_op.shape" (%0) {stop_gradient:[true],sym_shape_str:"shape[2], data[S0, S1]"} : (pd_op.tensor<-1x-1xf32>) -> pd_op.tensor<2xi32>
    (%2) = cinn_op.fusion () -> pd_op.tensor<i32> {
    (%3) = "cinn_op.slice" (%1) {axes:[(Int64)0],decrease_axis:[(Int64)0],ends:[(Int64)2],infer_flags:[(Int64)1],starts:[(Int64)1],stop_gradient:[true],sym_shape_str:"shape[1], data[S1]"} : (pd_op.tensor<2xi32>) -> pd_op.tensor<i32>
    () = "cf.yield" (%3) {} : (pd_op.tensor<i32>) ->  
 }
    (%4) = "cinn_op.broadcast" (%2) {broadcast_axes:[],out_shape:[(Int64)1],stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<i32>) -> pd_op.tensor<1xi32>
    (%5) = cinn_op.fusion () -> pd_op.tensor<b> {
    (%6) = "pd_op.full" () {dtype:(pd_op.DataType)int32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[1]",value:(Float)1} : () -> pd_op.tensor<1xi32>
    (%7) = "cinn_op.broadcast" (%6) {broadcast_axes:[(Int64)0],out_shape:[(Int64)-1],stop_gradient:[true]} : (pd_op.tensor<1xi32>) -> pd_op.tensor<-1xi32>
    (%8) = "pd_op.greater_than" (%4, %7) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<1xi32>, pd_op.tensor<-1xi32>) -> pd_op.tensor<b>
    () = "cf.yield" (%8) {} : (pd_op.tensor<b>) ->  
 }
    (%9) = pd_op.if (%5) {stop_gradient:[false],sym_shape_str:"shape[S0, S1], data[NULL]"} -> pd_op.tensor<-1x-1xf32>{
        (%10) = "pd_op.exp" (%0) {stop_gradient:[false],sym_shape_str:"shape[S0, S1], data[NULL]"} : (pd_op.tensor<-1x-1xf32>) -> pd_op.tensor<-1x-1xf32>
        (%11) = "pd_op.subtract" (%10, %0) {stop_gradient:[false],sym_shape_str:"shape[S0, S1], data[NULL]"} : (pd_op.tensor<-1x-1xf32>, pd_op.tensor<-1x-1xf32>) -> pd_op.tensor<-1x-1xf32>
        () = "cf.yield" (%11) {} : (pd_op.tensor<-1x-1xf32>) -> 
    } else {
        () = "cf.yield" (%0) {} : (pd_op.tensor<-1x-1xf32>) -> 
    }
    (%12) = cinn_op.fusion () -> pd_op.tensor<-1x-1xf32> {
    (%13) = "pd_op.relu" (%9) {stop_gradient:[false],sym_shape_str:"shape[S0, S1], data[NULL]"} : (pd_op.tensor<-1x-1xf32>) -> pd_op.tensor<-1x-1xf32>
    () = "cf.yield" (%13) {} : (pd_op.tensor<-1x-1xf32>) ->  
 }
    () = "builtin.shadow_output" (%12) {output_name:"output_0",sym_shape_str:"shape[S0, S1], data[NULL]"} : (pd_op.tensor<-1x-1xf32>) -> 
}

while 子图:
{
    (%0) = "pd_op.data" () {dtype:(pd_op.DataType)float32,name:"x",place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1,-1,768],stop_gradient:[false],sym_shape_str:"shape[1, S0, 768], data[NULL]"} : () -> pd_op.tensor<1x-1x768xf32>
    (%1) = cinn_op.fusion () -> pd_op.tensor<f32> {
    (%2) = "cinn_op.reduce_sum" (%0) {dim:[],keep_dim:false,stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1x-1x768xf32>) -> pd_op.tensor<f32>
    () = "cf.yield" (%2) {} : (pd_op.tensor<f32>) ->  
 }
    (%3) = "cinn_op.broadcast" (%1) {broadcast_axes:[],out_shape:[(Int64)1],stop_gradient:[false],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<f32>) -> pd_op.tensor<1xf32>
    (%4, %5) = cinn_op.fusion () -> pd_op.tensor<1xf32>, pd_op.tensor<1xb> {
    (%6) = "pd_op.full" () {dtype:(pd_op.DataType)int32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[1]",value:(Float)1} : () -> pd_op.tensor<1xi32>
    (%7) = "pd_op.cast" (%6) {dtype:(pd_op.DataType)float32,stop_gradient:[true],sym_shape_str:"shape[1], data[1]"} : (pd_op.tensor<1xi32>) -> pd_op.tensor<1xf32>
    (%8) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[0]",value:(Float)0} : () -> pd_op.tensor<1xf32>
    (%9) = "cinn_op.broadcast" (%8) {broadcast_axes:[(Int64)0],out_shape:[(Int64)-1],stop_gradient:[true]} : (pd_op.tensor<1xf32>) -> pd_op.tensor<-1xf32>
    (%10) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[0]",value:(Float)0} : () -> pd_op.tensor<1xf32>
    (%11) = "cinn_op.broadcast" (%7) {broadcast_axes:[(Int64)0],out_shape:[(Int64)-1],stop_gradient:[true]} : (pd_op.tensor<1xf32>) -> pd_op.tensor<-1xf32>
    (%12) = "cinn_op.broadcast" (%10) {broadcast_axes:[(Int64)0],out_shape:[(Int64)-1],stop_gradient:[true]} : (pd_op.tensor<1xf32>) -> pd_op.tensor<-1xf32>
    (%13) = "pd_op.greater_than" (%3, %9) {stop_gradient:[false],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<1xf32>, pd_op.tensor<-1xf32>) -> pd_op.tensor<1xb>
    (%14) = "pd_op.less_than" (%12, %11) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<-1xf32>, pd_op.tensor<-1xf32>) -> pd_op.tensor<1xb>
    (%15) = "pd_op.logical_and" (%13, %14) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<1xb>, pd_op.tensor<1xb>) -> pd_op.tensor<1xb>
    () = "cf.yield" (%10, %15) {} : (pd_op.tensor<1xf32>, pd_op.tensor<1xb>) ->  
 }
    (%16, %17) = "pd_op.while"(cond=%5, inputs=%4, %0) { 
    ^%arg0, %arg1
        (%18) = "pd_op.exp" (%arg1) {stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1x-1x768xf32>) -> pd_op.tensor<1x-1x768xf32>
        (%19) = "pd_op.subtract" (%18, %arg1) {stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1x-1x768xf32>, pd_op.tensor<1x-1x768xf32>) -> pd_op.tensor<1x-1x768xf32>
        (%20) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(cpu),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[1]",value:(Float)1} : () -> pd_op.tensor<1xf32>
        (%21) = "pd_op.scale" (%arg0, %20) {bias:(Float)1,bias_after_scale:true,stop_gradient:[true],sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<1xf32>
        (%22) = "pd_op.full_int_array" () {dtype:(pd_op.DataType)int64,place:(pd_op.Place)Place(cpu),stop_gradient:[true],sym_shape_str:"shape[0], data[]",value:[]} : () -> pd_op.tensor<0xi64>
        (%23) = "pd_op.sum" (%19, %22) {dtype:(pd_op.DataType)Undefined,keepdim:false,stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1x-1x768xf32>, pd_op.tensor<0xi64>) -> pd_op.tensor<f32>
        (%24) = "pd_op.full" () {dtype:(pd_op.DataType)float32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[0]",value:(Float)0} : () -> pd_op.tensor<1xf32>
        (%25) = "pd_op.greater_than" (%23, %24) {stop_gradient:[false],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<f32>, pd_op.tensor<1xf32>) -> pd_op.tensor<1xb>
        (%26) = "pd_op.full" () {dtype:(pd_op.DataType)int32,place:(pd_op.Place)Place(undefined:0),shape:(pd_op.IntArray)[1],stop_gradient:[true],sym_shape_str:"shape[1], data[1]",value:(Float)1} : () -> pd_op.tensor<1xi32>
        (%27) = "pd_op.cast" (%26) {dtype:(pd_op.DataType)float32,stop_gradient:[true],sym_shape_str:"shape[1], data[1]"} : (pd_op.tensor<1xi32>) -> pd_op.tensor<1xf32>
        (%28) = "pd_op.less_than" (%21, %27) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<1xf32>, pd_op.tensor<1xf32>) -> pd_op.tensor<1xb>
        (%29) = "pd_op.logical_and" (%25, %28) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (pd_op.tensor<1xb>, pd_op.tensor<1xb>) -> pd_op.tensor<1xb>
        () = "cf.yield" (%29, %21, %19) {} : (pd_op.tensor<1xb>, pd_op.tensor<1xf32>, pd_op.tensor<1x-1x768xf32>) -> 
    }
    () = "builtin.shadow_output" (%17) {output_name:"output_0",sym_shape_str:"shape[], data[NULL]"} : (pd_op.tensor<1x-1x768xf32>) -> 
}

risemeup1 and others added 28 commits February 20, 2024 10:43
@paddle-bot
Copy link

paddle-bot bot commented Feb 22, 2024

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

Comment on lines +233 to +240
if (shape_analysis.HasShapeOrDataForValue(op.operand_source(i))) {
const auto& shape_expr =
shape_analysis.GetShapeOrDataForValue(op.operand_source(i));
if (shape_expr.isa<symbol::TensorShapeOrDataDimExprs>() &&
shape_expr.data()) { // has shape data
continue;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

重构成

const auto XXX = [&](int i) {
  ...
};
for (...) {
  if (XXX(i)) continue;
}

Copy link
Contributor Author

@zyfncg zyfncg Mar 2, 2024

Choose a reason for hiding this comment

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

好的, 在后续PR里修改

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@zyfncg zyfncg merged commit 775cbdc into PaddlePaddle:develop Mar 3, 2024
@zyfncg zyfncg deleted the test_if branch March 3, 2024 16:54
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.

7 participants