|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 | +"""observe_pass — decorator to auto-collect pass input/output via Observatory.""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import functools |
| 12 | +import logging |
| 13 | +from typing import Any, Callable, Optional, Type, Union |
| 14 | + |
| 15 | +from torch.fx.passes.infra.pass_base import PassResult |
| 16 | + |
| 17 | + |
| 18 | +def observe_pass( |
| 19 | + target: Union[Callable, Type, None] = None, |
| 20 | + *, |
| 21 | + name: Optional[str] = None, |
| 22 | + collect_input: bool = True, |
| 23 | + collect_output: bool = True, |
| 24 | +) -> Any: |
| 25 | + """Wrap a pass to auto-collect its graph via Observatory. |
| 26 | +
|
| 27 | + Works as a class decorator, instance wrapper, or function wrapper. |
| 28 | +
|
| 29 | + Args: |
| 30 | + target: A PassBase subclass (class decorator), a pass instance, or a |
| 31 | + callable pass. When ``None``, returns a parameterized decorator. |
| 32 | + name: Override the record name (default: derived from class/function name). |
| 33 | + collect_input: Collect the input graph before the pass runs (default True). |
| 34 | + collect_output: Collect the output graph after the pass runs (default True). |
| 35 | + """ |
| 36 | + collect_both = collect_input and collect_output |
| 37 | + |
| 38 | + def _base_name(obj: Any) -> str: |
| 39 | + if name: |
| 40 | + return name |
| 41 | + if isinstance(obj, type): |
| 42 | + return obj.__name__ |
| 43 | + cls_name = type(obj).__name__ |
| 44 | + if cls_name == "function": |
| 45 | + return getattr(obj, "__name__", "pass") |
| 46 | + return cls_name |
| 47 | + |
| 48 | + def _collect_artifact(record_name: str, artifact: Any) -> None: |
| 49 | + from .observatory import Observatory |
| 50 | + |
| 51 | + try: |
| 52 | + Observatory.collect(record_name, artifact) |
| 53 | + except Exception as exc: |
| 54 | + logging.debug("[observe_pass] collection failed for %s: %s", record_name, exc) |
| 55 | + |
| 56 | + def _output_graph(gm: Any, result: Any) -> Any: |
| 57 | + if isinstance(result, tuple) and hasattr(result, "graph_module"): |
| 58 | + return result.graph_module |
| 59 | + if result is None: |
| 60 | + return gm |
| 61 | + return result |
| 62 | + |
| 63 | + def _wrap_callable(fn: Callable) -> Callable: |
| 64 | + @functools.wraps(fn) |
| 65 | + def wrapper(gm: Any, *args: Any, **kwargs: Any) -> Any: |
| 66 | + base = _base_name(fn) |
| 67 | + if collect_input: |
| 68 | + _collect_artifact(f"{base}/input" if collect_both else base, gm) |
| 69 | + result = fn(gm, *args, **kwargs) |
| 70 | + if collect_output: |
| 71 | + _collect_artifact( |
| 72 | + f"{base}/output" if collect_both else base, |
| 73 | + _output_graph(gm, result), |
| 74 | + ) |
| 75 | + return result |
| 76 | + |
| 77 | + return wrapper |
| 78 | + |
| 79 | + def _wrap_class(cls: Type) -> Type: |
| 80 | + original_call = cls.__call__ |
| 81 | + |
| 82 | + @functools.wraps(original_call) |
| 83 | + def patched_call(self: Any, gm: Any, *args: Any, **kwargs: Any) -> Any: |
| 84 | + base = _base_name(self) |
| 85 | + if collect_input: |
| 86 | + _collect_artifact(f"{base}/input" if collect_both else base, gm) |
| 87 | + result = original_call(self, gm, *args, **kwargs) |
| 88 | + if collect_output: |
| 89 | + _collect_artifact( |
| 90 | + f"{base}/output" if collect_both else base, |
| 91 | + _output_graph(gm, result), |
| 92 | + ) |
| 93 | + return result |
| 94 | + |
| 95 | + cls.__call__ = patched_call |
| 96 | + return cls |
| 97 | + |
| 98 | + # Dispatch based on how observe_pass was called. |
| 99 | + if target is None: |
| 100 | + # Parameterized: @observe_pass(name="X", collect_output=False) |
| 101 | + def decorator(t: Any) -> Any: |
| 102 | + if isinstance(t, type): |
| 103 | + return _wrap_class(t) |
| 104 | + return _wrap_callable(t) |
| 105 | + |
| 106 | + return decorator |
| 107 | + |
| 108 | + if isinstance(target, type): |
| 109 | + return _wrap_class(target) |
| 110 | + |
| 111 | + return _wrap_callable(target) |
0 commit comments