@@ -62,6 +62,23 @@ def stdev_us(self) -> float:
6262 # Instance helpers
6363 # ------------------------------------------------------------------
6464
65+ def summary_tuple (self ) -> tuple [int , float , float , float , float , float ]:
66+ """Return ``(count, mean_us, median_us, stdev_us, min_us, max_us)``.
67+
68+ Returns all-NaN tuple when there are no durations.
69+ """
70+ if not self .durations_us :
71+ _nan = float ("nan" )
72+ return 0 , _nan , _nan , _nan , _nan , _nan
73+ return (
74+ self .count ,
75+ self .mean_us ,
76+ self .median_us ,
77+ self .stdev_us ,
78+ self .min_us ,
79+ self .max_us ,
80+ )
81+
6582 def get (self , attr : str , default : float = nan ) -> float :
6683 """Safe attribute access returning *default* when count == 0."""
6784 if self .count > 0 :
@@ -83,6 +100,48 @@ def print_detail(self) -> None:
83100 print (f" Stdev: { self .stdev_us :,.2f} us ({ self .stdev_us / 1000 :,.4f} ms)" )
84101 print ()
85102
103+ # ------------------------------------------------------------------
104+ # Factory classmethods
105+ # ------------------------------------------------------------------
106+
107+ @classmethod
108+ def from_trace_file (
109+ cls ,
110+ trace_file : str ,
111+ kernel_pattern : str ,
112+ * ,
113+ match_mode : str = "contains" ,
114+ device_type : str | None = None ,
115+ name : str | None = None ,
116+ ) -> KernelStats :
117+ """Load a trace file, extract matching durations, and return aggregated stats.
118+
119+ This replaces the common 5-line pattern::
120+
121+ trace = KinetoTrace.from_file(path)
122+ bucketed = trace.extract_durations(pattern)
123+ durations = [d for durs in bucketed.values() for d in durs]
124+ stats = KernelStats(name=pattern, durations_us=durations)
125+
126+ Args:
127+ trace_file: Path to a ``.json`` or ``.json.gz`` Chrome trace.
128+ kernel_pattern: Pattern passed to
129+ :meth:`KinetoTrace.extract_durations`.
130+ match_mode: ``"contains"`` | ``"exact"`` | ``"startswith"`` |
131+ ``"regex"``.
132+ device_type: ``"cuda"``, ``"cpu"``, or ``None``.
133+ name: Name for the returned :class:`KernelStats`. Defaults to
134+ *kernel_pattern*.
135+ """
136+ from fbgemm_gpu .bench .analysis .trace import KinetoTrace
137+
138+ trace = KinetoTrace .from_file (trace_file )
139+ bucketed = trace .extract_durations (
140+ kernel_pattern , match_mode = match_mode , device_type = device_type
141+ )
142+ durations = [d for durs in bucketed .values () for d in durs ]
143+ return cls (name = name or kernel_pattern , durations_us = durations )
144+
86145 # ------------------------------------------------------------------
87146 # Collection-level classmethods
88147 # ------------------------------------------------------------------
0 commit comments