a-normal-username commited on
Commit
465d7c0
·
verified ·
1 Parent(s): 55a923d

Upload maxtext-setup/benchmark.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. maxtext-setup/benchmark.py +142 -0
maxtext-setup/benchmark.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Miniature load test of the mock server."""
16
+
17
+ import concurrent.futures
18
+ import functools
19
+ import time
20
+ from typing import Sequence, Iterator
21
+
22
+ from absl import app
23
+ import grpc
24
+
25
+ import json, random
26
+
27
+ from jetstream.core.proto import jetstream_pb2
28
+ from jetstream.core.proto import jetstream_pb2_grpc
29
+
30
+ server = 'dns:///[::1]'
31
+ port = 9000
32
+ prompt = ['quantum mechanics is the study of the behavior of']
33
+ maxtokens = 8192
34
+ batchsize = 32
35
+ nqueries = batchsize * 4
36
+ vocab = {
37
+ #'bos_id': 1,
38
+ # 'eos_id': 2,
39
+ # 'pad_id': 2,
40
+ }
41
+ nrepeats = 2
42
+
43
+ def collect_tokens(
44
+ response: Iterator[jetstream_pb2.DecodeResponse], print_interim: bool
45
+ ) -> list[str]:
46
+ tokens = []
47
+ genstarted = False
48
+ tokcount = 0
49
+ realtimetstart = time.perf_counter()
50
+ for token_list in response:
51
+ tok = token_list.response[0]
52
+ if not genstarted:
53
+ tstart = time.perf_counter()
54
+ if print_interim:
55
+ print("############# STARTING GENERATION OF NEW BATCH #############")
56
+ genstarted = True
57
+ tokcount += 1
58
+ if print_interim:
59
+ print(f"generated {tokcount} tokens in {time.perf_counter() - tstart:.2f}s. TPS: {tokcount / (time.perf_counter() - tstart):.2f}", end='\r', flush=True)
60
+ tokens.append(tok)
61
+ print(f"responded {tokcount} in {time.perf_counter() - tstart:.2f}s. TKPS: {tokcount / (time.perf_counter() - tstart):.2f}", end='\r', flush=True)
62
+ ttaken = time.perf_counter() - tstart
63
+ #print(f'\n{ttaken:.2f}s')
64
+ #print(f'TPS INDIVIDUAL: {len(tokens) / ttaken:.2f}')
65
+ return tokens, ttaken, time.perf_counter() - realtimetstart
66
+
67
+
68
+ def api_call(
69
+ stub: jetstream_pb2_grpc.OrchestratorStub,
70
+ text: str,
71
+ max_tokens: int,
72
+ session_cache: str = '',
73
+ print_interim: bool = True,
74
+ ) -> str:
75
+ """Sends a request to server and returns text."""
76
+ request = jetstream_pb2.DecodeRequest(
77
+ session_cache=session_cache,
78
+ additional_text=text,
79
+ priority=1,
80
+ max_tokens=max_tokens,
81
+ vocab=json.dumps(vocab),
82
+ )
83
+ response = stub.Decode(request)
84
+ print('---------------------- Sent!!!----------------------')
85
+ tokens, ttaken, realttaken = collect_tokens(response, print_interim=print_interim)
86
+
87
+ return ''.join(tokens), ttaken, len(tokens), realttaken
88
+
89
+
90
+ def ping(
91
+ stub: jetstream_pb2_grpc.OrchestratorStub, text: str, number: int
92
+ ) -> str:
93
+ index, text = text
94
+ response, ttaken, ntok, realttaken = api_call(stub, text, maxtokens, print_interim = False)#print_interim = True if index%96 == 0 else False)
95
+ print(f'Completed {number} of {ntok} tokens in {ttaken:.2f}s gen time, {realttaken:.2f}s real time. Individual no prefill TKPS: {ntok/ttaken:.2f}. Individual raw TKPS: {ntok/realttaken:.2f}')
96
+ return response, ttaken, ntok
97
+
98
+
99
+ def load_test(
100
+ stub: jetstream_pb2_grpc.OrchestratorStub,
101
+ text: list[str],
102
+ queries: int = 64,
103
+ ) -> list[str]:
104
+ """Sends many queries to the server."""
105
+ assert queries % len(text) == 0
106
+ # repeat out
107
+ text = text * (queries // len(text))
108
+ number = list(range(len(text)))
109
+ start = time.time()
110
+ ping_partial = functools.partial(ping, stub)
111
+ with concurrent.futures.ThreadPoolExecutor(
112
+ max_workers=queries) as executor:
113
+ responses = list(executor.map(ping_partial, enumerate(text), number))
114
+ time_taken = time.time() - start
115
+ print(responses[0][0])
116
+ print(f'Time taken: {time_taken}')
117
+ print(f'QPS: {queries/time_taken}')
118
+ total_toks_gend = 0
119
+ total_gen_time_no_prefill = 0
120
+ for _, ttaken, ntok in responses:
121
+ total_toks_gend += ntok
122
+ total_gen_time_no_prefill += ttaken
123
+ print(f'RAW TKPS: {total_toks_gend / time_taken}')
124
+ print(f'TKPS NO PREFILL: {total_toks_gend / total_gen_time_no_prefill}')
125
+ print(f'TOTAL TOKENS GENERATED: {total_toks_gend}')
126
+ return responses
127
+
128
+
129
+ def main():
130
+ address = f'{server}:{port}'
131
+ # Note: Uses insecure_channel only for local testing. Please add grpc credentials for Production.
132
+ with grpc.insecure_channel(
133
+ address
134
+ ) as channel:
135
+ grpc.channel_ready_future(channel).result()
136
+ stub = jetstream_pb2_grpc.OrchestratorStub(channel)
137
+ _ = load_test(stub, text=prompt, queries=nqueries)
138
+
139
+
140
+ if __name__ == '__main__':
141
+ for i in range(nrepeats):
142
+ main()