|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | +import unittest |
| 9 | +from typing import List, Optional, Tuple, Union |
| 10 | + |
| 11 | +import executorch.sdk.etdump.schema_flatcc as flatcc |
| 12 | +from executorch.sdk.etdb.inspector import ( |
| 13 | + Event, |
| 14 | + EventBlock, |
| 15 | + PerfData, |
| 16 | + ProfileEventSignature, |
| 17 | +) |
| 18 | +from executorch.sdk.etdump.schema_flatcc import ETDumpFlatCC |
| 19 | + |
| 20 | + |
| 21 | +class TestEventBlock(unittest.TestCase): |
| 22 | + |
| 23 | + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Test Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | + @staticmethod |
| 25 | + def _gen_sample_profile_event( |
| 26 | + name: str, |
| 27 | + instruction_id: int, |
| 28 | + time: Tuple[int, int], |
| 29 | + delegate_debug_id: Optional[Union[int, str]] = None, |
| 30 | + ) -> flatcc.ProfileEvent: |
| 31 | + """ |
| 32 | + Helper for generating test ProfileEvents |
| 33 | +
|
| 34 | + Notably: |
| 35 | + - the timestamp is specified as a tuple of two separate integers |
| 36 | + - delegate_debug_id takes either the str or int representation |
| 37 | + - chain_idx is auto-populated to 0 |
| 38 | + """ |
| 39 | + delegate_debug_id_int = ( |
| 40 | + delegate_debug_id if isinstance(delegate_debug_id, int) else -1 |
| 41 | + ) |
| 42 | + delegate_debug_id_str = ( |
| 43 | + delegate_debug_id if isinstance(delegate_debug_id, str) else "" |
| 44 | + ) |
| 45 | + return flatcc.ProfileEvent( |
| 46 | + name, |
| 47 | + 0, |
| 48 | + instruction_id, |
| 49 | + delegate_debug_id_int, |
| 50 | + delegate_debug_id_str, |
| 51 | + start_time=time[0], |
| 52 | + end_time=time[1], |
| 53 | + ) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def _get_sample_etdump_flatcc() -> flatcc.ETDumpFlatCC: |
| 57 | + """ |
| 58 | + Helper for getting a sample ETDumpFlatCC object with 3 RunData: |
| 59 | + - run_data_1 has a signature with just profile_1 |
| 60 | + - run_data_2 has the same signature with run_data_1, but differnt times |
| 61 | + - run_data_3 has a signature with both (profile_1, profile_2) |
| 62 | + """ |
| 63 | + profile_event_1 = TestEventBlock._gen_sample_profile_event( |
| 64 | + name="profile_1", instruction_id=1, time=(0, 1), delegate_debug_id=100 |
| 65 | + ) |
| 66 | + run_data_1 = flatcc.RunData( |
| 67 | + name="run_data_1", |
| 68 | + allocators=[], |
| 69 | + events=[ |
| 70 | + flatcc.Event( |
| 71 | + allocation_event=None, |
| 72 | + debug_event=None, |
| 73 | + profile_event=profile_event_1, |
| 74 | + ) |
| 75 | + ], |
| 76 | + ) |
| 77 | + profile_event_2 = TestEventBlock._gen_sample_profile_event( |
| 78 | + name="profile_1", instruction_id=1, time=(2, 4), delegate_debug_id=100 |
| 79 | + ) |
| 80 | + run_data_2 = flatcc.RunData( |
| 81 | + name="run_data_2", |
| 82 | + allocators=[], |
| 83 | + events=[ |
| 84 | + flatcc.Event( |
| 85 | + allocation_event=None, |
| 86 | + debug_event=None, |
| 87 | + profile_event=profile_event_2, |
| 88 | + ) |
| 89 | + ], |
| 90 | + ) |
| 91 | + |
| 92 | + profile_event_3 = TestEventBlock._gen_sample_profile_event( |
| 93 | + name="profile_1", instruction_id=1, time=(5, 6), delegate_debug_id=100 |
| 94 | + ) |
| 95 | + profile_event_4 = TestEventBlock._gen_sample_profile_event( |
| 96 | + name="profile_2", instruction_id=2, time=(7, 8), delegate_debug_id=100 |
| 97 | + ) |
| 98 | + run_data_3 = flatcc.RunData( |
| 99 | + name="run_data_3", |
| 100 | + allocators=[], |
| 101 | + events=[ |
| 102 | + flatcc.Event( |
| 103 | + allocation_event=None, |
| 104 | + debug_event=None, |
| 105 | + profile_event=profile_event_3, |
| 106 | + ), |
| 107 | + flatcc.Event( |
| 108 | + allocation_event=None, |
| 109 | + debug_event=None, |
| 110 | + profile_event=profile_event_4, |
| 111 | + ), |
| 112 | + ], |
| 113 | + ) |
| 114 | + |
| 115 | + return ETDumpFlatCC(version=0, run_data=[run_data_1, run_data_2, run_data_3]) |
| 116 | + |
| 117 | + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 118 | + |
| 119 | + def test_gen_from_etdump(self) -> None: |
| 120 | + """ |
| 121 | + Test "e2e" generation of EventBlocks given an ETDump |
| 122 | + - Generated via EventBock.gen_from_etdump |
| 123 | +
|
| 124 | + Specifically it tests for external correctness: |
| 125 | + - Correct number of EventBlocks |
| 126 | + - Correct number of Events and Raw Data values (iterations) |
| 127 | + """ |
| 128 | + etdump: ETDumpFlatCC = TestEventBlock._get_sample_etdump_flatcc() |
| 129 | + blocks: List[EventBlock] = EventBlock._gen_from_etdump(etdump) |
| 130 | + |
| 131 | + self.assertEqual(len(blocks), 2, f"Expected 2 runs, got {len(blocks)}") |
| 132 | + |
| 133 | + # One EventBlock should have 1 event with 2 iterations |
| 134 | + # The other EventBlock should have 2 events with 1 iterations |
| 135 | + run_counts = { |
| 136 | + (len(block.events), len(block.events[0].perf_data.raw)) for block in blocks |
| 137 | + } |
| 138 | + self.assertSetEqual(run_counts, {(1, 2), (2, 1)}) |
| 139 | + |
| 140 | + def test_inspector_event_generation(self) -> None: |
| 141 | + """ |
| 142 | + Test Inspector.Event derivation from various ProfileEvent cases |
| 143 | + - Non Delegated |
| 144 | + - Delegate with Int Debug ID |
| 145 | + - Delegate with String Debug ID |
| 146 | + """ |
| 147 | + |
| 148 | + def _test_profile_event_generation( |
| 149 | + name: str, |
| 150 | + instruction_id: int, |
| 151 | + delegate_debug_id_int: Optional[int] = None, |
| 152 | + delegate_debug_id_str: Optional[str] = None, |
| 153 | + ) -> None: |
| 154 | + """ |
| 155 | + Helper function for testing that the provided ProfileEvent fields are |
| 156 | + properly translated to Inspector.ProfileEventSignature and Inspector.Event |
| 157 | + """ |
| 158 | + delegate_debug_id = delegate_debug_id_int or delegate_debug_id_str |
| 159 | + profile_event: flatcc.ProfileEvent = ( |
| 160 | + TestEventBlock._gen_sample_profile_event( |
| 161 | + name, |
| 162 | + instruction_id, |
| 163 | + (0, 1), |
| 164 | + delegate_debug_id, |
| 165 | + ) |
| 166 | + ) |
| 167 | + |
| 168 | + # Test Signature Generation |
| 169 | + signature = ProfileEventSignature._gen_from_event(profile_event) |
| 170 | + expected_signature = ProfileEventSignature( |
| 171 | + name, instruction_id, delegate_debug_id_int, delegate_debug_id_str |
| 172 | + ) |
| 173 | + self.assertEqual(signature, expected_signature) |
| 174 | + |
| 175 | + # Test Event Generation |
| 176 | + durations = [10, 20, 30] |
| 177 | + profile_events: List[flatcc.ProfileEvent] = [ |
| 178 | + TestEventBlock._gen_sample_profile_event( |
| 179 | + name, |
| 180 | + instruction_id, |
| 181 | + (0, 10), |
| 182 | + delegate_debug_id, |
| 183 | + ) |
| 184 | + for time in durations |
| 185 | + ] |
| 186 | + event = Event._gen_from_profile_events(signature, profile_events) |
| 187 | + |
| 188 | + is_delegated = delegate_debug_id is not None |
| 189 | + expected_event = Event( |
| 190 | + str(delegate_debug_id) if is_delegated else name, |
| 191 | + PerfData([float(duration) / 1000 for duration in durations]), |
| 192 | + instruction_id=signature.instruction_id, |
| 193 | + delegate_debug_identifier=delegate_debug_id, |
| 194 | + is_delegated_op=is_delegated, |
| 195 | + ) |
| 196 | + self.assertEqual(event, expected_event) |
| 197 | + |
| 198 | + # Non Delegated |
| 199 | + _test_profile_event_generation("non-delegate", 1) |
| 200 | + |
| 201 | + # Delegate with Int Debug ID |
| 202 | + _test_profile_event_generation("delegate", 1, 100) |
| 203 | + |
| 204 | + # Delegate with String Debug ID |
| 205 | + _test_profile_event_generation("delegate", 1, None, "identifier") |
0 commit comments