Skip to content
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

fix aligned_alloc #2

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
#include <fcntl.h>
#if defined _WIN32
#include "win.h"
#define aligned_alloc _aligned_alloc
#define aligned_free _aligned_free
#else
#include <unistd.h>
#include <sys/mman.h>
#define aligned_free free
#endif

// ----------------------------------------------------------------------------
// Transformer and RunState structs, and related memory management

Expand Down Expand Up @@ -618,7 +622,9 @@ int main(int argc, char *argv[]) {
size_t weights_size = file_size - sizeof(Config);

// allocate memory for the weights, page aligned for best performance
data = (float*) aligned_alloc(sysconf(_SC_PAGESIZE), weights_size);
int pagesize = sysconf(_SC_PAGESIZE);
int pages_required = (weights_size + pagesize - 1) / pagesize;
data = (float*) aligned_alloc(pagesize, pages_required * pagesize);
if (!data) { fprintf(stderr, "aligned_alloc failed!\n"); return 1; }

// Read the weights into the allocated memory
Expand Down Expand Up @@ -738,6 +744,6 @@ int main(int argc, char *argv[]) {
free(vocab);
free(vocab_scores);
if (prompt_tokens != NULL) free(prompt_tokens);
free(data);
aligned_free(data);
return 0;
}