未验证 提交 f9e20747 编写于 作者: G gongweibao 提交者: GitHub

Modify program_to_desc functions to add types support. (#12991)

上级 9ae55dd7
...@@ -62,9 +62,12 @@ def variable_to_code(var): ...@@ -62,9 +62,12 @@ def variable_to_code(var):
Returns: Returns:
string: The formatted string. string: The formatted string.
""" """
if var.type == core.VarDesc.VarType.SELECTED_ROWS or var.type == core.VarDesc.VarType.LOD_TENSOR:
var_str = "{name} : fluid.{type}.shape{shape}.astype({dtype})".\ var_str = "{name} : fluid.{type}.shape{shape}.astype({dtype})".\
format(i="{", e="}", name=var.name, type=var.type, shape=var.shape, dtype=var.dtype) format(i="{", e="}", name=var.name, type=var.type, shape=var.shape, dtype=var.dtype)
else:
var_str = "{name} : fluid.{type})".\
format(i="{", e="}", name=var.name, type=var.type)
if type(var) == paddle.fluid.framework.Parameter: if type(var) == paddle.fluid.framework.Parameter:
if var.trainable: if var.trainable:
...@@ -142,27 +145,17 @@ def op_to_code(op): ...@@ -142,27 +145,17 @@ def op_to_code(op):
return op_str return op_str
def program_to_code(prog): def block_to_code(block, block_idx):
"""
Print readable codes of fluid program.
Args:
prog : A fluid program.
An example result like bellow:
https://github.com/PaddlePaddle/Paddle/pull/12673
"""
indent = 0 indent = 0
block_idx = 0
for block in prog.blocks:
print("{0}{1} // block {2}".format( print("{0}{1} // block {2}".format(
get_indent_space(indent), '{', block_idx)) get_indent_space(indent), '{', block_idx))
indent += 1 indent += 1
# sort all vars # sort all vars
all_vars = sorted(six.iteritems(block.vars), key=lambda x: x[0]) all_vars = sorted(block.vars.iteritems(), key=lambda x: x[0])
for var in all_vars: for var in all_vars:
print("{}{}".format( print("{}{}".format(get_indent_space(indent), variable_to_code(var[1])))
get_indent_space(indent), variable_to_code(var[1])))
if len(all_vars) > 0: if len(all_vars) > 0:
print("") print("")
...@@ -170,5 +163,21 @@ def program_to_code(prog): ...@@ -170,5 +163,21 @@ def program_to_code(prog):
for op in block.ops: for op in block.ops:
print("{}{}".format(get_indent_space(indent), op_to_code(op))) print("{}{}".format(get_indent_space(indent), op_to_code(op)))
indent -= 1 indent -= 1
print("{0}{1}".format(get_indent_space(indent), '}')) print("{0}{1}".format(get_indent_space(indent), '}'))
def program_to_code(prog):
"""
Print readable codes of fluid program.
Args:
prog : A fluid program.
An example result like bellow:
https://github.com/PaddlePaddle/Paddle/pull/12673
"""
block_idx = 0
for block in prog.blocks:
block_to_code(block, block_idx)
block_idx += 1 block_idx += 1
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册