-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Do not Merge] Make VeRL SGLang Native #3102
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import copy | ||
| import os | ||
| import uuid | ||
| from dataclasses import dataclass, field | ||
| from enum import Enum | ||
| from pathlib import Path | ||
| from typing import Any, Dict, List, Optional, Union | ||
| from uuid import uuid4 | ||
|
|
||
| import ray | ||
| import torch | ||
| from torch.utils.data import Dataset | ||
| from tqdm import tqdm | ||
| from transformers import AutoTokenizer | ||
|
|
||
| from verl.utils import hf_processor, hf_tokenizer | ||
| from verl.utils.dataset.rl_dataset import RLHFDataset | ||
|
|
||
|
|
||
| class Status(Enum): | ||
| PENDING = "pending" | ||
| COMPLETED = "completed" | ||
| TRUNCATED = "truncated" | ||
| ABORTED = "aborted" | ||
|
|
||
|
|
||
| class Buffer: | ||
| def __init__(self, config): | ||
| # init_wandb_secondary(args, wandb_run_id) | ||
| self.config = config | ||
|
|
||
| # 数据源相关属性 | ||
| self.epoch_id = 0 | ||
| self.sample_index = 0 | ||
| self.sample_offset = 0 | ||
| # TODO remove this | ||
| self.metadata = {} | ||
|
|
||
| # 初始化tokenizer和processor | ||
| local_path = self.config.actor_rollout_ref.model.path | ||
| self.tokenizer = hf_tokenizer(local_path, trust_remote_code=True) | ||
| # Used for multimodal LLM, could be None | ||
| self.processor = hf_processor(local_path, trust_remote_code=True, use_fast=True) | ||
|
|
||
| # 加载RLHF数据集 | ||
| rldataset = RLHFDataset( | ||
| data_files=self.config.data.train_files, | ||
| tokenizer=self.tokenizer, | ||
| processor=self.processor, | ||
| config=self.config.data, | ||
| ) | ||
| self.dataset = [] | ||
| for item in tqdm(rldataset, desc="Loading RLHF dataset", total=len(rldataset)): | ||
| self.dataset.append(item) | ||
|
|
||
| self.n_samples_per_prompt = self.config.actor_rollout_ref.rollout.n | ||
|
|
||
| self.buffer: List[List[Dict]] = [] | ||
|
|
||
| def get_num_rollout_per_epoch(self): | ||
| return len(self.dataset) //self.config.actor_rollout_ref.rollout.rollout_batch_size | ||
|
|
||
| def _get_samples_from_data_source(self, num_samples: int) -> List[List[Dict]]: | ||
| """从数据源获取样本,整合了原RolloutDataSource.get_samples的逻辑""" | ||
| samples = [] | ||
| # TODO unify the two branches | ||
| if self.sample_offset + num_samples <= len(self.dataset): | ||
| prompt_samples = self.dataset[self.sample_offset : self.sample_offset + num_samples] | ||
| self.sample_offset += num_samples | ||
| else: | ||
| prompt_samples = self.dataset[self.sample_offset :] | ||
| num_samples -= len(prompt_samples) | ||
| # self.epoch_id += 1 | ||
| # if self.args.rollout_shuffle: | ||
| # self.dataset.shuffle(self.epoch_id) | ||
| prompt_samples += self.dataset[:num_samples] | ||
| self.sample_offset = num_samples | ||
| # self.sample_offset = 0 | ||
|
|
||
|
|
||
| for prompt_sample in prompt_samples: | ||
| group = [] | ||
| prompt_sample["status"] = Status.PENDING | ||
| prompt_sample["response_length"] = 0 | ||
| prompt_sample["response"] = [] | ||
| prompt_sample["uid"] = str(uuid.uuid4()) | ||
|
|
||
| for _ in range(self.n_samples_per_prompt): | ||
| sample = copy.deepcopy(prompt_sample) | ||
| group.append(sample) | ||
| samples.append(group) | ||
|
|
||
| return samples | ||
|
|
||
| # TODO simplify remaining logic | ||
| def get_samples(self, num_samples: int) -> List[List[Dict]]: | ||
| """ | ||
| Return num_samples samples | ||
| """ | ||
|
|
||
| samples = self._get_samples_from_buffer(num_samples) | ||
| num_samples -= len(samples) | ||
|
|
||
| if num_samples == 0: | ||
| return samples | ||
|
|
||
| samples += self._get_samples_from_data_source(num_samples=num_samples) | ||
| return samples | ||
|
|
||
| def _get_samples_from_buffer(self, num_samples: int) -> List[List[Dict]]: | ||
| if len(self.buffer) == 0 or num_samples == 0: | ||
| return [] | ||
| num_to_pop = min(len(self.buffer), num_samples) | ||
| samples = self.buffer[:num_to_pop] | ||
| del self.buffer[:num_to_pop] | ||
| return samples | ||
|
|
||
| def add_samples(self, samples: List[List[Dict]]): | ||
| """ | ||
| Add a sample group to buffer. | ||
| """ | ||
| if not samples: | ||
| return | ||
| assert isinstance(samples, list), f"samples must be a list, got {type(samples)}" | ||
| assert isinstance(samples[0], list), f"the elements of samples must be list, got {type(samples[0])}" | ||
| for i in range(0, len(samples)): | ||
| assert len(samples[i]) == self.n_samples_per_prompt, ( | ||
| f"the length of the elements of samples must be equal to n_samples_per_prompt, got {len(samples[i])} != {self.n_samples_per_prompt}" | ||
| ) | ||
| group = samples[i] # type: ignore | ||
| self.buffer.append(group) | ||
|
|
||
| def get_buffer_length(self): | ||
| return len(self.buffer) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing
kwargswith[]is unsafe as it will raise aKeyErrorif the keyssglang_router_iporsglang_router_portare not provided by the caller. This can lead to runtime crashes that are hard to debug. For better robustness, you should usekwargs.get()and handle the case where the keys might be missing, or explicitly check for their existence and raise a more informativeValueError.