Skip to content

IDEA: Move FA defines into a common location #2687

Backlog

Description

@askmyteapot

While doing an initial fix (#2686) for the latest CI build failure https://github.com/LostRuins/koboldcpp/actions/runs/45856371881, the slop machine suggested pulling the Flash Attention Defines out of each .cu and into a common location.


Long-term fix: centralize CUDA FlashAttention GGML_CUDA_FA_* defines

Summary

A CUDA build fails in ggml/src/ggml-cuda/fattn-tile.cu:

fattn-tile.cu(66): error: identifier "GGML_CUDA_FA_F16_F16" is undefined

This is koboldcpp-specific, not an upstream llama.cpp bug. Upstream defines every
GGML_CUDA_FA_<K>_<V> macro globally from ggml/cmake/common.cmake
(ggml_cuda_fattn_instances() → add_compile_definitions(...)). This fork does not use
that machinery — there is no ggml/cmake/ directory and the top-level CMakeLists.txt
globs the CUDA sources directly — so the macros were reimplemented as a local block at
the top of fattn.cu only.

A #define is per translation unit. fattn.cu and fattn-tile.cu are compiled by
separate nvcc invocations, so the define in fattn.cu is invisible to fattn-tile.cu.
The same problem exists in the Makefile CUDA build, which also globs
ggml/src/ggml-cuda/*.cu and passes no -D flags.

Immediate unblock (not the long-term fix)

Add the missing define to fattn-tile.cu after the includes:

// kcpp: separate translation unit from fattn.cu, so its defines are not visible here.
// Only F16-F16 tile instances are compiled (generate_cu_files.py / fattn-tile.cuh).
#define GGML_CUDA_FA_F16_F16 1

Proposed long-term fix

1. One central definition of the macros (must-do)

Create ggml/src/ggml-cuda/fattn-defines.cuh containing the full set of 64 defines
currently hardcoded in fattn.cu:8–80, each wrapped in an #ifndef guard so an external
-D value still wins if upstream's configurable CMake logic is ever restored:

#pragma once

// kcpp defaults for the fixed FA K/V instance set.
// Guards let build-system -D values override these defaults.
#ifndef GGML_CUDA_FA_F16_F16
#define GGML_CUDA_FA_F16_F16 1
#endif
#ifndef GGML_CUDA_FA_Q4_0_F16
#define GGML_CUDA_FA_Q4_0_F16 0
#endif
/* ... all remaining combinations from fattn.cu:8-80 ... */

Include it from common.cuh (near the other includes, e.g. after ggml-common.h):

#include "fattn-defines.cuh"

common.cuh is already included by fattn.cu, fattn-tile.cu, and the generated
template-instance headers, so every CUDA translation unit gets the macros automatically.
This restores the upstream behavior (global visibility) in a way that works for both
the CMake and Makefile builds.

Then delete the hardcoded block from fattn.cu:8–80.

Enabled pairs (all others default to 0): f16-f16, q4_0-q4_0, q5_1-q5_1,
q8_0-q8_0, bf16-bf16. These match the five vec-instance objects already compiled by
both build systems.

Note: the if constexpr checks in fattn-tile.cu:66 and fattn.cu:519 are redundant in
this fork — both branches call the same F16/F16 dispatch — so they could be removed as a
separate cleanup. With the central header they are harmless and the upstream code shape
is preserved.

2. One source of truth for the enabled pair list (recommended)

The same five pairs are currently written in three places:

Location What it controls
fattn.cu:8–80 macro values (1 = compiled)
CMakeLists.txt:125–129 vec-instance .cu files added to the CMake target
Makefile:236–240 vec-instance objects added to the Makefile target

If one is edited without the others, the macros and the actually linked template
instances drift apart (link error if a macro claims a pair is compiled but the object is
missing; silent F16 fallback in the opposite case).

Define the list once and loop over it in both build files, e.g. in CMakeLists.txt:

set(KCPP_FA_QUANTS f16-f16 q4_0-q4_0 q5_1-q5_1 q8_0-q8_0 bf16-bf16)

foreach (FA_PAIR IN LISTS KCPP_FA_QUANTS)
    list(APPEND GGML_SOURCES_CUDA
        "ggml/src/ggml-cuda/template-instances/fattn-vec-instance-${FA_PAIR}.cu")
endforeach ()

Do the equivalent with patsubst/addprefix in the Makefile. The central header from
step 1 can remain the fallback source of the macro values; optional build-system -D
generation from the same list makes the set configurable later.

3. CI guard (recommended)

  • Add a clean CUDA build from scratch for both the CMake and Makefile paths, so a
    missing macro fails in CI rather than midway through a local build.
  • When merging upstream llama.cpp commits, check for new GGML_CUDA_FA_* tokens
    (git grep -n 'GGML_CUDA_FA_') and verify they are covered by the central header.
    Upstream can add a new usage in a new file, and this fork's diverged build glue will
    not define it automatically — exactly the failure mode being fixed here.

Files touched

File Change
ggml/src/ggml-cuda/fattn-defines.cuh new: guarded GGML_CUDA_FA_* defaults
ggml/src/ggml-cuda/common.cuh include the new header
ggml/src/ggml-cuda/fattn.cu remove the local 64-line define block
CMakeLists.txt (optional) derive vec-instance sources from one pair list
Makefile (optional) same, for the Makefile build
CI config (optional) clean CUDA builds for both paths

Verification

  1. Clean CMake CUDA build — fattn-tile.cu.obj must compile.
  2. Clean Makefile CUDA build — same file must compile there too.
  3. Confirm the enabled pairs still match the five vec-instance objects; no new
    "no FlashAttention vector kernel compiled ... converting to f16" warnings.

Activity

  1. LostRuins commented on Nov 27, 2026

    @LostRuins
    Owner

    no need a new file. i can just toss them all into common.cuh i think

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions