Skip to content

Slow Import Times (60+ seconds) When Using Unsloth via Entry Points #1859

Closed

Description

@oKatanaaa

Issue Description

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 retry
    module_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_module
    spec.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.

Steps to Reproduce

  1. Create a minimal project with an entry point:
# pyproject.toml
[project.scripts]
my-command = "my_package.main:run"
# my_package/main.py
import time

start = time.time()
import unsloth
elapsed = time.time() - start
print(f'Time passed: {elapsed:.3f}')

def run():
    pass
  1. Compare performance:
    • Fast: python -m my_package.main (~5 seconds)
    • Slow: my-command (~60+ seconds)

Technical Details

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

Activity

  1. danielhanchen commented on Mar 1, 2025

    @danielhanchen
    Member

    Oh let me re-investigate this - this was supposed to be a workaround for the Python interpreter

  2. danielhanchen commented on Mar 4, 2025

    @danielhanchen
    Member

    Just fixed it hopefully! I reduced it to 0.01 seconds per call

  3. added
    fixed - pending confirmation
    and removed
    currently fixing
    on Mar 4, 2025
  4. Namron2000 commented on Mar 5, 2025

    @Namron2000

    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.

  5. stcz commented on Mar 5, 2025

    @stcz

    I can also confirm this

  6. rolandtannous commented on Jun 30, 2025

    @rolandtannous
    Contributor

    Hello ,

    This should now be resolved in the latest version of Unsloth.
    Please make sure to upgrade your installation to the latest version using

    pip install --upgrade unsloth-zoo
    pip install --upgrade unsloth
    

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed - pending confirmation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions