Skip to content

Commit 52fe4a0

Browse files
authored
[Others] Enhance FlagCX allocator compilation process (#538)
Refactor FlagCX allocator compilation logic to check for existing library and handle distributed environment.
1 parent dc7e499 commit 52fe4a0

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

plugin/interservice/test_triton_lsa.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,65 @@
8383

8484

8585
def compile_flagcx_allocator():
86-
"""Compile the FlagCX allocator extension. Called once, result cached."""
8786
global _allocator, _allocator_wrapper, _flagcx_allocator_failed_to_compile
87+
8888
try:
8989
out_dir = tempfile.gettempdir()
9090
lib_name = "flagcx_allocator"
91-
92-
load_inline(
93-
name=lib_name,
94-
cpp_sources=flagcx_allocator_source,
95-
with_cuda=True,
96-
extra_ldflags=[f"-L{FLAGCX_LIB_PATH}", "-lflagcx",
97-
f"-Wl,-rpath,{FLAGCX_LIB_PATH}"],
98-
verbose=False,
99-
is_python_module=False,
100-
build_directory=out_dir,
101-
extra_include_paths=[FLAGCX_INCLUDE_PATH],
102-
)
91+
lib_path = os.path.join(out_dir, f"{lib_name}.so")
92+
93+
rank = 0
94+
if torch.distributed.is_available() and torch.distributed.is_initialized():
95+
rank = torch.distributed.get_rank()
96+
97+
if rank == 0:
98+
if not os.path.isfile(lib_path):
99+
print(
100+
f"[INFO] FlagCX allocator not found, compiling: {lib_path}",
101+
flush=True,
102+
)
103+
104+
load_inline(
105+
name=lib_name,
106+
cpp_sources=flagcx_allocator_source,
107+
with_cuda=True,
108+
extra_ldflags=[
109+
f"-L{FLAGCX_LIB_PATH}",
110+
"-lflagcx",
111+
f"-Wl,-rpath,{FLAGCX_LIB_PATH}",
112+
],
113+
verbose=True,
114+
is_python_module=False,
115+
build_directory=out_dir,
116+
extra_include_paths=[FLAGCX_INCLUDE_PATH],
117+
)
118+
else:
119+
print(
120+
f"[INFO] Using cached FlagCX allocator: {lib_path}",
121+
flush=True,
122+
)
123+
124+
if torch.distributed.is_available() and torch.distributed.is_initialized():
125+
torch.distributed.barrier()
126+
127+
if not os.path.isfile(lib_path):
128+
raise FileNotFoundError(
129+
f"FlagCX allocator library not found after compilation: {lib_path}"
130+
)
103131

104132
_allocator_wrapper = CUDAPluggableAllocator(
105-
f"{out_dir}/{lib_name}.so",
133+
lib_path,
106134
"flagcx_alloc_plug",
107135
"flagcx_free_plug",
108136
)
137+
109138
_allocator = _allocator_wrapper.allocator()
139+
110140
except Exception as e:
111141
_flagcx_allocator_failed_to_compile = True
112142
print(
113-
f"[WARNING] Failed to compile FlagCX memory allocator: {e}\n"
114-
f" Ensure FLAGCX_LIB_PATH ({FLAGCX_LIB_PATH}) contains libflagcx.so\n"
115-
f" and FLAGCX_INCLUDE_PATH ({FLAGCX_INCLUDE_PATH}) contains flagcx.h"
143+
f"[WARNING] Failed to load FlagCX memory allocator: {e}",
144+
flush=True,
116145
)
117146

118147

0 commit comments

Comments
 (0)