|
83 | 83 |
|
84 | 84 |
|
85 | 85 | def compile_flagcx_allocator(): |
86 | | - """Compile the FlagCX allocator extension. Called once, result cached.""" |
87 | 86 | global _allocator, _allocator_wrapper, _flagcx_allocator_failed_to_compile |
| 87 | + |
88 | 88 | try: |
89 | 89 | out_dir = tempfile.gettempdir() |
90 | 90 | 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 | + ) |
103 | 131 |
|
104 | 132 | _allocator_wrapper = CUDAPluggableAllocator( |
105 | | - f"{out_dir}/{lib_name}.so", |
| 133 | + lib_path, |
106 | 134 | "flagcx_alloc_plug", |
107 | 135 | "flagcx_free_plug", |
108 | 136 | ) |
| 137 | + |
109 | 138 | _allocator = _allocator_wrapper.allocator() |
| 139 | + |
110 | 140 | except Exception as e: |
111 | 141 | _flagcx_allocator_failed_to_compile = True |
112 | 142 | 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, |
116 | 145 | ) |
117 | 146 |
|
118 | 147 |
|
|
0 commit comments