Setting the file. One moment. Optimize Benchmark · AWS AI ML · aws/agent-toolkit-for-aws · Skills Docs70
Creating Amazon Aurora Db Cluster With Instances
93
Routing Traffic With Route53 And CloudFront
Resilience Program Design
Creating API Gateway Stage
(opens in a new tab)
references/model-deployment/code_templates/optimize-benchmark.py
Python·114 lines·5 KB
Attribution, set_attribution
16from sagemaker.serve import start_benchmark
17from sagemaker.serve.ai_inference_recommender import Workload
18
19set_attribution(Attribution.SAGEMAKER_AGENT_PLUGIN)
20
21ENDPOINT_NAME = "[ENDPOINT_NAME]"
22ROLE_ARN = "[ROLE_ARN]"
23S3_OUTPUT_LOCATION = "[S3_OUTPUT_LOCATION]"
24BENCHMARK_JOB_NAME = "[BENCHMARK_JOB_NAME]"
25WORKLOAD_CONFIG_NAME = "[WORKLOAD_CONFIG_NAME]"
26TOKENIZER = "[TOKENIZER]"
27
28# Cell 3: Define the Workload
29
30# Default: synthetic prompts against an OpenAI ChatCompletions endpoint.
31# For a load test, raise concurrency (e.g. 50-200) and request_count.
32workload = Workload.synthetic(
33 tokenizer=TOKENIZER,
34 prompt_input_tokens_mean=[INPUT_TOKENS_MEAN],
35 prompt_input_tokens_stddev=[INPUT_TOKENS_STDDEV],
36 output_tokens_mean=[OUTPUT_TOKENS_MEAN],
37 output_tokens_stddev=[OUTPUT_TOKENS_STDDEV],
38 concurrency=[CONCURRENCY],
39 request_count=[REQUEST_COUNT],
40 # Optional extra AIPerf params passthrough — e.g. ignore_eos when an exact
41 # output length is required, a low sampling temperature for speculative-decoding
42 # runs, or a warmup control if the runtime exposes one. Omit if not needed.
43 # **[EXTRA_PARAMS] # e.g. {"ignore_eos": True, "temperature": 0.2}
44)
45
46# Custom (non-OpenAI) endpoint format — provide a Jinja2 request template and a
47# JMESPath response_field (omit response_field to let AIPerf auto-detect):
48# workload = Workload.template(
49# "[REQUEST_TEMPLATE]", # local path or inline Jinja2 string
50# response_field="[RESPONSE_FIELD]", # e.g. "generated_text"
51# tokenizer=TOKENIZER, concurrency=[CONCURRENCY], request_count=[REQUEST_COUNT])
52#
53# Benchmark on your own data / real traffic — replay a dataset or SageMaker Data
54# Capture. custom_dataset_type is an OPTIONAL hint naming the schema so AIPerf
55# parses it ("openai-chat", "openai-completions", "sharegpt", or
56# "sagemaker-datacapture"); omit it to let AIPerf auto-detect. Inspect the data
57# to pick the value; if it's an unsupported format, convert it first with the
58# dataset-transformation skill.
59# workload = Workload.from_dataset(
60# "[DATASET_S3_URI]", tokenizer=TOKENIZER,
61# custom_dataset_type="openai-chat", # optional; from the schema you detected
62# concurrency=[CONCURRENCY], request_count=[REQUEST_COUNT])
63
64# Cell 4: Run the Benchmark
65
66job = start_benchmark(
67 endpoint=ENDPOINT_NAME,
68 workload=workload,
69 role=ROLE_ARN,
70 output_path=S3_OUTPUT_LOCATION,
71 name=BENCHMARK_JOB_NAME,
72 workload_config_name=WORKLOAD_CONFIG_NAME,
73 # inference_components=["[INFERENCE_COMPONENT_NAME]"], # only if the endpoint uses inference components
74 wait=True,
75)
76print(f"Benchmark job {job.get_name()} finished: {job.ai_benchmark_job_status}")
77
78# Cell 5: Display Results
79
80result = job.show_result()
81print(result)
82
83# Cell 6: Save Manifest
84# Save manifest - record output of workflow step for future reference
85from pathlib import Path
86manifest_dir = Path("[PROJECT_DIR]") / "manifests"
87manifest_dir.mkdir(parents=True, exist_ok=True)
88manifest_path = manifest_dir / f"benchmark-{BENCHMARK_JOB_NAME}.json"
89manifest_path.write_text(json.dumps({
90 "benchmark_job_name": BENCHMARK_JOB_NAME,
91 "endpoint_name": ENDPOINT_NAME,
92 "workload_config_name": WORKLOAD_CONFIG_NAME,
93 "s3_output_location": result.s3_output_location,
94 "status": job.ai_benchmark_job_status,
95}, indent=2))
96print(f"Manifest saved: {manifest_path}")
97
98# Cell 7: Compare Runs (optional)
99# Include this cell only when comparing this run against another — before/after an
100# optimization, synthetic vs. dataset, or a step in a concurrency sweep. compare_benchmarks
101# aligns every metric across runs and reports a signed Δ% oriented so "+" is always better
102# (higher throughput / lower latency). The FIRST result is the baseline. Reload a prior run
103# with BenchmarkJob.get("<job-name>").show_result().
104from sagemaker.serve.ai_inference_recommender import compare_benchmarks
105from sagemaker.serve.ai_inference_recommender.jobs import BenchmarkJob
106
107baseline_result = BenchmarkJob.get("[BASELINE_JOB_NAME]").show_result()
108comparison = compare_benchmarks(
109 baseline_result,
110 result,
111 names=["[BASELINE_LABEL]", "[THIS_RUN_LABEL]"],
112 stat="avg", # or "p50" / "p90" / "p95" / "p99" / "min" / "max"
113)
114print(comparison)