def profile(graph, run_meta=None, op_log=None, cmd='scope', options=_DEFAULT_PROFILE_OPTIONS): """Print model statistics. https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/README.md Args: graph: tf.Graph. run_meta: tensorflow::RunMetadata proto. When provided, also shows valid timing and memory information when 'select' option contains 'micros' and 'bytes'. op_log: tensorflow::tfprof::OpLog proto. users can use this proto to group together ops and use a op_type to select the group. cmd: string. Either 'op', 'scope', 'graph', 'code'. 'op' view organize outputs using operation type. (e.g. MatMul) 'scope' view organize outputs using graph node name scope. 'graph' view organize outputs using graph node inputs/outputs. 'code' view organize outputs using Python call stack. options: A dict of options. See core/profiler/g3doc/options.md. Returns: If cmd is 'scope' or 'graph', returns TFGraphNodeProto proto. If cmd is 'op' or 'code', returns TFMultiGraphNodeProto proto. Side effect: stdout/file/timeline.json depending on options['output'] """ if options == _DEFAULT_PROFILE_OPTIONS: options = TRAINABLE_VARS_PARAMS_STAT_OPTIONS.copy() # pylint: disable=protected-access op_log = tfprof_logger._merge_default_with_oplog(graph, op_log, run_meta, add_trace=cmd == 'code') # pylint: enable=protected-access opts = _build_options(options) run_meta_str = run_meta.SerializeToString() if run_meta else b'' if cmd == 'code' or cmd == 'op': tfprof_node = tfprof_output_pb2.TFMultiGraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString())) elif cmd == 'graph' or cmd == 'scope': tfprof_node = tfprof_output_pb2.TFGraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString())) else: raise errors.InvalidArgumentError(None, None, 'unknown cmd: %s\n' % cmd) return tfprof_node
def advise(graph, run_meta=None, options=_DEFAULT_ADVISE_OPTIONS): """Auto profile and advise. Builds profiles and automatically check anomalies of various aspects. For more details: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/README.md Args: graph: required tf.Graph. run_meta: optional tensorflow.RunMetadata proto. It is necessary to to support run time information profiling, such as time and memory. options: see ALL_ADVICE example above. Default checks everything. Returns: Returns AdviceProto proto """ if options == _DEFAULT_ADVISE_OPTIONS: options = ALL_ADVICE.copy() # pylint: disable=protected-access op_log = tfprof_logger._merge_default_with_oplog(graph, None, run_meta, add_trace=True) # pylint: enable=protected-access run_meta_str = run_meta.SerializeToString() if run_meta else b'' opts = _build_advisor_options(options) ret = tfprof_output_pb2.AdviceProto() ret.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), 'advise'.encode('utf-8'), opts.SerializeToString())) return ret
def advise(graph, run_meta=None, tfprof_options=ALL_ADVICE): # pylint: disable=dangerous-default-value """Auto profile and advise. Builds profiles and automatically check anormalies of various aspects. See go/tfprof or README for examples and tutorials. Args: graph: tf.Graph. run_meta: tensorflow::RunMetadata proto. Allows auto-profile time and memroy. tfprof_options: see ALL_ADVICE example above. Returns: Returns AdviceProto proto """ # pylint: disable=protected-access op_log = tfprof_logger._merge_default_with_oplog( graph, None, run_meta, add_trace=True) # pylint: enable=protected-access run_meta_str = run_meta.SerializeToString() if run_meta else b'' opts = _build_advisor_options(tfprof_options) ret = tfprof_output_pb2.AdviceProto() ret.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), 'advise'.encode('utf-8'), opts.SerializeToString())) return ret
def profile(graph=None, run_meta=None, op_log=None, cmd='scope', options=_DEFAULT_PROFILE_OPTIONS): """Profile model. Tutorials and examples can be found in: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/README.md Args: graph: tf.Graph. If None and eager execution is not enabled, use default graph. run_meta: optional tensorflow.RunMetadata proto. It is necessary to to support run time information profiling, such as time and memory. op_log: tensorflow.tfprof.OpLogProto proto. User can assign "types" to graph nodes with op_log. "types" allow user to flexibly group and account profiles using options['accounted_type_regexes']. cmd: string. Either 'op', 'scope', 'graph' or 'code'. 'op' view organizes profile using operation type. (e.g. MatMul) 'scope' view organizes profile using graph node name scope. 'graph' view organizes profile using graph node inputs/outputs. 'code' view organizes profile using Python call stack. options: A dict of options. See core/profiler/g3doc/options.md. Returns: If cmd is 'scope' or 'graph', returns GraphNodeProto proto. If cmd is 'op' or 'code', returns MultiGraphNodeProto proto. Side effect: stdout/file/timeline.json depending on options['output'] """ if not graph and not context.executing_eagerly(): graph = ops.get_default_graph() if options == _DEFAULT_PROFILE_OPTIONS: options = (option_builder.ProfileOptionBuilder. trainable_variables_parameter()) # pylint: disable=protected-access op_log = tfprof_logger.merge_default_with_oplog(graph, op_log, run_meta, add_trace=cmd == 'code') # pylint: enable=protected-access opts = _build_options(options) run_meta_str = run_meta.SerializeToString() if run_meta else b'' graph_str = _graph_string(graph) if cmd == 'code' or cmd == 'op': tfprof_node = tfprof_output_pb2.MultiGraphNodeProto() ret = print_mdl.PrintModelAnalysis(graph_str, run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString()) try: tfprof_node.ParseFromString(ret) except message.DecodeError as e: sys.stderr.write('Cannot parse returned proto: %s.\n' % e) elif cmd == 'graph' or cmd == 'scope': tfprof_node = tfprof_output_pb2.GraphNodeProto() ret = print_mdl.PrintModelAnalysis(graph_str, run_meta_str, op_log.SerializeToString(), cmd.encode('utf-8'), opts.SerializeToString()) try: tfprof_node.ParseFromString(ret) except message.DecodeError as e: sys.stderr.write('Cannot parse returned proto: %s.\n' % e) else: raise errors.InvalidArgumentError(None, None, 'unknown cmd: %s\n' % cmd) return tfprof_node
def testPrintModelAnalysis(self): opts = tfprof_options_pb2.OptionsProto() opts.max_depth = TEST_OPTIONS['max_depth'] opts.min_bytes = TEST_OPTIONS['min_bytes'] opts.min_micros = TEST_OPTIONS['min_micros'] opts.min_params = TEST_OPTIONS['min_params'] opts.min_float_ops = TEST_OPTIONS['min_float_ops'] opts.order_by = TEST_OPTIONS['order_by'] opts.step = -1 for p in TEST_OPTIONS['account_type_regexes']: opts.account_type_regexes.append(p) for p in TEST_OPTIONS['start_name_regexes']: opts.start_name_regexes.append(p) for p in TEST_OPTIONS['trim_name_regexes']: opts.trim_name_regexes.append(p) for p in TEST_OPTIONS['show_name_regexes']: opts.show_name_regexes.append(p) for p in TEST_OPTIONS['hide_name_regexes']: opts.hide_name_regexes.append(p) opts.account_displayed_op_only = TEST_OPTIONS[ 'account_displayed_op_only'] for p in TEST_OPTIONS['select']: opts.select.append(p) opts.output = TEST_OPTIONS['output'] with session.Session() as sess, ops.device('/cpu:0'): _ = self._BuildSmallModel() tfprof_pb = tfprof_output_pb2.TFGraphNodeProto() tfprof_pb.ParseFromString( print_mdl.PrintModelAnalysis( sess.graph.as_graph_def( add_shapes=True).SerializeToString(), b'', b'', b'scope', opts.SerializeToString())) expected_pb = tfprof_output_pb2.TFGraphNodeProto() text_format.Merge( r"""name: "_TFProfRoot" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 648 children { name: "Conv2D" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 2 } dim { size: 6 } dim { size: 6 } dim { size: 3 } } } input_shapes { key: 1 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW" exec_micros: 0 requested_bytes: 0 parameters: 648 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 648 children { name: "DW/Assign" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 children { name: "DW/Initializer/random_normal" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 children { name: "DW/Initializer/random_normal/RandomStandardNormal" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 4 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/mean" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/mul" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 1 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/shape" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } children { name: "DW/Initializer/random_normal/stddev" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } input_shapes { key: 1 value { dim { size: 1 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 6 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 7 } children { name: "DW/read" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 input_shapes { key: 0 value { dim { size: 6 } dim { size: 6 } dim { size: 3 } dim { size: 6 } } } accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 10 } children { name: "zeros" exec_micros: 0 requested_bytes: 0 total_exec_micros: 0 total_requested_bytes: 0 total_parameters: 0 float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 1 } float_ops: 0 total_float_ops: 0 accelerator_exec_micros: 0 cpu_exec_micros: 0 total_accelerator_exec_micros: 0 total_cpu_exec_micros: 0 run_count: 0 total_run_count: 0 total_definition_count: 13""", expected_pb) self.assertEqual(expected_pb, tfprof_pb)
def print_model_analysis(graph, run_meta=None, op_log=None, tfprof_cmd='scope', tfprof_options=TRAINABLE_VARS_PARAMS_STAT_OPTIONS): """Print model statistics. See go/tfprof or README for examples and tutorials. Run tfprof tool for help: 'bazel run third_party/tensorflow/tools/tfprof help' Args: graph: tf.Graph. run_meta: tensorflow::RunMetadata proto. When provided, also shows valid timing and memory information when 'select' option contains 'micros' and 'bytes'. op_log: tensorflow::tfprof::OpLog proto. users can use this proto to group together ops and use a op_type to select the group. tfprof_cmd: string. Either 'op', 'scope', 'graph', 'code'. 'op' view organize outputs using operation type. (e.g. MatMul) 'scope' view organize outputs using graph node name scope. 'graph' view organize outputs using graph node inputs/outputs. 'code' view organize outputs using Python call stack. tfprof_options: See 'tfprof help' for details. Returns: If tfprof_cmd is 'scope' or 'graph', returns TFGraphNodeProto proto. If tfprof_cmd is 'op' or 'code', returns TFMultiGraphNodeProto proto. Side effect: stdout/file/timeline.json depending on tfprof_options['output'] """ # pylint: disable=protected-access op_log = tfprof_logger._merge_default_with_oplog( graph, op_log, run_meta, add_trace=tfprof_cmd == 'code') # pylint: enable=protected-access opts = _build_options(tfprof_options) run_meta_str = run_meta.SerializeToString() if run_meta else b'' if tfprof_cmd == 'code' or tfprof_cmd == 'op': tfprof_node = tfprof_output_pb2.TFMultiGraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), tfprof_cmd.encode('utf-8'), opts.SerializeToString())) elif tfprof_cmd == 'graph' or tfprof_cmd == 'scope': tfprof_node = tfprof_output_pb2.TFGraphNodeProto() tfprof_node.ParseFromString( print_mdl.PrintModelAnalysis( graph.as_graph_def(add_shapes=True).SerializeToString(), run_meta_str, op_log.SerializeToString(), tfprof_cmd.encode('utf-8'), opts.SerializeToString())) else: raise errors.InvalidArgumentError( None, None, 'unknown tfprof_cmd: %s\n' % tfprof_cmd) return tfprof_node