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

feat/1001: sglang integration #1122

Merged
merged 14 commits into from
Feb 27, 2025
Merged

feat/1001: sglang integration #1122

merged 14 commits into from
Feb 27, 2025

Conversation

Jayon02
Copy link

@Jayon02 Jayon02 commented Feb 27, 2025

This PR closes #1001.

In this PR, I integrate sglang into Distilabel, allowing users to use sglang as the backend for dataset generation.

Tasks

  • Add sglang as a potential backend for data generation. See here.
  • Add sglang as a potential backend for embedding generation. See here.
  • Add unit tests for features above.
  • Update documentation.

Examples

Here are some examples for you to use sglang in distilabel. More detailed documentation will be added.

  • Use sglang backend to generate text directly.
from distilabel.models.llms import SGLang
if __name__ == "__main__":
    llm = SGLang(
        model="prometheus-eval/prometheus-7b-v2.0",
        chat_template="[INST] {{ messages[0]['content']}} [/INST]"
    )

    llm.load()
    # Call the model
    output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Hello world!"}]])
  • Use sglang backend in pipeline to generate dataset.
from distilabel.pipeline import Pipeline
from distilabel.llms import SGLang
from distilabel.steps import LoadDataFromDicts
from distilabel.steps.tasks import TextGeneration

with Pipeline(name="text-generation-pipeline") as pipeline:
    load_dataset = LoadDataFromDicts(
        name="load_dataset",
        data=[{"instruction": "Write a short story about a dragon that saves a princess from a tower."}],
    )

    text_generation = TextGeneration(
        name="text_generation",
        llm=SGLang(model="athirdpath/Llama-3-15b-Instruct"),
    )

    load_dataset >> text_generation

if __name__ == "__main__":
    pipeline.run(
        parameters={
            text_generation.name: {"llm": {"generation_kwargs": {"temperature": 0.3}}},
        },
        use_cache=False,
    )

Please let me know if there is anything that could be improved. Thanks to all SGLang team members for their help.

Jayon02 and others added 9 commits February 22, 2025 06:22
commit 6c9059f
Author: Jayon02 <[email protected]>
Date:   Thu Feb 27 02:50:07 2025 +0000

     add detokenize

commit 159f1ef
Author: Jayon02 <[email protected]>
Date:   Tue Feb 25 11:34:05 2025 +0000

    support sglang embedding

commit f2b2064
Author: Jayon02 <[email protected]>
Date:   Tue Feb 25 08:06:05 2025 +0000

     fix test bugs

commit ba3a5a7
Author: Jayon02 <[email protected]>
Date:   Mon Feb 24 16:55:53 2025 +0000

     fix bugs in multiple generations and add test

commit f893c6d
Author: Jayon02 <[email protected]>
Date:   Sat Feb 22 17:13:12 2025 +0000

    modify function annotation

commit ddde5cb
Author: Jayon02 <[email protected]>
Date:   Sat Feb 22 12:06:11 2025 +0000

    support structure output and SGLang openai client

commit 5ecedc4
Author: Jayon02 <[email protected]>
Date:   Sat Feb 22 06:22:42 2025 +0000

    support sglang
CudaDevicePlacementMixin.load(self)

try:
from sglang import Engine as _SGLang

Choose a reason for hiding this comment

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

why should use this by the name "_SGLang". COuld we directly use Engine?

Copy link
Author

Choose a reason for hiding this comment

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

I align with the format in vllm. Should I reformat what I commit?

Copy link
Contributor

Choose a reason for hiding this comment

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

you can use from sglang import Engine here, it's fine


def unload(self) -> None:
"""Unloads the `SGLang` model."""
self._cleanup_sglang_model()

Choose a reason for hiding this comment

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

where is the definition of _cleanup_sglang_model()? Also, do not use _ as this could be a public funciton.

Copy link
Author

Choose a reason for hiding this comment

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

fix it.

@plaguss plaguss changed the base branch from main to develop February 27, 2025 07:23
Copy link

codspeed-hq bot commented Feb 27, 2025

CodSpeed Performance Report

Merging #1122 will improve performances by ×2.1

Comparing Jayon02:main (503b3d3) with develop (2d4ec9a)

Summary

⚡ 1 improvements

Benchmarks breakdown

Benchmark BASE HEAD Change
test_cache_time 1,185.5 ms 553.1 ms ×2.1

@davidberenstein1957
Copy link
Member

Awesome PR, I've asked @plaguss and @gabrielmbmb to review it.

Copy link
Contributor

@plaguss plaguss left a comment

Choose a reason for hiding this comment

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

Awesome job! some small comments, and a couple of things to tackle

CudaDevicePlacementMixin.load(self)

try:
from sglang import Engine as _SGLang
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use from sglang import Engine here, it's fine

@plaguss
Copy link
Contributor

plaguss commented Feb 27, 2025

Could you add the required dependencies so that we can run:

uv pip install distilabel[sglang]

and have it ready? I run the command from the guide and it works:

uv pip install "sglang[all]>=0.4.3.post2" --find-links https://flashinfer.ai/whl/cu124/torch2.5/flashinfer-python

but should be reflected in the pyproject.toml

@plaguss
Copy link
Contributor

plaguss commented Feb 27, 2025

Also, the example from the structured outputs is failing:

from distilabel.models.llms import SGLang
from pydantic import BaseModel
if __name__ == "__main__":
    class User(BaseModel):
        name: str
        last_name: str
        id: int
    llm = SGLang(
        model="Qwen/Qwen2.5-Coder-3B-Instruct",
        structured_output={"format": "json", "schema": User},
    )
    llm.load()
    # Call the model
    output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Create a user profile for the following marathon"}]])

It throws the following trace:

[2025-02-27 09:04:17 TP0] Scheduler hit an exception: Traceback (most recent call last):
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1825, in run_scheduler_process
    scheduler.event_loop_overlap()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 496, in event_loop_overlap
    batch = self.get_next_batch_to_run()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 877, in get_next_batch_to_run
    new_batch = self.get_new_batch_prefill()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 898, in get_new_batch_prefill
    self.move_ready_grammar_requests()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1594, in move_ready_grammar_requests
    req.grammar = req.grammar.result(timeout=0.05)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/base_grammar_backend.py", line 53, in init_value
    entry.value = self.init_value_impl(key)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/outlines_backend.py", line 173, in init_value_impl
    guide = RegexGuide.from_regex(regex, self.outlines_tokenizer)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 92, in from_regex
    return super().from_regex(
           ^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 212, in from_regex
    ) = _create_states_mapping(
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 76, in cached_create_states_mapping
    return uncached_create_states_mapping(regex_string, tokenizer, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 141, in create_states_mapping
    return create_states_mapping_from_fsm(regex_fsm, tokenizer, frozen_tokens)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 178, in create_states_mapping_from_fsm
    states_to_token_maps, empty_token_ids = create_fsm_index_tokenizer(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/regex.py", line 473, in create_fsm_index_tokenizer
    tokens_to_token_ids, empty_token_ids = reduced_vocabulary(tokenizer)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/models/transformers.py", line 119, in __hash__
    return hash(Hasher.hash(self.tokenizer))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/fingerprint.py", line 188, in hash
    return cls.hash_bytes(dumps(value))
                          ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 109, in dumps
    dump(obj, file)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 103, in dump
    Pickler(file, recurse=True).dump(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 428, in dump
    StockPickler.dump(self, obj)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 487, in dump
    self.save(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 70, in save
    dill.Pickler.save(self, obj, save_persistent_id=save_persistent_id)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 422, in save
    StockPickler.save(self, obj, save_persistent_id)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 209, in _save_transformersPreTrainedTokenizerBase
    log(pickler, f"Tok: {obj}")
    ^^^
NameError: name 'log' is not defined

[2025-02-27 09:04:17] Received sigquit from a child proces. It usually means the child failed.
Killed

@plaguss
Copy link
Contributor

plaguss commented Feb 27, 2025

for the integration test failing, I already fixed in the the develop branch, if you merge it, that should do it. And for the docs they will appear in the components gallery section, it's okey, unless you want to add some additional example in the /examples folder or something 😄

@Jayon02
Copy link
Author

Jayon02 commented Feb 27, 2025

Also, the example from the structured outputs is failing:

from distilabel.models.llms import SGLang
from pydantic import BaseModel
if __name__ == "__main__":
    class User(BaseModel):
        name: str
        last_name: str
        id: int
    llm = SGLang(
        model="Qwen/Qwen2.5-Coder-3B-Instruct",
        structured_output={"format": "json", "schema": User},
    )
    llm.load()
    # Call the model
    output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Create a user profile for the following marathon"}]])

It throws the following trace:

[2025-02-27 09:04:17 TP0] Scheduler hit an exception: Traceback (most recent call last):
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1825, in run_scheduler_process
    scheduler.event_loop_overlap()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 496, in event_loop_overlap
    batch = self.get_next_batch_to_run()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 877, in get_next_batch_to_run
    new_batch = self.get_new_batch_prefill()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 898, in get_new_batch_prefill
    self.move_ready_grammar_requests()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1594, in move_ready_grammar_requests
    req.grammar = req.grammar.result(timeout=0.05)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/base_grammar_backend.py", line 53, in init_value
    entry.value = self.init_value_impl(key)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/outlines_backend.py", line 173, in init_value_impl
    guide = RegexGuide.from_regex(regex, self.outlines_tokenizer)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 92, in from_regex
    return super().from_regex(
           ^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 212, in from_regex
    ) = _create_states_mapping(
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 76, in cached_create_states_mapping
    return uncached_create_states_mapping(regex_string, tokenizer, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 141, in create_states_mapping
    return create_states_mapping_from_fsm(regex_fsm, tokenizer, frozen_tokens)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 178, in create_states_mapping_from_fsm
    states_to_token_maps, empty_token_ids = create_fsm_index_tokenizer(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/regex.py", line 473, in create_fsm_index_tokenizer
    tokens_to_token_ids, empty_token_ids = reduced_vocabulary(tokenizer)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/models/transformers.py", line 119, in __hash__
    return hash(Hasher.hash(self.tokenizer))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/fingerprint.py", line 188, in hash
    return cls.hash_bytes(dumps(value))
                          ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 109, in dumps
    dump(obj, file)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 103, in dump
    Pickler(file, recurse=True).dump(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 428, in dump
    StockPickler.dump(self, obj)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 487, in dump
    self.save(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 70, in save
    dill.Pickler.save(self, obj, save_persistent_id=save_persistent_id)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 422, in save
    StockPickler.save(self, obj, save_persistent_id)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 209, in _save_transformersPreTrainedTokenizerBase
    log(pickler, f"Tok: {obj}")
    ^^^
NameError: name 'log' is not defined

[2025-02-27 09:04:17] Received sigquit from a child proces. It usually means the child failed.
Killed

This issue seems quite strange and I don't find bugs about sglang in this context. In my environment, this code can work correctly. Could you use docker to run the repo? Maybe it can give more useful information and I will fix it.

@plaguss
Copy link
Contributor

plaguss commented Feb 27, 2025

Also, the example from the structured outputs is failing:

from distilabel.models.llms import SGLang
from pydantic import BaseModel
if __name__ == "__main__":
    class User(BaseModel):
        name: str
        last_name: str
        id: int
    llm = SGLang(
        model="Qwen/Qwen2.5-Coder-3B-Instruct",
        structured_output={"format": "json", "schema": User},
    )
    llm.load()
    # Call the model
    output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Create a user profile for the following marathon"}]])

It throws the following trace:

[2025-02-27 09:04:17 TP0] Scheduler hit an exception: Traceback (most recent call last):
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1825, in run_scheduler_process
    scheduler.event_loop_overlap()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 496, in event_loop_overlap
    batch = self.get_next_batch_to_run()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 877, in get_next_batch_to_run
    new_batch = self.get_new_batch_prefill()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 898, in get_new_batch_prefill
    self.move_ready_grammar_requests()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1594, in move_ready_grammar_requests
    req.grammar = req.grammar.result(timeout=0.05)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/base_grammar_backend.py", line 53, in init_value
    entry.value = self.init_value_impl(key)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/outlines_backend.py", line 173, in init_value_impl
    guide = RegexGuide.from_regex(regex, self.outlines_tokenizer)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 92, in from_regex
    return super().from_regex(
           ^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 212, in from_regex
    ) = _create_states_mapping(
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 76, in cached_create_states_mapping
    return uncached_create_states_mapping(regex_string, tokenizer, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 141, in create_states_mapping
    return create_states_mapping_from_fsm(regex_fsm, tokenizer, frozen_tokens)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 178, in create_states_mapping_from_fsm
    states_to_token_maps, empty_token_ids = create_fsm_index_tokenizer(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/regex.py", line 473, in create_fsm_index_tokenizer
    tokens_to_token_ids, empty_token_ids = reduced_vocabulary(tokenizer)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/models/transformers.py", line 119, in __hash__
    return hash(Hasher.hash(self.tokenizer))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/fingerprint.py", line 188, in hash
    return cls.hash_bytes(dumps(value))
                          ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 109, in dumps
    dump(obj, file)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 103, in dump
    Pickler(file, recurse=True).dump(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 428, in dump
    StockPickler.dump(self, obj)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 487, in dump
    self.save(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 70, in save
    dill.Pickler.save(self, obj, save_persistent_id=save_persistent_id)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 422, in save
    StockPickler.save(self, obj, save_persistent_id)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 209, in _save_transformersPreTrainedTokenizerBase
    log(pickler, f"Tok: {obj}")
    ^^^
NameError: name 'log' is not defined

[2025-02-27 09:04:17] Received sigquit from a child proces. It usually means the child failed.
Killed

This issue seems quite strange and I don't find bugs about sglang in this context. In my environment, this code can work correctly. Could you use docker to run the repo? Maybe it can give more useful information and I will fix it.

Have you run it? if that's the case, it can be just a case of having some of my dependencies outdated, will take a look

@Jayon02
Copy link
Author

Jayon02 commented Feb 27, 2025

for the integration test failing, I already fixed in the the develop branch, if you merge it, that should do it. And for the docs they will appear in the components gallery section, it's okey, unless you want to add some additional example in the /examples folder or something 😄

Ok!I will merge develop branch into my main branch. I also add dependency in pyproject.toml. Due to version compatibility, only transformers==4.48.3 supports the code to work correctly. So, I have locked this version.

Copy link
Contributor

@plaguss plaguss left a comment

Choose a reason for hiding this comment

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

just changing the version in the pyproject and should be ready to go

@Jayon02
Copy link
Author

Jayon02 commented Feb 27, 2025

Also, the example from the structured outputs is failing:

from distilabel.models.llms import SGLang
from pydantic import BaseModel
if __name__ == "__main__":
    class User(BaseModel):
        name: str
        last_name: str
        id: int
    llm = SGLang(
        model="Qwen/Qwen2.5-Coder-3B-Instruct",
        structured_output={"format": "json", "schema": User},
    )
    llm.load()
    # Call the model
    output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Create a user profile for the following marathon"}]])

It throws the following trace:

[2025-02-27 09:04:17 TP0] Scheduler hit an exception: Traceback (most recent call last):
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1825, in run_scheduler_process
    scheduler.event_loop_overlap()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 496, in event_loop_overlap
    batch = self.get_next_batch_to_run()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 877, in get_next_batch_to_run
    new_batch = self.get_new_batch_prefill()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 898, in get_new_batch_prefill
    self.move_ready_grammar_requests()
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/managers/scheduler.py", line 1594, in move_ready_grammar_requests
    req.grammar = req.grammar.result(timeout=0.05)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/base_grammar_backend.py", line 53, in init_value
    entry.value = self.init_value_impl(key)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/sglang/srt/constrained/outlines_backend.py", line 173, in init_value_impl
    guide = RegexGuide.from_regex(regex, self.outlines_tokenizer)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 92, in from_regex
    return super().from_regex(
           ^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 212, in from_regex
    ) = _create_states_mapping(
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/fsm/guide.py", line 76, in cached_create_states_mapping
    return uncached_create_states_mapping(regex_string, tokenizer, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 141, in create_states_mapping
    return create_states_mapping_from_fsm(regex_fsm, tokenizer, frozen_tokens)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/guide.py", line 178, in create_states_mapping_from_fsm
    states_to_token_maps, empty_token_ids = create_fsm_index_tokenizer(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines_core/fsm/regex.py", line 473, in create_fsm_index_tokenizer
    tokens_to_token_ids, empty_token_ids = reduced_vocabulary(tokenizer)
                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/outlines/models/transformers.py", line 119, in __hash__
    return hash(Hasher.hash(self.tokenizer))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/fingerprint.py", line 188, in hash
    return cls.hash_bytes(dumps(value))
                          ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 109, in dumps
    dump(obj, file)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 103, in dump
    Pickler(file, recurse=True).dump(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 428, in dump
    StockPickler.dump(self, obj)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 487, in dump
    self.save(obj)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 70, in save
    dill.Pickler.save(self, obj, save_persistent_id=save_persistent_id)
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/dill/_dill.py", line 422, in save
    StockPickler.save(self, obj, save_persistent_id)
  File "/admin/home/agustin_piqueres/.pyenv/versions/3.11.9/lib/python3.11/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/fsx/agustin_piqueres/test-distilabel/.venv/lib/python3.11/site-packages/datasets/utils/_dill.py", line 209, in _save_transformersPreTrainedTokenizerBase
    log(pickler, f"Tok: {obj}")
    ^^^
NameError: name 'log' is not defined

[2025-02-27 09:04:17] Received sigquit from a child proces. It usually means the child failed.
Killed

This issue seems quite strange and I don't find bugs about sglang in this context. In my environment, this code can work correctly. Could you use docker to run the repo? Maybe it can give more useful information and I will fix it.

Have you run it? if that's the case, it can be just a case of having some of my dependencies outdated, will take a look

Yes. I have just run it correctly.

@plaguss plaguss merged commit ab612a6 into argilla-io:develop Feb 27, 2025
1 check passed
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.

[FEATURE] sglang integration
4 participants