Skip to content

Use static libraries for AITER build#468

Open
Micky774 wants to merge 3 commits intodevfrom
zain/aiter-static
Open

Use static libraries for AITER build#468
Micky774 wants to merge 3 commits intodevfrom
zain/aiter-static

Conversation

@Micky774
Copy link
Contributor

@Micky774 Micky774 commented Mar 2, 2026

Description

Provides a TE-side build solution to assemble a static archive from intermediate object files generated during the AITER backend building process. These files are batch-processed into an archive, which is then used directly (and privately) in the compilation. This is done as an effort to avoid namespace collision for AITER runtime-loaded symbols provided by both TE-built AITER and third-party-built AITER libraries (e.g. via PyTorch).

The use of static-linking at build time is mediated via NVTE_AITER_STATIC_LINK, with a default set to 1 to proactively prevent the namespace collision issue. The archiving is batched, with batch-size controlled by AITER_ARCHIVE_BATCH_SIZE and set to 400 by default. Higher batch sizes are marginally more efficient, but provide less granular progress updates.

Note that using the static libraries does require a marginal increase in compilation time (a couple of minutes for the archiving process) but it also results in a smaller overall size for the necessary runtime libraries (saves ~300MB).

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Introduces NVTE_AITER_STATIC_LINK as an environment variable to mediate the use of static linking at build-time, defaulted to 1.
  • Adds cache validation for static archives
  • Caches static archives if they are built
  • Provides the capacity to utilize batch-archiving via AITER_ARCHIVE_BATCH_SIZE which is defaulted to 400.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@Micky774 Micky774 force-pushed the zain/aiter-static branch from 5c994a9 to 8451fc9 Compare March 2, 2026 21:52
@Micky774 Micky774 changed the title Zain/aiter static Use static libraries for AITER build Mar 2, 2026
@Micky774 Micky774 force-pushed the zain/aiter-static branch from 8451fc9 to 52163c5 Compare March 2, 2026 21:54
@Micky774 Micky774 force-pushed the zain/aiter-static branch from 52163c5 to 2c4c11b Compare March 2, 2026 22:27
exit 1
fi

mapfile -d '' obj_files < <(find "${src_obj_dir}" -type f -name '*.o' -print0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so complicated approach, just do what build systems do -> call ar cq @file_list


# Create static archives for both forward and backward passes
batch_size=${AITER_ARCHIVE_BATCH_SIZE:-400}
for lib in fwd bwd; do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is built and used by us, will it be more efficient to make single lib?

file(MAKE_DIRECTORY "${EXTRACT_DIR}")
message(STATUS "[AITER-PREBUILT] Caching locally built libs to ${EXTRACT_DIR}")
file(COPY "${SOURCE_DIR}/libmha_fwd.so" "${SOURCE_DIR}/libmha_bwd.so" DESTINATION "${EXTRACT_DIR}")
if(EXISTS "${SOURCE_DIR}/libmha_fwd.so" AND EXISTS "${SOURCE_DIR}/libmha_bwd.so")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should always exist because they are built by AITER build script before creating .a



set(AITER_MHA_INSTALL_PREFIX "transformer_engine" CACHE STRING "aiter mha shared lib install prefix in TE")
option(NVTE_AITER_STATIC_LINK "Statically link AITER MHA libs into ck_fused_attn" OFF)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By contract NVTE prefix is for env vriables. CMake options shoud rather be just AITER_STATIC_LINK


if [[ -z "${AITER_DIR}" || -z "${AITER_TEST_DIR}" || -z "${GPU_ARCHS_VAL}" ]]; then
echo "[AITER-PREBUILT] --aiter-dir, --aiter-test-dir, and --gpu-archs are required." >&2
echo "[AITER-BUILD] --aiter-dir, --aiter-test-dir, and --gpu-archs are required." >&2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency it should probably also be changed in CMakeLists.txt


find_package(hip)
list(APPEND ck_fused_attn_LINKER_LIBS hip::host hip::device roctx64 ${__AITER_MHA_PATH}/libmha_fwd.so ${__AITER_MHA_PATH}/libmha_bwd.so)
if(NVTE_AITER_STATIC_LINK)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add keep hip and roc libs here and only aiter libs under conditions

if(NVTE_AITER_STATIC_LINK)
set(__AITER_MHA_FWD_LIB "${__AITER_MHA_PATH}/libmha_fwd.a")
set(__AITER_MHA_BWD_LIB "${__AITER_MHA_PATH}/libmha_bwd.a")
if(NOT EXISTS "${__AITER_MHA_FWD_LIB}" OR NOT EXISTS "${__AITER_MHA_BWD_LIB}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't link FAIL anyways if libraries are not there and aren't those files checked when detected AITER cache?

@wangye805
Copy link
Collaborator

This current impl depends on detailed assumptions in aiter. We can add a rule archive into this section and pass an option to compile static lib:
https://github.com/ROCm/aiter/blob/00291e1a850dd636d21fa95c45115b7e662342dc/aiter/jit/utils/cpp_extension.py#L1726-L1735

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants