Add fused matrix vector multiplication #271
Closed
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.
This PR introduces a fused version of matrix multiplication that can be used when the same
x
vector is reused across multiple differentW
s andOut
s. This improves the speed by better reusing thex
vector when it is in the cache.Speed for single threaded sees a nice 10% boost. Unfortunately multi-threaded performance saw a 6% decrease on the 42M model. This makes me hesitant to call this a clear win because of this. I have made the macro generate normal matmul calls when OpenMP is enabled so that fused is only used in single threaded mode.
This idea was inspired by the work of Foundation42 in #94 and based off the fused matrix multiplication found in llama2.zig I had done previously.
The use of macros in C is quite ugly, if it does not fit in this repo then let it be an example for others to learn from and improve upon. I have tried to keep the code changes to a minimum.
Make commands
Results
Taken by averaging the shown runs below. CPU is an AMD 5900x.
Runfast Fused
awk to get average:
Runfast Master
Runomp Master
Runomp Fused
Extra data on stories42M.bin