Skip to content

Fix model loading time through prefetching the file on another thread #734

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,19 @@ int ggml_cpu_has_blas(void);
int ggml_cpu_has_sse3(void);
int ggml_cpu_has_vsx(void);

//
// threading for non posix systems
//
#ifndef _POSIX_THREADS
#if defined(_WIN32)
#include <windows.h>
#endif
typedef HANDLE pthread_t;
typedef DWORD thread_ret_t;
static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void*), void* arg);
static int pthread_join(pthread_t thread, void* unused);
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reinvent std::thread?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::thread does not exist in msvc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to exist at https://learn.microsoft.com/en-us/cpp/standard-library/thread-class?view=msvc-170 . But I didn't test, so feel free to resolve this thread if std::thread doesn't work on your machine

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::thread do exists in msvc


#ifdef __cplusplus
}
#endif
48 changes: 48 additions & 0 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,25 @@ struct llama_context_params llama_context_default_params() {
//
// model loading
//
typedef struct the_load_file_to_standby_struct{
char * fname;
char * addr;
size_t addrlen;
} the_load_file_to_standby_struct;

int load_file_to_standby(the_load_file_to_standby_struct * the_load_file_to_standby_struct1){
auto fin = std::ifstream(the_load_file_to_standby_struct1->fname, std::ios::binary);
if (!fin || !the_load_file_to_standby_struct1 || !the_load_file_to_standby_struct1->fname || !the_load_file_to_standby_struct1->addr || !the_load_file_to_standby_struct1->addrlen) {
return -1;
}
while(!fin.eof()) fin.read(the_load_file_to_standby_struct1->addr, the_load_file_to_standby_struct1->addrlen);
return 0;
}

static void *mmap_file(const char *fname, uint64_t *mm_length) {
the_load_file_to_standby_struct * the_load_file_to_standby_struct1 = 0;
const size_t readstridelen = 1 << 20;
size_t fnamelen = strlen(fname);
#if defined(_WIN32) && !defined(_POSIX_MAPPED_FILES)
HANDLE hFile = CreateFileA(fname,
GENERIC_READ,
Expand All @@ -326,6 +343,24 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) {
if (!hMapping) return 0;
void *addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
CloseHandle(hMapping);
hMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, sizeof(the_load_file_to_standby_struct) >> 32, sizeof(the_load_file_to_standby_struct), NULL);
if (hMapping){
the_load_file_to_standby_struct1 = (the_load_file_to_standby_struct*)MapViewOfFile(hMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
the_load_file_to_standby_struct1->fname = 0;
the_load_file_to_standby_struct1->addr = 0;
the_load_file_to_standby_struct1->addrlen = 0;
CloseHandle(hMapping);
hMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, fnamelen >> 32, fnamelen, NULL);
if (hMapping){
the_load_file_to_standby_struct1->fname = (char*)MapViewOfFile(hMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
CloseHandle(hMapping);
hMapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, readstridelen >> 32, readstridelen, NULL);
if (hMapping){
the_load_file_to_standby_struct1->addr = (char*)MapViewOfFile(hMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
CloseHandle(hMapping);
}
}
}
if (!addr) return 0;
#else
int fd = open(fname, O_RDONLY);
Expand All @@ -334,8 +369,21 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) {
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (addr == MAP_FAILED) return 0;
the_load_file_to_standby_struct1 = (the_load_file_to_standby_struct*)mmap(NULL, sizeof(the_load_file_to_standby_struct), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (the_load_file_to_standby_struct1 == MAP_FAILED) the_load_file_to_standby_struct1 = 0;
the_load_file_to_standby_struct1->fname = (char*)mmap(NULL, fnamelen, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (the_load_file_to_standby_struct1->fname == MAP_FAILED) the_load_file_to_standby_struct1->fname = 0;
the_load_file_to_standby_struct1->addr = (char*)mmap(NULL, readstridelen, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (the_load_file_to_standby_struct1->addr == MAP_FAILED) the_load_file_to_standby_struct1->addr = 0;
#endif
*mm_length = length;
if(the_load_file_to_standby_struct1){
the_load_file_to_standby_struct1->addrlen = readstridelen;
if(the_load_file_to_standby_struct1->fname){
memcpy(the_load_file_to_standby_struct1->fname, fname, fnamelen);
}
pthread_create(0, 0, (void *(*)(void*))(&load_file_to_standby), the_load_file_to_standby_struct1);
}
return addr;
}

Expand Down