Skip to content

Commit 4e81e22

Browse files
author
chengduo
authored
add op frequence (#13328)
1 parent fd4c4df commit 4e81e22

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ paddle.fluid.contrib.BeamSearchDecoder.early_stop ArgSpec(args=['self'], varargs
298298
paddle.fluid.contrib.BeamSearchDecoder.read_array ArgSpec(args=['self', 'init', 'is_ids', 'is_scores'], varargs=None, keywords=None, defaults=(False, False))
299299
paddle.fluid.contrib.BeamSearchDecoder.update_array ArgSpec(args=['self', 'array', 'value'], varargs=None, keywords=None, defaults=None)
300300
paddle.fluid.contrib.memory_usage ArgSpec(args=['program', 'batch_size'], varargs=None, keywords=None, defaults=None)
301+
paddle.fluid.contrib.op_freq_statistic ArgSpec(args=['program'], varargs=None, keywords=None, defaults=None)
301302
paddle.fluid.transpiler.DistributeTranspiler.__init__ ArgSpec(args=['self', 'config'], varargs=None, keywords=None, defaults=(None,))
302303
paddle.fluid.transpiler.DistributeTranspiler.get_pserver_program ArgSpec(args=['self', 'endpoint'], varargs=None, keywords=None, defaults=None)
303304
paddle.fluid.transpiler.DistributeTranspiler.get_pserver_programs ArgSpec(args=['self', 'endpoint'], varargs=None, keywords=None, defaults=None)

python/paddle/fluid/contrib/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@
1818
from .decoder import *
1919
from . import memory_usage_calc
2020
from .memory_usage_calc import *
21+
from . import op_frequence
22+
from .op_frequence import *
2123

22-
__all__ = decoder.__all__ + memory_usage_calc.__all__
24+
__all__ = []
25+
__all__ += decoder.__all__
26+
__all__ += memory_usage_calc.__all__
27+
__all__ += op_frequence.__all__
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
from collections import OrderedDict
17+
18+
from ..framework import Program
19+
20+
__all__ = ['op_freq_statistic']
21+
22+
23+
def op_freq_statistic(program):
24+
"""
25+
Statistics of Op frequency.
26+
27+
Args:
28+
program(Program): The current Program.
29+
30+
Returns:
31+
uni_op_freq(dict): the single op frequency.
32+
adj_2_op_freq(dict): the two adjacent ops frequency.
33+
34+
Examples:
35+
36+
>>> import paddle.fluid as fluid
37+
>>> uni_op_freq, adj_2_op_freq = fluid.contrib.op_freq_statistic(
38+
>>> fluid.default_main_program())
39+
>>> for op_type, op_num in uni_op_freq:
40+
>>> print("%s \t %d" % (op_type, op_num))
41+
>>> for op_type, op_num in adj_2_op_freq:
42+
>>> print("%s \t %d" % (op_type, op_num))
43+
44+
"""
45+
46+
if not isinstance(program, Program):
47+
raise TypeError("The input type should be Porgram."
48+
"But you passed in %s" % (type(program)))
49+
50+
uni_op_freq = OrderedDict()
51+
adj_2_op_freq = OrderedDict()
52+
op_in_ops = OrderedDict()
53+
54+
parameters = [p.name for p in program.blocks[0].all_parameters()]
55+
56+
# get uni_op_freq
57+
for op in program.global_block().ops:
58+
had_recorded = False
59+
for var_name in op.output_arg_names:
60+
if var_name in parameters:
61+
continue
62+
if not had_recorded and uni_op_freq.has_key(op.type):
63+
uni_op_freq[op.type] += 1
64+
had_recorded = True
65+
elif not had_recorded:
66+
uni_op_freq[op.type] = 1
67+
had_recorded = True
68+
69+
# get adj_2_op_freq
70+
var_gen_op = {}
71+
for op in program.global_block().ops:
72+
for var_name in op.input_arg_names:
73+
if var_name in parameters:
74+
continue
75+
if var_gen_op.has_key(var_name):
76+
assert len(var_gen_op[var_name]) > 0
77+
if op_in_ops.has_key(op.type):
78+
op_in_ops[op.type].append(var_gen_op[var_name][-1])
79+
else:
80+
op_in_ops[op.type] = [var_gen_op[var_name][-1]]
81+
else:
82+
print("Var's generate op is not found,%s, %s" %
83+
(var_name, op.type))
84+
85+
for var_name in op.output_arg_names:
86+
if var_gen_op.has_key(var_name):
87+
var_gen_op[var_name].append(op.type)
88+
else:
89+
var_gen_op[var_name] = [op.type]
90+
91+
for op, in_ops in op_in_ops.iteritems():
92+
for in_op in in_ops:
93+
op_op = in_op + "->" + op
94+
if adj_2_op_freq.has_key(op_op):
95+
adj_2_op_freq[op_op] += 1
96+
else:
97+
adj_2_op_freq[op_op] = 1
98+
99+
uni_op_freq = sorted(
100+
uni_op_freq.items(), key=lambda item: item[1], reverse=True)
101+
adj_2_op_freq = sorted(
102+
adj_2_op_freq.items(), key=lambda item: item[1], reverse=True)
103+
104+
return uni_op_freq, adj_2_op_freq

0 commit comments

Comments
 (0)