Skip to content

[Bug] sglang frontend has an incorrect dependency on torch #4929

Description

@seplos

Checklist

  • 1. I have searched related issues but cannot get the expected help.
  • 2. The bug has not been fixed in the latest version.
  • 3. Please note that if the bug-related issue you submitted lacks corresponding environment info and a minimal reproducible demo, it will be challenging for us to reproduce and resolve the issue, reducing the likelihood of receiving feedback.
  • 4. If the issue you raised is not a bug but a question, please raise a discussion at https://github.com/sgl-project/sglang/discussions/new/choose Otherwise, it will be closed.
  • 5. Please use English, otherwise it will be closed.

Describe the bug

I tried to use OpenAI models with the frontend language, and installed sglang by using pip install "sglang[openai]".

This correctly installed sglang-v0.4.4.post3 and its dependencies on my machine. However, when I tried to run the openai_example_chat.py example, I got the following error:

  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/__init__.py", line 4, in <module>
    from sglang.api import (
  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/api.py", line 7, in <module>
    from sglang.lang.backend.base_backend import BaseBackend
  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/lang/backend/base_backend.py", line 5, in <module>
    from sglang.lang.interpreter import StreamExecutor
  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/lang/interpreter.py", line 34, in <module>
    from sglang.utils import (
  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/utils.py", line 28, in <module>
    from sglang.srt.utils import kill_process_tree
  File "/usr/local/opt/miniconda/envs/sglang/lib/python3.12/site-packages/sglang/srt/utils.py", line 48, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

I found that this was due to the import statement in sglang/utils.py:

from sglang.srt.utils import kill_process_tree

It wrongly introduced a dependency on sglang.srt.utils and further introduced a dependency on torch.

I checked the usage of kill_process_tree in sglang/utils.py and found that it was only used once in the terminate_process function. Then I further checked the change history of sglang/utils.py and found that this commit moved the above import statement from inside the terminate_process function to the beginning of the file.

This change was committed between v0.4.3.post2 and v0.4.3.post3. v0.4.3.post2 can run openai_example_chat.py correctly, while v0.4.3.post3 reported the same error.

Putting the above import statement back inside the terminate_process function can solve this problem.

Reproduction

Just start from a new virtual environment and run pip install "sglang[openai]".

python3 openai_example_chat.py will raise ModuleNotFoundError: No module named 'torch'.

Environment

This script is not really runnable because it imports torch without checking for its existence. For the same reason as #3874.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions