Esempio n. 1
0
def produce_timeline_profile(profile_dir, resources_dir, profile_cnt, options):
  """Produces a timeline profile."""
  timeline_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + 'timeline')
  if not os.path.isfile(timeline_path):
    profiles = {}
    log_path = os.path.join(PROFILER_TMP_DIR, PROFILER_TMP_NAME)
    options['output'] = 'timeline:outfile=' + log_path
    opts = model_analyzer._build_options(options)  # pylint: disable=protected-access
    for idx, prof in enumerate(gfile.ListDirectory(profile_dir)):
      prof_file = os.path.join(profile_dir, prof)
      if not os.path.isfile(prof_file):
        continue
      chosen_profile = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + 'timeline_' + prof)
      profiles[prof] = chosen_profile
      if os.path.isfile(chosen_profile):
        if idx == 0:
          target_ts = get_timestamp(chosen_profile)
        continue
      tf.logging.info("Parse profile context %r" % prof_file)
      remove_tmp_files()
      # Parse profile context
      ProfilerFromFile(prof_file.encode('utf-8'))
      pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString())
      DeleteProfiler()
      if idx == 0:
        prof_names = get_informative_profiles(PROFILER_TMP_DIR, profile_cnt)
        target_ts = get_timestamp(os.path.join(PROFILER_TMP_DIR, prof_names[0]))
      else:
        prof_names = get_profiles_by_timestamp(PROFILER_TMP_DIR, target_ts, profile_cnt)
      tf.logging.info("Choose %r as the most informative profile context for %r" % (prof_names, prof))
      gen_profile([os.path.join(PROFILER_TMP_DIR, name) for name in prof_names], chosen_profile)
    merge_profiles(profiles, timeline_path)
  return load_profile(timeline_path)
  def serialize_data(self, graph, run_metadata_list):
    devnull = open(os.devnull, 'w')
    f = io.BytesIO()
    with stdout_redirector(f): #remove 'Parsing Inputs...'
      with RedirectStdStreams(stderr=devnull): #this stops a meaningless error on stderr
        profiler = Profiler(graph)

    for i, run_metadata in enumerate(run_metadata_list):
      profiler.add_step(i+1, run_metadata)
    
    #use these to print to stdout
#     profiler.profile_name_scope(ALL_OPTIONS_PRINT)

    all_opts = _build_options(ALL_OPTIONS)
    perf_opts = _build_options(PERF_OPTIONS)
    
    with stderr_redirector(f): #this stops some meaningless errors on stderr
      self._scope_all_stats_str = print_mdl.Profile('scope'.encode('utf-8'), all_opts.SerializeToString())
      self._op_all_stats_str = print_mdl.Profile('op'.encode('utf-8'), all_opts.SerializeToString())
      self._op_perf_stats_str = print_mdl.Profile('op'.encode('utf-8'), perf_opts.SerializeToString())
Esempio n. 3
0
def produce_other_profile(profile_dir, resources_dir, profile_cnt, options):
  other_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + options['view'])
  options['output'] = 'file:outfile=' + other_path
  opts = model_analyzer._build_options(options)  # pylint: disable=protected-access
  # Create profiler from the first profile context.
  profile_context = get_first_profile_context(profile_dir)
  ProfilerFromFile(profile_context.encode('utf-8'))
  pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString())
  DeleteProfiler()

  return load_profile(other_path)
Esempio n. 4
0
    def profile_operations(self, options):
        """Profile the statistics of the Operation types (e.g. MatMul, Conv2D).

    Args:
      options: A dict of options. See core/profiler/g3doc/options.md.
    Returns:
      a MultiGraphNodeProto that records the results.
    """
        opts = _build_options(options)
        tfprof_node = tfprof_output_pb2.MultiGraphNodeProto()
        tfprof_node.ParseFromString(
            print_mdl.Profile('op'.encode('utf-8'), opts.SerializeToString()))
        return tfprof_node
Esempio n. 5
0
  def advise(self, options=ALL_ADVICE):  # pylint: disable=dangerous-default-value
    """Automatically detect problems and generate reports.

    Args:
      options: A dict of options.
    Returns:
      A Advise proto that conains the reports from all checkers.
    """
    advise_pb = tfprof_output_pb2.AdviceProto()
    opts = _build_advisor_options(options)
    advise_pb.ParseFromString(
        print_mdl.Profile('advise'.encode('utf-8'), opts.SerializeToString()))
    return advise_pb
Esempio n. 6
0
  def profile_graph(self, options):
    """Profile the statistics of graph nodes, organized by dataflow graph.

    Args:
      options: A dict of profiler options.
    Returns:
      a TFGraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.TFGraphNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.Profile('graph'.encode('utf-8'), opts.SerializeToString()))
    return tfprof_node
Esempio n. 7
0
  def profile_name_scope(self, options):
    """Profile the statistics of graph nodes, organized by name scope.

    Args:
      options: A dict of options. See core/profiler/g3doc/options.md.
    Returns:
      a GraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.GraphNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.Profile('scope'.encode('utf-8'), opts.SerializeToString()))
    return tfprof_node
Esempio n. 8
0
  def profile_python_codes(self, options):
    """Profile the statistics of the Python codes.

      Hint: set options['show_name_regexes'] = ['.*my_code.py.*']

    Args:
      options: A dict of profiler options.
    Returns:
      a TFMultiGraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.TFMultiGraphNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.Profile('code'.encode('utf-8'), opts.SerializeToString()))
    return tfprof_node
Esempio n. 9
0
  def profile_graph(self, options):
    """Profile the statistics of graph nodes, organized by dataflow graph.

    Args:
      options: A dict of options. See core/profiler/g3doc/options.md.
    Returns:
      a GraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.GraphNodeProto()
    try:
      tfprof_node.ParseFromString(
          print_mdl.Profile('graph'.encode('utf-8'), opts.SerializeToString()))
    except message.DecodeError as e:
      sys.stderr.write('Cannot parse returned proto: %s.\n' % e)
    return tfprof_node
Esempio n. 10
0
  def profile_operations(self, options):
    """Profile the statistics of the Operation types (e.g. MatMul, Conv2D).

    Args:
      options: A dict of options. See core/profiler/g3doc/options.md.
    Returns:
      a MultiGraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.MultiGraphNodeProto()
    try:
      tfprof_node.ParseFromString(
          print_mdl.Profile('op'.encode('utf-8'), opts.SerializeToString()))
    except message.DecodeError as e:
      sys.stderr.write('Cannot parse returned proto: %s.\n' % e)
    return tfprof_node
Esempio n. 11
0
  def profile_python(self, options):
    """Profile the statistics of the Python codes.

      By default, it shows the call stack from root. To avoid
      redundant output, you may use options to filter as below
        options['show_name_regexes'] = ['.*my_code.py.*']

    Args:
      options: A dict of options. See core/profiler/g3doc/options.md.
    Returns:
      a MultiGraphNodeProto that records the results.
    """
    opts = _build_options(options)
    tfprof_node = tfprof_output_pb2.MultiGraphNodeProto()
    tfprof_node.ParseFromString(
        print_mdl.Profile('code'.encode('utf-8'), opts.SerializeToString()))
    return tfprof_node
Esempio n. 12
0
def produce_pprof_profile(profile_dir, resources_dir, profile_cnt, options):
  image_path = os.path.join(resources_dir, PROFILER_PPROF_IMAGE_NAME)
  if not os.path.isfile(image_path):
    tmp_path = os.path.join(resources_dir, PROFILER_COMMON_PREFIX + options['view'])
    if not os.path.isfile(tmp_path):
      options['view'] = 'code'
      options['output'] = 'pprof:outfile=' + tmp_path
      opts = model_analyzer._build_options(options)  # pylint: disable=protected-access
      # Create profiler from the first profile context.
      profile_context = get_first_profile_context(profile_dir)
      ProfilerFromFile(profile_context.encode('utf-8'))
      pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString())
      DeleteProfiler()

    """Produces a pprof profile."""
    subprocess.call([
        'pprof', '-svg', '--nodecount=100', '--sample_index=1',
        '-output={}'.format(image_path), tmp_path 
        ])
  return load_profile(image_path)
Esempio n. 13
0
def handle_profile_api():
  """Handles profile API requests."""
  options = json.loads(flask.request.args.get('options'))

  # Determine view and output format.
  if options['view'] == 'pprof':
    output_format = 'pprof'
    options['view'] = 'code'
  elif options['view'] == 'graph':
    output_format = 'timeline'
  else:
    output_format = 'file'

  # Produce a profile.
  options['output'] = output_format + ':outfile=' + PROFILER_LOG_PATH
  opts = model_analyzer._build_options(options)  # pylint: disable=protected-access
  pwtf.Profile(options['view'].encode('utf-8'), opts.SerializeToString())

  if output_format == 'pprof':
    return produce_pprof_profile()
  elif output_format == 'timeline':
    return produce_timeline_profile()
  else:
    return load_profile(PROFILER_LOG_PATH)