-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[Feature]: Improve import speed #7605
Comments
For me, it's super slow as well (even slower then OP): uv run --python 3.10 --with litellm python -X importtime -c "import litellm"
import time: 20107192 | 20857950 | litellm |
Hey Krish. The problem from my side was actually the network I was in and not the library itself, so all is okay from my side. Regards, litellm @krrishdholakia . |
@krrishdholakia I don’t have the bandwidth to contribute a fix unfortunately. But the command I shared in the OP outputs a full trace of all imports and so should be a good starting point to analyze what’s causing it to be slow. |
cc: @ishaan-jaff in case this is helpful in your perf work |
This would be an amazing upgrade, it should be extremely fast to import litellm |
@tcapelle if you want to contribute this fix - that would be helpful. I wasn't able to make much headway in my investigation. |
I don't have much time at the moment but o3-mini-high thinks this about the output of the The timing output shows that importing litellm takes nearly a second—which is a lot for a module import. A closer look at the trace reveals that litellm pulls in a very large dependency graph. In particular, heavy libraries such as OpenAI’s SDK, Pydantic, tokenizers (tiktoken, regex, etc.), and even parts of HTTP libraries are all being loaded up front. Some key points: Possible Remedies: In summary, the slowness is less an issue with litellm itself and more a side effect of a deep and heavy dependency graph. Addressing it will likely require a combination of lazy-loading and refactoring the package’s import structure. |
The 1000 lines init.py is probably the culprit... |
Here's an issue from huggingface diffusers where they are dealing with the same problem: huggingface/diffusers#4260 -- they are copying the lazy load patterns from hf's transformers package. |
And here's that repo's PR for their first sets of changes for that effort: huggingface/diffusers#4829 |
Indeed it feels very heavy for a 'lite' lib;
|
Thanks @fmmoret, this approach looks promising - https://github.com/huggingface/diffusers/pull/4829/files#diff-b828b278a068d46526a52d0facbdff41298fc776db6b6a5a8c04cd264ea3043f |
Added to march 2025 roadmap |
Just want to point out that #2677 is related and contains some more info possibly of interest to anyone tackling this. 🙏 |
The Feature
Simply importing LiteLLM can take up to 1 second, which is rather slow. Could the import speed be improved?
Minimal reproducible example:
uv run --python 3.10 --with litellm python -X importtime -c "import litellm"
Output:
From the logs, you can see that
litellm.utils
already takes 250ms to load for example.Motivation, pitch
If a project has 3 such dependencies, it would take 3 seconds to import that project.
Are you a ML Ops Team?
No
Twitter / LinkedIn details
@laurentsorber
The text was updated successfully, but these errors were encountered: