forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.py
More file actions
470 lines (396 loc) · 16.8 KB
/
Copy pathprofiler.py
File metadata and controls
470 lines (396 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
from __future__ import annotations
from collections.abc import Callable, Mapping
from contextlib import AbstractContextManager
from dataclasses import dataclass
from time import perf_counter
from typing import Any
import torch
@dataclass
class Stat:
"""Aggregated timing statistics for a labeled section."""
calls: int = 0
total_s: float = 0.0
def add(self, dt_s: float) -> None:
"""Accumulate a duration sample in seconds."""
self.calls += 1
self.total_s += dt_s
@property
def avg_s(self) -> float:
"""Average duration per call in seconds."""
return self.total_s / self.calls if self.calls else 0.0
class _Patcher:
"""Patch attributes and restore them on context exit."""
def __init__(self) -> None:
self._patched: list[tuple[object, str, Any]] = []
def patch(self, obj: object, name: str, new_value: Any) -> None:
"""Replace `obj.name` with `new_value`, recording the original value."""
old_value: Any = getattr(obj, name)
self._patched.append((obj, name, old_value))
setattr(obj, name, new_value)
def restore(self) -> None:
"""Restore patched attributes in reverse order."""
for obj, name, old_value in reversed(self._patched):
setattr(obj, name, old_value)
self._patched.clear()
class _TimedFn:
"""Callable wrapper that measures wall-clock time."""
def __init__(
self, fn: Callable[..., Any], on_time: Callable[[float], None]
) -> None:
self._fn: Callable[..., Any] = fn
self._on_time: Callable[[float], None] = on_time
def __call__(self, *args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return self._fn(*args, **kwargs)
finally:
torch.cuda.synchronize()
self._on_time(perf_counter() - t0)
class _TimedCallableProxy:
"""Callable proxy that times `obj(*args, **kwargs)`.
Additionally, selected method names can be timed when accessed, e.g.
`vae.encode(...)` and `vae.decode(...)`.
"""
def __init__(
self,
obj: Any,
on_time_call: Callable[[float], None],
timed_methods: Mapping[str, Callable[[float], None]] | None = None,
) -> None:
self._obj: Any = obj
self._on_time_call: Callable[[float], None] = on_time_call
self._timed_methods: Mapping[str, Callable[[float], None]] = (
timed_methods or {}
)
def __call__(self, *args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return self._obj(*args, **kwargs)
finally:
torch.cuda.synchronize()
self._on_time_call(perf_counter() - t0)
def __getattr__(self, name: str) -> Any:
attr: Any = getattr(self._obj, name)
on_time: Callable[[float], None] | None = self._timed_methods.get(name)
if on_time is not None and callable(attr):
return _TimedFn(attr, on_time)
return attr
class ExecuteProfiler(AbstractContextManager["ExecuteProfiler"]):
"""Profile diffusion pipelines with method- and component-level aggregation.
This profiler provides:
- method-level labels in `stats`
- module-level labels in `component_stats`
It guarantees component-level timings by replacing `pipeline.<component>`
attributes with proxies during the context.
"""
def __init__(
self,
pipeline_or_wrapper: Any,
*,
enabled: bool = True,
patch_tensor_ops: bool = False,
is_diffusers: bool = False,
) -> None:
self._obj: Any = pipeline_or_wrapper
self._enabled: bool = enabled
self._patch_tensor_ops: bool = patch_tensor_ops
self._is_diffusers: bool = is_diffusers
self.stats: dict[str, Stat] = {}
self.component_stats: dict[str, Stat] = {}
self._patcher: _Patcher = _Patcher()
self._model: Any | None = None
self._pipeline_module_label: str = "pipeline/unknown"
def __enter__(self) -> ExecuteProfiler:
if not self._enabled:
return self
self._model = self._resolve_inner_model(self._obj)
target: Any = self._model if self._model is not None else self._obj
self._pipeline_module_label = f"pipeline/{type(target).__name__}"
self._wrap_methods(target)
self._wrap_components(target)
if self._patch_tensor_ops:
self._wrap_tensor_ops()
return self
def __exit__(
self, exc_type: object, exc: BaseException | None, tb: object
) -> None:
self._patcher.restore()
return None
def report(self, unit: str = "ms") -> None:
"""Print a full profiling report with component and method tables."""
print("==================== PROFILING REPORT ==")
print(f"Component Timings:\n{self.report_components(unit=unit)}\n")
print(f"Method Timings:\n{self.report_methods(unit=unit)}")
print("==========================================")
def report_methods(self, *, unit: str = "ms") -> str:
"""Render a method-level timing table."""
mul: float = 1000.0 if unit == "ms" else 1.0
items: list[tuple[str, Stat]] = sorted(
self.stats.items(), key=lambda kv: kv[1].total_s, reverse=True
)
lines: list[str] = [
f"{'methods':<30} {'calls':>7} {'total':>12} {'avg':>12} ({unit})"
]
for name, st in items:
lines.append(
f"{name:<30} {st.calls:>7d} {st.total_s * mul:>12.3f} {st.avg_s * mul:>12.3f}"
)
return "\n".join(lines)
def report_components(self, *, unit: str = "ms") -> str:
"""Render a component-level timing table."""
mul: float = 1000.0 if unit == "ms" else 1.0
items: list[tuple[str, Stat]] = sorted(
self.component_stats.items(),
key=lambda kv: kv[1].total_s,
reverse=True,
)
lines: list[str] = [
f"{'components':<30} {'calls':>7} {'total':>12} {'avg':>12} ({unit})"
]
for name, st in items:
lines.append(
f"{name:<30} {st.calls:>7d} {st.total_s * mul:>12.3f} {st.avg_s * mul:>12.3f}"
)
return "\n".join(lines)
def _resolve_inner_model(self, obj: Any) -> Any | None:
"""Find an inner diffusion model instance inside wrapper pipelines."""
candidates: tuple[str, ...] = (
"pipeline_model",
"model",
"_model",
"_pipeline_model",
"diffusion_pipeline",
"_diffusion_pipeline",
)
for name in candidates:
if hasattr(obj, name):
inner: Any = getattr(obj, name)
if (
inner is not None
and hasattr(inner, "execute")
and callable(inner.execute)
):
return inner
return None
def _accum(self, label: str, dt_s: float) -> None:
self.stats.setdefault(label, Stat()).add(dt_s)
def _accum_component(self, label: str, dt_s: float) -> None:
self.component_stats.setdefault(label, Stat()).add(dt_s)
def _resolve_attr_path(self, obj: Any, path: str) -> tuple[Any, str]:
"""Resolve a dotted attribute path, returning (parent_obj, attr_name)."""
parts = path.split(".")
for part in parts[:-1]:
obj = getattr(obj, part)
return obj, parts[-1]
def _wrap_methods(self, target: Any) -> None:
"""Wrap pipeline method calls for timing."""
if not self._is_diffusers:
specs = [
("execute", "E2E execute"),
("prepare_prompt_embeddings", "prepare_embeddings"),
("decode_latents", "decode_latents"),
("prepare_scheduler", "prepare_scheduler"),
("scheduler_step", "scheduler_step"),
("preprocess_latents", "preprocess_latents"),
("prepare_image_latents", "prepare_image_latents"),
]
else:
specs = [
("__call__", "E2E execute"),
("encode_prompt", "encode_prompt"),
("prepare_latents", "prepare_latents"),
("retrieve_timesteps", "retrieve_timesteps"),
("_unpack_latents_with_ids", "_unpack_latents_with_ids"),
("_unpatchify_latents", "_unpatchify_latents"),
("scheduler.step", "scheduler.step"),
("image_processor.postprocess", "image_processor.postprocess"),
]
for method_name, label in specs:
self._wrap_method_if_exists(target, method_name, label)
def _wrap_method_if_exists(
self, obj: Any, method_name: str, label: str
) -> None:
"""Wrap `obj.method_name` and aggregate into method stats."""
try:
parent, attr_name = self._resolve_attr_path(obj, method_name)
except AttributeError:
return
# Special handling for __call__: patch on the type
if attr_name == "__call__":
target_obj: Any = type(parent)
else:
target_obj = parent
if not hasattr(target_obj, attr_name):
return
original: Any = getattr(target_obj, attr_name)
if not callable(original):
return
if getattr(original, "__is_execute_profiler_wrapper__", False):
return
def wrapper(*args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return original(*args, **kwargs)
finally:
torch.cuda.synchronize()
dt: float = perf_counter() - t0
self._accum(label, dt)
wrapper.__is_execute_profiler_wrapper__ = True # type: ignore[attr-defined]
wrapper.__wrapped__ = original # type: ignore[attr-defined]
try:
self._patcher.patch(target_obj, attr_name, wrapper)
except (AttributeError, TypeError):
pass
def _wrap_components(self, target: Any) -> None:
"""Replace `target.<component>` with a proxy or wrap its methods.
For the VAE, also time `.encode()` and `.decode()` calls explicitly.
"""
comps: Any = getattr(target, "components", {}) or {}
if not isinstance(comps, dict):
return
for name in comps:
if name in ("scheduler", "tokenizer"):
continue
if not hasattr(target, name):
continue
comp: Any = getattr(target, name)
if comp is None:
continue
base_label: str = f"component/{name}"
if self._is_diffusers:
self._wrap_component_method_if_exists(
comp, "forward", base_label
)
if name == "vae":
self._wrap_component_method_if_exists(
comp, "encode", "component/vae.encode"
)
self._wrap_component_method_if_exists(
comp, "decode", "component/vae.decode"
)
continue
# Non-diffusers (MAX): keep proxy approach
def make_on_time(lab: str) -> Callable[[float], None]:
def on_time(dt: float) -> None:
self._accum(lab, dt)
self._accum_component(lab, dt)
return on_time
timed_methods: dict[str, Callable[[float], None]] = {}
if name == "vae":
timed_methods["encode"] = make_on_time("component/vae.encode")
timed_methods["decode"] = make_on_time("component/vae.decode")
proxy: _TimedCallableProxy = _TimedCallableProxy(
comp,
on_time_call=make_on_time(base_label),
timed_methods=timed_methods,
)
try:
self._patcher.patch(target, name, proxy)
except (AttributeError, TypeError):
continue
def _wrap_component_method_if_exists(
self, obj: Any, method_name: str, label: str
) -> None:
"""Wrap `obj.method_name` and aggregate into component stats."""
if not hasattr(obj, method_name):
return
original: Any = getattr(obj, method_name)
if not callable(original):
return
if getattr(original, "__is_execute_profiler_wrapper__", False):
return
def wrapper(*args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return original(*args, **kwargs)
finally:
torch.cuda.synchronize()
dt: float = perf_counter() - t0
self._accum_component(label, dt)
wrapper.__is_execute_profiler_wrapper__ = True # type: ignore[attr-defined]
wrapper.__wrapped__ = original # type: ignore[attr-defined]
try:
self._patcher.patch(obj, method_name, wrapper)
except (AttributeError, TypeError):
pass
def _wrap_tensor_ops(self) -> None:
"""Patch Tensor conversion/movement ops (process-wide while active)."""
try:
from max.experimental.tensor import Tensor
except Exception:
return
if hasattr(Tensor, "from_dlpack") and callable(Tensor.from_dlpack):
orig_from: Any = Tensor.from_dlpack
if not getattr(orig_from, "__is_execute_profiler_wrapper__", False):
def from_dlpack_wrapped(*args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return orig_from(*args, **kwargs)
finally:
torch.cuda.synchronize()
dt: float = perf_counter() - t0
self._accum("tensor/from_dlpack", dt)
from_dlpack_wrapped.__is_execute_profiler_wrapper__ = True # type: ignore[attr-defined]
from_dlpack_wrapped.__wrapped__ = orig_from # type: ignore[attr-defined]
try:
self._patcher.patch(
Tensor, "from_dlpack", from_dlpack_wrapped
)
except (AttributeError, TypeError):
pass
for meth, label in (("to", "tensor/to"), ("cast", "tensor/cast")):
if hasattr(Tensor, meth) and callable(getattr(Tensor, meth)):
orig_m: Any = getattr(Tensor, meth)
if getattr(orig_m, "__is_execute_profiler_wrapper__", False):
continue
def _make_wrapped(
o: Callable[..., Any], meth_label: str
) -> Callable[..., Any]:
def wrapped(self_: Any, *args: Any, **kwargs: Any) -> Any:
torch.cuda.synchronize()
t0: float = perf_counter()
try:
return o(self_, *args, **kwargs)
finally:
torch.cuda.synchronize()
dt: float = perf_counter() - t0
self._accum(meth_label, dt)
wrapped.__is_execute_profiler_wrapper__ = True # type: ignore[attr-defined]
wrapped.__wrapped__ = o # type: ignore[attr-defined]
return wrapped
wrapped_fn: Callable[..., Any] = _make_wrapped(orig_m, label)
try:
self._patcher.patch(Tensor, meth, wrapped_fn)
except (AttributeError, TypeError):
pass
def profile_execute(
pipeline_or_wrapper: Any,
*,
enabled: bool = True,
patch_tensor_ops: bool = False,
is_diffusers: bool = False,
) -> ExecuteProfiler:
"""Create a profiler for a diffusion pipeline or a wrapper pipeline."""
return ExecuteProfiler(
pipeline_or_wrapper,
enabled=enabled,
patch_tensor_ops=patch_tensor_ops,
is_diffusers=is_diffusers,
)