@@ -312,6 +312,9 @@ class Event:
312312 _instruction_id : Optional [int ] = None
313313
314314 _delegate_metadata_parser : Optional [Callable [[List [str ]], Dict [str , Any ]]] = None
315+ _delegate_time_scale_converter : Optional [
316+ Callable [[Union [int , str ], Union [int , float ]], Union [int , float ]]
317+ ] = None
315318
316319 @cached_property
317320 def delegate_debug_metadatas (self ) -> Union [List [str ], Dict [str , Any ]]:
@@ -391,6 +394,9 @@ def _gen_from_inference_events(
391394 delegate_metadata_parser : Optional [
392395 Callable [[List [str ]], Dict [str , Any ]]
393396 ] = None ,
397+ delegate_time_scale_converter : Optional [
398+ Callable [[Union [int , str ], Union [int , float ]], Union [int , float ]]
399+ ] = None ,
394400 ) -> "Event" :
395401 """
396402 Given an EventSignature and a list of Events with that signature,
@@ -411,6 +417,7 @@ def _gen_from_inference_events(
411417 name = "" ,
412418 _instruction_id = signature .instruction_id ,
413419 _delegate_metadata_parser = delegate_metadata_parser ,
420+ _delegate_time_scale_converter = delegate_time_scale_converter ,
414421 )
415422
416423 # Populate fields from profile events
@@ -476,14 +483,35 @@ def _populate_profiling_related_fields(
476483 f"Expected exactly one profile event per InstructionEvent when generating Inspector Event, but got { len (profile_events )} "
477484 )
478485
486+ profile_event = profile_events [0 ]
487+
479488 # Scale factor should only be applied to non-delegated ops
480- scale_factor_updated = 1 if ret_event .is_delegated_op else scale_factor
489+ if (
490+ ret_event .is_delegated_op
491+ and ret_event ._delegate_time_scale_converter is not None
492+ ):
493+ scaled_time = ret_event ._delegate_time_scale_converter (
494+ ret_event .name ,
495+ profile_event .end_time ,
496+ # pyre-ignore
497+ ) - ret_event ._delegate_time_scale_converter (
498+ ret_event .name , profile_event .start_time
499+ )
500+ # If it's not a delegated op then we can just use the raw time values
501+ # and then scale them according to the scale factor that was passed in.
502+ elif not ret_event .is_delegated_op :
503+ scaled_time = (
504+ float (profile_event .end_time - profile_event .start_time )
505+ / scale_factor
506+ )
507+ # If there was no scale factor passed in just take a difference of the
508+ # end and start times.
509+ else :
510+ scaled_time = float (
511+ profile_event .end_time - profile_event .start_time
512+ )
481513
482- profile_event = profile_events [0 ]
483- data .append (
484- float (profile_event .end_time - profile_event .start_time )
485- / scale_factor_updated
486- )
514+ data .append (scaled_time )
487515 delegate_debug_metadatas .append (
488516 profile_event .delegate_debug_metadata
489517 if profile_event .delegate_debug_metadata
@@ -646,6 +674,9 @@ def _gen_from_etdump(
646674 delegate_metadata_parser : Optional [
647675 Callable [[List [str ]], Dict [str , Any ]]
648676 ] = None ,
677+ delegate_time_scale_converter : Optional [
678+ Callable [[Union [int , str ], Union [int , float ]], Union [int , float ]]
679+ ] = None ,
649680 ) -> List ["EventBlock" ]:
650681 """
651682 Given an etdump, generate a list of EventBlocks corresponding to the
@@ -743,6 +774,7 @@ class GroupedRunInstances:
743774 scale_factor ,
744775 output_buffer ,
745776 delegate_metadata_parser ,
777+ delegate_time_scale_converter ,
746778 )
747779 for signature , instruction_events in run_group .items ()
748780 ]
@@ -875,6 +907,9 @@ def __init__(
875907 delegate_metadata_parser : Optional [
876908 Callable [[List [str ]], Dict [str , Any ]]
877909 ] = None ,
910+ delegate_time_scale_converter : Optional [
911+ Callable [[Union [int , str ], Union [int , float ]], Union [int , float ]]
912+ ] = None ,
878913 enable_module_hierarchy : bool = False ,
879914 ) -> None :
880915 r"""
@@ -930,6 +965,7 @@ def __init__(
930965 self ._target_time_scale ,
931966 output_buffer ,
932967 delegate_metadata_parser = delegate_metadata_parser ,
968+ delegate_time_scale_converter = delegate_time_scale_converter ,
933969 )
934970
935971 # Connect ETRecord to EventBlocks
0 commit comments