Exemplo n.º 1
0
def _build_options(tfprof_options):
    """Build tfprof.OptionsProto.

  Args:
    tfprof_options: A dictionary of options.
  Returns:
    tfprof.OptionsProto.
  """
    opts = tfprof_options_pb2.OptionsProto()
    opts.max_depth = tfprof_options.get('max_depth', 10)
    opts.min_bytes = tfprof_options.get('min_bytes', 0)
    opts.min_micros = tfprof_options.get('min_micros', 0)
    opts.min_params = tfprof_options.get('min_params', 0)
    opts.min_float_ops = tfprof_options.get('min_float_ops', 0)
    opts.min_occurrence = tfprof_options.get('min_occurrence', 0)

    opts.step = tfprof_options.get('step', -1)

    opts.order_by = tfprof_options.get('order_by', 'name')

    for p in tfprof_options.get('account_type_regexes', []):
        opts.account_type_regexes.append(p)
    for p in tfprof_options.get('start_name_regexes', []):
        opts.start_name_regexes.append(p)
    for p in tfprof_options.get('trim_name_regexes', []):
        opts.trim_name_regexes.append(p)
    for p in tfprof_options.get('show_name_regexes', []):
        opts.show_name_regexes.append(p)
    for p in tfprof_options.get('hide_name_regexes', []):
        opts.hide_name_regexes.append(p)
    opts.account_displayed_op_only = tfprof_options.get(
        'account_displayed_op_only', False)

    for p in tfprof_options.get('select', []):
        opts.select.append(p)

    opts.output = tfprof_options.get('output', 'stdout')
    opts.dump_to_file = tfprof_options.get('dump_to_file', '')

    return opts
def print_model_analysis(graph,
                         run_meta=None,
                         op_log=None,
                         tfprof_cmd='scope',
                         tfprof_options=TRAINABLE_VARS_PARAMS_STAT_OPTIONS):
  """Print model statistics.

    Prints the model statistics to stdout. Also returns the results
    in a TFGraphNodeProto proto. See go/tfprof or run tfprof tool:
    'bazel run third_party/tensorflow/tools/tfprof help'

    Examples:
      Show the parameter/shape statistics of tf.trainable_variables().
        print_model_analysis(sess.graph).

      Show number of float ops. Only ops with RegisterStatistics defined
      are counted.
        show_float_op_opts = model_analyzer.FLOAT_OPS_OPTIONS
        print_model_analysis(sess.graph, tfprof_options=show_float_op_opts)

  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 'scope', 'graph', 'code'.
                'scope' view organize outputs using ops' name scope.
                'graph' view organize outputs using op's 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 'code', returns TFCodeNodeProto proto.
    Side effect: a formatted output to stdout.
  """
  # 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 = tfprof_options_pb2.OptionsProto()
  opts.max_depth = tfprof_options['max_depth']
  opts.min_bytes = tfprof_options['min_bytes']
  opts.min_micros = tfprof_options['min_micros']
  opts.min_params = tfprof_options['min_params']
  opts.min_float_ops = tfprof_options['min_float_ops']
  for p in tfprof_options['device_regexes']:
    opts.device_regexes.append(p)
  opts.order_by = tfprof_options['order_by']
  for p in tfprof_options['account_type_regexes']:
    opts.account_type_regexes.append(p)
  for p in tfprof_options['start_name_regexes']:
    opts.start_name_regexes.append(p)
  for p in tfprof_options['trim_name_regexes']:
    opts.trim_name_regexes.append(p)
  for p in tfprof_options['show_name_regexes']:
    opts.show_name_regexes.append(p)
  for p in tfprof_options['hide_name_regexes']:
    opts.hide_name_regexes.append(p)
  opts.account_displayed_op_only = tfprof_options['account_displayed_op_only']
  for p in tfprof_options['select']:
    opts.select.append(p)
  opts.output = tfprof_options['output']
  opts.dump_to_file = tfprof_options['dump_to_file']

  run_meta_str = run_meta.SerializeToString() if run_meta else b''

  if tfprof_cmd == 'code':
    tfprof_node = tfprof_output_pb2.TFCodeNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.PrintModelAnalysis(
            graph.as_graph_def().SerializeToString(),
            run_meta_str,
            op_log.SerializeToString(),
            tfprof_cmd.encode('utf-8'),
            opts.SerializeToString()))
  else:
    tfprof_node = tfprof_output_pb2.TFGraphNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.PrintModelAnalysis(
            graph.as_graph_def().SerializeToString(),
            run_meta_str,
            op_log.SerializeToString(),
            tfprof_cmd.encode('utf-8'),
            opts.SerializeToString()))

  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']
        for p in TEST_OPTIONS['device_regexes']:
            opts.device_regexes.append(p)
        opts.order_by = TEST_OPTIONS['order_by']
        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.viz = TEST_OPTIONS['viz']

        with tf.Session() as sess, tf.device('/cpu:0'):
            _ = self._BuildSmallModel()
            tfprof_pb = tfprof_output_pb2.TFProfNode()
            tfprof_pb.ParseFromString(
                print_mdl.PrintModelAnalysis(
                    sess.graph.as_graph_def().SerializeToString(), b'', b'',
                    b'scope', opts.SerializeToString()))

            expected_pb = tfprof_output_pb2.TFProfNode()
            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
            device: "/device:CPU:0"
            float_ops: 0
            total_float_ops: 0
          }
          children {
            name: "DW"
            exec_micros: 0
            requested_bytes: 0
            parameters: 648
            total_exec_micros: 0
            total_requested_bytes: 0
            total_parameters: 648
            device: "/device:CPU:0"
            children {
              name: "DW/Assign"
              exec_micros: 0
              requested_bytes: 0
              total_exec_micros: 0
              total_requested_bytes: 0
              total_parameters: 0
              device: "/device:CPU:0"
              float_ops: 0
              total_float_ops: 0
            }
            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
                device: "/device:CPU: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
                  device: "/device:CPU:0"
                  float_ops: 0
                  total_float_ops: 0
                }
                children {
                  name: "DW/Initializer/random_normal/mean"
                  exec_micros: 0
                  requested_bytes: 0
                  total_exec_micros: 0
                  total_requested_bytes: 0
                  total_parameters: 0
                  device: "/device:CPU:0"
                  float_ops: 0
                  total_float_ops: 0
                }
                children {
                  name: "DW/Initializer/random_normal/mul"
                  exec_micros: 0
                  requested_bytes: 0
                  total_exec_micros: 0
                  total_requested_bytes: 0
                  total_parameters: 0
                  device: "/device:CPU:0"
                  float_ops: 0
                  total_float_ops: 0
                }
                children {
                  name: "DW/Initializer/random_normal/shape"
                  exec_micros: 0
                  requested_bytes: 0
                  total_exec_micros: 0
                  total_requested_bytes: 0
                  total_parameters: 0
                  device: "/device:CPU:0"
                  float_ops: 0
                  total_float_ops: 0
                }
                children {
                  name: "DW/Initializer/random_normal/stddev"
                  exec_micros: 0
                  requested_bytes: 0
                  total_exec_micros: 0
                  total_requested_bytes: 0
                  total_parameters: 0
                  device: "/device:CPU:0"
                  float_ops: 0
                  total_float_ops: 0
                }
                float_ops: 0
                total_float_ops: 0
              }
              float_ops: 0
              total_float_ops: 0
            }
            children {
              name: "DW/read"
              exec_micros: 0
              requested_bytes: 0
              total_exec_micros: 0
              total_requested_bytes: 0
              total_parameters: 0
              device: "/device:CPU:0"
              float_ops: 0
              total_float_ops: 0
            }
            float_ops: 0
            total_float_ops: 0
          }
          children {
            name: "zeros"
            exec_micros: 0
            requested_bytes: 0
            total_exec_micros: 0
            total_requested_bytes: 0
            total_parameters: 0
            device: "/device:CPU:0"
            float_ops: 0
            total_float_ops: 0
          }
          float_ops: 0
          total_float_ops: 0""", expected_pb)
            self.assertEqual(expected_pb, tfprof_pb)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
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 = tfprof_options_pb2.OptionsProto()
  opts.max_depth = tfprof_options['max_depth']
  opts.min_bytes = tfprof_options['min_bytes']
  opts.min_micros = tfprof_options['min_micros']
  opts.min_params = tfprof_options['min_params']
  opts.min_float_ops = tfprof_options['min_float_ops']
  if 'min_occurrence' in tfprof_options:
    opts.min_occurrence = tfprof_options['min_occurrence']
  else:
    opts.min_occurrence = 0

  opts.order_by = tfprof_options['order_by']
  for p in tfprof_options['account_type_regexes']:
    opts.account_type_regexes.append(p)
  for p in tfprof_options['start_name_regexes']:
    opts.start_name_regexes.append(p)
  for p in tfprof_options['trim_name_regexes']:
    opts.trim_name_regexes.append(p)
  for p in tfprof_options['show_name_regexes']:
    opts.show_name_regexes.append(p)
  for p in tfprof_options['hide_name_regexes']:
    opts.hide_name_regexes.append(p)
  opts.account_displayed_op_only = tfprof_options['account_displayed_op_only']
  for p in tfprof_options['select']:
    opts.select.append(p)
  opts.output = tfprof_options['output']
  opts.dump_to_file = tfprof_options['dump_to_file']

  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().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().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