Benchmark tokenizer throughput with specified thread count.
(self, tokenizer, tokenizer_name: str, thread_count: int, test_text: str)
| 392 | ) |
| 393 | |
| 394 | def benchmark_throughput(self, tokenizer, tokenizer_name: str, thread_count: int, test_text: str) -> ThroughputResult: |
| 395 | """Benchmark tokenizer throughput with specified thread count.""" |
| 396 | print(f" Testing {tokenizer_name} with {thread_count} threads...") |
| 397 | |
| 398 | # Split text into chunks for processing |
| 399 | chunk_size = len(test_text) // (thread_count * self.iterations_per_thread) |
| 400 | text_chunks = [] |
| 401 | |
| 402 | for i in range(thread_count * self.iterations_per_thread): |
| 403 | start_idx = i * chunk_size |
| 404 | if i == (thread_count * self.iterations_per_thread) - 1: |
| 405 | # Last chunk gets remaining text |
| 406 | chunk = test_text[start_idx:] |
| 407 | else: |
| 408 | end_idx = (i + 1) * chunk_size |
| 409 | chunk = test_text[start_idx:end_idx] |
| 410 | text_chunks.append(chunk) |
| 411 | |
| 412 | # Benchmark the tokenizer |
| 413 | start_time = time.perf_counter() |
| 414 | |
| 415 | try: |
| 416 | token_results = tokenizer.encode_batch(text_chunks, num_threads=thread_count) |
| 417 | success = True |
| 418 | except Exception as e: |
| 419 | print(f" ERROR: {tokenizer_name} encode_batch failed: {e}") |
| 420 | return None |
| 421 | |
| 422 | end_time = time.perf_counter() |
| 423 | |
| 424 | if not success or not token_results: |
| 425 | print(f" ERROR: No successful tokenizations for {tokenizer_name}") |
| 426 | return None |
| 427 | |
| 428 | # Calculate statistics |
| 429 | total_time = end_time - start_time |
| 430 | total_bytes = sum(len(chunk.encode('utf-8')) for chunk in text_chunks) |
| 431 | total_tokens = sum(len(tokens) for tokens in token_results) |
| 432 | total_mb = total_bytes / (1024 * 1024) |
| 433 | |
| 434 | throughput_mb_per_sec = total_mb / total_time |
| 435 | throughput_tokens_per_sec = total_tokens / total_time |
| 436 | avg_latency_ms = (total_time / len(text_chunks)) * 1000 |
| 437 | |
| 438 | print(f" {tokenizer_name}: {throughput_mb_per_sec:.2f} MB/s, {throughput_tokens_per_sec:.0f} tokens/s") |
| 439 | |
| 440 | return ThroughputResult( |
| 441 | thread_count=thread_count, |
| 442 | tokenizer_name=tokenizer_name, |
| 443 | total_text_size_mb=total_mb, |
| 444 | total_tokens=total_tokens, |
| 445 | total_time_seconds=total_time, |
| 446 | throughput_mb_per_sec=throughput_mb_per_sec, |
| 447 | throughput_tokens_per_sec=throughput_tokens_per_sec, |
| 448 | avg_latency_ms=avg_latency_ms |
| 449 | ) |
| 450 | |
| 451 | def run_throughput_benchmarks(self): |
no test coverage detected