Ejemplo n.º 1
0
  def data_impl(self, request):
    """Retrieves and processes the tool data for a run and a host.

    Args:
      request: XMLHttpRequest

    Returns:
      A string that can be served to the frontend tool or None if tool,
        run or host is invalid.
    """
    run = request.args.get('run')
    tool = request.args.get('tag')
    host = request.args.get('host')
    run_dir = self._run_dir(run)
    # Profile plugin "run" is the last component of run dir.
    profile_run = os.path.basename(run_dir)

    if tool not in TOOLS:
      return None

    self.start_grpc_stub_if_necessary()
    if tool == 'trace_viewer@' and self.stub is not None:
      from tensorflow.core.profiler import profiler_analysis_pb2
      grpc_request = profiler_analysis_pb2.ProfileSessionDataRequest()
      grpc_request.repository_root = os.path.dirname(run_dir)
      grpc_request.session_id = profile_run
      grpc_request.tool_name = 'trace_viewer'
      # Remove the trailing dot if present
      grpc_request.host_name = host.rstrip('.')

      grpc_request.parameters['resolution'] = request.args.get('resolution')
      if request.args.get('start_time_ms') is not None:
        grpc_request.parameters['start_time_ms'] = request.args.get(
            'start_time_ms')
      if request.args.get('end_time_ms') is not None:
        grpc_request.parameters['end_time_ms'] = request.args.get('end_time_ms')
      grpc_response = self.stub.GetSessionToolData(grpc_request)
      return grpc_response.output

    if tool not in TOOLS:
      return None
    tool_name = str(host) + TOOLS[tool]
    asset_path = os.path.join(run_dir, tool_name)
    raw_data = None
    try:
      with tf.io.gfile.GFile(asset_path, 'rb') as f:
        raw_data = f.read()
    except tf.errors.NotFoundError:
      logger.warn('Asset path %s not found', asset_path)
    except tf.errors.OpError as e:
      logger.warn("Couldn't read asset path: %s, OpError %s", asset_path, e)

    if raw_data is None:
      return None
    if tool == 'trace_viewer':
      return process_raw_trace(raw_data)
    if tool in _RAW_DATA_TOOLS:
      return raw_data
    return None
Ejemplo n.º 2
0
    def data_impl(self, request):
        """Retrieves and processes the tool data for a run and a host.

    Args:
      request: XMLHttpRequest

    Returns:
      A string that can be served to the frontend tool or None if tool,
        run or host is invalid.
    """
        run = request.args.get('run')
        tool = request.args.get('tag')
        host = request.args.get('host')
        tqx = request.args.get('tqx')
        run_dir = self._run_dir(run)
        # Profile plugin "run" is the last component of run dir.
        profile_run = os.path.basename(run_dir)

        if tool not in TOOLS and not _use_xplane(tool):
            return None, None

        self.start_grpc_stub_if_necessary()
        if tool == 'trace_viewer@' and self.stub is not None:
            # Streaming trace viewer needs profiler_analysis service, which is only
            # supported in Cloud TPU. This code is unused when data was produced by
            # open-source TensorFlow. Only import the library when needed.
            # pylint: disable=g-import-not-at-top
            # pylint: disable=g-direct-tensorflow-import
            from tensorflow.core.profiler import profiler_analysis_pb2
            # pylint: enable=g-import-not-at-top
            # pylint: enable=g-direct-tensorflow-import
            grpc_request = profiler_analysis_pb2.ProfileSessionDataRequest()
            grpc_request.repository_root = os.path.dirname(run_dir)
            grpc_request.session_id = profile_run
            grpc_request.tool_name = 'trace_viewer'
            # Remove the trailing dot if present
            grpc_request.host_name = host.rstrip('.')

            grpc_request.parameters['resolution'] = request.args.get(
                'resolution', 8000)
            if request.args.get('start_time_ms') is not None:
                grpc_request.parameters['start_time_ms'] = request.args.get(
                    'start_time_ms')
            if request.args.get('end_time_ms') is not None:
                grpc_request.parameters['end_time_ms'] = request.args.get(
                    'end_time_ms')
            grpc_response = self.stub.GetSessionToolData(grpc_request)
            return grpc_response.output, None

        asset_path = os.path.join(run_dir, _make_filename(host, tool))

        data, content_encoding = None, None
        if _use_xplane(tool):
            if host == ALL_HOSTS:
                file_pattern = _make_filename('*', 'xplane')
                try:
                    asset_paths = tf.io.gfile.glob(
                        os.path.join(run_dir, file_pattern))
                except tf.errors.OpError as e:
                    logger.warning(
                        'Cannot read asset directory: %s, OpError %s', run_dir,
                        e)
            else:
                asset_paths = [asset_path]

            try:
                data = convert.xspace_to_tool_data(asset_paths, tool, tqx)
            except AttributeError:
                logger.warning(
                    'XPlane converters are available after Tensorflow 2.4')
            return data, content_encoding

        raw_data = None
        try:
            with tf.io.gfile.GFile(asset_path, 'rb') as f:
                raw_data = f.read()
        except tf.errors.NotFoundError:
            logger.warning('Asset path %s not found', asset_path)
        except tf.errors.OpError as e:
            logger.warning("Couldn't read asset path: %s, OpError %s",
                           asset_path, e)

        if raw_data is None:
            return None, None

        if tool in _RAW_DATA_TOOLS:
            data = raw_data
            if tool[-1] == '#':
                content_encoding = 'gzip'
        else:
            data = convert.tool_proto_to_tool_data(raw_data, tool, tqx)
        return data, content_encoding
Ejemplo n.º 3
0
    def data_impl(self, request):
        """Retrieves and processes the tool data for a run and a host.

    Args:
      request: XMLHttpRequest

    Returns:
      A string that can be served to the frontend tool or None if tool,
        run or host is invalid.
    """
        run = request.args.get('run')
        tool = request.args.get('tag')
        host = request.args.get('host')
        tqx = request.args.get('tqx')
        run_dir = self._run_dir(run)
        # Profile plugin "run" is the last component of run dir.
        profile_run = os.path.basename(run_dir)

        if tool not in TOOLS:
            return None, None

        self.start_grpc_stub_if_necessary()
        if tool == 'trace_viewer@' and self.stub is not None:
            # Streaming trace viewer needs profiler_analysis service, which is only
            # supported in Cloud TPU. This code is unused when data was produced by
            # open-source TensorFlow. Only import the library when needed.
            # pylint: disable=g-import-not-at-top
            # pylint: disable=g-direct-tensorflow-import
            from tensorflow.core.profiler import profiler_analysis_pb2
            # pylint: enable=g-import-not-at-top
            # pylint: enable=g-direct-tensorflow-import
            grpc_request = profiler_analysis_pb2.ProfileSessionDataRequest()
            grpc_request.repository_root = os.path.dirname(run_dir)
            grpc_request.session_id = profile_run
            grpc_request.tool_name = 'trace_viewer'
            # Remove the trailing dot if present
            grpc_request.host_name = host.rstrip('.')

            grpc_request.parameters['resolution'] = request.args.get(
                'resolution')
            if request.args.get('start_time_ms') is not None:
                grpc_request.parameters['start_time_ms'] = request.args.get(
                    'start_time_ms')
            if request.args.get('end_time_ms') is not None:
                grpc_request.parameters['end_time_ms'] = request.args.get(
                    'end_time_ms')
            grpc_response = self.stub.GetSessionToolData(grpc_request)
            return grpc_response.output, None

        asset_path = os.path.join(run_dir, _make_filename(host, tool))
        raw_data = None
        try:
            with tf.io.gfile.GFile(asset_path, 'rb') as f:
                raw_data = f.read()
        except tf.errors.NotFoundError:
            logger.warning('Asset path %s not found', asset_path)
        except tf.errors.OpError as e:
            logger.warning("Couldn't read asset path: %s, OpError %s",
                           asset_path, e)

        if raw_data is None:
            return None, None
        data, content_encoding = None, None
        if tool == 'trace_viewer':
            data = process_raw_trace(raw_data)
        elif tool == 'tensorflow_stats':
            if tqx == 'out:csv;':
                data = tf_stats_proto_to_gviz.to_csv(raw_data)
            else:
                data = tf_stats_proto_to_gviz.to_json(raw_data)
        elif tool == 'overview_page@':
            data = overview_page_proto_to_gviz.to_json(raw_data)
        elif tool == 'input_pipeline_analyzer@':
            data = input_pipeline_proto_to_gviz.to_json(raw_data)
        elif tool == 'kernel_stats':
            if tqx == 'out:csv;':
                data = kernel_stats_proto_to_gviz.to_csv(raw_data)
            else:
                data = kernel_stats_proto_to_gviz.to_json(raw_data)
        elif tool == 'memory_profile':
            logger.info('Accessing memory_profile data')
            data = raw_data
            content_encoding = 'gzip'
        elif tool in _RAW_DATA_TOOLS:
            data = raw_data
            if tool[-1] == '#':
                content_encoding = 'gzip'
        return data, content_encoding