Skip to content

Commit f9e2074

Browse files
authored
Modify program_to_desc functions to add types support. (#12991)
1 parent 9ae55dd commit f9e2074

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

python/paddle/fluid/transpiler/details/program_utils.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ def variable_to_code(var):
6262
Returns:
6363
string: The formatted string.
6464
"""
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)
6871

6972
if type(var) == paddle.fluid.framework.Parameter:
7073
if var.trainable:
@@ -142,6 +145,28 @@ def op_to_code(op):
142145
return op_str
143146

144147

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+
145170
def program_to_code(prog):
146171
"""
147172
Print readable codes of fluid program.
@@ -152,23 +177,7 @@ def program_to_code(prog):
152177
An example result like bellow:
153178
https://github.com/PaddlePaddle/Paddle/pull/12673
154179
"""
155-
indent = 0
156180
block_idx = 0
157181
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)
174183
block_idx += 1

0 commit comments

Comments
 (0)