[Benchmark] Fix generated_shared_prefix attribute naming and remove args dependency (#19363)
Co-authored-by: Alison Shao <alisonshao@Mac.attlocal.net> Co-authored-by: sglang-bot <sglangbot@gmail.com>
This commit is contained in:
co-authored by
Alison Shao
sglang-bot
parent
6e82183f5a
commit
a0a8f1473c
@@ -442,7 +442,15 @@ def sample_generated_shared_prefix_requests(
|
|||||||
disable_shuffle: bool = False,
|
disable_shuffle: bool = False,
|
||||||
) -> SampleOutput:
|
) -> SampleOutput:
|
||||||
"""Generate benchmark requests with shared system prompts using random tokens and caching."""
|
"""Generate benchmark requests with shared system prompts using random tokens and caching."""
|
||||||
cache_path = get_gen_prefix_cache_path(args, tokenizer)
|
cache_path = get_gen_prefix_cache_path(
|
||||||
|
args.seed,
|
||||||
|
num_groups,
|
||||||
|
prompts_per_group,
|
||||||
|
system_prompt_len,
|
||||||
|
question_len,
|
||||||
|
output_len,
|
||||||
|
tokenizer,
|
||||||
|
)
|
||||||
|
|
||||||
# Try to load from cache first
|
# Try to load from cache first
|
||||||
if cache_path.exists():
|
if cache_path.exists():
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import argparse
|
|
||||||
import pickle
|
import pickle
|
||||||
import random
|
import random
|
||||||
import uuid
|
import uuid
|
||||||
@@ -29,10 +28,10 @@ class GeneratedSharedPrefixDataset(BaseDataset):
|
|||||||
output_len: int
|
output_len: int
|
||||||
range_ratio: float
|
range_ratio: float
|
||||||
seed: int
|
seed: int
|
||||||
gsp_fast_prepare: bool
|
fast_prepare: bool
|
||||||
gsp_send_routing_key: bool
|
send_routing_key: bool
|
||||||
gsp_num_turns: int
|
num_turns: int
|
||||||
gsp_ordered: bool
|
ordered: bool
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_args(cls, args: Namespace) -> "GeneratedSharedPrefixDataset":
|
def from_args(cls, args: Namespace) -> "GeneratedSharedPrefixDataset":
|
||||||
@@ -45,10 +44,10 @@ class GeneratedSharedPrefixDataset(BaseDataset):
|
|||||||
output_len=args.gsp_output_len,
|
output_len=args.gsp_output_len,
|
||||||
range_ratio=getattr(args, "gsp_range_ratio", 1.0),
|
range_ratio=getattr(args, "gsp_range_ratio", 1.0),
|
||||||
seed=args.seed,
|
seed=args.seed,
|
||||||
gsp_fast_prepare=getattr(args, "gsp_fast_prepare", False),
|
fast_prepare=getattr(args, "gsp_fast_prepare", False),
|
||||||
gsp_send_routing_key=getattr(args, "gsp_send_routing_key", False),
|
send_routing_key=getattr(args, "gsp_send_routing_key", False),
|
||||||
gsp_num_turns=getattr(args, "gsp_num_turns", 1),
|
num_turns=getattr(args, "gsp_num_turns", 1),
|
||||||
gsp_ordered=getattr(args, "gsp_ordered", False),
|
ordered=getattr(args, "gsp_ordered", False),
|
||||||
)
|
)
|
||||||
|
|
||||||
def load(
|
def load(
|
||||||
@@ -62,18 +61,29 @@ class GeneratedSharedPrefixDataset(BaseDataset):
|
|||||||
output_len=self.output_len,
|
output_len=self.output_len,
|
||||||
range_ratio=self.range_ratio,
|
range_ratio=self.range_ratio,
|
||||||
tokenizer=tokenizer,
|
tokenizer=tokenizer,
|
||||||
args=self,
|
seed=self.seed,
|
||||||
|
send_routing_key=self.send_routing_key,
|
||||||
|
num_turns=self.num_turns,
|
||||||
|
fast_prepare=self.fast_prepare,
|
||||||
|
ordered=self.ordered,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_gen_prefix_cache_path(args, tokenizer):
|
def get_gen_prefix_cache_path(
|
||||||
|
seed: int,
|
||||||
|
num_groups: int,
|
||||||
|
prompts_per_group: int,
|
||||||
|
system_prompt_len: int,
|
||||||
|
question_len: int,
|
||||||
|
output_len: int,
|
||||||
|
tokenizer,
|
||||||
|
):
|
||||||
"""Create cache directory under ~/.cache/sglang/benchmark"""
|
"""Create cache directory under ~/.cache/sglang/benchmark"""
|
||||||
cache_dir = Path.home() / ".cache" / "sglang" / "benchmark"
|
cache_dir = Path.home() / ".cache" / "sglang" / "benchmark"
|
||||||
|
|
||||||
# Create a unique cache filename based on the generation parameters
|
|
||||||
cache_key = (
|
cache_key = (
|
||||||
f"gen_shared_prefix_{args.seed}_{args.gsp_num_groups}_{args.gsp_prompts_per_group}_"
|
f"gen_shared_prefix_{seed}_{num_groups}_{prompts_per_group}_"
|
||||||
f"{args.gsp_system_prompt_len}_{args.gsp_question_len}_{args.gsp_output_len}_"
|
f"{system_prompt_len}_{question_len}_{output_len}_"
|
||||||
f"{tokenizer.__class__.__name__}.pkl"
|
f"{tokenizer.__class__.__name__}.pkl"
|
||||||
)
|
)
|
||||||
return cache_dir / cache_key
|
return cache_dir / cache_key
|
||||||
@@ -87,13 +97,22 @@ def sample_generated_shared_prefix_requests(
|
|||||||
output_len: int,
|
output_len: int,
|
||||||
range_ratio: float,
|
range_ratio: float,
|
||||||
tokenizer: PreTrainedTokenizerBase,
|
tokenizer: PreTrainedTokenizerBase,
|
||||||
args: argparse.Namespace,
|
seed: int,
|
||||||
|
send_routing_key: bool = False,
|
||||||
|
num_turns: int = 1,
|
||||||
|
fast_prepare: bool = False,
|
||||||
|
ordered: bool = False,
|
||||||
) -> List[DatasetRow]:
|
) -> List[DatasetRow]:
|
||||||
"""Generate benchmark requests with shared system prompts using random tokens and caching."""
|
"""Generate benchmark requests with shared system prompts using random tokens and caching."""
|
||||||
send_routing_key = getattr(args, "gsp_send_routing_key", False)
|
cache_path = get_gen_prefix_cache_path(
|
||||||
num_turns = getattr(args, "gsp_num_turns", 1)
|
seed,
|
||||||
|
num_groups,
|
||||||
cache_path = get_gen_prefix_cache_path(args, tokenizer)
|
prompts_per_group,
|
||||||
|
system_prompt_len,
|
||||||
|
question_len,
|
||||||
|
output_len,
|
||||||
|
tokenizer,
|
||||||
|
)
|
||||||
should_cache = (range_ratio == 1) and not send_routing_key and num_turns == 1
|
should_cache = (range_ratio == 1) and not send_routing_key and num_turns == 1
|
||||||
|
|
||||||
# Try to load from cache first
|
# Try to load from cache first
|
||||||
@@ -168,11 +187,7 @@ def sample_generated_shared_prefix_requests(
|
|||||||
1:
|
1:
|
||||||
]
|
]
|
||||||
full_prompt = turn_prompts[0] if num_turns == 1 else turn_prompts
|
full_prompt = turn_prompts[0] if num_turns == 1 else turn_prompts
|
||||||
prompt_len = (
|
prompt_len = 1 if fast_prepare else len(tokenizer.encode(turn_prompts[0]))
|
||||||
1
|
|
||||||
if getattr(args, "gsp_fast_prepare", False)
|
|
||||||
else len(tokenizer.encode(turn_prompts[0]))
|
|
||||||
)
|
|
||||||
output_len_val = int(output_lens[group_idx, prompt_idx])
|
output_len_val = int(output_lens[group_idx, prompt_idx])
|
||||||
|
|
||||||
input_requests.append(
|
input_requests.append(
|
||||||
@@ -186,7 +201,7 @@ def sample_generated_shared_prefix_requests(
|
|||||||
total_input_tokens += prompt_len
|
total_input_tokens += prompt_len
|
||||||
total_output_tokens += output_len_val
|
total_output_tokens += output_len_val
|
||||||
|
|
||||||
if not getattr(args, "gsp_ordered", False):
|
if not ordered:
|
||||||
random.shuffle(input_requests)
|
random.shuffle(input_requests)
|
||||||
|
|
||||||
# Print statistics
|
# Print statistics
|
||||||
@@ -195,7 +210,7 @@ def sample_generated_shared_prefix_requests(
|
|||||||
print(f"Prompts per group: {prompts_per_group}")
|
print(f"Prompts per group: {prompts_per_group}")
|
||||||
print(f"Number of turns: {num_turns}")
|
print(f"Number of turns: {num_turns}")
|
||||||
print(f"Total prompts: {len(input_requests)}")
|
print(f"Total prompts: {len(input_requests)}")
|
||||||
if not getattr(args, "gsp_fast_prepare", False):
|
if not fast_prepare:
|
||||||
print(f"Total input tokens: {total_input_tokens}")
|
print(f"Total input tokens: {total_input_tokens}")
|
||||||
print(f"Total output tokens: {total_output_tokens}")
|
print(f"Total output tokens: {total_output_tokens}")
|
||||||
print(
|
print(
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ class TestBenchmarkDatasetsAPI(unittest.TestCase):
|
|||||||
self.assertIn("tools", rows[1].extra_request_body)
|
self.assertIn("tools", rows[1].extra_request_body)
|
||||||
|
|
||||||
def test_generated_shared_prefix_sampler(self):
|
def test_generated_shared_prefix_sampler(self):
|
||||||
args = make_args(gsp_range_ratio=0.0, gsp_num_groups=2, gsp_prompts_per_group=2)
|
args = make_args(gsp_num_groups=2, gsp_prompts_per_group=2)
|
||||||
rows = sample_generated_shared_prefix_requests(
|
rows = sample_generated_shared_prefix_requests(
|
||||||
num_groups=args.gsp_num_groups,
|
num_groups=args.gsp_num_groups,
|
||||||
prompts_per_group=args.gsp_prompts_per_group,
|
prompts_per_group=args.gsp_prompts_per_group,
|
||||||
@@ -303,7 +303,7 @@ class TestBenchmarkDatasetsAPI(unittest.TestCase):
|
|||||||
output_len=args.gsp_output_len,
|
output_len=args.gsp_output_len,
|
||||||
range_ratio=args.gsp_range_ratio,
|
range_ratio=args.gsp_range_ratio,
|
||||||
tokenizer=self.tokenizer,
|
tokenizer=self.tokenizer,
|
||||||
args=args,
|
seed=args.seed,
|
||||||
)
|
)
|
||||||
self.assertEqual(len(rows), 4)
|
self.assertEqual(len(rows), 4)
|
||||||
self.assertTrue(all(isinstance(row, DatasetRow) for row in rows))
|
self.assertTrue(all(isinstance(row, DatasetRow) for row in rows))
|
||||||
@@ -416,6 +416,15 @@ class TestBenchmarkDatasetsAPI(unittest.TestCase):
|
|||||||
mmmu_rows = get_dataset(mmmu_args, self.tokenizer, model_id="dummy-model")
|
mmmu_rows = get_dataset(mmmu_args, self.tokenizer, model_id="dummy-model")
|
||||||
self.assertEqual(len(mmmu_rows), 1)
|
self.assertEqual(len(mmmu_rows), 1)
|
||||||
|
|
||||||
|
gsp_args = make_args(
|
||||||
|
dataset_name="generated-shared-prefix",
|
||||||
|
gsp_num_groups=2,
|
||||||
|
gsp_prompts_per_group=2,
|
||||||
|
)
|
||||||
|
gsp_rows = get_dataset(gsp_args, self.tokenizer, model_id="dummy-model")
|
||||||
|
self.assertEqual(len(gsp_rows), 4)
|
||||||
|
self.assertTrue(all(isinstance(row, DatasetRow) for row in gsp_rows))
|
||||||
|
|
||||||
def test_get_dataset_unknown_dataset(self):
|
def test_get_dataset_unknown_dataset(self):
|
||||||
args = make_args(dataset_name="not-a-dataset")
|
args = make_args(dataset_name="not-a-dataset")
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
|
|||||||
Reference in New Issue
Block a user