import os import time import statistics from typing import List, Tuple, Dict import torch import torch.cuda.nvtx as nvtx from vllm import LLM, SamplingParams from transformers import AutoTokenizer # ========= 强制使用 vLLM V1 ========= os.environ.setdefault("VLLM_USE_V1", "1") os.environ.setdefault("VLLM_WORKER_MULTIPROC_METHOD", "spawn") # 可选:打开 V1 metrics 统计 os.environ.setdefault("VLLM_LOGGING_LEVEL", "INFO") # ========= 试图导入 V1 metrics 类型(兼容不同版本)========= try: from vllm.v1.metrics.reader import Counter, Gauge, Histogram, Vector # type: ignore except Exception: Counter = Gauge = Histogram = Vector = type("X", (), {}) # dummy # ========= 配置 ========= MODEL_NAME = "Qwen/Qwen2-1.5B" DTYPE = "bfloat16" TP = 1 GPU_MEM_UTIL = 0.90 TRUST_REMOTE_CODE = True # 场景:prefill=输入tokens,decode=输出tokens SCENARIOS = [ # {"name": "prefill640_decode1", "prompt_tokens": 640, "max_new_tokens": 1}, # {"name": "prefill1_decode512", "prompt_tokens": 1, "max_new_tokens": 512}, {"name": "prefill640_decode512", "prompt_tokens": 640, "max_new_tokens": 512}, ] BATCH_SIZES = [1, 2, 4, 8, 16, 32, 64, 128, 256] SEED = 1234 TEMPERATURE = 0.0 TOP_P = 1.0 WARMUP_PER_BS = 1 # 每个批次做一次预热 # ========= 构造“精确 token 数量”的 prompt ========= def build_exact_token_prompt(tokenizer, target_len: int) -> str: if target_len <= 1: # 最小化 prompt:用一个简单 token(避免空串导致0 token) ids = tokenizer("A", add_special_tokens=False)["input_ids"] if len(ids) >= 1: return tokenizer.decode(ids[:1], skip_special_tokens=True, clean_up_tokenization_spaces=False) base_text = ( "You are a helpful assistant. " "Please analyze the following input and respond succinctly. " ) chunk = " ".join(["data"] * 100) + ". " text = base_text + chunk * 200 # 足够长的文本 lo, hi = 0, len(text) target_ids = None while lo <= hi: mid = (lo + hi) // 2 ids = tokenizer(text[:mid], add_special_tokens=False)["input_ids"] if len(ids) == target_len: target_ids = ids break if len(ids) < target_len: lo = mid + 1 else: hi = mid - 1 if target_ids is None: ids = tokenizer(text[:lo], add_special_tokens=False)["input_ids"] if len(ids) > target_len: target_ids = ids[:target_len] else: filler = " data" while len(ids) < target_len: ids = tokenizer(tokenizer.decode(ids) + filler, add_special_tokens=False)["input_ids"] target_ids = ids[:target_len] prompt = tokenizer.decode(target_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) # 断言精确长度 assert len(tokenizer(prompt, add_special_tokens=False)["input_ids"]) == target_len return prompt # ========= V1 metrics 抽取工具 ========= TTFT_METRIC_NAME = "vllm:time_to_first_token_seconds" TPOT_METRIC_NAME = "vllm:time_per_output_token_seconds" # per-output-token latency def _iter_children_of_vector(vec_obj): for attr in ("children", "metrics", "series", "values", "samples", "items"): if hasattr(vec_obj, attr): val = getattr(vec_obj, attr) if isinstance(val, dict): for v in val.values(): yield v else: try: for v in val: yield v except TypeError: pass def _collect_hist_sum_count(metrics, metric_name: str): total_sum = 0.0 total_count = 0.0 for m in metrics: mname = getattr(m, "name", None) if mname != metric_name: continue # 直接 Histogram if isinstance(m, Histogram) or m.__class__.__name__ == "Histogram": total_sum += float(getattr(m, "sum", 0.0)) total_count += float(getattr(m, "count", 0.0)) continue # Vector[Histogram] if isinstance(m, Vector) or m.__class__.__name__ == "Vector": for child in _iter_children_of_vector(m): if isinstance(child, Histogram) or child.__class__.__name__ == "Histogram": total_sum += float(getattr(child, "sum", 0.0)) total_count += float(getattr(child, "count", 0.0)) return total_sum, total_count def _metrics_snapshot(llm) -> Dict[str, float]: try: mets = llm.get_metrics() # V1: 返回 Metric 列表(包含 Histogram/Vector 等) except Exception: return {"ttft_sum": 0.0, "ttft_cnt": 0.0, "tpot_sum": 0.0, "tpot_cnt": 0.0} ttft_sum, ttft_cnt = _collect_hist_sum_count(mets, TTFT_METRIC_NAME) tpot_sum, tpot_cnt = _collect_hist_sum_count(mets, TPOT_METRIC_NAME) return {"ttft_sum": ttft_sum, "ttft_cnt": ttft_cnt, "tpot_sum": tpot_sum, "tpot_cnt": tpot_cnt} def _metrics_delta(before: dict, after: dict): return { "ttft_sum": after["ttft_sum"] - before["ttft_sum"], "ttft_cnt": after["ttft_cnt"] - before["ttft_cnt"], "tpot_sum": after["tpot_sum"] - before["tpot_sum"], "tpot_cnt": after["tpot_cnt"] - before["tpot_cnt"], } # ========= 内存 / KV cache 统计辅助 ========= def _try_import_pynvml(): try: import pynvml # type: ignore pynvml.nvmlInit() return pynvml except Exception: return None def _gpu_mem_stats(device_idx: int = 0): # PyTorch 视角 try: torch.cuda.synchronize(device_idx) allocated = torch.cuda.memory_allocated(device_idx) reserved = torch.cuda.memory_reserved(device_idx) max_alloc = torch.cuda.max_memory_allocated(device_idx) max_resv = torch.cuda.max_memory_reserved(device_idx) except Exception: allocated = reserved = max_alloc = max_resv = float("nan") # NVML 视角(接近 nvidia-smi) pynvml = _try_import_pynvml() nvml_total = nvml_used = nvml_free = float("nan") if pynvml is not None: try: h = pynvml.nvmlDeviceGetHandleByIndex(device_idx) mem = pynvml.nvmlDeviceGetMemoryInfo(h) nvml_total, nvml_used, nvml_free = float(mem.total), float(mem.used), float(mem.free) except Exception: pass def _fmt(b): # 以 MiB 显示 if b != b: # NaN return "NaN" return f"{b/1024/1024:.1f} MiB" return { "torch_allocated": _fmt(allocated), "torch_reserved": _fmt(reserved), "torch_max_alloc": _fmt(max_alloc), "torch_max_resvd": _fmt(max_resv), "nvml_total": _fmt(nvml_total), "nvml_used": _fmt(nvml_used), "nvml_free": _fmt(nvml_free), } # —— vLLM v1 metrics 中收集 Gauge/Counter 最新值(含 Vector 包装)—— def _collect_latest_metric(metrics, metric_name: str): latest = None for m in metrics: mname = getattr(m, "name", None) is_vector = getattr(m, "__class__", type("X", (), {})).__name__ in ("Vector",) if mname != metric_name and not (is_vector and any(getattr(child, "name", None) == metric_name for child in _iter_children_of_vector(m))): continue # 直接 Gauge/Counter if m.__class__.__name__ in ("Gauge", "Counter"): val = getattr(m, "value", None) if val is None: val = getattr(m, "_value", None) if isinstance(val, (int, float)): latest = float(val) # Vector[Gauge/Counter] if is_vector: for child in _iter_children_of_vector(m): if getattr(child, "name", None) == metric_name and child.__class__.__name__ in ("Gauge", "Counter"): val = getattr(child, "value", None) if val is None: val = getattr(child, "_value", None) if isinstance(val, (int, float)): latest = float(val) return latest # —— 从 metrics 中“尽力而为”获取 KV cache 使用情况 —— def _kv_cache_stats_from_metrics(llm): try: mets = llm.get_metrics() except Exception: return None # 常见/候选指标名(不同版本命名可能略有差异,尽可能覆盖) names_used_blocks = [ "vllm:kv_cache_gpu_blocks_in_use", "vllm:kv_cache:gpu_blocks_in_use", "vllm:cache:gpu_blocks_in_use", "vllm:num_gpu_blocks_in_use", ] names_total_blocks = [ "vllm:kv_cache_gpu_blocks_total", "vllm:kv_cache:gpu_blocks_total", "vllm:cache:gpu_blocks_total", "vllm:num_gpu_blocks_total", ] names_used_bytes = [ "vllm:kv_cache_usage_bytes", "vllm:kv_cache:usage_bytes", "vllm:cache:kv_bytes_used", ] used_blocks = None total_blocks = None used_bytes = None for n in names_used_blocks: v = _collect_latest_metric(mets, n) if v is not None: used_blocks = v; break for n in names_total_blocks: v = _collect_latest_metric(mets, n) if v is not None: total_blocks = v; break for n in names_used_bytes: v = _collect_latest_metric(mets, n) if v is not None: used_bytes = v; break util = None if used_blocks is not None and total_blocks and total_blocks > 0: util = used_blocks / total_blocks def _fmt_bytes(b): if b is None: return None return f"{b/1024/1024:.1f} MiB" return { "used_blocks": used_blocks, "total_blocks": total_blocks, "utilization": (f"{util*100:.1f}%" if util is not None else None), "used_bytes": _fmt_bytes(used_bytes), } # —— 回退:从内部引擎尝试估算 KV cache(不同版本可能不兼容,失败就返回 None)—— def _kv_cache_stats_from_engine(llm): try: eng = getattr(llm, "llm_engine", None) if eng is None: return None # 常见位置:eng.scheduler.cache_config 或 eng.cache_config cfg = getattr(getattr(eng, "scheduler", eng), "cache_config", None) or getattr(eng, "cache_config", None) if cfg is None: return None num_gpu_blocks = getattr(cfg, "num_gpu_blocks", None) block_size = getattr(cfg, "block_size", None) # 通常为 tokens/块 # 注:准确的字节大小依赖实现细节(层数、头数、dtype等),这里仅报告 blocks 维度,避免误导 return { "used_blocks": None, # 无法直接从 config 得到“已用”,只知道上限 "total_blocks": float(num_gpu_blocks) if num_gpu_blocks is not None else None, "utilization": None, "used_bytes": None, "notes": f"block_size={block_size} (tokens per block)" if block_size is not None else None, } except Exception: return None def _print_mem_and_kv(llm, header: str): print(f"[MEM/KV] {header}") # GPU 内存 mem = _gpu_mem_stats(0) print(f" PyTorch allocated/reserved: {mem['torch_allocated']} / {mem['torch_reserved']} " f"(peak {mem['torch_max_alloc']} / {mem['torch_max_resvd']})") if mem["nvml_total"] != "NaN": print(f" NVML total/used/free: {mem['nvml_total']} / {mem['nvml_used']} / {mem['nvml_free']}") # KV cache(优先从 metrics;不行再从引擎配置) kv = _kv_cache_stats_from_metrics(llm) if kv is None: kv = _kv_cache_stats_from_engine(llm) if kv is None: print(" KV cache: 未能获取(该 vLLM 版本可能未暴露对应指标)") else: parts = [] if kv.get("used_blocks") is not None: parts.append(f"used_blocks={int(kv['used_blocks'])}") if kv.get("total_blocks") is not None: parts.append(f"total_blocks={int(kv['total_blocks'])}") if kv.get("utilization") is not None: parts.append(f"util={kv['utilization']}") if kv.get("used_bytes") is not None: parts.append(f"used≈{kv['used_bytes']}") if kv.get("notes"): parts.append(kv["notes"]) if parts: print(" KV cache: " + ", ".join(parts)) else: print(" KV cache: 指标存在但内容为空/不兼容") # ========= 带 NVTX 的 generate 包装 ========= def decorated_generate(llm: LLM, prompts: List[str], params: SamplingParams): return llm.generate(prompts, params) # ========= 统计格式化 ========= def fmt_stats(x: List[float]) -> Tuple[float, float, float]: xs = [v for v in x if (v == v)] # 过滤 NaN if not xs: return (float("nan"), float("nan"), float("nan")) return (statistics.mean(xs), statistics.median(xs), statistics.quantiles(xs, n=10)[-1]) # p90 def main(): print("--- vLLM V1 基准测试(含 NVTX 标记)---") print(f"模型: {MODEL_NAME}") print(f"批量大小: {BATCH_SIZES}") print(f"场景: {[s['name'] for s in SCENARIOS]}") print("-" * 60) if not torch.cuda.is_available(): print("错误:需要 CUDA GPU。") return print("加载分词器/模型中...") tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True, trust_remote_code=TRUST_REMOTE_CODE) # 用 NVTX 标记模型加载阶段 nvtx.range_push("LLM_init") llm = LLM( model=MODEL_NAME, tensor_parallel_size=TP, dtype=DTYPE, trust_remote_code=TRUST_REMOTE_CODE, gpu_memory_utilization=GPU_MEM_UTIL, max_num_seqs=256, # 足够覆盖本次扫描 max_model_len=8192, disable_log_stats=False, # 开启 V1 metrics 收集 ) nvtx.range_pop() print("模型加载完成。") # —— 新增:打印一次基线内存/KV 状态 _print_mem_and_kv(llm, "after LLM_init") for sc in SCENARIOS: name = sc["name"] prompt_tokens = sc["prompt_tokens"] max_new_tokens = sc["max_new_tokens"] print(f"\n===== 场景:{name} | prefill={prompt_tokens}, decode={max_new_tokens} =====") # 准备精确长度 prompt prompt_text = build_exact_token_prompt(tokenizer, prompt_tokens) # 采样参数(贪心) sampling_params = SamplingParams( max_tokens=max_new_tokens, temperature=TEMPERATURE, top_p=TOP_P, seed=SEED, n=1, ) # 记录每个 bs 的结果(便于后续统计或外部解析) for bs in BATCH_SIZES: print(f"\n--- 批量大小 bs={bs} ---") prompts = [prompt_text] * bs # 预热(如需) # print("预热中...") # nvtx.range_push(f"WARMUP [{name}] bs={bs}") # _ = decorated_generate(llm, [prompts[0]], sampling_params) # torch.cuda.synchronize() # nvtx.range_pop() # 正式计时与 V1 metrics # nvtx.range_push(f"RUN [{name}] bs={bs}") torch.cuda.synchronize() snap_before = _metrics_snapshot(llm) t0 = time.perf_counter() nvtx.range_push(f"generate [{name}] bs={bs}") outputs = decorated_generate(llm, prompts, sampling_params) nvtx.range_pop() # generate torch.cuda.synchronize() t1 = time.perf_counter() snap_after = _metrics_snapshot(llm) # nvtx.range_pop() # RUN duration = t1 - t0 # 统计 token 与吞吐 total_output_tokens = sum(len(o.outputs[0].token_ids) for o in outputs) avg_prompt_tokens = sum(len(o.prompt_token_ids) for o in outputs) / bs throughput = total_output_tokens / duration if duration > 0 else float("inf") # 解析 V1 TTFT / 解码吞吐 delta = _metrics_delta(snap_before, snap_after) if delta["ttft_cnt"] > 0: ttft = delta["ttft_sum"] / delta["ttft_cnt"] else: ttft = float("nan") if delta["tpot_cnt"] > 0: avg_tpot = delta["tpot_sum"] / delta["tpot_cnt"] # seconds/token decode_tps = 1.0 / avg_tpot else: decode_tps = float("nan") print(f"执行时间: {duration:.4f} s") print(f"实际平均输入 tokens: {avg_prompt_tokens:.2f}(目标 {prompt_tokens})") print(f"生成总 tokens: {total_output_tokens}") print(f"吞吐(生成tokens/秒): {throughput:.2f}") print(f"TTFT (V1 metrics): {ttft:.4f} s") print(f"解码吞吐 (V1 metrics): {decode_tps:.2f} tok/s") # —— 新增:打印该批次后内存 / KV cache 状态 _print_mem_and_kv(llm, f"after generate [{name}] bs={bs}") print("\n完成。提示:在 Nsight Systems 中可通过 NVTX 区间快速定位各场景/批量的调用。") if __name__ == "__main__": print(f"CUDA_VISIBLE_DEVICES = {os.getenv('CUDA_VISIBLE_DEVICES')}") main()