|
| 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