Repository navigation
Enable Fused-Multiply-Add (FMA) and F16C/CVT16 vector extensions on MSVC - #375
Conversation
|
It seems I'm too tired to find the button for converting to a draft, but anyway. |
|
I haven't checked out the compiled output at the disassembly level yet, so especially in the case of F16C there is the consideration as to which extent the compiler had already optimized the generic ggml_compute_fp16_to_fp32 and fp32_to_fp16 to use the cvt/f16c instructions. The answer to that question also answers the question whether this change can bring possibly a significant performance increase or do pretty much nothing at all. |
|
Avx2/avx512 also implies all the simd instructions being enabled like sse3 |
Yeah, but the e: i guess its a good addition anyway if possibly used in the future. won't hurt. |
It doesn't, that is the entire point. |
|
Do you observe improved performance with this change? |
|
This got FMA enabled while building from VS, windows, on i7 8th gen. However, time per token seems to be the same (under 1% diff) |
I'll have to take a in-depth look later analysing the binary code and timing the performance, until then no idea. In the case of FMA the difference between That didn't really answer your question. 😄 Thanks @lofcz for providing some initial testing. If anyone else wants to chip in with their results including the processor and model parameters of the test, that'd be greatly appreciated. I'd expect this to increase performance in range of +0% to +X%, but especially important would be to make sure that this will not decrease performance in any case. |
|
These are my runtimes on my Ryzen 4500U (Zen 2) Without FMA is built upon e4412b4 while with FMA just adds on the commits in this pull request Without FMA runs faster? Values are from tokens / ms here: EDIT: |
|
I've tried to do the same as @nicknitewolf I have an Intel Xeon W-2295 So I guess on my system there is little to no influence on the performance for the eval time, however the sample time seems to be a bit better. However, the sample time has little effect on the total time. The original systems information and loading timings for the 7B and 65B are: |
|
Huge thanks @nicknitewolf and @KASR providing some statistics. 👍 🥳 I've concluded that unfortunately as my CPU is dog and only has 4 threads total, I can't provide useful statistics myself since even It seems that while @KASR's results are inconclusive being inside margin of error, @nicknitewolf did produce a significant 6.24% increase in performance. @KASR could you run the tests with |
__FMA__ macro does not exist in MSVC
__F16C__ macro does not exist in MSVC, but is implied with AVX2/AVX512
even though it's not currently used for anything when AVX is defined
d1e4a18 to
b8a80f9
Compare
|
Rebased the branch to master for easier testing. |
Ah I should have done that, setting a specified thread count, maybe thats why my results vary so much |
|
Even based on your tests alone I would think that merging this is a good idea. All the other platforms use the extensions already and MSVC is also supposed to use them as they are implied with /arch:AVX2, the only problem here is really the #define not being set so MSVC takes a slower codepath than all the other compilers. The reason for variance is probably just Windows, unfortunately, and there is not much to be done about it except running more tests to decrease their significance. I'm actually still maining Windows 7 for exactly this reason, since a fresh Win7 installation with the crap cut down runs under 100 threads total when idle. ~5 Watt and ~0,5% CPU usage. Windows 10 on the other hand has 800+ at any given time and can go up into the thousands when some updates or whatever Store/Xbox/Cortana nonsense is going in the background. Open your task manager and see ur threadcount when you are supposedly doing nothing and see all the bloat eating up your cpu cycles. It's really hard to properly perftest anything in the modern windowses and most of the crap is baked in so heavily into the system that its nigh-impossible to remove it all without crippling the OS. That being said, I do not recommend maining Win7 anymore since hardware and software support is on it's very last legs. Unfortunate, since the OS itself is pretty much perfect and 100% stable, haven't had a system crash or even a malfunction in years. |
|
@slaren And looking at the current alternative paints a pretty clear picture 😄 After PR GGML_COMPUTE_FP16_TO_FP32 PROC ; COMDAT
movzx eax, cx
vmovd xmm0, eax
vcvtph2ps xmm0, xmm0
ret 0
GGML_COMPUTE_FP16_TO_FP32 ENDP
GGML_COMPUTE_FP32_TO_FP16 PROC ; COMDAT
vmovaps xmm1, xmm0
vxorps xmm0, xmm0, xmm0
vmovss3 xmm1, xmm0, xmm1
vcvtps2ph xmm2, xmm1, 0
vpextrw eax, xmm2, 0
ret 0
GGML_COMPUTE_FP32_TO_FP16 ENDPBefore PR GGML_COMPUTE_FP16_TO_FP32 PROC ; COMDAT
movzx eax, cx
shl eax, 16
mov edx, eax
and edx, -2147483648 ; 80000000H
lea ecx, DWORD PTR [rax+rax]
mov eax, ecx
shr eax, 4
add eax, 1879048192 ; 70000000H
mov DWORD PTR fp32$3[rsp], eax
mov eax, ecx
shr eax, 17
or eax, 1056964608 ; 3f000000H
mov DWORD PTR fp32$2[rsp], eax
cmp ecx, 134217728 ; 08000000H
jae SHORT $LN5@GGML_COMPU
vmovss xmm0, DWORD PTR fp32$2[rsp]
vsubss xmm1, xmm0, DWORD PTR __real@3f000000
vmovss DWORD PTR tv87[rsp], xmm1
mov eax, DWORD PTR tv87[rsp]
or eax, edx
mov DWORD PTR fp32$1[rsp], eax
vmovss xmm0, DWORD PTR fp32$1[rsp]
ret 0
$LN5@GGML_COMPU:
vmovss xmm0, DWORD PTR fp32$3[rsp]
vmulss xmm1, xmm0, DWORD PTR __real@07800000
vmovss DWORD PTR tv87[rsp], xmm1
mov eax, DWORD PTR tv87[rsp]
or eax, edx
mov DWORD PTR fp32$1[rsp], eax
vmovss xmm0, DWORD PTR fp32$1[rsp]
ret 0
GGML_COMPUTE_FP16_TO_FP32 ENDP
GGML_COMPUTE_FP32_TO_FP16 PROC ; COMDAT
$LN17:
sub rsp, 56 ; 00000038H
vmovaps XMMWORD PTR [rsp+32], xmm6
vmovaps xmm6, xmm0
vcvtss2sd xmm0, xmm6, xmm0
vmovq rcx, xmm0
call fabsf
vmovd r8d, xmm6
vmovaps xmm6, XMMWORD PTR [rsp+32]
mov ecx, 1895825408 ; 71000000H
vxorps xmm1, xmm1, xmm1
vcvtsi2ss xmm1, xmm1, eax
vmulss xmm2, xmm1, DWORD PTR __real@77800000
vmulss xmm3, xmm2, DWORD PTR __real@08800000
lea edx, DWORD PTR [r8+r8]
and r8d, -2147483648 ; 80000000H
mov eax, edx
and eax, -16777216 ; ff000000H
cmp eax, ecx
cmovb eax, ecx
shr eax, 1
add eax, 125829120 ; 07800000H
mov DWORD PTR fp32$1[rsp], eax
vaddss xmm1, xmm3, DWORD PTR fp32$1[rsp]
vmovd ecx, xmm1
mov eax, ecx
and ecx, 4095 ; 00000fffH
shr eax, 13
and eax, 31744 ; 00007c00H
add eax, ecx
mov ecx, 32256 ; 00007e00H
cmp edx, -16777216 ; ff000000H
cmova ax, cx
shr r8d, 16
or ax, r8w
add rsp, 56 ; 00000038H
ret 0
GGML_COMPUTE_FP32_TO_FP16 ENDP |
|
Hold merging this until #546 is merged. |
Yes sure, I've updated to the newest commit (at the time of writing) and enabled AVX512. I only performed with the 7B model, let me know if it's interesting to also see the results for the 65B. I've used the command: Original settings: After adjustments: I've also added the results using t 20, ( i list the xx ms/token as value ): so using t4 --> 6.55% speedup (which is very close to the value @nicknitewolf had) |
|
Much appreciated ! 👍 Thanks to everyone taking part to this, with special thanks to @nicknitewolf and @KASR to take the time to do benchmarks. While a 5% speed increase might not be very noticeable on its' own, all the performance increases add up and are important as parts of the big picture. After such well made research we can be definitely confident that this PR is a go. I'll merge this right after the CI is fixed #546 |
…SVC (ggml-org#375) * Enable Fused-Multiply-Add (FMA) instructions on MSVC __FMA__ macro does not exist in MSVC * Enable F16C/CVT16 vector extensions on MSVC __F16C__ macro does not exist in MSVC, but is implied with AVX2/AVX512 * MSVC cvt intrinsics * Add __SSE3__ macro for MSVC too because why not even though it's not currently used for anything when AVX is defined
…SVC (ggml-org#375) * Enable Fused-Multiply-Add (FMA) instructions on MSVC __FMA__ macro does not exist in MSVC * Enable F16C/CVT16 vector extensions on MSVC __F16C__ macro does not exist in MSVC, but is implied with AVX2/AVX512 * MSVC cvt intrinsics * Add __SSE3__ macro for MSVC too because why not even though it's not currently used for anything when AVX is defined
Co-authored-by: Iwan Kawrakow <[email protected]>
…SVC (ggml-org#375) * Enable Fused-Multiply-Add (FMA) instructions on MSVC __FMA__ macro does not exist in MSVC * Enable F16C/CVT16 vector extensions on MSVC __F16C__ macro does not exist in MSVC, but is implied with AVX2/AVX512 * MSVC cvt intrinsics * Add __SSE3__ macro for MSVC too because why not even though it's not currently used for anything when AVX is defined
…SVC (ggml-org#375) * Enable Fused-Multiply-Add (FMA) instructions on MSVC __FMA__ macro does not exist in MSVC * Enable F16C/CVT16 vector extensions on MSVC __F16C__ macro does not exist in MSVC, but is implied with AVX2/AVX512 * MSVC cvt intrinsics * Add __SSE3__ macro for MSVC too because why not even though it's not currently used for anything when AVX is defined
… in capabilities (hydra_vortex ggml-org#375 ggml-org#376) ## ggml-org#375 — advertise combined/pipeline in engine_info server-context.cpp: the engine_info (0x41) handler now lists 'combined' and 'pipeline' in the capabilities set. The build that ships this binary is always the llama-engine fork, which carries the COMBINED code path; this is the engine-side mirror of the static workers.json fields. ## ggml-org#376 — fail-open on RPC handshake ggml-rpc.cpp: RPC_STATUS_ASSERT in negotiate_hello (line 363) replaced with if(!status){log; return false;}. The TCP connection succeeds before the RPC server's HELLO handler is ready, especially under compose where two llama-engine instances start simultaneously. Aborting on a transient HELLO failure crashes the head for no reason — returning false lets the caller (get_socket -> get_device_count -> add_server) return 0 devs, which keeps the head alive and degrades to solo instead. Refs: ddvnguyen/hydra_vortex#375, ddvnguyen/hydra_vortex#376 Co-Authored-By: Claude Opus 4.8 <[email protected]>
* fork: bind-on-activation + registry epoch + ne-guard + fail-open (hydra_vortex ggml-org#368) Makes COMBINED zero-copy binding (llama.cpp#20) swap-safe and re-callable in preparation for dynamic quant swap (ggml-org#263, SWAP_QUANT 0x45). Also fixes the COMBINED startup race (hydra_vortex ggml-org#357) by binding on demand at SET_EXPERT_MODE("combined") instead of one-shot at startup. Forward-compatible slice per docs/dynamic-quant-swap-combined.md: this lands the protocol + binding + fail-open + tests now, so ggml-org#263 can plug in the actual model free/reload against a stable contract. ## What ### 1. Registry epoch (ggml-rpc.cpp) - rpc_msg_resolve_tensor_rsp gets a new uint32_t registry_epoch; the server returns its g_hydra_registry_epoch with every resolve. - g_hydra_registry_epoch (atomic<uint32_t>) is bumped on every ggml_backend_rpc_clear_local_tensors() call. A head re-checks ggml_backend_rpc_get_remote_registry_epoch() before each use of a binding; mismatch = stale = the head must rebind or stay solo. ### 2. ggml_backend_rpc_clear_local_tensors New public proc. Clears the local-tensor registry and bumps the epoch. Idempotent. Foreign-buffer clearing is a defense-in-depth follow-up (the head's epoch check is the primary safety). ### 3. ggml_backend_rpc_get_remote_registry_epoch New public proc. Probes the peer's current epoch over RPC (reuses RPC_CMD_RESOLVE_TENSOR with a sentinel name and reads back the epoch field of the response — single round trip). Returns 0 on any failure; callers must treat 0 as "couldn't verify" not "epoch is 0". ### 4. Fail-open in ggml_backend_rpc_bind_remote_tensor Replaced RPC_STATUS_ASSERT(status) (which calls GGML_ABORT) with "if (!status) { log; return nullptr; }" — peer drop, malformed response, or transient RPC error must NOT crash the engine. The bind result is still observable: nullptr means "fall back to solo". ### 5. ne-guard ggml_backend_rpc_bind_remote_tensor takes a new const uint32_t* expected_ne parameter (may be NULL to skip). After resolve, the response's ne[] is compared element-wise; mismatch = refuse the bind (return nullptr) — only type/nb (the quant layout) may differ between bindings; ne (the count along each axis) is structural. A head bound to a different peer's resident model fails loud, not by running on the wrong weights (which would corrupt the KV cache silently). ### 6. llama_hydra_rebind_combined_experts (new) Extracted from llama_hydra_load_combined_experts. Reuses the per-peer backend (which is registered once at startup), frees the previous binding's meta_ctx, allocates a new one, and re-runs the bind loop with the new epoch + ne-guard. Per-peer resource map (s_hydra_combined_bindings) owns the meta_ctx — replacing an entry drops the previous ctx (the per-rebind leak fix; old code pushed ctxs into a static vector and never freed). ### 7. llama_hydra_register_local_tensors_for_rpc re-callable Now calls ggml_backend_rpc_clear_local_tensors before the registration loop. First call: clear is a no-op (epoch 0 → 1). Subsequent calls (after a model swap / re-register on the peer) bump the epoch again, so any outstanding binding becomes observably stale. ### 8. SET_EXPERT_MODE(combined) handler now binds on activation server-context.cpp:3504. Replaces the implicit reliance on a startup-only binding. On each SET_EXPERT_MODE("combined") the head calls llama_hydra_rebind_combined_experts and degrades to solo if the rebind fails (peer unreachable, ne-guard, RPC error). This is the ggml-org#357 fix: the head binds on demand when the peer is reachable, every time. No abort path. ## Tests (test-hydra-rpc-bind.cpp) - original round-trip (zero-copy read of server's live tensor) - registry_epoch in resolve response - ne-guard: rejected bind with mismatched shape (returns null) - ne-guard: NULL expected_ne skips the check (legacy callers) - missing name: returns null - clear → epoch bumps; re-register → epoch unchanged (only clear bumps) - re-bind after clear+re-register succeeds and observes the new epoch - fail-open: bind to unreachable endpoint returns null (no abort) - re-callable register is monotonic (no spurious epoch bump) Run: `./bin/test-hydra-rpc-bind` → "OK: zero-copy RESOLVE_TENSOR/ bind_remote_tensor round-trips correctly (incl. ggml-org#368 epoch, ne-guard, fail-open, re-callable register)" ## Build verification - sm_120 (CUDA 13.2): `cmake --build build_sm120 --target llama-engine test-hydra-rpc-bind` — clean. Test runs and passes. - sm_60 build deferred to P100 host (the AGENTS.md note about the gcc-15 / cuda-12.9 toolchain incompatibility is unchanged). ## Out of scope (lands with ggml-org#263) The actual SWAP_QUANT model free/reload + re-register on the engine (0x45 handler body) — slots into this contract once built. The Core side (WorkerSchedulerService SWAPPING state) is in the parent repo (ddvnguyen/hydra_vortex#368 §3). ## Cross-repo - ddvnguyen/hydra_vortex#368 (parent issue) - ddvnguyen/hydra_vortex#366 (merged — the SOLO_BUSY/COMBINED_SERVING state machine foundation that ggml-org#368 §3 extends) - ddvnguyen/hydra_vortex#357 (startup race — fixed here by bind-on- activation) - #22 (fork-side tracker) AI assistance note: this commit was produced with AI assistance (Claude) under explicit owner direction. Per AGENTS.md the private fork ddvnguyen/llama.cpp is exempt from the upstream llama.cpp AI- submission policy. Co-Authored-By: Claude Opus 4.8 <[email protected]> * fix(rpc): set registry_epoch on not-found path; bump RPC_PROTO_MINOR_VERSION to 1 P1: rpc_server::resolve_tensor left response.registry_epoch uninitialised on the not-found early return. ggml_backend_rpc_get_remote_registry_epoch probes with a sentinel name that is never found, so it was reading indeterminate stack bytes instead of the real epoch. Fix: populate registry_epoch before the not-found return, inside the lock where g_hydra_registry_epoch is already loaded for the found path. P2: rpc_msg_resolve_tensor_rsp gained a uint32_t registry_epoch field but RPC_PROTO_MINOR_VERSION was not bumped. A new RTX head talking to an old P100 server (not yet rebuilt) would size-mismatch on RESOLVE_TENSOR and silently fail-open to permanent solo with no diagnostic. Fix: bump minor to 1 and tighten negotiate_hello to require an exact minor match (reject both older AND newer peers) so the mismatch is diagnosed at hello, not buried in a silent fail-open. Fixes review findings P1 and P2 on #23. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * fix: fail-open on RPC handshake (HELLO) + advertise combined/pipeline in capabilities (hydra_vortex ggml-org#375 ggml-org#376) ## ggml-org#375 — advertise combined/pipeline in engine_info server-context.cpp: the engine_info (0x41) handler now lists 'combined' and 'pipeline' in the capabilities set. The build that ships this binary is always the llama-engine fork, which carries the COMBINED code path; this is the engine-side mirror of the static workers.json fields. ## ggml-org#376 — fail-open on RPC handshake ggml-rpc.cpp: RPC_STATUS_ASSERT in negotiate_hello (line 363) replaced with if(!status){log; return false;}. The TCP connection succeeds before the RPC server's HELLO handler is ready, especially under compose where two llama-engine instances start simultaneously. Aborting on a transient HELLO failure crashes the head for no reason — returning false lets the caller (get_socket -> get_device_count -> add_server) return 0 devs, which keeps the head alive and degrades to solo instead. Refs: ddvnguyen/hydra_vortex#375, ddvnguyen/hydra_vortex#376 Co-Authored-By: Claude Opus 4.8 <[email protected]> * fix: bind-on-activation and OT-pattern stripping for COMBINED mode Two bugs prevented SET_EXPERT_MODE("combined") from ever activating when the peer (RTX 3060) came up after the head (RTX 5060 Ti) had already started: 1. server-context.cpp: want_combined was gated on hydra_combined_head_attached, which is only set true when the startup llama_hydra_load_combined_experts call succeeds. If the peer was down at boot, the flag stayed false and the entire bind-on-activation code path (ggml-org#368) was unreachable. Fixed by gating on "is the engine configured as a combined head" (non-empty peer addr + OT pattern) instead, which correctly reflects what the engine is configured to do rather than an accident of startup ordering. Also latches hydra_combined_head_attached=true on first successful rebind so INFO RPC advertises combined capability. 2. llama-hydra.cpp: tensor_pattern from --combined-ot-pattern uses the override-tensor format "<name_regex>=<backend>" (e.g. "blk\.([0-9]+)\.ffn_.*_exps\.weight=CPU"), but the regex was matched against raw tensor names which never contain "=CPU". Fixed by stripping everything from the last '=' onward before compiling the regex. With both fixes applied: 120/120 expert tensors across 40 layers are bound to the peer on the first COMBINED request, and the engine reports applied='combined' instead of falling back to solo. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Hydra Engineering <hydra-engineering@local> Co-authored-by: Claude Opus 4.8 <[email protected]>
cuda: make the mul_mat bias, residual and GLU epilogue fusions observable














__FMA__and__F16C__are defined in GCC and Clang__FMA__and__F16C__are not defined in MSVC, however they are implied with AVX2/AVX512https://learn.microsoft.com/en-us/cpp/build/reference/arch-x64?view=msvc-160
Thus, enable FMA and F16C in MSVC if either AVX2/AVX512 is enabled