|
9 | 9 | -- on `infinicore_cpp_api` where `os.iorunv` is available in this xmake sandbox. |
10 | 10 | end |
11 | 11 |
|
| 12 | +-- Resolve MetaX flash-attn .so path (used only from this file: `before_link` sandbox cannot see globals from `xmake.lua`). |
| 13 | +local FLASH_ATTN_METAX_CUDA_SO_CONTAINER_DEFAULT = |
| 14 | + "/opt/conda/lib/python3.10/site-packages/flash_attn_2_cuda.cpython-310-aarch64-linux-gnu.so" |
| 15 | + |
| 16 | +local function metax_flash_attn_cuda_so_path() |
| 17 | + -- Highest priority: override the exact `.so` file to link. |
| 18 | + local env_path = os.getenv("FLASH_ATTN_2_CUDA_SO") |
| 19 | + if env_path and env_path ~= "" then |
| 20 | + env_path = env_path:trim() |
| 21 | + if os.isfile(env_path) then |
| 22 | + return env_path |
| 23 | + end |
| 24 | + print(string.format("warning: metax+flash-attn: FLASH_ATTN_2_CUDA_SO is not a file: %s, fallback to container/default path", env_path)) |
| 25 | + end |
| 26 | + |
| 27 | + -- Second priority: allow overriding the "expected" container path via env. |
| 28 | + local container_path = os.getenv("FLASH_ATTN_METAX_CUDA_SO_CONTAINER") |
| 29 | + if not container_path or container_path == "" then |
| 30 | + container_path = FLASH_ATTN_METAX_CUDA_SO_CONTAINER_DEFAULT |
| 31 | + end |
| 32 | + |
| 33 | + if not os.isfile(container_path) then |
| 34 | + print( |
| 35 | + string.format( |
| 36 | + "warning: metax+flash-attn: expected %s; install flash-attn in conda env, or export FLASH_ATTN_2_CUDA_SO.", |
| 37 | + container_path |
| 38 | + ) |
| 39 | + ) |
| 40 | + end |
| 41 | + return container_path |
| 42 | +end |
| 43 | + |
12 | 44 | -- Set numeric HPCC version macro for flash-attn signature/call compatibility. |
13 | 45 | -- Must be done before compiling `infinicore_cpp_api` sources. |
14 | 46 | target("infinicore_cpp_api") |
@@ -39,41 +71,19 @@ target("infinicore_cpp_api") |
39 | 71 | end |
40 | 72 | end |
41 | 73 | end) |
42 | | -target_end() |
43 | | - |
44 | | --- Resolve MetaX flash-attn .so path. |
45 | | --- `xmake.lua` calls `_metax_flash_attn_cuda_so_path()` during `infinicore_cpp_api` |
46 | | --- linking, so this helper must live here (and must be global, not `local`). |
47 | | -local FLASH_ATTN_METAX_CUDA_SO_CONTAINER_DEFAULT = |
48 | | - "/opt/conda/lib/python3.10/site-packages/flash_attn_2_cuda.cpython-310-aarch64-linux-gnu.so" |
49 | | - |
50 | | -function _metax_flash_attn_cuda_so_path() |
51 | | - -- Highest priority: override the exact `.so` file to link. |
52 | | - local env_path = os.getenv("FLASH_ATTN_2_CUDA_SO") |
53 | | - if env_path and env_path ~= "" then |
54 | | - env_path = env_path:trim() |
55 | | - if os.isfile(env_path) then |
56 | | - return env_path |
57 | | - end |
58 | | - print(string.format("warning: metax+flash-attn: FLASH_ATTN_2_CUDA_SO is not a file: %s, fallback to container/default path", env_path)) |
59 | | - end |
60 | | - |
61 | | - -- Second priority: allow overriding the "expected" container path via env. |
62 | | - local container_path = os.getenv("FLASH_ATTN_METAX_CUDA_SO_CONTAINER") |
63 | | - if not container_path or container_path == "" then |
64 | | - container_path = FLASH_ATTN_METAX_CUDA_SO_CONTAINER_DEFAULT |
65 | | - end |
66 | | - |
67 | | - if not os.isfile(container_path) then |
68 | | - print( |
69 | | - string.format( |
70 | | - "warning: metax+flash-attn: expected %s; install flash-attn in conda env, or export FLASH_ATTN_2_CUDA_SO.", |
71 | | - container_path |
| 74 | + if get_config("flash-attn") and get_config("flash-attn") ~= "" then |
| 75 | + before_link(function (target) |
| 76 | + local flash_so_metax = metax_flash_attn_cuda_so_path() |
| 77 | + local flash_dir_metax = path.directory(flash_so_metax) |
| 78 | + local flash_name_metax = path.filename(flash_so_metax) |
| 79 | + target:add( |
| 80 | + "shflags", |
| 81 | + "-Wl,--no-as-needed -L" .. flash_dir_metax .. " -l:" .. flash_name_metax .. " -Wl,-rpath," .. flash_dir_metax, |
| 82 | + {force = true} |
72 | 83 | ) |
73 | | - ) |
| 84 | + end) |
74 | 85 | end |
75 | | - return container_path |
76 | | -end |
| 86 | +target_end() |
77 | 87 |
|
78 | 88 | add_includedirs(MACA_ROOT .. "/include") |
79 | 89 | add_linkdirs(MACA_ROOT .. "/lib") |
|
0 commit comments