-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Create PMLL.c #819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drQedwards
wants to merge
1
commit into
karpathy:master
Choose a base branch
from
drQedwards:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create PMLL.c #819
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /******************************************************************** | ||
| PMLL.c — Persistent-Memory Logic Loop (CPU reference) | ||
| • ring-buffer KV store per attention head | ||
| • zero external dependencies | ||
| • thread-safe if you call it from a single OpenMP thread per batch | ||
| • MIT licence (same as llm.c) | ||
| *********************************************************************/ | ||
|
|
||
| #include "pmll.h" /* public ABI */ | ||
| #include <stdlib.h> /* malloc / free */ | ||
| #include <string.h> /* memcpy / memset */ | ||
|
|
||
| /*--------------------------------------------------------------- | ||
| Life-cycle helpers | ||
| ----------------------------------------------------------------*/ | ||
|
|
||
| /* Allocate and zero-initialise the ring buffers. | ||
| Return 0 on success, −1 on out-of-memory. */ | ||
| int pmll_init(pmll_state *S, int NH, int hs) | ||
| { | ||
| S->T = 0; | ||
| S->hs = hs; | ||
|
|
||
| const size_t bytes = (size_t)NH * MAX_MEM_T * hs * sizeof(float); | ||
|
|
||
| S->k = (float*)calloc(1, bytes); | ||
| S->v = (float*)calloc(1, bytes); | ||
|
|
||
| return (S->k && S->v) ? 0 : -1; | ||
| } | ||
|
|
||
| /* Discard history but keep the allocated buffers. */ | ||
| void pmll_reset(pmll_state *S) { S->T = 0; } | ||
|
|
||
| /* Free device memory. */ | ||
| void pmll_free(pmll_state *S) | ||
| { | ||
| free(S->k); | ||
| free(S->v); | ||
| S->k = S->v = NULL; | ||
| S->T = S->hs = 0; | ||
| } | ||
|
|
||
| /*--------------------------------------------------------------- | ||
| Data flow | ||
| ----------------------------------------------------------------*/ | ||
|
|
||
| /* Read historic KV for head *h* to caller-provided scratch. | ||
| out_k/out_v length = (Tmem + Tctx) * hs floats. */ | ||
| void pmll_read(float *out_k, float *out_v, | ||
| const pmll_state *S, int h, int Tctx) | ||
| { | ||
| const int hs = S->hs; | ||
| const int Tmem = S->T; | ||
|
|
||
| const float *src_k = S->k + (size_t)h * MAX_MEM_T * hs; | ||
| const float *src_v = S->v + (size_t)h * MAX_MEM_T * hs; | ||
|
|
||
| /* 1. copy history */ | ||
| const size_t hist_bytes = (size_t)Tmem * hs * sizeof(float); | ||
| memcpy(out_k, src_k, hist_bytes); | ||
| memcpy(out_v, src_v, hist_bytes); | ||
|
|
||
| /* 2. zero-pad the fresh region so the attention kernel | ||
| sees “no data” there until we overwrite it. */ | ||
| const size_t pad_bytes = (size_t)Tctx * hs * sizeof(float); | ||
| memset(out_k + Tmem*hs, 0, pad_bytes); | ||
| memset(out_v + Tmem*hs, 0, pad_bytes); | ||
| } | ||
|
|
||
| /* Blend-write new KV into the ring buffer. | ||
| gate[t] ∈ [0,1] decides how much of the new vector to keep. | ||
| Pass gate = NULL to “remember everything” (g == 1). */ | ||
| void pmll_write(pmll_state *S, int h, | ||
| const float *new_k, const float *new_v, | ||
| int Tctx, const float *gate) | ||
| { | ||
| const int hs = S->hs; | ||
|
|
||
| float *dst_k = S->k + (size_t)h * MAX_MEM_T * hs; | ||
| float *dst_v = S->v + (size_t)h * MAX_MEM_T * hs; | ||
|
|
||
| for (int t = 0; t < Tctx; ++t) | ||
| { | ||
| const float g = gate ? gate[t] : 1.0f; /* default keep-all */ | ||
| const int idx = (S->T + t) % MAX_MEM_T; /* ring offset */ | ||
|
|
||
| const float *nk = new_k + t*hs; | ||
| const float *nv = new_v + t*hs; | ||
| float *dk = dst_k + idx*hs; | ||
| float *dv = dst_v + idx*hs; | ||
|
|
||
| for (int i = 0; i < hs; ++i) { | ||
| dk[i] = g * nk[i] + (1.f - g) * dk[i]; | ||
| dv[i] = g * nv[i] + (1.f - g) * dv[i]; | ||
| } | ||
| } | ||
|
|
||
| /* advance ring pointer */ | ||
| S->T += Tctx; | ||
| if (S->T > MAX_MEM_T) S->T = MAX_MEM_T; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless there is a specific reason not to do so,
pmll_read()should have theconst pmll_state *Sargument first for consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it!