-
Notifications
You must be signed in to change notification settings - Fork 5
other: Script for debugging and metrics #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rebel-eunji
wants to merge
15
commits into
main
Choose a base branch
from
scripts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dfcdb23
add new script
rebel-eunji 6bce545
pre-commit
rebel-eunji 55f15de
rename the max_new_tokens -> max_tokens
rebel-eunji 1591042
change the default block_size
rebel-eunji 365cee1
fix the pre-commit bug
rebel-eunji 0fa1e98
change the default option
rebel-eunji bdf2271
add typing
rebel-eunji 2eac0e8
fix typing
rebel-eunji bd00789
fix typing and add logging about prefill/decode
rebel-eunji 3e5c775
fix block_size
rebel-eunji b50f76e
fix batch_size
rebel-eunji a353bc9
fix the logging style
rebel-eunji 2ac5f15
add latency check code
rebel-eunji 86c5960
pre-commit
rebel-eunji 930c1c2
Merge branch 'main' into scripts
rebel-eunji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright 2025 Rebellions Inc. All rights reserved. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at: | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Reference - https://github.com/vllm-project/vllm/blob/v0.9.1/benchmarks/benchmark_throughput.py | ||
| import argparse | ||
| import os | ||
| import time | ||
| import urllib.request | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import torch | ||
| from transformers import AutoTokenizer | ||
|
|
||
| if TYPE_CHECKING: | ||
| from vllm import SamplingParams | ||
| from vllm.outputs import RequestOutput | ||
|
|
||
| MODEL_NAME = "meta-llama/Llama-3.2-1B" | ||
| PREFILL_CHUNK_SIZE = 128 | ||
|
|
||
|
|
||
| def get_wiki_prompt(): | ||
| wiki_txt_url = "https://raw.githubusercontent.com/huggingface/optimum-neuron/refs/heads/main/benchmark/text-generation/performance/wiki.txt" | ||
| with urllib.request.urlopen(wiki_txt_url) as resp: | ||
| source_data = resp.read().decode("utf-8") | ||
| return source_data | ||
|
|
||
|
|
||
| def generate_llm_args(batch_size: int): | ||
| return { | ||
| "model": "meta-llama/Llama-3.2-1B", | ||
| "max_model_len": 40 * 1024, | ||
| "enable_chunked_prefill": True, | ||
| "max_num_seqs": batch_size, | ||
| "block_size": 1024, | ||
| "max_num_batched_tokens": PREFILL_CHUNK_SIZE, | ||
| } | ||
|
|
||
|
|
||
| def generate_prompts(prompt_length: int, batch_size: int) -> list[str]: | ||
| wiki_prompt = get_wiki_prompt() | ||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | ||
| tokens = tokenizer(wiki_prompt, return_tensors="pt").input_ids[0] | ||
| assert len(tokens) > prompt_length * batch_size | ||
| prompts = [] | ||
| # Leave 1 token for special token(bos) in the vllm | ||
| real_prompt_length = prompt_length - 1 | ||
| for i in range(batch_size): | ||
| start_pos = i * real_prompt_length | ||
| end_pos = (i + 1) * real_prompt_length | ||
| prompt = tokenizer.decode(tokens[start_pos:end_pos]) | ||
| prompts.append(prompt) | ||
| return prompts | ||
|
|
||
|
|
||
| def run_llm( | ||
| llm, prompts: list[str], sampling_params: "SamplingParams" | ||
| ) -> tuple[float, list["RequestOutput"]]: | ||
| start = time.perf_counter() | ||
| outputs = llm.generate(prompts, sampling_params=sampling_params) | ||
| end = time.perf_counter() | ||
| elapsed_time = end - start | ||
| return elapsed_time, outputs | ||
|
|
||
|
|
||
| def _worker(prompts: list[str], args: Any): | ||
| llm_args = generate_llm_args(args.batch_size) | ||
| os.environ["VLLM_RBLN_METRICS"] = "1" | ||
| os.environ.pop("VLLM_PLUGINS", None) | ||
| os.environ["RBLN_KERNEL_MODE"] = "triton" | ||
| os.environ["VLLM_USE_V1"] = "0" | ||
| os.environ["USE_VLLM_MODEL"] = "1" | ||
| os.environ["VLLM_DISABLE_COMPILE_CACHE"] = "0" | ||
| # 1 means disable using compile cache | ||
| from vllm import LLM, SamplingParams | ||
| sampling_params = SamplingParams( | ||
| temperature=0.0, | ||
| top_p=1.0, | ||
| ignore_eos=True, | ||
| max_tokens=args.max_tokens, | ||
| ) | ||
| total_elapsed_time = 0.0 | ||
| # FIXME: In rbln, re-initializing LLM | ||
| # in each iteration triggers runtime error: | ||
| # (Runtime) code=203 INIT_ALREADY_CREATED: | ||
| # A runtime has already been created for that compiled model | ||
| # (Context failed to be created, compile_id=0). | ||
| # Try creating a runtime on a different NPU(s), or use an existing runtime. | ||
| llm = LLM(**llm_args) | ||
| for _ in range(args.num_iter): | ||
| elapsed_time, outputs = run_llm(llm, prompts, sampling_params) | ||
| total_elapsed_time += elapsed_time | ||
| return total_elapsed_time | ||
|
|
||
|
|
||
| def calculate_avg_throughput_and_latency(elapsed_time: float, batch_size: int, | ||
| max_tokens: int, | ||
| num_iter: int) -> tuple[float, float]: | ||
| avg_throughput = (batch_size * max_tokens * num_iter) / elapsed_time | ||
| avg_latency = elapsed_time / num_iter | ||
| return avg_throughput, avg_latency | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-l", "--prompt_length", type=int, default=128) | ||
| parser.add_argument("-m", "--max_tokens", type=int, default=1) | ||
| parser.add_argument("-b", "--batch_size", type=int, default=1) | ||
| parser.add_argument("-n", "--num_iter", type=int, default=1) | ||
| args = parser.parse_args() | ||
|
|
||
| torch.manual_seed(42) | ||
|
|
||
| prompts = generate_prompts(args.prompt_length, args.batch_size) | ||
| elapsed_time = _worker(prompts, args) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||
| # Copyright 2025 Rebellions Inc. All rights reserved. | ||||||
|
|
||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| # you may not use this file except in compliance with the License. | ||||||
| # You may obtain a copy of the License at: | ||||||
|
|
||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
|
||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
|
|
||||||
| import argparse | ||||||
| import os | ||||||
| import urllib.request | ||||||
| from multiprocessing import get_context | ||||||
| from multiprocessing.queues import Queue as MPQueue | ||||||
| from typing import TYPE_CHECKING, Any | ||||||
|
|
||||||
| import torch | ||||||
| from transformers import AutoTokenizer | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from vllm import SamplingParams | ||||||
|
|
||||||
| MODEL_NAME = "meta-llama/Llama-3.2-1B" | ||||||
| PREFILL_CHUNK_SIZE = 128 | ||||||
| VOCAB_SIZE = 128256 | ||||||
| EPSILON = 1e-1 * 5 | ||||||
|
|
||||||
|
|
||||||
| def get_wiki_prompt(): | ||||||
| wiki_txt_url = "https://raw.githubusercontent.com/huggingface/optimum-neuron/refs/heads/main/benchmark/text-generation/performance/wiki.txt" | ||||||
| with urllib.request.urlopen(wiki_txt_url) as resp: | ||||||
| source_data = resp.read().decode("utf-8") | ||||||
| return source_data | ||||||
|
|
||||||
|
|
||||||
| def generate_llm_args(device: str, batch_size: int): | ||||||
| llm_args = { | ||||||
| "model": "meta-llama/Llama-3.2-1B", | ||||||
| "max_model_len": 40 * 1024, | ||||||
| "enable_chunked_prefill": True, | ||||||
| "max_num_seqs": batch_size, | ||||||
| "max_logprobs": VOCAB_SIZE, | ||||||
| } | ||||||
| if device == "cpu": | ||||||
| llm_args["block_size"] = 128 # 1024 is not working for long prompt | ||||||
|
||||||
| llm_args["block_size"] = 128 # 1024 is not working for long prompt | |
| llm_args["block_size"] = 128 # On CPU, using a block_size of 1024 can cause excessive memory usage or performance issues with long prompts, leading to failures. Reducing block_size to 128 avoids these issues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This FIXME comment describes a known issue but should include a reference to a tracking issue or ticket number for resolution.