Repository navigation
Enhance Windows 7 and below compatibility. - #2592
Conversation
|
Here is a screenshot of this code allowing functioning on Windows 7. The executable was built using Windows 10. Here are the executables in case anyone wants to test this on Windows 10 but doesn't want to build (or for windows 7 users who don't want to wait for this to get merged): llamaforkbuilt.zip |
|
Maybe you could use a typedef (or a 'using' statement since this is C++) to avoid writing the signature twice. |
Avoid duplicate function signature as requested
Done. |
DannyDaemonic
left a comment
There was a problem hiding this comment.
I fixed a different Windows 7 runtime issue recently. For this issue here, there's already a preprocessor check around the code. It shouldn't be compiled in if you're targeting Windows 7 with your compiler options. That said, the official binaries don't target Windows 7, so this change will allow the official binaries to run on Windows 7.
I realize you may be trying to submit as small a change as possible, but there's a preprocessor check you're missing here. It throws an exception when someone tried to mmap when compiled for Windows 7:
throw std::runtime_error(std::string("mmap not supported"));The preprocessor check is no longer needed with this change and should be removed, but maybe the user should be warned that prefetch is not supported under this version of Windows? Either way, I don't think an exception is necessary though, since mmap will work without prefetch -- it just won't get any performance improvement related to prefetching the data.
My review came a minute too late. All you needed was a Edit: But whatever is fine. |
|
Yeah I just didnt want to start changing a bunch of stuff. But the less ifdefs the better I guess. |
DannyDaemonic
left a comment
There was a problem hiding this comment.
I don't believe any of this is called in a tight loop or anything, but another improvement could be to store the pointer in a static variable:
if (prefetch) {
static BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG) = nullptr;
static bool functionResolved = false;
if (!functionResolved) {
// The PrefetchVirtualMemory API is only present on Windows 8 and above, so we
// will dynamically load it using GetProcAddress.
HMODULE hKernel32;
// This call is almost guaranteed to succeed.
hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (!hKernel32) {
fprintf(stderr, "error: Unable to get handle for kernel32.dll\n");
return;
}
// This call may fail if on a pre-Win8 system.
pPrefetchVirtualMemory = reinterpret_cast<decltype(pPrefetchVirtualMemory)> GetProcAddress(hKernel32, "PrefetchVirtualMemory");
functionResolved = true;
}
if (pPrefetchVirtualMemory) {
// Advise the kernel to preload the mapped memory.
WIN32_MEMORY_RANGE_ENTRY range;
range.VirtualAddress = addr;
range.NumberOfBytes = (SIZE_T)size;
if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
llama_format_win_err(GetLastError()).c_str());
}
}
}I'll approve it as is though.
|
One of the compilers is throwing an error. It's complaining about missing ()s. Maybe try: |
|
The If you're curious about // Define the function pointer type using the 'using' keyword
using pPrefetchVirtualMemoryType = BOOL (WINAPI *)(HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
// Then, when you retrieve the function address:
pPrefetchVirtualMemoryType pPrefetchVirtualMemory;
// This call is guaranteed to succeed.
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
// This call may fail if on a pre-Win8 system.
pPrefetchVirtualMemory = reinterpret_cast<pPrefetchVirtualMemoryType>(GetProcAddress(hKernel32, "PrefetchVirtualMemory")); |
|
This seems to have been reverted with the removal of llama-util.h? |
|
Yeah, somehow it got replaced by the old version with the _WIN32_WINNT_WIN8 check instead. |
|
It likely happened by accident when I was merging the GGUF branch |
|
I'll make a new PR to put it back unless anyone else wants to deal with it. |
|
Reimplemented in #2930. |
* Enhance Windows 7 compatibility. * Clean away unnecessary preprocessor conditional
* Enhance Windows 7 compatibility. * Clean away unnecessary preprocessor conditional
* Enhance Windows 7 compatibility. * Clean away unnecessary preprocessor conditional
* Enhance Windows 7 compatibility. * Clean away unnecessary preprocessor conditional
* Enhance Windows 7 compatibility. * Clean away unnecessary preprocessor conditional


The PrefetchVirtualMemory API is not available on Windows 7. The usage of this API within Llama.cpp is the only reason why the software cannot work on Windows 7. This pull request includes additional code to detect the presence of the PrefetchVirtualMemory API and not call it if it is not present. The behavior of the code should not otherwise change.