You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Slow Import Times (60+ seconds) When Using Unsloth via Entry Points #1859
When importing Unsloth via Python entry points (defined in pyproject.toml), import times can exceed 60 seconds. The same code importing Unsloth via direct module execution (python -m) completes in about 5 seconds.
Root Cause
The slowdown occurs in the PatchFastRL function. When running via an entry point, dynamic module imports fail due to differences in sys.path, forcing the code to use a fallback method with retries and sleeps.
Specifically, the issue is here:
try:
new_module=importlib.import_module(UNSLOTH_COMPILE_LOCATION+"."+name)
except:
# Fallback with sleep and retrymodule_name=f"unsloth_cache_{name}"file_location=os.path.join(UNSLOTH_COMPILE_LOCATION, name) +".py"spec=importlib.util.spec_from_file_location(module_name, file_location)
new_module=importlib.util.module_from_spec(spec)
sys.modules[module_name] =new_modulespec.loader.exec_module(new_module)
time.sleep(0.2+trial)
This is called repeatedly for multiple modules with increasing sleep times, resulting in significant delays.
When running through an entry point, sys.path is different:
Entry point: First entry is the bin directory (e.g., /opt/conda/bin)
Module execution: First entry is the current working directory
This causes the relative imports in the PatchFastRL function to fail, triggering the much slower fallback path.
Workaround
Until this issue is fixed, users can avoid the slow import time by running their scripts directly with the Python module syntax instead of using entry points:
# Slow
my-command
# Fast
python -m my_package.main
Hey, I'm facing a similar issue now. It takes forever to load when I do the following. I create a test file: import unsloth print("success")
I execute it via python test.py , and it works fine, printing "success." However, when I try running it through a folder (e.g., python tmp/test.py), it never prints "success." I waited for 3 minutes before stopping, and it just gets stuck without any error.
Activity
danielhanchen commentedon Mar 1, 2025
Oh let me re-investigate this - this was supposed to be a workaround for the Python interpreter
danielhanchen commentedon Mar 4, 2025
Just fixed it hopefully! I reduced it to 0.01 seconds per call
Namron2000 commentedon Mar 5, 2025
Hey, I'm facing a similar issue now. It takes forever to load when I do the following. I create a test file:
import unslothprint("success")I execute it via
python test.py, and it works fine, printing "success." However, when I try running it through a folder (e.g.,python tmp/test.py), it never prints "success." I waited for 3 minutes before stopping, and it just gets stuck without any error.stcz commentedon Mar 5, 2025
I can also confirm this
rolandtannous commentedon Jun 30, 2025
Hello ,
This should now be resolved in the latest version of Unsloth.
Please make sure to upgrade your installation to the latest version using
Will close this for now.
Please feel free to comment back if you are still facing the same issue after trying the above.
Thank you for using Unsloth