Commit a59298e for stable-diffusion.cpp

commit a59298ed510a091314c9fc9112cb1bce65ad3bc5
Author: leejet <leejet714@gmail.com>
Date:   Sat Nov 14 02:15:16 2026 +0800

    fix: align CUDA extension architectures with ggml (#2347)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 14901f8..98ec75f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -368,8 +368,15 @@ if (SD_CUDA)
     set_property(SOURCE src/core/ggml_extend_backend.cpp APPEND PROPERTY COMPILE_DEFINITIONS SD_USE_CUDA)
     if (NOT SD_USE_UPSTREAM_GGML)
         include(cmake/cutlass.cmake)
+        if (TARGET ggml-cuda)
+            # ggml resolves CUDA architectures in a child directory scope.
+            get_target_property(CMAKE_CUDA_ARCHITECTURES ggml-cuda CUDA_ARCHITECTURES)
+        endif()
         enable_language(CUDA)
-        set_target_properties(${SD_LIB} PROPERTIES CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON)
+        set_target_properties(${SD_LIB} PROPERTIES
+            CUDA_STANDARD 17
+            CUDA_STANDARD_REQUIRED ON
+            CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}")
         if (SD_CUDA_CUTLASS_PATH)
             target_sources(${SD_LIB} PRIVATE src/core/cuda/cute_attention.cu src/core/cuda/i8_projection.cu src/core/cuda/i8_gated_residual.cu src/core/cuda/i8_gemm.cu src/core/cuda/bf16_gemm.cu)
             set_source_files_properties(src/core/cuda/cute_attention.cu src/core/cuda/i8_projection.cu src/core/cuda/i8_gated_residual.cu src/core/cuda/i8_gemm.cu src/core/cuda/bf16_gemm.cu PROPERTIES
diff --git a/docs/build.md b/docs/build.md
index 575436a..ad91a0a 100644
--- a/docs/build.md
+++ b/docs/build.md
@@ -130,6 +130,11 @@ cmake .. -DSD_CUDA=ON
 cmake --build . --config Release
 ```

+When building GGML from source, sd.cpp's CUDA extensions use the same target
+architectures as GGML, including native GPU detection with `GGML_NATIVE=ON`.
+To select target GPUs explicitly, pass `-DCMAKE_CUDA_ARCHITECTURES=89` (Ada),
+for example, or a quoted list such as `-DCMAKE_CUDA_ARCHITECTURES="80;89"`.
+
 CUDA extensions use sd.cpp's `thirdparty/cutlass` submodule by default
 for eligible INT8 GEMM and CuTe attention operations. If its headers are missing,
 CMake stops with an initialization hint; it does not download them automatically.
diff --git a/src/core/cuda/cute_attention.cu b/src/core/cuda/cute_attention.cu
index 32bc891..3f753d7 100644
--- a/src/core/cuda/cute_attention.cu
+++ b/src/core/cuda/cute_attention.cu
@@ -37,6 +37,7 @@
 #include <cuda_fp16.h>
 #include <cuda_runtime.h>
 #include <cutlass/numeric_conversion.h>
+#include <algorithm>
 #include <climits>
 #include <cstdlib>
 #include <cstring>
@@ -809,7 +810,18 @@ static void attention_destroy(void* state, void*) {
 }

 void sd_ggml_register_cuda_attention(ggml_backend_dev_t device) {
-    if (!cuda_device(device))
+    const auto* info = cuda_device(device);
+    if (!info || info->properties.major < 8)
+        return;
+    int previous;
+    if (cudaGetDevice(&previous) != cudaSuccess || cudaSetDevice(info->ordinal) != cudaSuccess)
+        return;
+    cudaFuncAttributes attributes{};
+    const auto error    = cudaFuncGetAttributes(&attributes, sd_fattn_cute::kernel<64, 64>);
+    const auto restored = cudaSetDevice(previous);
+    // JIT preserves instruction guards from the original PTX architecture.
+    if (restored != cudaSuccess || error != cudaSuccess ||
+        std::min(attributes.binaryVersion, attributes.ptxVersion) < 80)
         return;
     ggml_ext_kernel_desc desc = {};
     desc.abi_version          = GGML_EXT_ABI_VERSION;