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: load model library when using pyinstaller to build executable fi… #2530

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

appflowy
Copy link

@appflowy appflowy commented Jul 3, 2024

Describe your changes

Fix loading llmmodel library when using pyinstaller to build executable binary that includes gpt4all

Issue ticket number and link

Fix pyinstaller issues: #1727

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.
  • I have added thorough documentation for my code.
  • I have tagged PR with relevant project labels. I acknowledge that a PR without labels may be dismissed.
  • If this PR addresses a bug, I have provided both a screenshot/video of the original bug and the working solution.

@appflowy appflowy force-pushed the fix_1727 branch 3 times, most recently from e0e5b74 to fe000b7 Compare July 3, 2024 11:16
Comment on lines 57 to +88
def load_llmodel_library():
"""
Loads the llmodel shared library based on the current operating system.

This function attempts to load the shared library using the appropriate file
extension for the operating system. It first tries to load the library with the
'lib' prefix (common for macOS, Linux, and MinGW on Windows). If the file is not
found and the operating system is Windows, it attempts to load the library without
the 'lib' prefix (common for MSVC on Windows).

Returns:
ctypes.CDLL: The loaded shared library.

Raises:
OSError: If the shared library cannot be found.
"""
# Determine the appropriate file extension for the shared library based on the platform
ext = {"Darwin": "dylib", "Linux": "so", "Windows": "dll"}[platform.system()]

# Define library names with and without the 'lib' prefix
library_name_with_lib_prefix = f"libllmodel.{ext}"
library_name_without_lib_prefix = "llmodel.dll"
base_path = MODEL_LIB_PATH

try:
# macOS, Linux, MinGW
lib = ctypes.CDLL(str(MODEL_LIB_PATH / f"libllmodel.{ext}"))
except FileNotFoundError:
if ext != 'dll':
# Attempt to load the shared library with the 'lib' prefix (common for macOS, Linux, and MinGW)
lib = ctypes.CDLL(str(base_path / library_name_with_lib_prefix))
except OSError: # OSError is more general and includes FileNotFoundError
if ext != "dll":
raise
# MSVC
lib = ctypes.CDLL(str(MODEL_LIB_PATH / "llmodel.dll"))
# For Windows (ext == 'dll'), attempt to load the shared library without the 'lib' prefix (common for MSVC)
lib = ctypes.CDLL(str(base_path / library_name_without_lib_prefix))
Copy link
Member

Choose a reason for hiding this comment

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

Documentation is great, but this is a bit much. All this change really needs to be is:

  • Replace FileNotFoundError with OSError
  • Add a comment mentioning the specific OSError that appears when using pyinstaller (it will at least have an error code, but often it is a specific subclass of OSError)
    • Example: except OSError: # e.g. FileNotFoundError, PermissionError (pyinstaller)
  • Make the quotes consistent
  • Add a concise docstring - a sentence or two is more than enough

Anything else is unnecessary and IMO too verbose.

Ideally the except clause is as narrow as possible. If there is a specific exception that applies:

try:
    ...
except (FileNotFoundError, PermissionError):  # pyinstaller raises PermissionError
    ...

Otherwise:

try:
    ...
except OSError as e:
    if e.errno not in (errno.ENOENT, errno.EPERM):  # pyinstaller fails with EPERM
        raise
    ...

And if we're going to add a return type to the docstring, I would prefer we annotate it as well (load_llmodel_library() -> ctypes.CDLL:).

Copy link
Author

Choose a reason for hiding this comment

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

Thank a bunch! I will make changes later

@cebtenzzre
Copy link
Member

cebtenzzre commented Aug 6, 2024

I recently discovered that _ctypes.dlopen on POSIX systems genuinely emits OSError without setting the errno attribute of the exception. This is a limitation of dlopen(3), as it reports errors as a string only via dlerror(3). Though for the existing code to work on Windows, the NT equivalent must emit FileNotFoundError, at least in normal operation.

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.

2 participants