From f97babd0c83433450ce653850748bc71010ed3d4 Mon Sep 17 00:00:00 2001 From: munder-sa Date: Fri, 20 Mar 2026 11:33:41 +0900 Subject: [PATCH 1/3] Fix Windows MSVC build and concurrency issues --- README.md | 5 ++++- sageattention3_blackwell/setup.py | 25 +++++++++++++++++++++++-- setup.py | 23 +++++++++++++++++++---- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 88a3fb78..ae047ed9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -# SageAttention +# SageAttention (Windows Blackwell Fork) + +> **Windows Build Fixes**: This fork includes critical fixes for compiling SageAttention on Windows with MSVC. Specifically, it resolves the `C2872: 'std': ambiguous symbol` error by appending `/Zc:preprocessor`, `/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING`, `-DUSE_CUDA`, and `-Usmall` to the compilation flags in `setup.py` and `sageattention3_blackwell/setup.py`. It also addresses thread-safety issues during concurrent builds by creating shallow copies of compiler flag lists (`CXX_FLAGS[:]`), preventing `TORCH_EXTENSION_NAME` from being inadvertently overwritten. + diff --git a/sageattention3_blackwell/setup.py b/sageattention3_blackwell/setup.py index 04e3888b..512446b1 100644 --- a/sageattention3_blackwell/setup.py +++ b/sageattention3_blackwell/setup.py @@ -108,6 +108,27 @@ def append_nvcc_threads(nvcc_extra_args): "-DCTA256", "-DDQINRMEM", ] + + cxx_flags = ["-O3", "-std=c++17"] + + import sys + if sys.platform == "win32": + win_flags = [ + "/Zc:preprocessor", + "/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING", + "-DUSE_CUDA", + "-Usmall" + ] + cxx_flags.extend(win_flags) + + nvcc_win_flags = [ + "-Xcompiler", "/Zc:preprocessor", + "-Xcompiler", "/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING", + "-DUSE_CUDA", + "-Usmall" + ] + nvcc_flags.extend(nvcc_win_flags) + include_dirs = [ repo_dir / "sageattn3", cutlass_dir / "include", @@ -119,7 +140,7 @@ def append_nvcc_threads(nvcc_extra_args): name="fp4attn_cuda", sources=["sageattn3/blackwell/api.cu"], extra_compile_args={ - "cxx": ["-O3", "-std=c++17"], + "cxx": cxx_flags[:], "nvcc": append_nvcc_threads( nvcc_flags + ["-DEXECMODE=0"] + cc_flag ), @@ -134,7 +155,7 @@ def append_nvcc_threads(nvcc_extra_args): name="fp4quant_cuda", sources=["sageattn3/quantization/fp4_quantization_4d.cu"], extra_compile_args={ - "cxx": ["-O3", "-std=c++17"], + "cxx": cxx_flags[:], "nvcc": append_nvcc_threads( nvcc_flags + ["-DEXECMODE=0"] + cc_flag ), diff --git a/setup.py b/setup.py index 6b2c5b43..5ae98d7e 100644 --- a/setup.py +++ b/setup.py @@ -60,6 +60,21 @@ "-diag-suppress=174", ] + # Windows specific flags + if sys.platform == "win32": + CXX_FLAGS += [ + "/Zc:preprocessor", + "/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING", + "-DUSE_CUDA", + "-Usmall" + ] + NVCC_FLAGS += [ + "-Xcompiler", "/Zc:preprocessor", + "-Xcompiler", "/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING", + "-DUSE_CUDA", + "-Usmall" + ] + # Append flags from env if provided cxx_append = os.getenv("CXX_APPEND_FLAGS", "").strip() if cxx_append: @@ -179,7 +194,7 @@ def get_nvcc_cuda_version(cuda_dir: str) -> Version: "csrc/qattn/pybind_sm80.cpp", "csrc/qattn/qk_int_sv_f16_cuda_sm80.cu", ], - extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS}, + extra_compile_args={"cxx": CXX_FLAGS[:], "nvcc": NVCC_FLAGS[:]}, ) ) @@ -197,7 +212,7 @@ def get_nvcc_cuda_version(cuda_dir: str) -> Version: "csrc/qattn/sm89_qk_int8_sv_f8_accum_f32_fuse_v_scale_attn_inst_buf.cu", "csrc/qattn/sm89_qk_int8_sv_f8_accum_f16_fuse_v_scale_attn_inst_buf.cu", ], - extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS}, + extra_compile_args={"cxx": CXX_FLAGS[:], "nvcc": NVCC_FLAGS[:]}, ) ) @@ -209,7 +224,7 @@ def get_nvcc_cuda_version(cuda_dir: str) -> Version: "csrc/qattn/pybind_sm90.cpp", "csrc/qattn/qk_int_sv_f8_cuda_sm90.cu", ], - extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS}, + extra_compile_args={"cxx": CXX_FLAGS[:], "nvcc": NVCC_FLAGS[:]}, extra_link_args=['-lcuda'], ) ) @@ -218,7 +233,7 @@ def get_nvcc_cuda_version(cuda_dir: str) -> Version: CUDAExtension( name="sageattention._fused", sources=["csrc/fused/pybind.cpp", "csrc/fused/fused.cu"], - extra_compile_args={"cxx": CXX_FLAGS, "nvcc": NVCC_FLAGS}, + extra_compile_args={"cxx": CXX_FLAGS[:], "nvcc": NVCC_FLAGS[:]}, ) ) From 9195f3ba4d00a1cb3a2f64aea4c6f322306c33db Mon Sep 17 00:00:00 2001 From: munder-sa Date: Fri, 20 Mar 2026 11:41:10 +0900 Subject: [PATCH 2/3] Fix Windows rpcndr.h 'small' macro conflict in sageattn3 --- sageattention3_blackwell/sageattn3/blackwell/api.cu | 5 +++++ .../sageattn3/quantization/fp4_quantization_4d.cu | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/sageattention3_blackwell/sageattn3/blackwell/api.cu b/sageattention3_blackwell/sageattn3/blackwell/api.cu index 196978f5..f2cdba1a 100644 --- a/sageattention3_blackwell/sageattn3/blackwell/api.cu +++ b/sageattention3_blackwell/sageattn3/blackwell/api.cu @@ -18,6 +18,11 @@ #include #include #include + +#ifdef _WIN32 +#undef small +#endif + #include #include diff --git a/sageattention3_blackwell/sageattn3/quantization/fp4_quantization_4d.cu b/sageattention3_blackwell/sageattn3/quantization/fp4_quantization_4d.cu index 381687ce..25baeaca 100644 --- a/sageattention3_blackwell/sageattn3/quantization/fp4_quantization_4d.cu +++ b/sageattention3_blackwell/sageattn3/quantization/fp4_quantization_4d.cu @@ -17,6 +17,11 @@ #include #include #include + +#ifdef _WIN32 +#undef small +#endif + #include #include #include From 3ecb520cd9fdaf457799627f009ee6fcea8707cd Mon Sep 17 00:00:00 2001 From: munder-sa Date: Fri, 20 Mar 2026 11:44:05 +0900 Subject: [PATCH 3/3] Update README with Windows Build Fixes info and Wheel requirements --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index ae047ed9..3009e796 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # SageAttention (Windows Blackwell Fork) > **Windows Build Fixes**: This fork includes critical fixes for compiling SageAttention on Windows with MSVC. Specifically, it resolves the `C2872: 'std': ambiguous symbol` error by appending `/Zc:preprocessor`, `/DCCCL_IGNORE_MSVC_TRADITIONAL_PREPROCESSOR_WARNING`, `-DUSE_CUDA`, and `-Usmall` to the compilation flags in `setup.py` and `sageattention3_blackwell/setup.py`. It also addresses thread-safety issues during concurrent builds by creating shallow copies of compiler flag lists (`CXX_FLAGS[:]`), preventing `TORCH_EXTENSION_NAME` from being inadvertently overwritten. +> +> ### Pre-built Wheel Requirements +> The `.whl` files provided in this repository are compiled specifically for the following environment: +> - **Python Version:** 3.12 +> - **PyTorch Version:** 2.10.0 +> - **CUDA Version:** 13.1 (compatible with cu130/cu131) +> - **OS:** Windows 10 / Windows 11 +> - **Supported Architectures:** RTX 3000 / 4000 / 5000 Series GPUs (Compute Capability: `sm_80`, `sm_89`, `sm_120a`)