ggml-cpu: remove the per-matmul global barrier in ggml_compute_forward_mul_mat - #38
Open
codspeed-hq[bot] wants to merge 1 commit into
Conversation
…d_mul_mat The barrier at the start of every matmul only had to publish the src1 rows converted to vec_dot_type and to make thread 0's reset of the shared chunk counter visible. When no conversion is needed (src1->type == vec_dot_type) threads only read src0/src1 and write to disjoint parts of dst, so nothing has to be published. Move the barrier inside the conversion branch and give matmul a dedicated chunk counter that is always nth when a matmul starts: it is initialized once per graph and restored by the thread drawing the last ticket of the matmul (exactly nchunk tickets are handed out from nth, so nth + nchunk - 1 is provably the last one). Dynamic chunk stealing is preserved. The shared current_chunk counter is left untouched for the other chunked ops.
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ggml_compute_forward_mul_mat()ran a globalggml_barrier()on every matmul node, even when there was nothing to synchronize. The barrier had two jobs:src1rows converted tovec_dot_type(only needed when a conversion actually happens), andcurrent_chunk = nth) visible before any thread steals a chunk.For an F32 matmul (
src1->type == vec_dot_type) no conversion happens at all: threads only readsrc0/src1and write to disjoint parts ofdst. The barrier existed purely for the counter reset - a full n-thread rendezvous per matmul, and a transformer layer contains 9 of them.How
src1->type != vec_dot_typebranch, so it only runs when converted rows really have to be published.mul_mat_chunk) with the invariant "it is alwaysnthwhen a matmul starts":nth, a matmul hands out exactlynchunktickets (nchunk - nthremaining chunks + one out-of-range ticket per thread, which is how each thread leaves the loop), so the thread receivingnth + nchunk - 1is provably the last one to touch the counter and can reset it for the next matmul.current_chunkcounter is left untouched, so the other chunked ops (repack, sgemm, kleidiai, spacemit, ...) keep their current behaviour.Dynamic chunk stealing (and therefore load balancing) is preserved - only the rendezvous disappears. The per-node barrier in
ggml_graph_compute_thread()still separates consecutive nodes, so the restored counter is visible to every thread before the next matmul starts.Correctness
test-backend-ops -b CPU -o MUL_MAT: 1177/1177 tests pass against the reference implementation (theCONV_2Dset, which drives matmul throughggml_call_mul_mat, also passes: 1578/1578).n_threadsafter every graph execution (and that the assertion does fire if the reset is removed).-DGGML_SANITIZE_THREAD=ON -DGGML_OPENMP=OFF) reports no warnings.test-barrier,test-quantize-fns,test-rope,test-col2im-1dandtest-optall pass.Measured effect
CodSpeed simulation on the macro benchmarks (same machine, base vs head):
decode_layer[f32]decode_deep[f32]decode_layer[q4_0],decode_layer[q4_k],decode_layer[q8_0],prompt_layer[f32],prompt_layer[q4_k]The gain concentrates on graphs whose matmuls need no
src1conversion; quantized matmuls keep their (still necessary) publication barrier and are unchanged.Note: the simulation instrument amplifies synchronization costs (libgomp spins much longer when threads are serialized under Valgrind), so the walltime numbers from the macro-runner job on this PR are the ones to trust for the real-world magnitude.