mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #395] [CLOSED] Feature/nvidia p40 #2620
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#2620
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/395
Author: @rafal-prasal
Created: 10/1/2026
Status: ❌ Closed
Base:
main← Head:feature/nvidia_p40📝 Commits (2)
b8525f5NVIDIA TESLA P400261133only pre-Ampere should use prea-Ampere code📊 Changes
10 files changed (+369 additions, -53 deletions)
View changed files
📝
CMakeLists.txt(+65 -9)📝
Dockerfile(+30 -8)📝
README.md(+10 -1)📝
docs/DETAILS.md(+6 -4)📝
docs/MULTI_GPU.md(+32 -4)📝
include/strata/kernels/dp4a.hpp(+3 -3)📝
include/strata/prefill/gemm.hpp(+4 -0)📝
setup.py(+86 -20)📝
src/core/device.cu(+20 -4)📝
src/prefill/gemm.cu(+113 -0)📄 Description
Fix
STRATA_EXPERIMENTAL_SM60: the existing Pascal option cannot run on a Pascal cardBranch:
NVIDIA TESLA P40· Base:30ec18e(engine 0.1.30) · Size: +356 / −53 across 10 filesThe short version
CMakeLists.txtalready offers-DSTRATA_EXPERIMENTAL_SM60=ONanddp4a.hppalready ships bothkernel-level fallbacks. The scaffolding is there and it is dead code — a build made with it compiles
cleanly and then refuses to start on the only card it was written for. This PR finishes it, corrects it
(
sm_60→sm_61), and adds the one piece that was missing everywhere: a runtime path for the fact thatcuBLAS has no bf16 GEMM below
sm_80.Everything is opt-in. Default builds keep the
sm_75floor and are byte-for-byte unaffected.Why the existing option does not work
Four independent reasons, each verified against
30ec18e:1. The option is never propagated to a single source file.
It is never
add_compile_definitions'd, so it changes nothing for the code.2. The runtime floor is unconditional, so the binary refuses its own target card.
src/core/device.cu:138readsif (d.cc_major * 10 + d.cc_minor < 75). A Pascal build thereforecompiles successfully and then throws at startup:
3. There is no bf16 fallback, and one is required.
src/prefill/gemm.cu:352callscublasGemmExwithbf16 inputs unconditionally. cuBLAS's bf16 GEMM is an
sm_80feature and there is no flag that gives apre-Ampere card one — it is the library, not the compiler. On Pascal it returns
NOT_SUPPORTED.4. The option names the wrong card.
sm_60is GP100, which predates__dp4aentirely (that arrived in6.1). The viable datacenter card is
sm_61.So today the flag's only observable effect is that CMake stops refusing
CMAKE_CUDA_ARCHITECTURES=61— andthen the engine refuses to run on it.
What this PR changes
CMakeLists.txtSTRATA_EXPERIMENTAL_PASCAL; floor 61 (opt-in) vs 75; refuse a Pascal +sm_120mix (no toolkit compiles both); propagateSTRATA_EXPERIMENTAL_PASCAL=1— but only when the arch list actually contains a pre-sm_75target, so a build that passes the option with no Pascal arch still refuses 6.1 rather than claiming a card it holds no cubin for.src/prefill/gemm.cubf16_via_f32()path: both operands widened by an exact shift, then a tiledcublasSgemm. Gated on the runtime CC, because a fat binary can carry several archs and a Pascal build is expected to run on newer cards too.src/core/device.cusm_75build still refuses a P40 — a Pascal binary cannot be handed to a card nobody measured it on.setup.py/Dockerfile61/sm_120build.dp4a.hppsm_60/GP100).Why fp32 and not fp16.
f32_from_bf16is a shift, so the widening is exact. bf16 has 8 exponent bitsand fp16 has 5, so an fp16 round trip would silently overflow a weight above 65504 and flush one below ~6e-5.
The GEMM also keeps the same
CUBLAS_COMPUTE_32Faccumulator the Ampere path uses — this is not alower-precision answer, it is the same answer computed more slowly through more memory.
Why tiling. The scratch is sized in fp16 elements, so it holds half as many fp32 elements. X is
[T,K], W is[N,K], and a prompt chunk times K easily exceeds the scratch, so both dimensions are tiled.Each tile is an independent GEMM over the full K, so a tile boundary changes nothing arithmetically.
Validation
Numerically, on a real Tesla P40, against a double-precision CPU reference. The real
Gemm::bf16()fromthis branch was compiled
sm_61, linked, and run on the card: 22 cases, worst relative error 2.66e-11.Covers tiled and untiled shapes,
ldy > N(row padding must not be touched),beta0 and 1, andscratch-starved budgets down to
cap/K = 4.The suite has teeth — reintroducing the
betabug this PR fixed makes it fail at 4.6e18 on thebeta=0cases, because the caller's uninitialised
Yis folded into rows nothing has written yet.End-to-end, through the official Dockerfile path. The image builds an
sm_61engine from this branch(
BUILD.json:"archs": [61],"source": "local") with the stock CUDA 12.9 toolkit, and runs healthy onthe P40 — real 180B-class IQ3_S generation with speculative decoding, ~98% VRAM occupancy.
The gate, under real CMake. The architecture-gate block is exercised verbatim: 21/21 cases, including
that
STRATA_EXPERIMENTAL_PASCAL=1reaches the compile flags for61/62/70/74but not for ansm_120build that merely passed the option.Measured, for calibration
Tesla P40 (sm_61, 22.4 GB), IQ3_S, engine 0.1.30, one engine load. These are the engine's own
llama.cpp-style timings, the same ones
docs/DETAILS.mdreports:Against the documented RTX 5070 figures for the same model (prompt 427 / 913 / 1,624; decode 52.4 / 53.3 /
48.3) that is 0.51 / 0.34 / 0.22× on prompt and a steady ~0.60× on decode. Pascal has no tensor cores
at all, and fp16 buys only 1.26× over fp32 there because it is half-rate FMA rather than
mma. Decode holdsup better than the raw core ratio (~0.38×) because 22.4 GB of VRAM keeps 8,145 expert slots resident where a
12 GB card cannot.
This is a slow path and is documented as one. It is not a recommendation; it is for people who already
own the card.
Two corrections found on the way
A card's generation is not a card. The sm_61 band also contains a 2 GB GT 1030 — measured on this host,
nvidia-smireports compute capability 6.1 for it. It passes any architecture check and cannot hold any partof the model.
gpu_problem()therefore gained a 6 GB floor, so it is refused on size with a sentence a usercan act on rather than failing later inside the engine. Note this floor also catches small
sm_75+cards,which was a pre-existing hole.
A comment in
qsa.cuclaimed hardware behaviour that does not hold. It asserted that a Pascal cardreports 96 KB of opt-in shared memory, accepts
cudaFuncSetAttributefor it, then fails the launch.Measured on the P40:
MaxSharedMemoryPerBlockOptinis 49152 — identical to the non-optin attribute — and a64 KB request is refused outright. I had written a change on the strength of that comment; measuring it
showed the change was a no-op, so it is not in this PR and the file is byte-identical to base. The
pre-existing comment in
fused_gr.cumakes a similar claim and is left alone rather than silently rewritten— flagging it as a follow-up.
Safety
_min_cc = 75, no define emitted, no new branch taken.sm_120mix instead of emitting a binary that cannotexist.
bf16_via_f32is gated on runtime CC, so it is inert onsm_80+and on HIP (which is byte-identical —RDNA3/4 have bf16 in hardware).
Suggested commit split
Kept separate so the small fix is reviewable on its own:
Propagate STRATA_EXPERIMENTAL_PASCAL and fix the 6.1 runtime floor— the two-line bug fix for #1/#2 above.Add a pre-Ampere bf16 GEMM path (exact widen + tiled cublasSgemm)— the substantive change, with its tests.setup.py / Dockerfile: detect Pascal, require CUDA 12.x, refuse mixed 61+120.docs: P40 instructions, and a 6 GB VRAM floor— including theqsa.cunote as a comment for a follow-up.Honest limitations
available. IQ packs require
--native SHARD1, which bypassesprefill.cpp— sobf16_via_f32isvalidated numerically but not exercised by the end-to-end benchmark above. On a native pack what
actually costs this card its speed is llama.cpp's MMQ on DP4A instead of tensor cores, plus the existing
__CUDA_ARCH__ < 800fp32 fallbacks innative_qsa_scoreandqsa_prompt_attn.is the real ongoing cost of shipping this, and the most reasonable reason to decline it.
pool is a visible share of each decode round.
Reviewer's guide
The one thing worth reading closely is
bf16_via_f32insrc/prefill/gemm.cu. It has three easy-to-get-wrongdetails, each of which the tests cover: the
cublasSgemmis column-major (m= the N slice,n= the Tslice,
C's leading dimension stillldy); the output offset isY + n0 + t0 * ldy— an N slice moves arow, a T slice moves a column; and
betagoes to every tile, because a T tile is a disjoint set ofrows and an N tile a disjoint set of columns, so each element is written exactly once.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.