@@ -62,9 +62,12 @@ def variable_to_code(var):
62
62
Returns:
63
63
string: The formatted string.
64
64
"""
65
-
66
- var_str = "{name} : fluid.{type}.shape{shape}.astype({dtype})" .\
67
- format (i = "{" , e = "}" , name = var .name , type = var .type , shape = var .shape , dtype = var .dtype )
65
+ if var .type == core .VarDesc .VarType .SELECTED_ROWS or var .type == core .VarDesc .VarType .LOD_TENSOR :
66
+ var_str = "{name} : fluid.{type}.shape{shape}.astype({dtype})" .\
67
+ format (i = "{" , e = "}" , name = var .name , type = var .type , shape = var .shape , dtype = var .dtype )
68
+ else :
69
+ var_str = "{name} : fluid.{type})" .\
70
+ format (i = "{" , e = "}" , name = var .name , type = var .type )
68
71
69
72
if type (var ) == paddle .fluid .framework .Parameter :
70
73
if var .trainable :
@@ -142,6 +145,28 @@ def op_to_code(op):
142
145
return op_str
143
146
144
147
148
+ def block_to_code (block , block_idx ):
149
+ indent = 0
150
+
151
+ print ("{0}{1} // block {2}" .format (
152
+ get_indent_space (indent ), '{' , block_idx ))
153
+
154
+ indent += 1
155
+ # sort all vars
156
+ all_vars = sorted (block .vars .iteritems (), key = lambda x : x [0 ])
157
+ for var in all_vars :
158
+ print ("{}{}" .format (get_indent_space (indent ), variable_to_code (var [1 ])))
159
+
160
+ if len (all_vars ) > 0 :
161
+ print ("" )
162
+
163
+ for op in block .ops :
164
+ print ("{}{}" .format (get_indent_space (indent ), op_to_code (op )))
165
+ indent -= 1
166
+
167
+ print ("{0}{1}" .format (get_indent_space (indent ), '}' ))
168
+
169
+
145
170
def program_to_code (prog ):
146
171
"""
147
172
Print readable codes of fluid program.
@@ -152,23 +177,7 @@ def program_to_code(prog):
152
177
An example result like bellow:
153
178
https://github.com/PaddlePaddle/Paddle/pull/12673
154
179
"""
155
- indent = 0
156
180
block_idx = 0
157
181
for block in prog .blocks :
158
- print ("{0}{1} // block {2}" .format (
159
- get_indent_space (indent ), '{' , block_idx ))
160
- indent += 1
161
- # sort all vars
162
- all_vars = sorted (six .iteritems (block .vars ), key = lambda x : x [0 ])
163
- for var in all_vars :
164
- print ("{}{}" .format (
165
- get_indent_space (indent ), variable_to_code (var [1 ])))
166
-
167
- if len (all_vars ) > 0 :
168
- print ("" )
169
-
170
- for op in block .ops :
171
- print ("{}{}" .format (get_indent_space (indent ), op_to_code (op )))
172
- indent -= 1
173
- print ("{0}{1}" .format (get_indent_space (indent ), '}' ))
182
+ block_to_code (block , block_idx )
174
183
block_idx += 1
0 commit comments