Repository navigation
CUDA: CUDA-core FMA path for GTX 16 (Turing without tensor cores) #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8ec65f7
f71f857
b3cddb9
5e7755a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1085,14 +1085,16 @@ __device__ __forceinline__ void ldsm4t(u32* r, const u16* p) { | ||||||||||||||||
| } | |||||||||||||||||
| __device__ __forceinline__ void mma16816(float* c, u32 a0, u32 a1, u32 a2, u32 a3, u32 b0, u32 b1) { | |||||||||||||||||
| #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800 | |||||||||||||||||
| #if __CUDA_ARCH__ >= 750 | |||||||||||||||||
| #if __CUDA_ARCH__ >= 750 && !MMA_FMA | |||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 4 of 5 The sm_75 MMA_FMA branch is only checked for NVRTC compilation, never for numerical equivalence with the tensor-core path. The FMA branch of mma16816 could compute the wrong product, such as a transposed B fragment or a wrong k-half offset. The new test only confirms that the source compiles for sm_75, so the wrong result would ship on GTX 16 cards unnoticed. A small GEMM comparing the FMA and m16n8k8 outputs would catch this. Prompt for your AI agentReply
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked on a GeForce GTX 1660 SUPER (driver 550.163.01, NVRTC 12.4) with 5e7755a, running the existing GPU tests that cover both users of
So the FMA branch matches both the reference and the tensor-core path. No new test, since the existing ones exercise this product; they just need a GPU, which CI doesn't have. |
|||||||||||||||||
| // Turing (sm_75) has no m16n8k16 f16 mma: the same product is two m16n8k8 steps, k = 0..7 and k = 8..15 | |||||||||||||||||
| // (the a0/a1, a2/a3 and b0, b1 fragments are those half-K fragments of the k16 layout). | |||||||||||||||||
| asm volatile("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};" | |||||||||||||||||
| : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) : "r"(a0), "r"(a1), "r"(b0)); | |||||||||||||||||
| asm volatile("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};" | |||||||||||||||||
| : "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) : "r"(a2), "r"(a3), "r"(b1)); | |||||||||||||||||
| #else | |||||||||||||||||
| // Also taken on sm_75 with MMA_FMA: the GTX 16 series (TU116/TU117) has no tensor cores and runs m16n8k8 far | |||||||||||||||||
| // slower than this CUDA-core version (see `turing_fma` in mod.rs). | |||||||||||||||||
| // D[16,8] += A[16,16] * B[16,8] on CUDA cores. mma.sync.aligned.m16n8k16 is sm_80+ (unlike ldmatrix, which is | |||||||||||||||||
| // sm_75+, hence the different guard). Lane l = 4*g + t (g = l >> 2, t = l & 3) owns A rows g / g+8 at the | |||||||||||||||||
| // k-halves {2t, 2t+1, 2t+8, 2t+9}, and B column g at those same k. The four accumulators are sums over all 16 | |||||||||||||||||
|
|
|||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,7 +215,8 @@ pub fn kv_q8() -> bool { kv_mode() == 1 } | |
| pub fn kv_legacy_symmetric(k: u32, v: u32) -> bool { k == v && k <= 3 } | ||
|
|
||
| /// `STRATA_GEMM_FMA`: force the CUDA-core FMA fallback in the f16 prefill kernels instead of the sm_70 tensor-core | ||
| /// path (`GEMM_TC` in kernels.cu). It covers both the f16 prefill GEMM and the prefill attention (`attn_prefill_t`): | ||
| /// path (`GEMM_TC` in kernels.cu), and on sm_75 instead of `m16n8k8` (`MMA_FMA`, see `turing_fma`; Turing cards without | ||
| /// tensor cores take it by default). It covers both the f16 prefill GEMM and the prefill attention (`attn_prefill_t`): | ||
| /// the attention's P staging tile is part of every request, so the shared-memory size does not depend on it. | ||
| /// Ignored on sm_80+, which always uses `mma.m16n8k16`. | ||
| pub fn gemm_fma() -> bool { std::env::var_os("STRATA_GEMM_FMA").is_some() } | ||
|
|
@@ -364,12 +365,29 @@ fn device_log(device: usize, name: &str, arch: &str, cc: (i32, i32), gemm_fma: b | |
| (c, _) if c < (7, 0) => " (pre-Volta, not supported)", // not reached from `Gpu::new`: `min_arch_error` | ||
| (c, false) if c < (7, 5) => " (Volta, m8n8k4)", | ||
| (c, true) if c < (7, 5) => " (Volta fallback)", | ||
| (c, true) if c < (8, 0) => " (Turing, CUDA-core FMA)", | ||
| (c, _) if c < (8, 0) => " (Turing, m16n8k8)", | ||
| _ => "", // sm_80+ always uses mma.m16n8k16 | ||
| }; | ||
| format!("cuda device {device}: {name}, {arch}{path}") | ||
| } | ||
|
|
||
| /// The Turing chips without tensor cores, TU116 and TU117: the GTX 16 series, MX450/MX550, the T400-T2000 workstation | ||
| /// and laptop cards and the CMP 30HX. CUDA does not report tensor cores, so the product name decides. | ||
| fn no_tensor_cores(name: &str) -> bool { | ||
| const TU11X: [&str; 10] = ["MX450", "MX550", "T400", "T500", "T550", "T600", "T1000", "T1200", "T2000", "30HX"]; | ||
| name.contains("GTX 16") || name.split_whitespace().any(|w| TU11X.contains(&w)) | ||
| } | ||
|
|
||
| /// Turing cards without tensor cores still execute `mma.sync` m16n8k8, but far slower than CUDA-core FMA: on a GTX | ||
| /// 1660 SUPER the f16 prefill ran ~3.5x slower than llama.cpp's. On sm_75 such a card, or `gemm_fma` | ||
| /// (`STRATA_GEMM_FMA`) on any sm_75 card, compiles the CUDA-core branch of `mma16816` instead (`MMA_FMA` in | ||
| /// kernels.cu, covering the f16 prefill GEMM and the prefill attention); `keep_tc` (`STRATA_GEMM_TC=1`) keeps the | ||
| /// tensor cores. | ||
| fn turing_fma(name: &str, cc: (i32, i32), gemm_fma: bool, keep_tc: bool) -> bool { | ||
| cc == (7, 5) && !keep_tc && (gemm_fma || no_tensor_cores(name)) | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 1 of 5 Name-based detection misses other TU116/TU117 cards (MX450, T600 and similar). A laptop with an MX450 or a Quadro T600 (TU117, no tensor cores) reports a name without the substring "GTX 16", so turing_fma is false and it runs the slow m16n8k8 path that this change is meant to avoid. Gating on the device name is fragile, and compute capability alone cannot separate TU116/TU117 from TU102/104/106. Prompt for your AI agentReply
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 5e7755a. |
||
| /// Whether the device has int8 tensor cores, i.e. whether the `gemmi_*`/`gemmis_*` prefill GEMM exists in its cubin | ||
| /// and may be launched. Volta (sm_70) does not: the s8 `mma` shapes need sm_80, so the kernels are compiled out of | ||
| /// the sm_70 build (see the `#if __CUDA_ARCH__ >= 800` guards in kernels.cu) and dropped from the loaded name list. | ||
|
|
@@ -614,9 +632,14 @@ impl Gpu { | |
| .unwrap_or(48 << 10).max(48 << 10) as u32; | ||
| let arch: &'static str = Box::leak(format!("sm_{maj}{min}").into_boxed_str()); | ||
| let gemm_fma = gemm_fma(); | ||
| crate::say!("{}", device_log(device, &ctx.name().unwrap_or_default(), arch, (maj, min), gemm_fma)); | ||
| let name = ctx.name().unwrap_or_default(); | ||
| let mma_fma = turing_fma(&name, (maj, min), gemm_fma, std::env::var_os("STRATA_GEMM_TC").is_some()); | ||
| let fma_path = if (maj, min) >= (7, 5) { mma_fma } else { gemm_fma }; // the branch actually compiled | ||
| crate::say!("{}", device_log(device, &name, arch, (maj, min), fma_path)); | ||
| let stream = ctx.new_stream()?; | ||
| let module = ctx.load_module(ptx(&source(kv_k_mode, kv_v_mode, !gemm_fma && (maj, min) < (7, 5)), arch, (maj, min))?).context("load cubin")?; | ||
| let src = source(kv_k_mode, kv_v_mode, !gemm_fma && (maj, min) < (7, 5)); | ||
| let src = if mma_fma { format!("#define MMA_FMA 1\n{src}") } else { src }; | ||
| let module = ctx.load_module(ptx(&src, arch, (maj, min))?).context("load cubin")?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 2 of 5 Module is built through ptx(&src, arch, cc); if that cache is keyed on arch and cc only, the MMA_FMA define is ignored. If ptx() memoises compiled output by (arch, cc) without the source text, a process that first compiles the default source on sm_75 and then loads the GTX 16 variant gets the tensor-core cubin. The FMA path is silently not used, and the device log still claims CUDA-core FMA. This cannot be verified from the diff. Prompt for your AI agentReply
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /dismiss This can't happen here. |
||
| let i8_gemm = int8_gemm_arch((maj, min)); | ||
| let turing = !int8_mma && (maj, min) >= (7, 5); | ||
| // The per-block dynamic shared memory ceiling the driver reports: 96 KiB on Volta per the roadmap, but | ||
|
|
@@ -1810,6 +1833,9 @@ mod tests { | |
| super::nvrtc::cubin(&super::source(2, 2, false), "sm_75").unwrap_or_else(|e| panic!("sm_75 kv_q4 must compile: {e:#}")); | ||
| super::nvrtc::cubin(&super::source(3, 3, false), "sm_75").unwrap_or_else(|e| panic!("sm_75 kv_q2 must compile: {e:#}")); | ||
| super::nvrtc::cubin(&super::source(4, 4, false), "sm_75").unwrap_or_else(|e| panic!("sm_75 kv_turbo4 must compile: {e:#}")); | ||
| // The GTX 16 build: the CUDA-core branch of mma16816 on sm_75 (see `turing_fma`). | ||
| let fma = format!("#define MMA_FMA 1\n{}", super::source(0, 0, false)); | ||
| super::nvrtc::cubin(&fma, "sm_75").unwrap_or_else(|e| panic!("sm_75 MMA_FMA (GTX 16) must compile: {e:#}")); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -1826,6 +1852,33 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn turing_fma_only_for_turing_without_tensor_cores() { | ||
| // TU116/TU117 cards, at sm_75 only; `gemm_fma` (STRATA_GEMM_FMA) turns it on for any sm_75 card and `keep_tc` | ||
| // (STRATA_GEMM_TC) off. Flags are passed in, so the caller's environment does not matter. | ||
| for name in ["NVIDIA GeForce GTX 1660 SUPER", "NVIDIA GeForce GTX 1650", "NVIDIA GeForce GTX 1650 Ti with Max-Q Design", | ||
| "NVIDIA GeForce MX450", "NVIDIA T600 Laptop GPU", "Quadro T1000", "NVIDIA T1000 8GB", "NVIDIA CMP 30HX"] { | ||
| assert!(super::turing_fma(name, (7, 5), false, false), "{name}"); | ||
| assert!(!super::turing_fma(name, (7, 5), false, true), "{name} with STRATA_GEMM_TC"); | ||
| } | ||
| for name in ["NVIDIA GeForce RTX 2070", "Tesla T4", "Quadro RTX 4000", "NVIDIA TITAN RTX", "NVIDIA RTX A2000"] { | ||
| assert!(!super::turing_fma(name, (7, 5), false, false), "{name}"); | ||
| } | ||
| assert!(super::turing_fma("NVIDIA GeForce RTX 2070", (7, 5), true, false)); // STRATA_GEMM_FMA | ||
| assert!(!super::turing_fma("NVIDIA GeForce GTX 1660 SUPER", (8, 6), false, false)); | ||
| assert!(!super::turing_fma("Tesla V100-SXM2-32GB", (7, 0), true, false)); | ||
| assert!(super::device_log(0, "x", "sm_75", (7, 5), true).contains("CUDA-core FMA")); | ||
| assert!(super::device_log(0, "x", "sm_75", (7, 5), false).contains("m16n8k8")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cubin_cache_key_changes_with_mma_fma() { | ||
| // A GTX 16 card must not load the tensor-core cubin from the cache: the define is in the source the key hashes. | ||
| let src = super::source(0, 0, false); | ||
| let fma = format!("#define MMA_FMA 1\n{src}"); | ||
| assert_ne!(super::cache_key(&src, "sm_75", "12.4"), super::cache_key(&fma, "sm_75", "12.4")); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 1 of 1 cubin_cache_key test passes for any prepended string, so it does not check the MMA_FMA define. The test prepends '#define MMA_FMA 1' to source(0, 0, false) and asserts the hash differs. Any prefix would change the hash, so the test still passes if the real GTX 16 source never gets the define, or if the define is applied to the wrong source. A regression in how the define is attached would go unnoticed. Prompt for your AI agentReply |
||
|
|
||
| #[test] | ||
| fn device_log_names_turing_not_volta() { | ||
| assert!(super::device_log(0, "x", "sm_75", (7, 5), false).contains("Turing")); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Low · Maintainability · 5 of 5
The sm_75 row says nothing about STRATA_GEMM_FMA, but on sm_75 that variable now also switches the kernel branch. turing_fma returns true for any sm_75 card when gemm_fma is set, so an RTX 2070 user who sets STRATA_GEMM_FMA=1, which the row for sm_70 documents as Volta-only, now gets the CUDA-core path. Their performance changes and the documentation does not explain why.
Prompt for your AI agent
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5e7755a. The sm_75 tier row now says that
STRATA_GEMM_FMA=1selects the CUDA-core branch on any sm_75 card, and thatSTRATA_GEMM_TC=1keepsm16n8k8.