PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
21.63k stars 5.44k forks source link

pir-to-py-code converter #63911

Closed tc20042008 closed 1 week ago

tc20042008 commented 1 week ago

PR Category

CINN

PR Types

Improvements

Description

pcard-76996

Convert program to python code

paddle-bot[bot] commented 1 week ago

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

tc20042008 commented 1 week ago

给定pir Program:

{
    (%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]"} : () -> builtin.tensor<1x-1x768xf32>
    (%1) = "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} : () -> builtin.tensor<1xf32>
    (%2) = cinn_op.fusion () -> builtin.tensor<1xb> {
      (%3) = "cinn_op.reduce_sum" (%0) {dim:[],keep_dim:false,stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (builtin.tensor<1x-1x768xf32>) -> builtin.tensor<f32>
      (%4) = "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} : () -> builtin.tensor<1xf32>
      (%5) = "cinn_op.broadcast" (%3) {broadcast_axes:[],out_shape:[(Int64)1],stop_gradient:[false]} : (builtin.tensor<f32>) -> builtin.tensor<1xf32>
      (%6) = "pd_op.greater_than" (%5, %4) {stop_gradient:[false],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xf32>, builtin.tensor<1xf32>) -> builtin.tensor<1xb>
      (%7) = "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[1]",value:(Float)1} : () -> builtin.tensor<1xf32>
      (%8) = "pd_op.less_than" (%1, %7) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xf32>, builtin.tensor<1xf32>) -> builtin.tensor<1xb>
      (%9) = "pd_op.logical_and" (%6, %8) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xb>, builtin.tensor<1xb>) -> builtin.tensor<1xb>
      () = "cf.yield" (%9) {} : (builtin.tensor<1xb>) ->  
 }
    (%10, %11) = "pd_op.while"(cond=%2, inputs=%1, %0) { 
    ^%arg_0, %arg_1
        (%12) = cinn_op.fusion () -> builtin.tensor<1x-1x768xf32> {
          (%13) = "pd_op.exp" (%arg_1) {stop_gradient:[false],sym_shape_str:"shape[1, S0, 768], data[NULL]"} : (builtin.tensor<1x-1x768xf32>) -> builtin.tensor<1x-1x768xf32>
          (%14) = "pd_op.subtract" (%13, %arg_1) {stop_gradient:[false],sym_shape_str:"shape[1, S0, 768], data[NULL]"} : (builtin.tensor<1x-1x768xf32>, builtin.tensor<1x-1x768xf32>) -> builtin.tensor<1x-1x768xf32>
          () = "cf.yield" (%14) {} : (builtin.tensor<1x-1x768xf32>) ->  
 }
        (%15) = cinn_op.fusion () -> builtin.tensor<1xf32> {
          (%16) = "cinn_op.scale" (%arg_0) {bias:(Float)1,bias_after_scale:true,scale:(Float)1,stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xf32>) -> builtin.tensor<1xf32>
          () = "cf.yield" (%16) {} : (builtin.tensor<1xf32>) ->  
 }
        (%17) = cinn_op.fusion () -> builtin.tensor<1xb> {
          (%18) = "cinn_op.reduce_sum" (%12) {dim:[],keep_dim:false,stop_gradient:[false],sym_shape_str:"shape[], data[NULL]"} : (builtin.tensor<1x-1x768xf32>) -> builtin.tensor<f32>
          (%19) = "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} : () -> builtin.tensor<1xf32>
          (%20) = "cinn_op.broadcast" (%18) {broadcast_axes:[],out_shape:[(Int64)1],stop_gradient:[false]} : (builtin.tensor<f32>) -> builtin.tensor<1xf32>
          (%21) = "pd_op.greater_than" (%20, %19) {stop_gradient:[false],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xf32>, builtin.tensor<1xf32>) -> builtin.tensor<1xb>
          (%22) = "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[1]",value:(Float)1} : () -> builtin.tensor<1xf32>
          (%23) = "pd_op.less_than" (%15, %22) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xf32>, builtin.tensor<1xf32>) -> builtin.tensor<1xb>
          (%24) = "pd_op.logical_and" (%21, %23) {stop_gradient:[true],sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xb>, builtin.tensor<1xb>) -> builtin.tensor<1xb>
          () = "cf.yield" (%24) {} : (builtin.tensor<1xb>) ->  
 }
        () = "cf.yield" (%17, %15, %12) {sym_shape_str:"shape[1], data[NULL]"} : (builtin.tensor<1xb>, builtin.tensor<1xf32>, builtin.tensor<1x-1x768xf32>) -> 
    }
    (%25) = cinn_op.fusion () -> builtin.tensor<1x-1x768xf32> {
      (%26) = "pd_op.exp" (%11) {stop_gradient:[false],sym_shape_str:"shape[1, S0, 768], data[NULL]"} : (builtin.tensor<1x-1x768xf32>) -> builtin.tensor<1x-1x768xf32>
      () = "cf.yield" (%26) {} : (builtin.tensor<1x-1x768xf32>) ->  
 }
    () = "builtin.shadow_output" (%25) {output_name:"output_0",sym_shape_str:"shape[1, S0, 768], data[NULL]"} : (builtin.tensor<1x-1x768xf32>) -> 
}

本pr会将如下python原胚代码打印到日志里

class Program:

  def __init__(self):

    self.data_201 = self.Op("pd_op.data", 201, input_types=[], output_types=[self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), name=self.a_str("x"), shape=self.a_intarray(1, -1, 768), stop_gradient=self.a_array(self.a_bool(False))))

    self.full_202 = self.Op("pd_op.full", 202, input_types=[], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), shape=self.a_intarray(1), value=self.a_f32("0"), stop_gradient=self.a_array(self.a_bool(True))))

    self.reduce_sum_233 = self.Op("cinn_op.reduce_sum", 233, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([], self.t_f32())], attrs=dict(keep_dim=self.a_bool(False), stop_gradient=self.a_array(self.a_bool(False)), dim=self.a_array()))

    self.full_212 = self.Op("pd_op.full", 212, input_types=[], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), shape=self.a_intarray(1), value=self.a_f32("0"), stop_gradient=self.a_array(self.a_bool(True))))

    self.greater_than_213 = self.Op("pd_op.greater_than", 213, input_types=[self.t_dtensor([], self.t_f32()), self.t_dtensor([1], self.t_f32())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(False))))

    self.full_214 = self.Op("pd_op.full", 214, input_types=[], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), shape=self.a_intarray(1), value=self.a_f32("1"), stop_gradient=self.a_array(self.a_bool(True))))

    self.less_than_215 = self.Op("pd_op.less_than", 215, input_types=[self.t_dtensor([1], self.t_f32()), self.t_dtensor([1], self.t_f32())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(True))))

    self.logical_and_216 = self.Op("pd_op.logical_and", 216, input_types=[self.t_dtensor([1], self.t_bool()), self.t_dtensor([1], self.t_bool())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(True))))

    self.yield_237 = self.Op("cf.yield", 237, input_types=[self.t_dtensor([1], self.t_bool())], output_types=[], attrs=dict())

    self.group_236 = self.Op("cinn_op.group", 236, input_types=[], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(), block_positional_arg_names=[[[]]], block_keyword_arg_names=[[{}]])

    self.exp_218 = self.Op("pd_op.exp", 218, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(stop_gradient=self.a_array(self.a_bool(False))))

    self.subtract_219 = self.Op("pd_op.subtract", 219, input_types=[self.t_dtensor([1, -1, 768], self.t_f32()), self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(stop_gradient=self.a_array(self.a_bool(False))))

    self.reduce_sum_235 = self.Op("cinn_op.reduce_sum", 235, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([], self.t_f32())], attrs=dict(keep_dim=self.a_bool(False), stop_gradient=self.a_array(self.a_bool(False)), dim=self.a_array()))

    self.scale_234 = self.Op("cinn_op.scale", 234, input_types=[self.t_dtensor([1], self.t_f32())], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(bias=self.a_f32("1"), stop_gradient=self.a_array(self.a_bool(True)), bias_after_scale=self.a_bool(True), scale=self.a_f32("1")))

    self.full_224 = self.Op("pd_op.full", 224, input_types=[], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), shape=self.a_intarray(1), value=self.a_f32("0"), stop_gradient=self.a_array(self.a_bool(True))))

    self.greater_than_225 = self.Op("pd_op.greater_than", 225, input_types=[self.t_dtensor([], self.t_f32()), self.t_dtensor([1], self.t_f32())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(False))))

    self.full_226 = self.Op("pd_op.full", 226, input_types=[], output_types=[self.t_dtensor([1], self.t_f32())], attrs=dict(dtype=self.a_dtype("float32"), place=self.a_place("undefined", 0), shape=self.a_intarray(1), value=self.a_f32("1"), stop_gradient=self.a_array(self.a_bool(True))))

    self.less_than_227 = self.Op("pd_op.less_than", 227, input_types=[self.t_dtensor([1], self.t_f32()), self.t_dtensor([1], self.t_f32())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(True))))

    self.logical_and_228 = self.Op("pd_op.logical_and", 228, input_types=[self.t_dtensor([1], self.t_bool()), self.t_dtensor([1], self.t_bool())], output_types=[self.t_dtensor([1], self.t_bool())], attrs=dict(stop_gradient=self.a_array(self.a_bool(True))))

    self.yield_241 = self.Op("cf.yield", 241, input_types=[self.t_dtensor([1, -1, 768], self.t_f32()), self.t_dtensor([1], self.t_f32()), self.t_dtensor([1], self.t_bool())], output_types=[], attrs=dict())

    self.group_240 = self.Op("cinn_op.group", 240, input_types=[], output_types=[self.t_dtensor([1, -1, 768], self.t_f32()), self.t_dtensor([1], self.t_f32()), self.t_dtensor([1], self.t_bool())], attrs=dict(), block_positional_arg_names=[[[]]], block_keyword_arg_names=[[{}]])

    self.yield_229 = self.Op("cf.yield", 229, input_types=[self.t_dtensor([1], self.t_bool()), self.t_dtensor([1], self.t_f32()), self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[], attrs=dict())

    self.while_217 = self.Op("pd_op.while", 217, input_types=[self.t_dtensor([1], self.t_bool()), self.t_dtensor([1], self.t_f32()), self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([1], self.t_f32()), self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(stop_gradient=self.a_array(self.a_bool(True), self.a_bool(False))), block_positional_arg_names=[[["arg_1127035536", "arg_1127035392"]]], block_keyword_arg_names=[[{}]])

    self.exp_230 = self.Op("pd_op.exp", 230, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(stop_gradient=self.a_array(self.a_bool(False))))

    self.yield_239 = self.Op("cf.yield", 239, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[], attrs=dict())

    self.group_238 = self.Op("cinn_op.group", 238, input_types=[], output_types=[self.t_dtensor([1, -1, 768], self.t_f32())], attrs=dict(), block_positional_arg_names=[[[]]], block_keyword_arg_names=[[{}]])

    self.shadow_output_231 = self.Op("builtin.shadow_output", 231, input_types=[self.t_dtensor([1, -1, 768], self.t_f32())], output_types=[], attrs=dict(output_name=self.a_str("output_0")))

    self.module_199 = self.Op("builtin.module", 199, input_types=[], output_types=[], attrs=dict(program=self.a_pointer("0x432ebd30")), block_positional_arg_names=[[[]]], block_keyword_arg_names=[[{}]])

  def group_236_block00(self, call, data_2010, full_2020):

    def ret_lambda():

      reduce_sum_2330, = call(self.reduce_sum_233, data_2010)

      full_2120, = call(self.full_212)

      greater_than_2130, = call(self.greater_than_213, reduce_sum_2330, full_2120)

      full_2140, = call(self.full_214)

      less_than_2150, = call(self.less_than_215, full_2020, full_2140)

      logical_and_2160, = call(self.logical_and_216, greater_than_2130, less_than_2150)

      return call(self.yield_237, logical_and_2160)

    return ret_lambda

  def group_240_block00(self, call, arg_1127035392, arg_1127035536):

    def ret_lambda():

      exp_2180, = call(self.exp_218, arg_1127035392)

      subtract_2190, = call(self.subtract_219, exp_2180, arg_1127035392)

      reduce_sum_2350, = call(self.reduce_sum_235, subtract_2190)

      scale_2340, = call(self.scale_234, arg_1127035536)

      full_2240, = call(self.full_224)

      greater_than_2250, = call(self.greater_than_225, reduce_sum_2350, full_2240)

      full_2260, = call(self.full_226)

      less_than_2270, = call(self.less_than_227, scale_2340, full_2260)

      logical_and_2280, = call(self.logical_and_228, greater_than_2250, less_than_2270)

      return call(self.yield_241, subtract_2190, scale_2340, logical_and_2280)

    return ret_lambda

  def while_217_block00(self, call):

    def ret_lambda(arg_1127035536, arg_1127035392):

      group_2400, group_2401, group_2402, = call(self.group_240, blocks=[[(self.group_240_block00, arg_1127035392, arg_1127035536)]])

      return call(self.yield_229, group_2402, group_2401, group_2400)

    return ret_lambda

  def group_238_block00(self, call, while_2171):

    def ret_lambda():

      exp_2300, = call(self.exp_230, while_2171)

      return call(self.yield_239, exp_2300)

    return ret_lambda

  def module_199_block00(self, call):

    def ret_lambda():

      data_2010, = call(self.data_201)

      full_2020, = call(self.full_202)

      group_2360, = call(self.group_236, blocks=[[(self.group_236_block00, data_2010, full_2020)]])

      while_2170, while_2171, = call(self.while_217, group_2360, full_2020, data_2010, blocks=[[(self.while_217_block00)]])

      group_2380, = call(self.group_238, blocks=[[(self.group_238_block00, while_2171)]])

      call(self.shadow_output_231, group_2380)

    return ret_lambda

  def __call__(self, call, *args, **kwargs):

    self.SetArgs(args)

    self.SetKeywordArgs(kwargs)

    return call(self.module_199, blocks=[[(self.module_199_block00)]])
tc20042008 commented 1 week ago

本pr不会影响develop已有的任何功能,只是打印些日志而已。