Skip to content

Skip barriers of noops#19

Merged
ikawrakow merged 1 commit intomainfrom
ik/skip_noop_barriers
Aug 14, 2024
Merged

Skip barriers of noops#19
ikawrakow merged 1 commit intomainfrom
ik/skip_noop_barriers

Conversation

@ikawrakow
Copy link
Copy Markdown
Owner

GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_PERMUTE, GGML_OP_TRANSPOSE, along with GGML_OP_NONE, are all noops in ggml. I.e., nothing happens. But ggml still has a thread barrier after them, which wastes time. The waste is not too bad for large models where computations are long compared to the time taken for thread synchronization. But for small models skipping those unnecessary waits makes a noticeable difference.

Let's look at a really tiny model - the 99M parameter TriLM ternary model quantized with IQ2_TN. The following table compares performance for PP-512 and TG-128 with and without the change in this PR

CPU threads test t/s (main) t/s (PR) Speedup
Ryzen-7950X 16 pp512 11386.75 ± 19.08 11587.58 ± 34.26 1.018
Ryzen-7950X 8 tg128 1312.25 ± 1.02 1460.80 ± 1.69 1.113
M2-Max 8 pp512 7642.81 ± 22.07 7680.29 ± 9.29 1.005
M2-Max 8 tg128 992.83 ± 18.17 1096.47 ± 14.45 1.104

So, basically, for such a small model ggml spends 10% of its time waiting for threads to pass through a barrier after a noop when generating tokens.

There are other barriers that can be eliminated. E.g., the typical attention block involves matrix multiplications of the Q, K and V tensors with the same activations, so there is no need to synchronize threads after each such matrix multiplications. In a similar way, in the feed-forward portion of the network the ffn_up and ffn_gate tensors multiply the same activations, so one can save another barrier there. This is left for a future PR.

GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_PERMUTE, GGML_OP_TRANSPOSE,
along with GGML_OP_NONE, are all noops. I.e., nothinh happens.
But ggml still has a barrier after them, which wastes time.
The waste is not too bad for large models where computations are
long compared to the time taken for thread synchronization.
But for small models skipping those unnecessary waits makes
a significant difference. E.g., for the 99M TriLMamodel,
TG-500 goes up to 1426 t/s from 1240 t/s.
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