File size: 3,897 Bytes
1ccd153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import argparse
import time

import datasets
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig


MODEL_ID = "Qwen/Qwen3-4B-Instruct-2507"


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--samples", type=int, default=100, help="Number of prompts to run")
    parser.add_argument("--batch-size", "-bs", type=int, default=32, help="Static batch size")
    parser.add_argument("--max-new-tokens", type=int, default=512, help="Max new tokens per request")
    parser.add_argument("--warmup", type=int, default=1, help="Warmup batches (excluded from timing)")
    args = parser.parse_args()

    # Load model (static batching, SDPA attention), BF16 for speed/memory
    model = AutoModelForCausalLM.from_pretrained(
        MODEL_ID,
        attn_implementation="sdpa",
        torch_dtype=torch.bfloat16,
    ).cuda().eval()

    # Tokenizer: left padding is typically better for batched causal LMs
    tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, padding_side="left")
    if tokenizer.pad_token_id is None:
        tokenizer.pad_token = tokenizer.eos_token

    # Dataset: GSM8K (socratic) questions only
    dataset = datasets.load_dataset("openai/gsm8k", "socratic", split="test")
    dataset = dataset.select(range(args.samples))

    # Tokenize up front (no padding yet; we’ll pad per-batch for efficiency)
    encoded = tokenizer(list(dataset["question"]), padding=False, truncation=False)
    inputs = [{"input_ids": ids, "attention_mask": attn}
              for ids, attn in zip(encoded["input_ids"], encoded["attention_mask"])]

    # Generation config
    gen_cfg = GenerationConfig(
        do_sample=False,
        max_new_tokens=args.max_new_tokens,
        eos_token_id=tokenizer.eos_token_id,
        pad_token_id=tokenizer.pad_token_id,
        use_cuda_graph=False,  # keep simple/portable
    )

    # Helper to create a padded batch on-device
    def make_batch(items):
        batch = tokenizer.pad(items, padding=True, return_tensors="pt")
        return {k: v.cuda(non_blocking=True) for k, v in batch.items()}

    # Optional warmup (excluded from timing)
    model_inputs = []
    if args.warmup > 0:
        warm = make_batch(inputs[: min(len(inputs), args.batch_size * args.warmup)])
        with torch.no_grad():
            _ = model.generate(**warm, generation_config=gen_cfg)

    # Timed generation over all batches
    token_count = 0
    bs = args.batch_size
    start = time.time()
    with torch.no_grad():
        for i in range(0, len(inputs), bs):
            batch_items = inputs[i : i + bs]
            batch = make_batch(batch_items)

            # Run generate()
            outputs = model.generate(**batch, generation_config=gen_cfg)

            # Count newly generated tokens per sequence
            # new_tokens = (#non-pad tokens after the original prompt length)
            pad_id = tokenizer.pad_token_id
            input_lens = batch["attention_mask"].sum(dim=1).tolist()
            for row, in_len in zip(outputs, input_lens):
                seq = row.tolist()
                gen_part = seq[int(in_len):]
                token_count += sum(1 for t in gen_part if t != pad_id)

    end = time.time()
    elapsed = end - start
    tps = token_count / elapsed if elapsed > 0 else 0.0

    print("-" * 20)
    print("--- Finished Static Batching Benchmark ---\n")
    print(f"Model: {MODEL_ID}")
    print(f"Attention: sdpa | Batch size: {args.batch_size} | Samples: {args.samples} | Max new tokens: {args.max_new_tokens}")
    print(f"Generation time (no warmup): {elapsed:.2f} s for {token_count} generated tokens -> {tps:.2f} tok/s")


if __name__ == "__main__":
    main()

#Attention: sdpa | Batch size: 32 | Samples: 100 | Max new tokens: 512
# Generation time (no warmup): 153.98 s for 53427 generated tokens -> 346.98 tok/s