-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Optimize benchmark_classifier.py with ThreadPoolExecutor #155
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
fca2934
975fd39
1b034e0
fd03408
a62481d
3bb5bf3
0f80670
2c960fa
84d7b35
74fbe53
6f3fc36
1d6ebe9
1392ef8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| """Benchmark gemma4-26a4b-routing classifier against labeled dataset.""" | ||
| import os | ||
| import concurrent.futures | ||
| import threading | ||
| import json, urllib.request, time, sys | ||
| from collections import defaultdict, Counter | ||
| from pathlib import Path | ||
|
|
@@ -54,7 +56,8 @@ def classify(prompt): | |
| per_tier = {t: {"correct": 0, "total": 0} for t in TIERS} | ||
| confusion = defaultdict(Counter) # confusion[expected][predicted] | ||
|
|
||
| for i, item in enumerate(dataset.get("prompts", [])): | ||
|
|
||
| def process_item(item): | ||
| prompt = item["prompt"] | ||
| # Support both old schema ("tier") and new schema ("llm_tier" / "clf_tier") | ||
| expected = item.get("tier") or item.get("llm_tier") or item.get("clf_tier", "") | ||
|
|
@@ -64,31 +67,57 @@ def classify(prompt): | |
| except Exception as e: | ||
| predicted = f"ERROR: {str(e)[:50]}" | ||
|
|
||
| results.append({ | ||
| "prompt": prompt[:100], | ||
| "expected": expected, | ||
| "predicted": predicted, | ||
| }) | ||
|
|
||
| # Only score against known tiers — skip ERROR/unknown labels gracefully | ||
| if expected not in per_tier: | ||
| confusion[expected][predicted] += 1 | ||
| continue | ||
| return expected, predicted | ||
|
|
||
| per_tier[expected]["total"] += 1 | ||
| if predicted == expected: | ||
| correct += 1 | ||
| per_tier[expected]["correct"] += 1 | ||
| results_list = [None] * total | ||
|
|
||
| confusion[expected][predicted] += 1 | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: | ||
| # Submit all tasks immediately, but executor map will process them and yield results in order | ||
| # To maintain rate limit properly without the quadratic delay or blocking progress, we can use a threading.Lock | ||
| rate_limit_lock = threading.Lock() | ||
|
|
||
| # Progress | ||
| if (i + 1) % 20 == 0: | ||
| scored_so_far = sum(t["total"] for t in per_tier.values()) | ||
| acc = (correct / scored_so_far * 100) if scored_so_far > 0 else 0.0 | ||
| print(f" {i+1}/{total} — accuracy {acc:.1f}%") | ||
| def process_item_with_rate_limit(index_and_item): | ||
| i, item = index_and_item | ||
| # Enforce rate limit globally across all threads | ||
| with rate_limit_lock: | ||
| time.sleep(0.05) | ||
|
|
||
| expected, predicted = process_item(item) | ||
| return i, item, expected, predicted | ||
|
|
||
| # We map over items, but map blocks if we process in order. We can use as_completed and store by index to preserve order. | ||
| futures = [executor.submit(process_item_with_rate_limit, (i, item)) for i, item in enumerate(dataset.get("prompts", []))] | ||
|
|
||
| time.sleep(0.05) # minimal rate-limit (model handles concurrency via llama-server slots) | ||
| completed_count = 0 | ||
| for future in concurrent.futures.as_completed(futures): | ||
| i, item, expected, predicted = future.result() | ||
|
Comment on lines
+92
to
+93
Contributor
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. Calling for future in concurrent.futures.as_completed(futures):
try:
i, item, expected, predicted = future.result()
except Exception as e:
print(f"Error processing task: {e}")
completed_count += 1
continue |
||
|
|
||
| results_list[i] = { | ||
| "prompt": item["prompt"][:100], | ||
| "expected": expected, | ||
| "predicted": predicted, | ||
| } | ||
|
|
||
| # Only score against known tiers — skip ERROR/unknown labels gracefully | ||
| if expected not in per_tier: | ||
| confusion[expected][predicted] += 1 | ||
| else: | ||
| per_tier[expected]["total"] += 1 | ||
| if predicted == expected: | ||
| correct += 1 | ||
| per_tier[expected]["correct"] += 1 | ||
| confusion[expected][predicted] += 1 | ||
|
Comment on lines
+102
to
+109
Contributor
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 confusion matrix update if expected in per_tier:
per_tier[expected]["total"] += 1
if predicted == expected:
correct += 1
per_tier[expected]["correct"] += 1
confusion[expected][predicted] += 1 |
||
|
|
||
| completed_count += 1 | ||
|
|
||
| # Progress | ||
| if completed_count % 20 == 0: | ||
| scored_so_far = sum(t["total"] for t in per_tier.values()) | ||
| acc = (correct / scored_so_far * 100) if scored_so_far > 0 else 0.0 | ||
| print(f" {completed_count}/{total} — accuracy {acc:.1f}%") | ||
|
|
||
| # Filter out Nones if any | ||
| results = [r for r in results_list if r is not None] | ||
|
|
||
| # Report | ||
| scored_total = sum(t["total"] for t in per_tier.values()) | ||
|
|
||
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.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Don’t use
clf_tieras the expected label.clf_tieris produced as a classifier output inscripts/reclassify_all.py, so using it asexpectedbenchmarks the current classifier against a previous classifier prediction whentier/llm_tierare absent. Prefer skipping those rows by falling back to"".Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents