Skip to content

Add a default delegate time scale converter #5076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions devtools/inspector/_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe

import dataclasses
import logging
import sys
Expand Down Expand Up @@ -39,6 +41,7 @@
)
from executorch.devtools.etrecord import ETRecord, parse_etrecord
from executorch.devtools.inspector._inspector_utils import (
calculate_time_scale_factor,
create_debug_handle_to_op_node_mapping,
EDGE_DIALECT_GRAPH_KEY,
EXCLUDED_COLUMNS_WHEN_PRINTING,
Expand All @@ -52,7 +55,6 @@
is_inference_output_equal,
ProgramOutput,
RESERVED_FRAMEWORK_EVENT_NAMES,
TIME_SCALE_DICT,
TimeScale,
verify_debug_data_equivalence,
)
Expand Down Expand Up @@ -799,9 +801,7 @@ class GroupedRunInstances:

# Construct the EventBlocks
event_blocks = []
scale_factor = (
TIME_SCALE_DICT[source_time_scale] / TIME_SCALE_DICT[target_time_scale]
)
scale_factor = calculate_time_scale_factor(source_time_scale, target_time_scale)
for run_signature, grouped_run_instance in run_groups.items():
run_group: OrderedDict[EventSignature, List[InstructionEvent]] = (
grouped_run_instance.events
Expand Down Expand Up @@ -966,6 +966,9 @@ def __init__(
debug_buffer_path: Debug buffer file path that contains the debug data referenced by ETDump for intermediate and program outputs.
delegate_metadata_parser: Optional function to parse delegate metadata from an Profiling Event. Expected signature of the function is:
(delegate_metadata_list: List[bytes]) -> Union[List[str], Dict[str, Any]]
delegate_time_scale_converter: Optional function to convert the time scale of delegate profiling data. If not given, use the conversion ratio of
target_time_scale/source_time_scale.
enable_module_hierarchy: Enable submodules in the operator graph. Defaults to False.

Returns:
None
Expand All @@ -980,6 +983,14 @@ def __init__(
self._source_time_scale = source_time_scale
self._target_time_scale = target_time_scale

if delegate_time_scale_converter is None:
scale_factor = calculate_time_scale_factor(
source_time_scale, target_time_scale
)
delegate_time_scale_converter = (
lambda event_name, input_time: input_time / scale_factor
)

if etrecord is None:
self._etrecord = None
elif isinstance(etrecord, ETRecord):
Expand All @@ -1002,10 +1013,10 @@ def __init__(
)

self.event_blocks = EventBlock._gen_from_etdump(
etdump,
self._source_time_scale,
self._target_time_scale,
output_buffer,
etdump=etdump,
source_time_scale=self._source_time_scale,
target_time_scale=self._target_time_scale,
output_buffer=output_buffer,
delegate_metadata_parser=delegate_metadata_parser,
delegate_time_scale_converter=delegate_time_scale_converter,
)
Expand Down
11 changes: 11 additions & 0 deletions devtools/inspector/_inspector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe

import math
from enum import Enum
from typing import Dict, List, Mapping, Optional, Tuple, TypeAlias, Union
Expand Down Expand Up @@ -63,6 +65,15 @@ class TimeScale(Enum):
}


def calculate_time_scale_factor(
source_time_scale: TimeScale, target_time_scale: TimeScale
) -> float:
"""
Calculate the factor (source divided by target) between two time scales
"""
return TIME_SCALE_DICT[source_time_scale] / TIME_SCALE_DICT[target_time_scale]


# Model Debug Output
InferenceOutput: TypeAlias = Union[
torch.Tensor, List[torch.Tensor], int, float, str, bool, None
Expand Down
32 changes: 31 additions & 1 deletion devtools/inspector/tests/inspector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe

import random
import statistics
import tempfile
import unittest
from contextlib import redirect_stdout

from typing import List
from typing import Callable, List

from unittest.mock import patch

Expand All @@ -32,6 +34,7 @@
InstructionEvent,
InstructionEventSignature,
ProfileEventSignature,
TimeScale,
)

from executorch.exir import ExportedProgram
Expand Down Expand Up @@ -88,6 +91,33 @@ def test_inspector_constructor(self):
# Because we mocked parse_etrecord() to return None, this method shouldn't be called
mock_gen_graphs_from_etrecord.assert_not_called()

def test_default_delegate_time_scale_converter(self):
# Create a context manager to patch functions called by Inspector.__init__
with patch.object(
_inspector, "parse_etrecord", return_value=None
), patch.object(
_inspector, "gen_etdump_object", return_value=None
), patch.object(
EventBlock, "_gen_from_etdump"
) as mock_gen_from_etdump, patch.object(
_inspector, "gen_graphs_from_etrecord"
), patch.object(
_inspector, "create_debug_handle_to_op_node_mapping"
):
# Call the constructor of Inspector
Inspector(
etdump_path=ETDUMP_PATH,
etrecord=ETRECORD_PATH,
source_time_scale=TimeScale.US,
target_time_scale=TimeScale.S,
)

# Verify delegate_time_scale_converter is set to be a callable
self.assertIsInstance(
mock_gen_from_etdump.call_args.get("delegate_time_scale_converter"),
Callable,
)

def test_inspector_print_data_tabular(self):
# Create a context manager to patch functions called by Inspector.__init__
with patch.object(
Expand Down
17 changes: 17 additions & 0 deletions devtools/inspector/tests/inspector_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-unsafe

import tempfile
import unittest
from typing import Dict, Tuple
Expand All @@ -23,11 +25,13 @@

from executorch.devtools.etrecord.tests.etrecord_test import TestETRecord
from executorch.devtools.inspector._inspector_utils import (
calculate_time_scale_factor,
create_debug_handle_to_op_node_mapping,
EDGE_DIALECT_GRAPH_KEY,
find_populated_event,
gen_graphs_from_etrecord,
is_inference_output_equal,
TimeScale,
)


Expand Down Expand Up @@ -170,6 +174,19 @@ def test_is_inference_output_equal_returns_true_for_same_strs(self):
)
)

def test_calculate_time_scale_factor_second_based(self):
self.assertEqual(
calculate_time_scale_factor(TimeScale.NS, TimeScale.MS), 1000000
)
self.assertEqual(
calculate_time_scale_factor(TimeScale.MS, TimeScale.NS), 1 / 1000000
)

def test_calculate_time_scale_factor_cycles(self):
self.assertEqual(
calculate_time_scale_factor(TimeScale.CYCLES, TimeScale.CYCLES), 1
)


def gen_mock_operator_graph_with_expected_map() -> (
Tuple[OperatorGraph, Dict[int, OperatorNode]]
Expand Down
Loading