From f6388c99c897b35626ef38e9e487ee9070904b84 Mon Sep 17 00:00:00 2001 From: Andrej Karpathy Date: Mon, 24 Jul 2023 05:10:55 +0000 Subject: [PATCH] delete the copy function in favor of memcpy. sadly we have to import string.h now... --- run.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/run.c b/run.c index e5004d6..fded591 100644 --- a/run.c +++ b/run.c @@ -12,6 +12,7 @@ $ ./run #include #include #include +#include // ---------------------------------------------------------------------------- // Transformer and RunState structs, and related memory management @@ -168,12 +169,6 @@ int checkpoint_init_weights(TransformerWeights *w, Config* p, FILE* f) { // ---------------------------------------------------------------------------- // neural net blocks -void copy(float *a, float *b, int size) { - for (int i = 0; i < size; i++) { - a[i] = b[i]; - } -} - void accum(float *a, float *b, int size) { for (int i = 0; i < size; i++) { a[i] += b[i]; @@ -236,7 +231,7 @@ void transformer(int token, int pos, Config* p, RunState* s, TransformerWeights* // copy the token embedding into x float* content_row = &(w->token_embedding_table[token * dim]); - copy(x, content_row, dim); + memcpy(x, content_row, dim*sizeof(*x)); // pluck out the "pos" row of freq_cis_real and freq_cis_imag float* freq_cis_real_row = w->freq_cis_real + pos * head_size / 2; @@ -277,8 +272,8 @@ void transformer(int token, int pos, Config* p, RunState* s, TransformerWeights* int loff = l * p->seq_len * dim; // kv cache layer offset for convenience float* key_cache_row = s->key_cache + loff + pos * dim; float* value_cache_row = s->value_cache + loff + pos * dim; - copy(key_cache_row, s->k, dim); - copy(value_cache_row, s->v, dim); + memcpy(key_cache_row, s->k, dim*sizeof(*key_cache_row)); + memcpy(value_cache_row, s->v, dim*sizeof(*value_cache_row)); // multihead attention. iterate over all heads for (int h = 0; h < p->n_heads; h++) {