[GH-ISSUE #5100] [Engine] nvcc ignores #pragma clang fp contract(off), so native_moe.cu gates the shared expert with a contracted multiply #2382

Open
opened 2026-11-04 01:58:51 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @ackalker on GitHub (Nov 3, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/5100

Area

Engine

Desktop app, Terminal or source?

Source (START-HERE.bat, setup.sh or a build) — a build from ./update.sh

What happened

The engine source says, in three places, that a multiply must be rounded on its own and must not fuse into an
addition, and it asks for that with #pragma clang fp contract(off). nvcc does not know that pragma: it prints
warning #20199-D: unrecognized #pragma in device code for the two sites it happens to warn about, and it keeps
contraction on. In native_moe.cu the product is contracted, so the gated shared-expert combine does not round the
way its comment says it does.

The two warnings in today's build:

/home/miki/Projects/git/Strata/src/kernels/cuda/verify_kernels.cu(305): warning #20199-D: unrecognized #pragma in device code
  #pragma clang fp contract(off)

/home/miki/Projects/git/Strata/src/kernels/cuda/iq_kernels.cu(3840): warning #20199-D: unrecognized #pragma in device code
  #pragma clang fp contract(off)

Five places use this pragma. Two of them handle it correctly, and the house pattern is already there:

  • src/kernels/cuda/iq_kernels.cu:2853-2855 and :2883-2885 wrap it in #if defined(__HIPCC__) (so it only reaches
    the HIP/clang compiler that understands it), and for CUDA they round by hand:
    iq_kernels.cu:2863 const float h = __fmul_rn(g / (1.0f + __expf(-g)), up[i]);
  • three are unguarded and so reach nvcc: src/kernels/cuda/native_moe.cu:58,
    src/kernels/cuda/verify_kernels.cu:305, src/kernels/cuda/iq_kernels.cu:3840

native_moe.cu:56-61 is the one where I can show what nvcc did:

    if constexpr (GATED) {
        // contraction off in this block only: the product is rounded on its own (scale_rows' store), then added
#pragma clang fp contract(off)
        const float g = __fdividef(1.0f, 1.0f + __expf(-sg[tk]));
        const float sh = shared[col] * g;
        sum += sh;

src/kernels/cuda/native_moe.cu:35-37 says this must match shared_expert_multi: "one rounded multiply =
scale_rows_kernel's out *= g, then added as before".

What I measured (my machine, not the model)

  1. The pragma changes nothing under nvcc. Two copies of the native_moe.cu:56-64 arithmetic, one with the pragma
    and one without, nvcc 13.4 -arch=sm_120 -ptx: the arithmetic instructions are identical, and both contain
    fma.rn.f32. nvcc printed #20199-D for the file that had the pragma.

  2. The built object has the fused form. cuobjdump -sass of the object this build produced,
    build/CMakeFiles/strata_kernels.dir/src/kernels/cuda/native_moe.cu.o, function
    ...combineILb1ELb1EEvPKfS4_S4_PfliS4_ (combine<true, true>, i.e. GATED=true), the end of the gated block:

    /*0bc0*/  FMUL.FTZ R0, R10, -1.4426950216293334961 ;
    /*0bd0*/  MUFU.EX2 R0, R0 ;
    /*0be0*/  FADD.FTZ R10, R0, 1 ;
    /*0bf0*/  MUFU.RCP R10, R10 ;
    /*0c00*/  FFMA.FTZ R35, R4, R10, R35 ;
    

    R10 is the sigmoid (EX2 + RCP of 1 + exp), R35 is the running sum, so the last line is
    sum = fma(shared[col], g, sum): one rounding, not the multiply-then-add the comment asks for.

  3. The in-house fix works. The same snippet with __fmul_rn(shared[col], g) instead of * gives
    mul.f32 and a separate add.f32, no fma.

Smallest steps that reproduce it

./update.sh            # the #20199-D lines are in the CUDA pass of the engine build
cuobjdump -sass build/CMakeFiles/strata_kernels.dir/src/kernels/cuda/native_moe.cu.o |
    awk '/combineILb1ELb1EEvPKfS4_S4_PfliS4_/,/Function :/' | grep -B4 FFMA

Reproduction status

Reproduced locally (the compilation and the disassembly). What I did not test: I did not run a model, so I have
no bitwise A/B of STRATA_LFUSE=1 against the separate scale_rows + add path, and I cannot say how many outputs
differ or whether any check in the repo already catches it. At the other two sites
(verify_kernels.cu:305, iq_kernels.cu:3840) I ran the same kind of snippet copy and nvcc 13.4 did not fuse the
product there, so today those two look harmless — the missing #if defined(__HIPCC__) guard there is latent, not a
measured difference. Marking the whole thing "measured for native_moe.cu only".

Suggested smallest fix

Use the pattern iq_kernels.cu already uses (iq_kernels.cu:2863): __fmul_rn for the product at the three unguarded sites (and
#if defined(__HIPCC__) around the pragma so the HIP build still gets it). Two build notes:

  • CMakeLists.txt:666-670 already does the
    same thing for HIP with -ffp-contract=off ("Keep Q8_K's multiply and magic-bias rounding as separate FP32
    operations on HIP; contraction to an FMA changes tie results"). There is no CUDA equivalent anywhere in the build,
    and -fmad=false would change every other expression in those files, so I would not go that way.
  • CMakeLists.txt:651-665 puts --use_fast_math on src/kernels/cuda/native_moe.cu (and 14 other
    native_*.cu files) for CUDA. nvcc documents --use_fast_math as implying --fmad=true, so for that file
    contraction is switched on by a flag as well as by the default. The flag part is from the nvcc documentation, not
    measured: my probe had contraction on with and without the flag.

Strata version

Strata 0.2.10 · engine 0.1.56 · source checkout at ffa80d73 (the pinned llama.cpp is ffa80d73, ggml 0.24.0)

Graphics card, memory and system

RTX 5080 16 GB · 96 GB RAM · Linux (Arch), gcc 16.2.1, CMake 4.4.3, ninja 1.13.2, CUDA/nvcc 13.4
(-DSTRATA_ENABLE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=120)

Originally created by @ackalker on GitHub (Nov 3, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/5100 ### Area Engine ### Desktop app, Terminal or source? Source (START-HERE.bat, setup.sh or a build) — a build from `./update.sh` ### What happened The engine source says, in three places, that a multiply must be rounded on its own and must not fuse into an addition, and it asks for that with `#pragma clang fp contract(off)`. nvcc does not know that pragma: it prints `warning #20199-D: unrecognized #pragma in device code` for the two sites it happens to warn about, and it keeps contraction on. In `native_moe.cu` the product is contracted, so the gated shared-expert combine does not round the way its comment says it does. The two warnings in today's build: ``` /home/miki/Projects/git/Strata/src/kernels/cuda/verify_kernels.cu(305): warning #20199-D: unrecognized #pragma in device code #pragma clang fp contract(off) /home/miki/Projects/git/Strata/src/kernels/cuda/iq_kernels.cu(3840): warning #20199-D: unrecognized #pragma in device code #pragma clang fp contract(off) ``` Five places use this pragma. Two of them handle it correctly, and the house pattern is already there: - `src/kernels/cuda/iq_kernels.cu:2853-2855` and `:2883-2885` wrap it in `#if defined(__HIPCC__)` (so it only reaches the HIP/clang compiler that understands it), and for CUDA they round by hand: `iq_kernels.cu:2863` `const float h = __fmul_rn(g / (1.0f + __expf(-g)), up[i]);` - three are unguarded and so reach nvcc: `src/kernels/cuda/native_moe.cu:58`, `src/kernels/cuda/verify_kernels.cu:305`, `src/kernels/cuda/iq_kernels.cu:3840` `native_moe.cu:56-61` is the one where I can show what nvcc did: ```cpp if constexpr (GATED) { // contraction off in this block only: the product is rounded on its own (scale_rows' store), then added #pragma clang fp contract(off) const float g = __fdividef(1.0f, 1.0f + __expf(-sg[tk])); const float sh = shared[col] * g; sum += sh; ``` `src/kernels/cuda/native_moe.cu:35-37` says this must match `shared_expert_multi`: "one rounded multiply = `scale_rows_kernel`'s `out *= g`, then added as before". ### What I measured (my machine, not the model) 1. **The pragma changes nothing under nvcc.** Two copies of the `native_moe.cu:56-64` arithmetic, one with the pragma and one without, `nvcc 13.4 -arch=sm_120 -ptx`: the arithmetic instructions are identical, and both contain `fma.rn.f32`. nvcc printed `#20199-D` for the file that had the pragma. 2. **The built object has the fused form.** `cuobjdump -sass` of the object this build produced, `build/CMakeFiles/strata_kernels.dir/src/kernels/cuda/native_moe.cu.o`, function `...combineILb1ELb1EEvPKfS4_S4_PfliS4_` (`combine<true, true>`, i.e. `GATED=true`), the end of the gated block: ``` /*0bc0*/ FMUL.FTZ R0, R10, -1.4426950216293334961 ; /*0bd0*/ MUFU.EX2 R0, R0 ; /*0be0*/ FADD.FTZ R10, R0, 1 ; /*0bf0*/ MUFU.RCP R10, R10 ; /*0c00*/ FFMA.FTZ R35, R4, R10, R35 ; ``` `R10` is the sigmoid (`EX2` + `RCP` of `1 + exp`), `R35` is the running `sum`, so the last line is `sum = fma(shared[col], g, sum)`: one rounding, not the multiply-then-add the comment asks for. 3. **The in-house fix works.** The same snippet with `__fmul_rn(shared[col], g)` instead of `*` gives `mul.f32` and a separate `add.f32`, no `fma`. ### Smallest steps that reproduce it ```sh ./update.sh # the #20199-D lines are in the CUDA pass of the engine build cuobjdump -sass build/CMakeFiles/strata_kernels.dir/src/kernels/cuda/native_moe.cu.o | awk '/combineILb1ELb1EEvPKfS4_S4_PfliS4_/,/Function :/' | grep -B4 FFMA ``` ### Reproduction status Reproduced locally (the compilation and the disassembly). **What I did not test:** I did not run a model, so I have no bitwise A/B of `STRATA_LFUSE=1` against the separate `scale_rows` + add path, and I cannot say how many outputs differ or whether any check in the repo already catches it. At the other two sites (`verify_kernels.cu:305`, `iq_kernels.cu:3840`) I ran the same kind of snippet copy and nvcc 13.4 did *not* fuse the product there, so today those two look harmless — the missing `#if defined(__HIPCC__)` guard there is latent, not a measured difference. Marking the whole thing "measured for `native_moe.cu` only". ### Suggested smallest fix Use the pattern `iq_kernels.cu` already uses (`iq_kernels.cu:2863`): `__fmul_rn` for the product at the three unguarded sites (and `#if defined(__HIPCC__)` around the pragma so the HIP build still gets it). Two build notes: - `CMakeLists.txt:666-670` already does the same thing for HIP with `-ffp-contract=off` ("Keep Q8_K's multiply and magic-bias rounding as separate FP32 operations on HIP; contraction to an FMA changes tie results"). There is no CUDA equivalent anywhere in the build, and `-fmad=false` would change every other expression in those files, so I would not go that way. - `CMakeLists.txt:651-665` puts `--use_fast_math` on `src/kernels/cuda/native_moe.cu` (and 14 other `native_*.cu` files) for CUDA. nvcc documents `--use_fast_math` as implying `--fmad=true`, so for that file contraction is switched on by a flag as well as by the default. The flag part is from the nvcc documentation, not measured: my probe had contraction on with and without the flag. ### Strata version Strata 0.2.10 · engine 0.1.56 · source checkout at `ffa80d73` (the pinned llama.cpp is `ffa80d73`, ggml 0.24.0) ### Graphics card, memory and system RTX 5080 16 GB · 96 GB RAM · Linux (Arch), gcc 16.2.1, CMake 4.4.3, ninja 1.13.2, CUDA/nvcc 13.4 (`-DSTRATA_ENABLE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=120`)
Author
Owner

@github-actions[bot] commented on GitHub (Nov 3, 2026):

Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing:

  • Fill in "Area".
  • Fill in "Desktop app, Terminal or source?".

Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment /reopen.

Details: CONTRIBUTING.md

<!-- gh-comment-id:6603499489 --> @github-actions[bot] commented on GitHub (Nov 3, 2026): Thanks for the issue. To keep the tracker readable, every item needs an area prefix in the title and the parts of the issue form filled in. This one is missing: - [ ] Fill in "Area". - [ ] Fill in "Desktop app, Terminal or source?". Edit the issue (title and description) and this note goes away on its own. If it is still missing parts in 5 days it will be closed, which is not a rejection: fix it and comment `/reopen`. Details: [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md)
Author
Owner

@github-actions[bot] commented on GitHub (Nov 3, 2026):

Thanks, this follows the format now.

<!-- gh-comment-id:6603721787 --> @github-actions[bot] commented on GitHub (Nov 3, 2026): Thanks, this follows the format now.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2382
No description provided.