|
| 1 | +import json |
| 2 | +import os |
| 3 | +import random |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Dict, List |
| 6 | + |
| 7 | +import yaml |
| 8 | +from common.llmperf.utils.token_benchmark import run_token_benchmark |
| 9 | +from common.llmperf.utils.utils import reset_prefill_cache |
| 10 | + |
| 11 | + |
| 12 | +def run_test_cases(test_cases, timestamp_dir, model, server_url, tokenizer_path): |
| 13 | + """ |
| 14 | + Execute all test cases and return the list of failed case indices and hit_rate mapping for each case. |
| 15 | + Parameters: |
| 16 | + test_cases — List of test cases read from the configuration file |
| 17 | + timestamp_dir — Directory Path to save results |
| 18 | + model — Model name |
| 19 | + server_url — Base URL of the service |
| 20 | + tokenizer_path— Path to the tokenizer |
| 21 | + Returns: |
| 22 | + failed_cases — List of failed case indices |
| 23 | + """ |
| 24 | + print(f"[INFO] Total {len(test_cases)} test cases to be executed") |
| 25 | + all_summaries = [] |
| 26 | + failed_case = [] |
| 27 | + |
| 28 | + # Clear proxy environment variables |
| 29 | + env = os.environ.copy() |
| 30 | + env.pop("http_proxy", None) |
| 31 | + env.pop("https_proxy", None) |
| 32 | + |
| 33 | + for i, case in enumerate(test_cases): |
| 34 | + print(f"\n>>> Executing test case {i + 1} <<<") |
| 35 | + reset_prefill_cache(env, server_url) |
| 36 | + # Use a fixed random_seed for each test to control PC hit_rate |
| 37 | + random_seed = random.randint(1, 100000) |
| 38 | + summary = {} |
| 39 | + |
| 40 | + # Read parameters from configuration file |
| 41 | + mean_input = case.get("mean_input_tokens", 5000) |
| 42 | + stddev_input = case.get("stddev_input_tokens", 0) |
| 43 | + mean_output = case.get("mean_output_tokens", 1000) |
| 44 | + stddev_output = case.get("stddev_output_tokens", 0) |
| 45 | + max_completed = case.get("max_num_completed_requests", 1) |
| 46 | + concurrent = case.get("concurrent_requests", 1) |
| 47 | + llm_api = case.get("llm_api", "openai") |
| 48 | + additional_sampling_params = case.get("additional_sampling_params", "{}") |
| 49 | + timeout = case.get("timeout", 60000) |
| 50 | + hit_rate = case.get("hit_rate", 0) |
| 51 | + |
| 52 | + try: |
| 53 | + # Determine if two runs are needed (PC hit_rate test) |
| 54 | + if hit_rate == 0: |
| 55 | + summary = run_token_benchmark( |
| 56 | + llm_api=llm_api, |
| 57 | + model=model, |
| 58 | + test_timeout_s=timeout, |
| 59 | + max_num_completed_requests=max_completed, |
| 60 | + concurrent_requests=concurrent, |
| 61 | + mean_input_tokens=mean_input, |
| 62 | + stddev_input_tokens=stddev_input, |
| 63 | + mean_output_tokens=mean_output, |
| 64 | + stddev_output_tokens=stddev_output, |
| 65 | + additional_sampling_params=additional_sampling_params, |
| 66 | + results_dir=str(timestamp_dir), |
| 67 | + random_seed=random_seed, |
| 68 | + openai_api_base=server_url + "/v1", |
| 69 | + tokenizer_path=tokenizer_path, |
| 70 | + user_metadata={"case_idx": i}, |
| 71 | + ) |
| 72 | + else: |
| 73 | + print( |
| 74 | + f"[INFO] hit_rate > 0 detected, entering prefill mode, PC hit rate: {hit_rate} %" |
| 75 | + ) |
| 76 | + # hit_rate > 0: first prefill mode |
| 77 | + prefill_mean_input = int(mean_input * hit_rate / 100) |
| 78 | + print( |
| 79 | + f"[INFO] Prefill execution: mean_input_tokens={prefill_mean_input}" |
| 80 | + ) |
| 81 | + run_token_benchmark( |
| 82 | + llm_api=llm_api, |
| 83 | + model=model, |
| 84 | + test_timeout_s=timeout, |
| 85 | + max_num_completed_requests=max_completed, |
| 86 | + concurrent_requests=concurrent, |
| 87 | + mean_input_tokens=prefill_mean_input, |
| 88 | + stddev_input_tokens=stddev_input, |
| 89 | + mean_output_tokens=2, |
| 90 | + stddev_output_tokens=stddev_output, |
| 91 | + additional_sampling_params=additional_sampling_params, |
| 92 | + results_dir=str(timestamp_dir), |
| 93 | + random_seed=random_seed, |
| 94 | + openai_api_base=server_url + "/v1", |
| 95 | + tokenizer_path=tokenizer_path, |
| 96 | + user_metadata={"case_idx": i, "phase": "prefill"}, |
| 97 | + ) |
| 98 | + reset_prefill_cache(env, server_url) |
| 99 | + # Then run normal mode |
| 100 | + print("[INFO] Prefill completed, switching to normal mode execution") |
| 101 | + summary = run_token_benchmark( |
| 102 | + llm_api=llm_api, |
| 103 | + model=model, |
| 104 | + test_timeout_s=timeout, |
| 105 | + max_num_completed_requests=max_completed, |
| 106 | + concurrent_requests=concurrent, |
| 107 | + mean_input_tokens=mean_input, |
| 108 | + stddev_input_tokens=stddev_input, |
| 109 | + mean_output_tokens=mean_output, |
| 110 | + stddev_output_tokens=stddev_output, |
| 111 | + additional_sampling_params=additional_sampling_params, |
| 112 | + results_dir=str(timestamp_dir), |
| 113 | + random_seed=random_seed, |
| 114 | + openai_api_base=server_url + "/v1", |
| 115 | + tokenizer_path=tokenizer_path, |
| 116 | + user_metadata={"case_idx": i, "phase": "normal"}, |
| 117 | + ) |
| 118 | + all_summaries.append(summary) |
| 119 | + except Exception as e: |
| 120 | + failed_case.append(i) |
| 121 | + |
| 122 | + return all_summaries, failed_case |
| 123 | + |
| 124 | + |
| 125 | +def inference_results(): |
| 126 | + config_file = Path(__file__).parent.parent.parent / "config.yaml" |
| 127 | + all_smmaries = {} |
| 128 | + print("[INFO] Initialization complete, starting main process") |
| 129 | + print(f"[INFO] Reading configuration file: {config_file}") |
| 130 | + with open(config_file, "r", encoding="utf-8") as f: |
| 131 | + config = yaml.safe_load(f) |
| 132 | + model = config.get("llm_connection", {}).get("model", "") |
| 133 | + server_url = config.get("llm_connection", {}).get("server_url", "") |
| 134 | + tokenizer_path = config.get("llm_connection", {}).get("tokenizer_path", "") |
| 135 | + test_cases = config.get("llmperf_test_cases", []) |
| 136 | + timestamp_dir = Path("results") |
| 137 | + timestamp_dir.mkdir(parents=True, exist_ok=True) |
| 138 | + print(f"[INFO] Created results directory: {timestamp_dir}") |
| 139 | + |
| 140 | + all_summaries, failed_cases = run_test_cases( |
| 141 | + test_cases, timestamp_dir, model, server_url, tokenizer_path |
| 142 | + ) |
| 143 | + total = len(test_cases) |
| 144 | + print( |
| 145 | + f"\n[INFO] All tests completed! Success: {total - len(failed_cases)}/{total}" |
| 146 | + ) |
| 147 | + if failed_cases: |
| 148 | + print(f"[WARN] Failed case indices: {failed_cases}") |
| 149 | + return all_summaries |
0 commit comments