|
| 1 | +import argparse |
| 2 | +import subprocess |
| 3 | +import time |
| 4 | +import os |
| 5 | +import requests |
| 6 | +import sys |
| 7 | +import json |
| 8 | + |
| 9 | + |
| 10 | +def parse_args(): |
| 11 | + parser = argparse.ArgumentParser() |
| 12 | + parser.add_argument("--tp", type=int, required=True, help="Number of GPUs to use.") |
| 13 | + parser.add_argument("--model_dir", type=str, required=True, help="Directory of the model.") |
| 14 | + return parser.parse_args() |
| 15 | + |
| 16 | + |
| 17 | +def start_server(tp, model_dir): |
| 18 | + cmd = [ |
| 19 | + "python", |
| 20 | + "-m", |
| 21 | + "lightllm.server.api_server", |
| 22 | + "--tp", |
| 23 | + str(tp), |
| 24 | + "--model_dir", |
| 25 | + model_dir, |
| 26 | + "--data_type", |
| 27 | + "fp16", |
| 28 | + "--mode", |
| 29 | + "triton_gqa_flashdecoding", |
| 30 | + "--trust_remote_code", |
| 31 | + "--tokenizer_mode", |
| 32 | + "fast", |
| 33 | + "--host", |
| 34 | + "0.0.0.0", |
| 35 | + "--port", |
| 36 | + "8080", |
| 37 | + ] |
| 38 | + process = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 39 | + return process |
| 40 | + |
| 41 | + |
| 42 | +def check_health(): |
| 43 | + health_url = "http://localhost:8080/health" |
| 44 | + try: |
| 45 | + r = requests.get(health_url, timeout=2) |
| 46 | + return r.status_code == 200 |
| 47 | + except Exception: |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +def send_prompts(prompts, output_file): |
| 52 | + for prompt in prompts: |
| 53 | + while not check_health(): |
| 54 | + time.sleep(1) |
| 55 | + |
| 56 | + request_data = { |
| 57 | + "inputs": prompt, |
| 58 | + "parameters": {"max_new_tokens": 1024, "frequency_penalty": 1, "do_sample": False}, |
| 59 | + "multimodal_params": {}, |
| 60 | + } |
| 61 | + |
| 62 | + try: |
| 63 | + r = requests.post("http://localhost:8080/generate", json=request_data, timeout=10) |
| 64 | + response_json = json.loads(r.text) |
| 65 | + generated_text = ( |
| 66 | + response_json["generated_text"][0] if "generated_text" in response_json else "No generated_text." |
| 67 | + ) |
| 68 | + except Exception as e: |
| 69 | + generated_text = f"ERROR: {str(e)}" |
| 70 | + |
| 71 | + with open(output_file, "a", encoding="utf-8") as f: |
| 72 | + f.write(f"===== prompt: {prompt} =====\n") |
| 73 | + f.write(f"{generated_text}\n\n") |
| 74 | + |
| 75 | + print(f"===================Ouput saved in {output_file}===========================") |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + # args |
| 80 | + args = parse_args() |
| 81 | + tp = args.tp |
| 82 | + model_dir = args.model_dir |
| 83 | + |
| 84 | + # output_file |
| 85 | + output_file = "test_results.txt" |
| 86 | + |
| 87 | + if os.path.exists(output_file): |
| 88 | + os.remove(output_file) |
| 89 | + |
| 90 | + # start server |
| 91 | + process = start_server(tp, model_dir) |
| 92 | + |
| 93 | + # prompts |
| 94 | + prompts = [ |
| 95 | + "What is the machine learning?", |
| 96 | + "1+1等于几", |
| 97 | + "What role does attention play in transformer architectures?", |
| 98 | + "西红柿炒鸡蛋怎么做?", |
| 99 | + "Describe the concept of overfitting and underfitting.", |
| 100 | + "CPU和GPU的区别是什么?", |
| 101 | + "What is the role of a loss function in machine learning?", |
| 102 | + ] |
| 103 | + |
| 104 | + send_prompts(prompts, output_file) |
| 105 | + |
| 106 | + # shutdown server |
| 107 | + process.terminate() |
| 108 | + process.wait() |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
| 113 | + |
| 114 | +# python test_accuracy.py --tp 2 --model_dir /xx/xx |
0 commit comments