forked from ray-project/llmperf
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add bedrock client #1
Open
takipipo
wants to merge
3
commits into
main
Choose a base branch
from
feature/add-bedrock-client
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 2 commits
Commits
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 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 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,91 @@ | ||
import io | ||
import json | ||
import os | ||
import time | ||
from typing import Any, Dict | ||
|
||
import boto3 | ||
import ray | ||
import json | ||
|
||
from llmperf import common_metrics | ||
from llmperf.models import RequestConfig | ||
from llmperf.ray_llm_client import LLMClient | ||
|
||
|
||
@ray.remote | ||
class BedrockClient(LLMClient): | ||
"""Client for AWS Bedrock Foundation Model on Llama-2-13b-chat""" | ||
|
||
def __init__(self): | ||
# Sagemaker doesn't return the number of tokens that are generated so we approximate it by | ||
# using the llama tokenizer. | ||
# self.tokenizer = LlamaTokenizerFast.from_pretrained( | ||
# "hf-internal-testing/llama-tokenizer" | ||
# ) | ||
|
||
def llm_request(self, request_config: RequestConfig) -> Dict[str, Any]: | ||
if not os.environ.get("AWS_ACCESS_KEY_ID"): | ||
raise ValueError("AWS_ACCESS_KEY_ID must be set.") | ||
if not os.environ.get("AWS_SECRET_ACCESS_KEY"): | ||
raise ValueError("AWS_SECRET_ACCESS_KEY must be set.") | ||
if not os.environ.get("AWS_REGION_NAME"): | ||
raise ValueError("AWS_REGION_NAME must be set.") | ||
|
||
prompt = request_config.prompt | ||
prompt, _ = prompt | ||
model = request_config.model | ||
|
||
bedrock_runtime = boto3.client(service_name="bedrock-runtime", region_name="us-west-2") | ||
|
||
sampling_params = request_config.sampling_params | ||
|
||
if "max_tokens" in sampling_params: | ||
sampling_params["max_new_tokens"] = sampling_params["max_tokens"] | ||
del sampling_params["max_tokens"] | ||
|
||
body = { | ||
"prompt": prompt, | ||
"temperature": 0.5, | ||
"top_p": 0.9, | ||
"max_gen_len": 512, | ||
} | ||
time_to_next_token = [] | ||
tokens_received = 0 | ||
ttft = 0 | ||
error_response_code = None | ||
generated_text = "" | ||
error_msg = "" | ||
output_throughput = 0 | ||
total_request_time = 0 | ||
metrics = {} | ||
|
||
start_time = time.monotonic() | ||
most_recent_received_token_time = time.monotonic() | ||
try: | ||
response = bedrock_runtime.invoke_model(modelId="meta.llama2-13b-chat-v1", body = json.dumps(body)) | ||
total_request_time = time.monotonic() - start_time | ||
|
||
response_body = json.loads(response["body"].read()) | ||
tokens_received = response_body["generation_token_count"] | ||
prompt_token = response_body["prompt_token_count"] | ||
|
||
output_throughput = tokens_received / total_request_time | ||
|
||
except Exception as e: | ||
print(f"Warning Or Error: {e}") | ||
print(error_response_code) | ||
error_msg = str(e) | ||
error_response_code = 500 | ||
|
||
metrics[common_metrics.ERROR_MSG] = error_msg | ||
metrics[common_metrics.ERROR_CODE] = error_response_code | ||
metrics[common_metrics.INTER_TOKEN_LAT] = 0 | ||
metrics[common_metrics.TTFT] = 0 | ||
metrics[common_metrics.E2E_LAT] = total_request_time | ||
metrics[common_metrics.REQ_OUTPUT_THROUGHPUT] = output_throughput | ||
metrics[common_metrics.NUM_TOTAL_TOKENS] = tokens_received + prompt_token | ||
metrics[common_metrics.NUM_OUTPUT_TOKENS] = tokens_received | ||
metrics[common_metrics.NUM_INPUT_TOKENS] = prompt_token | ||
|
||
return metrics, generated_text, request_config |
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.
Still not sure please confirm
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.
ถ้าในกรณีที่ไม่ได้ add
request_config.sampling_params
ตอนรัน commandมันจะถูก set ว่า
request_config.sampling_params
= {"max_tokens": num_output_tokens}ref:
llmperf/token_benchmark_ray.py
Lines 67 to 69 in 7512357
llmperf/token_benchmark_ray.py
Lines 92 to 93 in 7512357
llmperf/token_benchmark_ray.py
Lines 147 to 159 in 7512357
soln:
แก้ "max_token" ให้เป็น parameter ที่ใช้บอก "max_token" ในแต่ละ client เช่น sagemaker ใช้ "max_new_tokens"
llmperf/src/llmperf/ray_clients/sagemaker_client.py
Lines 45 to 47 in 7512357