|
| 1 | +"""Small deterministic routing ablation; no model downloads or API calls. |
| 2 | +
|
| 3 | +Run with `python benchmarks/routing_reliability.py`. Install the optional faiss |
| 4 | +extra to include the fit/reload case. Output is JSON suitable for comparing two |
| 5 | +checkouts with the same Python and dependencies. |
| 6 | +""" |
| 7 | +import json |
| 8 | +import tempfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +from sklearn.linear_model import LogisticRegression |
| 13 | + |
| 14 | +from tracer.api import fit, load_router |
| 15 | +from tracer.config import FitConfig |
| 16 | +from tracer.fit.pipeline import _calibrate_threshold, _predict |
| 17 | + |
| 18 | + |
| 19 | +def run(): |
| 20 | + scores = np.linspace(.50, .99, 36) |
| 21 | + teacher = np.zeros(36, dtype=int) |
| 22 | + predictions = teacher.copy() |
| 23 | + predictions[:6] = 1 |
| 24 | + chosen = _calibrate_threshold(scores, predictions, teacher, .8) |
| 25 | + X = np.array([[-3.], [-2.], [-1.], [1.], [2.], [3.]]) |
| 26 | + y = np.array([1, 1, 1, 3, 3, 3]) |
| 27 | + model = LogisticRegression(random_state=42).fit(X, y) |
| 28 | + results = { |
| 29 | + 'small_calibration': {key: chosen[key] for key in |
| 30 | + ('coverage', 'teacher_agreement', 'teacher_agreement_lower')}, |
| 31 | + 'subset_label_agreement': float((_predict(model, X)[0] == y).mean()), |
| 32 | + } |
| 33 | + try: |
| 34 | + import faiss |
| 35 | + except ImportError: |
| 36 | + results['fit_reload'] = 'skipped: install tracer-llm[faiss]' |
| 37 | + return results |
| 38 | + |
| 39 | + rng = np.random.RandomState(24) |
| 40 | + centers = rng.normal(size=(3, 8)) * 10 |
| 41 | + y_train = np.repeat(np.arange(3), 80) |
| 42 | + y_test = np.repeat(np.arange(3), 200) |
| 43 | + train = (centers[y_train] + rng.normal(size=(240, 8)) * .2).astype(np.float32) |
| 44 | + heldout = (centers[y_test] + rng.normal(size=(600, 8)) * .2).astype(np.float32) |
| 45 | + original = train.copy() |
| 46 | + skip = ('logreg_c10', 'sgd_log', 'mlp_1h', 'mlp_2h', 'dt', 'rf', 'et', 'gbt', 'xgb') |
| 47 | + with tempfile.TemporaryDirectory() as directory: |
| 48 | + root = Path(directory) |
| 49 | + traces = root / 'traces.jsonl' |
| 50 | + traces.write_text(''.join(json.dumps({'input': str(i), 'teacher': str(label)}) + '\n' |
| 51 | + for i, label in enumerate(y_train))) |
| 52 | + fitted = fit(traces, root / 'model', train, |
| 53 | + FitConfig(frontier_targets=(.9,), skip_candidates=skip, verbose=False)) |
| 54 | + router = load_router(root / 'model') |
| 55 | + output = router.predict_batch(heldout) |
| 56 | + accepted = output['handled'] |
| 57 | + labels = np.asarray(output['labels']) |
| 58 | + results['fit_reload'] = { |
| 59 | + 'faiss_version': faiss.__version__, |
| 60 | + 'training_input_unchanged': bool(np.array_equal(train, original)), |
| 61 | + 'calibration_coverage': fitted.manifest.coverage_cal, |
| 62 | + 'heldout_coverage': float(accepted.mean()), |
| 63 | + 'heldout_teacher_agreement': float((labels[accepted] == y_test[accepted].astype(str)).mean()) if accepted.any() else None, |
| 64 | + 'heldout_rows': len(y_test), |
| 65 | + } |
| 66 | + return results |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + print(json.dumps(run(), indent=2)) |
0 commit comments