-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
llmodel: dont load libs in static initialization #1546
Conversation
Tried it again in the following combination of configurations and so far I didn't see any problems like in v2.5.0:
|
gpt4all-backend/llmodel.cpp
Outdated
static auto* libs = new std::vector<Implementation>([] () { | ||
std::vector<Implementation> fres; | ||
|
||
if(!s_scanned) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s_impl_libs should be a function-local static.
If we're at all concerned about thread safety, we should use a function to compute s_impl_libs and initialize it on the same line as the declaration - C++ guarantees that the initializer will be run only once. Otherwise, we should at least put if (s_scanned) { return s_impl_libs; }
at the top to reduce the indentation level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moving the decl/init into the fn, but the whole thing here is that we can't actually compute it (do the dlopen, etc.) at static init time because the search path may not actually be set yet (in the GUI case at least it is set by a different static initializer in a different file and it sometimes worked if that one ran first, but it is not guaranteed that it will)
if we want to actually be threadsafe around this we can't have a global mutable search path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Static local variables are not initialized until the first time control passes through their declaration. See cppreference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's chat about this. From what I can tell, you're both right. It should be static local, but the problem remains if that function is called before the global search path is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeahh either way if the method to set the search path is used it needs to have the re-checking behavior if it's not clearly documented to be useless unless called very early
I am not really clear on how this had been being called before that but I definitely caught this code running and loading libs before the chat gui had changed the path from the default .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does the chat client call setImplementationsSearchPath in the constructor of LLM instead of main.cpp?
And I think it would be a good idea to guard this code with a mutex (including the setting of s_scanned to false) in case it is called from more than one thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, yeah, all the globalInstance()s should also be deferred til first use so should get run in the order they are called, and at least spot-checking it it does set the search path before its read when running from the build tree - built an installer to check and that also remains the case - so its likely the bigger culprit here was it dlopen
ing dlls it shouldn't and that check is probably doing more work here than anything else
For reference, here is a description of the issue this PR addresses:
|
this fixes some issues that were being seen on installed windows builds of 2.5.0 setImplementationsSearchPath did not work if llmodel was initialized before it was called * do not actually load impl dlls in the static initializer, wait until we're actually trying to load a model for the first time * rescan on the next model load attempt if the search path has been changed * only load dlls that actually might be model impl dlls, otherwise we pull all sorts of random junk into the process before it might expect to be Co-authored-by: cebtenzzre <[email protected]> Signed-off-by: Aaron Miller <[email protected]>
removed unnecessary unique_ptr wrapping that was only needed when I was attempting to stage the |
#1556 fixes the issues I was encountering on its own, this (messing with the init order) probably isn't necessary |
this fixes some issues that were being seen on installed windows builds of 2.5.0
setImplementationsSearchPath did not work if llmodel was initialized before it was called
do not actually load impl dlls in the static initializer, wait until we're actually trying to load a model for the first time
rescan on the next model load attempt if the search path has been changed
only load dlls that actually might be model impl dlls, otherwise we pull all sorts of random junk into the process before it might expect to be