Create sb-bench.py
Browse files- sb-bench.py +99 -0
sb-bench.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import datasets
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
from transformers.generation import GenerationConfig
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MODEL_ID = "Qwen/Qwen3-4B-Instruct-2507"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
parser = argparse.ArgumentParser()
|
| 15 |
+
parser.add_argument("--samples", type=int, default=100, help="Number of prompts to run")
|
| 16 |
+
parser.add_argument("--batch-size", "-bs", type=int, default=32, help="Static batch size")
|
| 17 |
+
parser.add_argument("--max-new-tokens", type=int, default=512, help="Max new tokens per request")
|
| 18 |
+
parser.add_argument("--warmup", type=int, default=1, help="Warmup batches (excluded from timing)")
|
| 19 |
+
args = parser.parse_args()
|
| 20 |
+
|
| 21 |
+
# Load model (static batching, SDPA attention), BF16 for speed/memory
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
MODEL_ID,
|
| 24 |
+
attn_implementation="sdpa",
|
| 25 |
+
torch_dtype=torch.bfloat16,
|
| 26 |
+
).cuda().eval()
|
| 27 |
+
|
| 28 |
+
# Tokenizer: left padding is typically better for batched causal LMs
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, padding_side="left")
|
| 30 |
+
if tokenizer.pad_token_id is None:
|
| 31 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 32 |
+
|
| 33 |
+
# Dataset: GSM8K (socratic) questions only
|
| 34 |
+
dataset = datasets.load_dataset("openai/gsm8k", "socratic", split="test")
|
| 35 |
+
dataset = dataset.select(range(args.samples))
|
| 36 |
+
|
| 37 |
+
# Tokenize up front (no padding yet; we’ll pad per-batch for efficiency)
|
| 38 |
+
encoded = tokenizer(list(dataset["question"]), padding=False, truncation=False)
|
| 39 |
+
inputs = [{"input_ids": ids, "attention_mask": attn}
|
| 40 |
+
for ids, attn in zip(encoded["input_ids"], encoded["attention_mask"])]
|
| 41 |
+
|
| 42 |
+
# Generation config
|
| 43 |
+
gen_cfg = GenerationConfig(
|
| 44 |
+
do_sample=False,
|
| 45 |
+
max_new_tokens=args.max_new_tokens,
|
| 46 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 47 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 48 |
+
use_cuda_graph=False, # keep simple/portable
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Helper to create a padded batch on-device
|
| 52 |
+
def make_batch(items):
|
| 53 |
+
batch = tokenizer.pad(items, padding=True, return_tensors="pt")
|
| 54 |
+
return {k: v.cuda(non_blocking=True) for k, v in batch.items()}
|
| 55 |
+
|
| 56 |
+
# Optional warmup (excluded from timing)
|
| 57 |
+
model_inputs = []
|
| 58 |
+
if args.warmup > 0:
|
| 59 |
+
warm = make_batch(inputs[: min(len(inputs), args.batch_size * args.warmup)])
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
_ = model.generate(**warm, generation_config=gen_cfg)
|
| 62 |
+
|
| 63 |
+
# Timed generation over all batches
|
| 64 |
+
token_count = 0
|
| 65 |
+
bs = args.batch_size
|
| 66 |
+
start = time.time()
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
for i in range(0, len(inputs), bs):
|
| 69 |
+
batch_items = inputs[i : i + bs]
|
| 70 |
+
batch = make_batch(batch_items)
|
| 71 |
+
|
| 72 |
+
# Run generate()
|
| 73 |
+
outputs = model.generate(**batch, generation_config=gen_cfg)
|
| 74 |
+
|
| 75 |
+
# Count newly generated tokens per sequence
|
| 76 |
+
# new_tokens = (#non-pad tokens after the original prompt length)
|
| 77 |
+
pad_id = tokenizer.pad_token_id
|
| 78 |
+
input_lens = batch["attention_mask"].sum(dim=1).tolist()
|
| 79 |
+
for row, in_len in zip(outputs, input_lens):
|
| 80 |
+
seq = row.tolist()
|
| 81 |
+
gen_part = seq[int(in_len):]
|
| 82 |
+
token_count += sum(1 for t in gen_part if t != pad_id)
|
| 83 |
+
|
| 84 |
+
end = time.time()
|
| 85 |
+
elapsed = end - start
|
| 86 |
+
tps = token_count / elapsed if elapsed > 0 else 0.0
|
| 87 |
+
|
| 88 |
+
print("-" * 20)
|
| 89 |
+
print("--- Finished Static Batching Benchmark ---\n")
|
| 90 |
+
print(f"Model: {MODEL_ID}")
|
| 91 |
+
print(f"Attention: sdpa | Batch size: {args.batch_size} | Samples: {args.samples} | Max new tokens: {args.max_new_tokens}")
|
| 92 |
+
print(f"Generation time (no warmup): {elapsed:.2f} s for {token_count} generated tokens -> {tps:.2f} tok/s")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
main()
|
| 97 |
+
|
| 98 |
+
#Attention: sdpa | Batch size: 32 | Samples: 100 | Max new tokens: 512
|
| 99 |
+
# Generation time (no warmup): 153.98 s for 53427 generated tokens -> 346.98 tok/s
|