-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add benchmarking harness with TTFT, throughput, and latency metrics #43
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| from mlx_lm.sample_utils import make_repetition_penalty, make_sampler | ||
|
|
||
| from ..reasoning_utils import ReasoningExtractor, StreamingReasoningParser | ||
| from ..schemas import GenerationMetrics | ||
|
|
||
|
|
||
| def get_model_context_length(model_path: str) -> int: | ||
|
|
@@ -475,6 +476,7 @@ def generate_streaming( | |
| # Track generation metrics | ||
| start_time = time.time() | ||
| tokens_generated = 0 | ||
| ttft = None # Time to first token | ||
|
|
||
| # Create sampler with our parameters | ||
| sampler = make_sampler(temp=temperature, top_p=top_p) | ||
|
|
@@ -567,6 +569,19 @@ def generate_streaming( | |
| yield formatted_token | ||
| else: | ||
| yield new_part_before_stop | ||
|
|
||
| # Yield metrics before returning | ||
| if reasoning_parser: | ||
| yield from reasoning_parser.finalize() | ||
| total_latency = time.time() - start_time | ||
| tokens_per_second = tokens_generated / total_latency if total_latency > 0 else 0 | ||
| ttft_ms = (ttft * 1000) if ttft is not None else 0 | ||
| yield GenerationMetrics( | ||
| ttft_ms=ttft_ms, | ||
| total_tokens=tokens_generated, | ||
| tokens_per_second=tokens_per_second, | ||
| total_latency_s=total_latency | ||
| ) | ||
| return # Stop generation without yielding stop token | ||
|
|
||
| # Only check chat stop tokens if no native stop token found (fallback) | ||
|
|
@@ -597,9 +612,26 @@ def generate_streaming( | |
| yield formatted_token | ||
| else: | ||
| yield new_part_before_stop | ||
|
|
||
| # Yield metrics before returning | ||
| if reasoning_parser: | ||
| yield from reasoning_parser.finalize() | ||
|
Comment on lines
+617
to
+618
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this check?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The StreamingReasoningParser buffers and formats this content as it streams. When generation stops (either naturally or via stop token), we need to:
|
||
| total_latency = time.time() - start_time | ||
| tokens_per_second = tokens_generated / total_latency if total_latency > 0 else 0 | ||
| ttft_ms = (ttft * 1000) if ttft is not None else 0 | ||
| yield GenerationMetrics( | ||
| ttft_ms=ttft_ms, | ||
| total_tokens=tokens_generated, | ||
| tokens_per_second=tokens_per_second, | ||
| total_latency_s=total_latency | ||
| ) | ||
|
Comment on lines
+615
to
+627
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same token-count issue for chat stop tokens. This branch has the same token-counting issue as the native stop token branch (lines 572-584). See the earlier comment for details and fix. |
||
| return # Stop generation without yielding stop token | ||
|
|
||
| # No stop token found, process the new text | ||
| # Capture time to first token | ||
| if ttft is None: | ||
| ttft = time.time() - start_time | ||
|
madclaws marked this conversation as resolved.
|
||
|
|
||
| if reasoning_parser: | ||
| # Process through reasoning parser for formatting | ||
| for formatted_token in reasoning_parser.process_token(new_text): | ||
|
|
@@ -617,6 +649,18 @@ def generate_streaming( | |
| if reasoning_parser: | ||
| yield from reasoning_parser.finalize() | ||
|
|
||
| # Yield metrics at the end | ||
| total_latency = time.time() - start_time | ||
| tokens_per_second = tokens_generated / total_latency if total_latency > 0 else 0 | ||
| ttft_ms = (ttft * 1000) if ttft is not None else 0 | ||
| metrics = GenerationMetrics( | ||
| ttft_ms=ttft_ms, | ||
| total_tokens=tokens_generated, | ||
| tokens_per_second=tokens_per_second, | ||
| total_latency_s=total_latency | ||
| ) | ||
| yield metrics | ||
|
|
||
| # Print generation statistics if verbose | ||
| if self.verbose: | ||
| generation_time = time.time() - start_time | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.