|
| 1 | +import sys |
| 2 | +import importlib |
| 3 | + |
| 4 | + |
| 5 | +class BaseHintHandler: |
| 6 | + # dynamicly find method |
| 7 | + def trigger(self, hook_name, *args, **kwargs): |
| 8 | + if hasattr(self, hook_name): |
| 9 | + method = getattr(self, hook_name) |
| 10 | + if callable(method): |
| 11 | + try: |
| 12 | + return method(*args, **kwargs) |
| 13 | + |
| 14 | + except TypeError as e: |
| 15 | + import inspect |
| 16 | + |
| 17 | + try: |
| 18 | + sig = inspect.signature(method) |
| 19 | + expected = str(sig) |
| 20 | + except Exception: |
| 21 | + expected = "(unknown)" |
| 22 | + |
| 23 | + actual_args = f"{len(args)} positional" |
| 24 | + actual_kwargs = f"keys={list(kwargs.keys())}" if kwargs else "no keywords" |
| 25 | + |
| 26 | + print(f"\n[Hint Trigger Mismatch] {self.__class__.__name__}.{hook_name}") |
| 27 | + print(f" > Expect : {expected}") |
| 28 | + print(f" > Actual : {actual_args}, {actual_kwargs}") |
| 29 | + print(f" > Reason : {e}\n") |
| 30 | + |
| 31 | + raise e |
| 32 | + return None |
| 33 | + |
| 34 | + |
| 35 | +class HintManager: |
| 36 | + |
| 37 | + def __init__(self, backend_name): |
| 38 | + self.backend_name = backend_name |
| 39 | + # load Handler with backend name |
| 40 | + self.handler = self._load_handler(backend_name) |
| 41 | + |
| 42 | + def _load_handler(self, backend): |
| 43 | + if backend == 'npu': |
| 44 | + try: |
| 45 | + module = importlib.import_module("triton.backends.ascend.ascend_hint_handler") |
| 46 | + return module.AscendHintHandler() |
| 47 | + except ImportError as e: |
| 48 | + print(f"[FlagTree] Warning: Failed to load Ascend Hint Handler: {e}", file=sys.stderr) |
| 49 | + return BaseHintHandler() |
| 50 | + elif backend == 'aipu': |
| 51 | + try: |
| 52 | + module = importlib.import_module("triton.backends.aipu.aipu_hint_handler") |
| 53 | + return module.AipuHintHandler() |
| 54 | + except ImportError as e: |
| 55 | + print(f"[FlagTree] Warning: Failed to load aipu Hint Handler: {e}", file=sys.stderr) |
| 56 | + return BaseHintHandler() |
| 57 | + elif backend == 'cuda': |
| 58 | + try: |
| 59 | + module = importlib.import_module("triton.backends.nvidia.nvidia_hint_handler") |
| 60 | + return module.NvidiaHintHandler() |
| 61 | + except ImportError as e: |
| 62 | + print(f"[FlagTree] Warning: Failed to load Nvidia Hint Handler: {e}", file=sys.stderr) |
| 63 | + return BaseHintHandler() |
| 64 | + else: |
| 65 | + return BaseHintHandler() |
| 66 | + |
| 67 | + |
| 68 | +# supported backend with matched version |
| 69 | +SUPPORTED_BACKENDS = ["aipu", "npu", "cuda"] |
| 70 | + |
| 71 | +# TODO : npu will have conflicts if more backend involved |
| 72 | +# mapping name |
| 73 | +BACKEND_ALIASES = { |
| 74 | + "ascend": "npu", |
| 75 | + "huawei": "npu", |
| 76 | + "nvidia": "cuda", |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +def normalize_backend_name(name: str) -> str: |
| 81 | + if not name: |
| 82 | + return "" |
| 83 | + name = name.lower() |
| 84 | + return BACKEND_ALIASES.get(name, name) |
| 85 | + |
| 86 | + |
| 87 | +def hint_get_flagtree_backend() -> str: |
| 88 | + detected_backend = "" |
| 89 | + |
| 90 | + import torch |
| 91 | + |
| 92 | + # Priority 1: Triton Driver |
| 93 | + try: |
| 94 | + from triton.runtime import driver |
| 95 | + if hasattr(driver, 'active') and hasattr(driver.active, 'get_active_torch_device'): |
| 96 | + device = driver.active.get_active_torch_device() |
| 97 | + if isinstance(device, torch.device): |
| 98 | + detected_backend = device.type |
| 99 | + # unimplemented support |
| 100 | + elif isinstance(device, str): |
| 101 | + detected_backend = device |
| 102 | + except ImportError: |
| 103 | + pass |
| 104 | + |
| 105 | + # TODO : some backend may not support priority 1, so keep priority 2 is necessary |
| 106 | + # Priority 2: Torch Global State |
| 107 | + if not detected_backend: |
| 108 | + check_priority = ["aipu", "npu", "cuda"] |
| 109 | + |
| 110 | + # 3. parse according to benefit |
| 111 | + for candidate in check_priority: |
| 112 | + module = getattr(torch, candidate, None) |
| 113 | + if module and hasattr(module, "is_available") and module.is_available(): |
| 114 | + detected_backend = candidate |
| 115 | + break |
| 116 | + |
| 117 | + # (Normalization and Validation) |
| 118 | + canonical_backend = normalize_backend_name(detected_backend) |
| 119 | + |
| 120 | + if not canonical_backend or canonical_backend not in SUPPORTED_BACKENDS: |
| 121 | + return "" |
| 122 | + |
| 123 | + return canonical_backend |
| 124 | + |
| 125 | + |
| 126 | +# lazy load after first call hint trigger |
| 127 | +_global_hint_manager = None |
| 128 | + |
| 129 | + |
| 130 | +def hint_trigger(hook_name, *args, **kwargs): |
| 131 | + global _global_hint_manager |
| 132 | + |
| 133 | + if _global_hint_manager is None: |
| 134 | + _global_hint_manager = HintManager(hint_get_flagtree_backend()) |
| 135 | + return _global_hint_manager.handler.trigger(hook_name, *args, **kwargs) |
0 commit comments