1- import os
21import sys
32import importlib
43
@@ -49,24 +48,32 @@ def _load_handler(self, backend):
4948 print (f"[FlagTree] Warning: Failed to load Ascend Hint Handler: { e } " , file = sys .stderr )
5049 return BaseHintHandler ()
5150 elif backend == 'aipu' :
52- from .backends .aipu import AipuHintHandler
53- return AipuHintHandler ()
51+ try :
52+ module = importlib .import_module ("third_party.aipu.backend.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 ("third_party.nvidia.backend.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 ()
5464 else :
5565 return BaseHintHandler ()
5666
5767
5868# supported backend with matched version
59- SUPPORTED_CONFIG = {
60- "cuda" : {"3.5" },
61- "npu" : {"3.2" },
62- "aipu" : {"3.3" },
63- }
69+ SUPPORTED_BACKENDS = ["aipu" , "npu" , "cuda" ]
6470
71+ # TODO : npu will have conflicts if more backend involved
6572# mapping name
6673BACKEND_ALIASES = {
6774 "ascend" : "npu" ,
6875 "huawei" : "npu" ,
69- "nv " : "cuda" ,
76+ "nvidia " : "cuda" ,
7077}
7178
7279
@@ -81,7 +88,6 @@ def hint_get_flagtree_backend() -> str:
8188 detected_backend = ""
8289
8390 import torch
84- import triton
8591
8692 # Priority 1: Triton Driver
8793 try :
@@ -96,42 +102,24 @@ def hint_get_flagtree_backend() -> str:
96102 except ImportError :
97103 pass
98104
105+ # TODO : some backend may not support priority 1, so keep priority 2 is necessary
99106 # Priority 2: Torch Global State
100107 if not detected_backend :
101- candidates = list (SUPPORTED_CONFIG .keys ())
102- # cuda priority least
103- candidates .sort (key = lambda x : 1 if x == "cuda" else 0 )
108+ check_priority = ["aipu" , "npu" , "cuda" ]
104109
105110 # 3. parse according to benefit
106- for candidate in candidates :
107- module_name = candidate
108- module = getattr (torch , module_name , None )
111+ for candidate in check_priority :
112+ module = getattr (torch , candidate , None )
109113 if module and hasattr (module , "is_available" ) and module .is_available ():
110114 detected_backend = candidate
111115 break
112116
113- # Priority 3: Environment Variable (need to remove!!!)
114- if not detected_backend :
115- detected_backend = os .environ .get ("FLAGTREE_BACKEND" , "" )
116-
117117 # (Normalization and Validation)
118118 canonical_backend = normalize_backend_name (detected_backend )
119119
120- if not canonical_backend or canonical_backend not in SUPPORTED_CONFIG :
120+ if not canonical_backend or canonical_backend not in SUPPORTED_BACKENDS :
121121 return ""
122122
123- # verify name and version match
124- try :
125- current_triton_version = "." .join (triton .__version__ .split ("." )[:2 ])
126- supported_versions = SUPPORTED_CONFIG [canonical_backend ]
127- if current_triton_version not in supported_versions :
128- msg = (f"[Flagtree] Hint ignored: Detected backend '{ canonical_backend } ' but current Triton version "
129- f"'{ current_triton_version } ' matches no supported versions { supported_versions } ." )
130- print (msg , file = sys .stderr )
131- return ""
132- except Exception :
133- pass
134-
135123 return canonical_backend
136124
137125
0 commit comments