Skip to content

CUDA: CUDA-core FMA path for GTX 16 (Turing without tensor cores) - #134

Open
PedroRossi wants to merge 4 commits into
RustingStudio:mainfrom
PedroRossi:pr/gtx16-cuda-core-fma
Open

PedroRossi wants to merge 4 commits into
RustingStudio:mainfrom
PedroRossi:pr/gtx16-cuda-core-fma

Conversation

@PedroRossi

@PedroRossi PedroRossi commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

The GTX 16 series (TU116/TU117: GTX 1630, 1650, 1660 and their Ti/SUPER variants) reports compute capability 7.5 but
has no tensor cores. It still executes mma.sync.aligned.m16n8k8, but far slower than the same work on its CUDA cores.
On these cards, the f16 prefill GEMM and the prefill attention, the two users of mma16816, run well below what the
card can do.

mma16816 already has a CUDA-core branch: the sm_70 fallback behind STRATA_GEMM_FMA. This PR lets sm_75 use it.

  • kernels.cu: on sm_75, mma16816 keeps its two m16n8k8 steps unless MMA_FMA is defined. With it, it takes
    the existing CUDA-core branch.
  • mod.rs: turing_fma() turns MMA_FMA on for sm_75 cards without tensor cores. CUDA doesn't report tensor cores,
    so no_tensor_cores() goes by the TU116/TU117 product names: the GTX 16 series, MX450/MX550, T400-T2000, CMP 30HX.
    STRATA_GEMM_FMA turns it on for any sm_75 card, and STRATA_GEMM_TC=1 keeps the tensor-core path.
    • The define is prepended to the kernel source, so the cubin cache key changes with it.
    • The startup line names the compiled path: sm_75 (Turing, CUDA-core FMA).
  • Tests:
    • turing_fma_only_for_turing_without_tensor_cores checks the selection rules, by name and flags, and the startup line.
    • cubin_cache_key_changes_with_mma_fma checks that a GTX 16 card can't load the tensor-core cubin.
    • nvrtc_compiles_full_source_for_sm75 now also compiles the sm_75 source with MMA_FMA.
  • Docs: GTX 1660 SUPER rows in SUPPORTED_GPUS.md (CUDA and Vulkan), and the sm_75 tier note.

Decode doesn't go through mma16816 and is unchanged. Turing cards with tensor cores (RTX 20, Quadro RTX, T4) keep m16n8k8.

Results

The test machine:

  • GPU: GeForce GTX 1660 SUPER 6 GB, driver 550.163.01, NVRTC 12.4.
  • Host: Ryzen 7 5700X, 32 GB DDR4-3000, Debian 13.
  • Model and settings: Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf, --ctx 32768, MTP off. With 2,494 of 10,240 experts
    resident, most expert work runs on the CPU.
  • The GPU clocks were locked at 1200 MHz for stability on this machine, so the absolute numbers are below stock.

rustingstrata bench --gpu --backend cuda --pp 4096,16384 --tg 128 --reps 2 --prompt-file <real text: project docs and C++ source>:

prefill pp 4096 prefill pp 16384 decode at 4096 decode at 16384
m16n8k8 (before; now STRATA_GEMM_TC=1) 103 tok/s 88 tok/s 48.5 tok/s 43.6 tok/s
CUDA-core FMA (this PR, the default on GTX 16) 214 tok/s 162 tok/s 50.0 tok/s 44.2 tok/s
for reference: Vulkan (_dp kernels, no cooperative matrices) 274 tok/s 188 tok/s 38.0 tok/s 33.6 tok/s
  • Prefill: 2.1x faster at 4K and 1.8x at 16K; decode is unchanged.
  • Why it's the tensor-core path: before the change, prefill scaled with the core clock (129.5 tok/s at 1530 MHz,
    102.8 at 1200 MHz) with the GPU at 100% utilisation, which points to a compute-bound kernel.
  • calibrate (pp 512/4096 on the same text, 22 samples) kept the defaults: chunk 4096, MTP off. MTP 1/2/4 scored 0.91-0.98.
  • Vulkan: rustingstrata check --backend vulkan passes on this card.

Tests

  • cargo test --release runs in a container with NVRTC 12.4 and no GPU, so the GPU and model tests skip themselves.
    • Selected tests pass: turing_fma_only_for_turing_without_tensor_cores, cubin_cache_key_changes_with_mma_fma,
      nvrtc_compiles_full_source_for_sm75 (with the new MMA_FMA compile), device_log_names_turing_not_volta and
      device_log_does_not_call_pascal_volta.
    • Full suite: 425 passed, 0 failed, 11 ignored (upstream's #[ignore] speed tests), across 31 test binaries.
      The library's own tests show 164 passed, 1 ignored.
  • GPU tests on the GTX 1660 SUPER, both paths:
test CUDA-core FMA (default here) STRATA_GEMM_TC=1 (m16n8k8)
kernels: gemm_matches_reference_for_every_type, small_batch_gemm_matches_tile_gemm pass pass
kernels: the four attn_prefill_* tests pass pass
gpu_model: gpu_forward_matches_cpu_reference, prefill_matches_reference (chunked prefill vs the CPU f32 reference) pass -
greedy generate, 2,405-token prompt, 64 tokens same tokens as m16n8k8; prefill 211.8 tok/s prefill 100.6 tok/s
  • The server (OpenAI and Anthropic APIs, image input) has run this build in production since the PR opened.

The GTX 16 series (TU116/TU117) is sm_75 but has no tensor cores. It still
executes mma.sync m16n8k8, far slower than CUDA cores: on a GTX 1660 SUPER
the f16 prefill ran about 3.5x slower than llama.cpp's.

On sm_75, a "GTX 16" device name or STRATA_GEMM_FMA now compiles the existing
CUDA-core branch of mma16816 (MMA_FMA), which covers the f16 prefill GEMM and
the prefill attention. STRATA_GEMM_TC=1 keeps the tensor-core path. The
startup line names the path that was compiled.
@aletheore

aletheore Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["src/cuda/mod.rs"]
    n1["backend.rs"]
    n2["src/hip/mod.rs"]
    n3["device.rs"]
    n4["lib.rs"]
    n5["src/vulkan/mod.rs"]
    n6["preflight.rs"]
    n7["tests/common/mod.rs"]
    n8["gpu_model.rs"]
    n9["kv_q8.rs"]
    n10["kv_turbo.rs"]
    n11["llama_numerics.rs"]
    n12["vulkan_split_kv.rs"]
    n0 --> n1
    n0 --> n2
    n0 --> n3
    n0 --> n4
    n0 --> n5
    n0 --> n6
    n0 --> n7
    n0 --> n8
    n0 --> n9
    n0 --> n10
    n0 --> n11
    n0 --> n12
    classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
    classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
    classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
    class n0 changed
    class n1 dependent
    class n2 dependent
    class n3 dependent
    class n4 dependent
    class n5 dependent
    class n6 dependent
    class n7 dependent
    class n8 dependent
    class n9 dependent
    class n10 dependent
    class n11 dependent
    class n12 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • docs/SUPPORTED_GPUS.md · modified · +4/-2
  • src/cuda/kernels.cu · modified · +3/-1
  • src/cuda/mod.rs · modified · +56/-3 · +4 functions · 12 other dependents

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 4

Comment thread src/cuda/mod.rs
fn turing_fma(name: &str, cc: (i32, i32), gemm_fma: bool) -> bool {
cc == (7, 5) && std::env::var_os("STRATA_GEMM_TC").is_none() && (gemm_fma || name.contains("GTX 16"))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/cuda/mod.rs
Line: 381
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 5e7755a. no_tensor_cores() now matches every TU116/TU117 product name: the GTX 16 series, MX450/MX550, T400-T2000 and CMP 30HX. The test covers those names, plus Turing cards that do have tensor cores (RTX 2070, Tesla T4, Quadro RTX 4000, TITAN RTX).

Comment thread src/cuda/mod.rs
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")?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/cuda/mod.rs
Line: 633
Symbol: new
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

/dismiss This can't happen here. ptx() names the cached cubin cache_key(src, arch, &tag), and src is the full kernel source with #define MMA_FMA 1 prepended, so the two variants hash to different files. 5e7755a adds cubin_cache_key_changes_with_mma_fma to pin that.

Comment thread src/cuda/mod.rs Outdated
#[test]
fn turing_fma_only_for_gtx16_on_sm75() {
// Reads STRATA_GEMM_TC; the test does not set it, so skip when the caller did.
if std::env::var_os("STRATA_GEMM_TC").is_some() { return; }

@aletheore aletheore Bot Nov 27, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No longer detected as of 5e7755af89ea.


🔵 Low · Maintainability · 3 of 5

The new unit test returns early and passes vacuously when STRATA_GEMM_TC is set. A developer who has STRATA_GEMM_TC exported in their shell runs cargo test. turing_fma_only_for_gtx16_on_sm75 returns before any assertion and reports success, so a regression in the GTX 16 gating goes unnoticed. The test should set or remove the variable itself, or test a pure helper that takes the flag as a parameter.

Prompt for your AI agent
File: src/cuda/mod.rs
Line: 1849
Symbol: min_arch_error_allows_volta_and_newer
Issue: The new unit test returns early and passes vacuously when STRATA_GEMM_TC is set. A developer who has STRATA_GEMM_TC exported in their shell runs cargo test. turing_fma_only_for_gtx16_on_sm75 returns before any assertion and reports success, so a regression in the GTX 16 gating goes unnoticed. The test should set or remove the variable itself, or test a pure helper that takes the flag as a parameter.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 5e7755a. turing_fma takes STRATA_GEMM_TC as a parameter (keep_tc), and the test passes both values explicitly, so the caller's environment no longer matters.

Comment thread src/cuda/kernels.cu
__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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/cuda/kernels.cu
Line: 1088
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 mma16816:

test CUDA-core FMA (default on this card) STRATA_GEMM_TC=1 (m16n8k8)
kernels: gemm_matches_reference_for_every_type, small_batch_gemm_matches_tile_gemm pass pass
kernels: the four attn_prefill_* tests pass pass
gpu_model: gpu_forward_matches_cpu_reference, prefill_matches_reference (chunked prefill vs the CPU f32 reference), on Qwen3.6-35B-A3B-UD-Q2_K_XL.gguf pass -
greedy generate, 2,405-token prompt, 64 tokens same tokens as m16n8k8; prefill 211.8 tok/s prefill 100.6 tok/s

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.

Comment thread docs/SUPPORTED_GPUS.md
@@ -45,7 +47,7 @@ row.
| Arch | Matrix multiply | Int8 GEMM | Async copy | Shared memory per block |
| --- | --- | --- | --- | --- |
| sm_80 and newer | `mma.m16n8k16` | yes | `cp.async` | 100 KB or more |

Copy link
Copy Markdown

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
File: docs/SUPPORTED_GPUS.md
Line: 49
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown
Author

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=1 selects the CUDA-core branch on any sm_75 card, and that STRATA_GEMM_TC=1 keeps m16n8k8.

@aletheore

aletheore Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Aletheore Flash Lite review

What this PR does. This pull request widens the Turing CUDA-core FMA fallback, which replaces the slower m16n8k8 tensor-core path for the f16 prefill GEMM and prefill attention, so it now applies to any sm_75 card without tensor cores (GTX 16, MX450/MX550, T400 to T2000, CMP 30HX) instead of only names containing "GTX 16". It also adds a STRATA_GEMM_FMA override for any sm_75 card, keeps STRATA_GEMM_TC=1 as the tensor-core opt-out, and updates the supported-GPU docs and unit tests to match.

Merge confidence Review effort Merge risk
3 / 5 2 / 5 Medium
Area Files Lines
docs 1 +2 −2
src/cuda 1 +39 −17

1 finding(s) posted as inline review comment(s). 4 earlier finding(s) from previous pushes still open. 1 earlier finding(s) no longer detected in this push.

# Severity Category Where
1 🔵 Low Maintainability mod.rs:1880 cubin_cache_key test passes for any prepended string, so it does not check the MMA_FMA define. The test…
- 🟠 High Correctness mod.rs:381 Earlier finding, still open
- 🟠 High Correctness mod.rs:633 Earlier finding, still open
- 🔵 Low Maintainability kernels.cu:1088 Earlier finding, still open
- 🔵 Low Maintainability SUPPORTED_GPUS.md:49 Earlier finding, still open
Review details

Reviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 1 of 1 proposed finding(s).

Fix all with your agent
File: src/cuda/mod.rs
Line: 1880
Symbol: cache_key_depends_on_nvrtc_tag
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

- Detect every Turing chip without tensor cores (TU116/TU117) by product name: the
  GTX 16 series, MX450/MX550, T400-T2000 and CMP 30HX, not only "GTX 16".
- turing_fma takes STRATA_GEMM_TC as a parameter, so its test no longer depends on
  the caller's environment (it returned early when the variable was set).
- New test: the cubin cache key changes with MMA_FMA, so a GTX 16 card can't load
  the tensor-core cubin.
- Docs: STRATA_GEMM_FMA also selects the CUDA-core branch on sm_75.
PedroRossi added a commit to PedroRossi/RustingStrata that referenced this pull request Nov 27, 2026
Comment thread src/cuda/mod.rs
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"));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 agent
File: src/cuda/mod.rs
Line: 1880
Symbol: cache_key_depends_on_nvrtc_tag
Issue: 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.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

PedroRossi added a commit to PedroRossi/RustingStrata that referenced this pull request Nov 27, 2026
…ustingStudio#135 (Messages API thinking blocks)

Integration branch for the ai-lab stack image; not for upstream.
PedroRossi added a commit to PedroRossi/portainer-stacks that referenced this pull request Nov 27, 2026
The fork's deploy/ai-lab branch now builds on upstream RustingStrata 1.4.2, with both
patches as revised in review (RustingStudio/RustingStrata#134 and #135).
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.

1 participant