Skip to content

Commit 4392695

Browse files
author
yi.wu
committed
update
1 parent 0cc1bfa commit 4392695

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,136 @@ def find_op_by_output_arg(block, arg_name):
3939
if arg_name in op.output_arg_names:
4040
return index
4141
return -1
42+
43+
44+
def get_indent_space(indent, space_num=4):
45+
ret = ""
46+
for i in range(0, indent * space_num):
47+
ret += " "
48+
49+
return ret
50+
51+
52+
def variable_to_code(var):
53+
"""
54+
Get readable codes of fluid variable.
55+
Args:
56+
var: A fluid operator.
57+
Returns:
58+
string: The formatted string.
59+
"""
60+
if var.type == core.VarDesc.VarType.SELECTED_ROWS or var.type == core.VarDesc.VarType.LOD_TENSOR:
61+
var_str = "{name} : fluid.{type}.shape{shape}.astype({dtype})".\
62+
format(i="{", e="}", name=var.name, type=var.type, shape=var.shape, dtype=var.dtype)
63+
else:
64+
var_str = "{name} : fluid.{type})".\
65+
format(i="{", e="}", name=var.name, type=var.type)
66+
67+
if type(var) == paddle.fluid.framework.Parameter:
68+
if var.trainable:
69+
var_str = "trainable parameter " + var_str
70+
else:
71+
var_str = "parameter " + var_str
72+
else:
73+
var_str = "var " + var_str
74+
75+
if var.persistable:
76+
var_str = "persist " + var_str
77+
78+
return var_str
79+
80+
81+
def op_to_code(op):
82+
"""
83+
Get readable codes of fluid operator.
84+
Args:
85+
op: A fluid operator.
86+
Returns:
87+
string: The foramtted string.
88+
"""
89+
90+
outputs_str = "{"
91+
for i in range(0, len(op.output_names)):
92+
outputs_str += "{name}=".format(name=op.output_names[i])
93+
o = op.output(op.output_names[i])
94+
outputs_str += "{value}".format(value=o)
95+
if i != len(op.output_names) - 1:
96+
outputs_str += ", "
97+
outputs_str += "}"
98+
99+
inputs_str = "{"
100+
for i in range(0, len(op.input_names)):
101+
inputs_str += "{name}=".format(name=op.input_names[i])
102+
o = op.input(op.input_names[i])
103+
inputs_str += "{value}".format(value=o)
104+
105+
if i != len(op.input_names) - 1:
106+
inputs_str += ", "
107+
inputs_str += "}"
108+
109+
attrs_str = ""
110+
for i in range(0, len(op.attr_names)):
111+
name = op.attr_names[i]
112+
113+
attr_type = op.desc.attr_type(name)
114+
if attr_type == core.AttrType.BLOCK:
115+
a = "{name} = block[{value}]".format(
116+
name=name, type=attr_type, value=op.block_attr_id(name))
117+
attrs_str += a
118+
continue
119+
120+
if attr_type == core.AttrType.BLOCKS:
121+
a = "{name} = blocks{value}".format(
122+
name=name, type=attr_type, value=op.blocks_attr_ids(name))
123+
attrs_str += a
124+
continue
125+
126+
a = "{name} = {value}".format(
127+
name=name, type=attr_type, value=op.desc.attr(name))
128+
attrs_str += a
129+
if i != len(op.attr_names) - 1:
130+
attrs_str += ", "
131+
132+
if outputs_str != "{}":
133+
op_str = "{outputs} = {op_type}(inputs={inputs}, {attrs})".\
134+
format(outputs = outputs_str, op_type=op.type, inputs=inputs_str, attrs=attrs_str)
135+
else:
136+
op_str = "{op_type}(inputs={inputs}, {attrs})".\
137+
format(op_type=op.type, inputs=inputs_str, attrs=attrs_str)
138+
return op_str
139+
140+
141+
def block_to_code(block, block_idx):
142+
indent = 0
143+
144+
print("{0}{1} // block {2}".format(
145+
get_indent_space(indent), '{', block_idx))
146+
147+
indent += 1
148+
# sort all vars
149+
all_vars = sorted(block.vars.iteritems(), key=lambda x: x[0])
150+
for var in all_vars:
151+
print("{}{}".format(get_indent_space(indent), variable_to_code(var[1])))
152+
153+
if len(all_vars) > 0:
154+
print("")
155+
156+
for op in block.ops:
157+
print("{}{}".format(get_indent_space(indent), op_to_code(op)))
158+
indent -= 1
159+
160+
print("{0}{1}".format(get_indent_space(indent), '}'))
161+
162+
163+
def program_to_code(prog):
164+
"""
165+
Print readable codes of fluid program.
166+
Args:
167+
prog : A fluid program.
168+
An example result like bellow:
169+
https://github.com/PaddlePaddle/Paddle/pull/12673
170+
"""
171+
block_idx = 0
172+
for block in prog.blocks:
173+
block_to_code(block, block_idx)
174+
block_idx += 1

0 commit comments

Comments
 (0)