Skip to content

Enhance Windows 7 and below compatibility. - #2592

Merged
DannyDaemonic merged 4 commits into
masterfrom
unknown repository
Aug 14, 2023
Merged

DannyDaemonic merged 4 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Aug 12, 2023

Copy link
Copy Markdown

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.

@ghost

ghost commented Aug 12, 2023

Copy link
Copy Markdown
Author

Here is a screenshot of this code allowing functioning on Windows 7. The executable was built using Windows 10.

image

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

@cebtenzzre

Copy link
Copy Markdown
Collaborator

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
@ghost

ghost commented Aug 12, 2023

Copy link
Copy Markdown
Author

Maybe you could use a typedef (or a 'using' statement since this is C++) to avoid writing the signature twice.

Done.

@DannyDaemonic DannyDaemonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread llama-util.h Resolved
Comment thread llama-util.h Resolved
@DannyDaemonic

DannyDaemonic commented Aug 12, 2023 •

Copy link
Copy Markdown
Contributor

Maybe you could use a typedef (or a 'using' statement since this is C++) to avoid writing the signature twice.

Done.

My review came a minute too late. All you needed was a decltype. If you aren't going to be using it elsewhere, a typedef or using clause is probably unnecessary.

Edit: But whatever is fine.

@ghost

ghost commented Aug 12, 2023

Copy link
Copy Markdown
Author

Yeah I just didnt want to start changing a bunch of stuff. But the less ifdefs the better I guess.
Also, I have changed the cast according to your suggestion. I am primarily a C developer, so I didn't know of that way of casting.

@DannyDaemonic DannyDaemonic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@DannyDaemonic

Copy link
Copy Markdown
Contributor

One of the compilers is throwing an error. It's complaining about missing ()s. Maybe try:

pPrefetchVirtualMemory = reinterpret_cast<decltype(pPrefetchVirtualMemory)>(GetProcAddress(hKernel32, "PrefetchVirtualMemory"));

@ghost

ghost commented Aug 12, 2023 •

Copy link
Copy Markdown
Author

image
Looks like MSVC is not happy with that cast. Should I change it back to the typedef?
Edit: Will try your suggestion 1st

@DannyDaemonic

Copy link
Copy Markdown
Contributor

The () aren't required according to the C++ standard, but it seems to have fixed the issue. 🤷🏻‍♂️ The cast operators have a template-like syntax and probably reuse some of the same code. Since decltype is deducing the type like templates often do, it might be falling back on that code in this case. I think it still looks the cleanest of all the options?

If you're curious about using as mentioned by Cebtenzzre, this is what it would have looked like:

// 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"));

@DannyDaemonic
DannyDaemonic merged commit f31b539 into ggml-org:master Aug 14, 2023
@LostRuins

Copy link
Copy Markdown
Collaborator

This seems to have been reverted with the removal of llama-util.h?

@DannyDaemonic

Copy link
Copy Markdown
Contributor

Yeah, somehow it got replaced by the old version with the _WIN32_WINNT_WIN8 check instead.

@ggerganov

Copy link
Copy Markdown
Member

It likely happened by accident when I was merging the GGUF branch

@DannyDaemonic

Copy link
Copy Markdown
Contributor

I'll make a new PR to put it back unless anyone else wants to deal with it.

@DannyDaemonic

Copy link
Copy Markdown
Contributor

Reimplemented in #2930.

Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* Enhance Windows 7 compatibility.
* Clean away unnecessary preprocessor conditional
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* Enhance Windows 7 compatibility.
* Clean away unnecessary preprocessor conditional
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* Enhance Windows 7 compatibility.
* Clean away unnecessary preprocessor conditional
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* Enhance Windows 7 compatibility.
* Clean away unnecessary preprocessor conditional
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* Enhance Windows 7 compatibility.
* Clean away unnecessary preprocessor conditional
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants