adaptive-local 0.5.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. adaptive_local/__init__.py +3 -0
  2. adaptive_local/benchmark/__init__.py +4 -0
  3. adaptive_local/benchmark/comparison.py +87 -0
  4. adaptive_local/benchmark/quality.py +80 -0
  5. adaptive_local/benchmark/reporting.py +142 -0
  6. adaptive_local/benchmark/runner.py +396 -0
  7. adaptive_local/benchmark/sampling.py +130 -0
  8. adaptive_local/benchmark/schemas.py +235 -0
  9. adaptive_local/benchmark/sentinel.py +78 -0
  10. adaptive_local/benchmark/statistics.py +72 -0
  11. adaptive_local/benchmark/storage.py +69 -0
  12. adaptive_local/benchmark/workloads.py +202 -0
  13. adaptive_local/cli/__init__.py +3 -0
  14. adaptive_local/cli/__main__.py +3 -0
  15. adaptive_local/cli/main.py +1048 -0
  16. adaptive_local/config/__init__.py +3 -0
  17. adaptive_local/config/settings.py +60 -0
  18. adaptive_local/core/__init__.py +1 -0
  19. adaptive_local/diagnostics/__init__.py +3 -0
  20. adaptive_local/diagnostics/doctor.py +79 -0
  21. adaptive_local/errors.py +30 -0
  22. adaptive_local/hardware/__init__.py +4 -0
  23. adaptive_local/hardware/benchmarks.py +187 -0
  24. adaptive_local/hardware/cache.py +71 -0
  25. adaptive_local/hardware/capabilities.py +150 -0
  26. adaptive_local/hardware/collectors.py +476 -0
  27. adaptive_local/hardware/inspect.py +245 -0
  28. adaptive_local/hardware/schemas.py +283 -0
  29. adaptive_local/hub/__init__.py +19 -0
  30. adaptive_local/hub/service.py +419 -0
  31. adaptive_local/models/__init__.py +3 -0
  32. adaptive_local/models/gguf.py +39 -0
  33. adaptive_local/models/registry.py +111 -0
  34. adaptive_local/optimization/__init__.py +1 -0
  35. adaptive_local/optimization/autotune/__init__.py +4 -0
  36. adaptive_local/optimization/autotune/budgets.py +20 -0
  37. adaptive_local/optimization/autotune/cache.py +81 -0
  38. adaptive_local/optimization/autotune/constraints.py +58 -0
  39. adaptive_local/optimization/autotune/controller.py +191 -0
  40. adaptive_local/optimization/autotune/evaluator.py +160 -0
  41. adaptive_local/optimization/autotune/explanation.py +24 -0
  42. adaptive_local/optimization/autotune/objective.py +73 -0
  43. adaptive_local/optimization/autotune/pareto.py +29 -0
  44. adaptive_local/optimization/autotune/schemas.py +184 -0
  45. adaptive_local/optimization/autotune/search_space.py +97 -0
  46. adaptive_local/optimization/autotune/strategies/__init__.py +6 -0
  47. adaptive_local/optimization/autotune/strategies/heuristic.py +18 -0
  48. adaptive_local/optimization/autotune/strategies/random_search.py +11 -0
  49. adaptive_local/optimization/autotune/strategies/successive_halving.py +28 -0
  50. adaptive_local/optimization/autotune/strategies/tpe.py +35 -0
  51. adaptive_local/optimization/autotune/validation.py +63 -0
  52. adaptive_local/resources/__init__.py +51 -0
  53. adaptive_local/resources/actions.py +175 -0
  54. adaptive_local/resources/admission.py +114 -0
  55. adaptive_local/resources/capabilities.py +46 -0
  56. adaptive_local/resources/controller.py +272 -0
  57. adaptive_local/resources/feedback.py +195 -0
  58. adaptive_local/resources/linux_controls.py +534 -0
  59. adaptive_local/resources/monitor.py +173 -0
  60. adaptive_local/resources/ownership.py +53 -0
  61. adaptive_local/resources/schemas.py +150 -0
  62. adaptive_local/resources/session.py +816 -0
  63. adaptive_local/runtimes/__init__.py +23 -0
  64. adaptive_local/runtimes/artifacts.py +138 -0
  65. adaptive_local/runtimes/base.py +165 -0
  66. adaptive_local/runtimes/cache.py +25 -0
  67. adaptive_local/runtimes/catalog.json +30 -0
  68. adaptive_local/runtimes/compatibility.py +21 -0
  69. adaptive_local/runtimes/errors.py +56 -0
  70. adaptive_local/runtimes/installer.py +153 -0
  71. adaptive_local/runtimes/lifecycle.py +42 -0
  72. adaptive_local/runtimes/llamacpp/__init__.py +3 -0
  73. adaptive_local/runtimes/llamacpp/adapter.py +611 -0
  74. adaptive_local/runtimes/manager.py +26 -0
  75. adaptive_local/runtimes/registry.py +53 -0
  76. adaptive_local/runtimes/schemas.py +162 -0
  77. adaptive_local-0.5.0a1.dist-info/METADATA +218 -0
  78. adaptive_local-0.5.0a1.dist-info/RECORD +81 -0
  79. adaptive_local-0.5.0a1.dist-info/WHEEL +4 -0
  80. adaptive_local-0.5.0a1.dist-info/entry_points.txt +2 -0
  81. adaptive_local-0.5.0a1.dist-info/licenses/LICENSE +202 -0
@@ -0,0 +1,3 @@
1
+ """Adaptive local inference foundation."""
2
+
3
+ __version__ = "0.5.0a1"
@@ -0,0 +1,4 @@
1
+ from adaptive_local.benchmark.runner import BenchmarkRunner
2
+ from adaptive_local.benchmark.workloads import load_suite
3
+
4
+ __all__ = ["BenchmarkRunner", "load_suite"]
@@ -0,0 +1,87 @@
1
+ from __future__ import annotations
2
+
3
+ from adaptive_local.benchmark.schemas import BenchmarkRun, ComparisonResult, ExperimentManifest
4
+ from adaptive_local.benchmark.statistics import bootstrap_median_ci, summarize
5
+
6
+
7
+ def compare_configurations(
8
+ manifest: ExperimentManifest, runs: list[BenchmarkRun], baseline_id: str, candidate_id: str
9
+ ) -> ComparisonResult:
10
+ ids = {item.configuration_id for item in manifest.configurations}
11
+ if baseline_id not in ids or candidate_id not in ids:
12
+ raise ValueError("configuration not present in experiment")
13
+ warnings = []
14
+ successful = [run for run in runs if run.status == "success" and run.run_kind == "warm"]
15
+ baseline_workloads = {
16
+ run.workload_id for run in successful if run.configuration_id == baseline_id
17
+ }
18
+ candidate_workloads = {
19
+ run.workload_id for run in successful if run.configuration_id == candidate_id
20
+ }
21
+ compatible = baseline_workloads == candidate_workloads and bool(baseline_workloads)
22
+ if not compatible:
23
+ warnings.append("configuration workload sets differ; direct speedup is invalid")
24
+ fields = {
25
+ "decode_tokens_per_second_runtime": "decode_tokens_per_second_runtime",
26
+ "ttft_seconds": "ttft_seconds",
27
+ "request_latency_seconds": "request_latency_seconds",
28
+ "observed_peak_process_rss_bytes": "observed_peak_process_rss_bytes",
29
+ }
30
+ metrics = {}
31
+ for name, field in fields.items():
32
+ base = [
33
+ float(value)
34
+ for run in successful
35
+ if run.configuration_id == baseline_id
36
+ and (value := getattr(run.metrics, field)) is not None
37
+ ]
38
+ candidate = [
39
+ float(value)
40
+ for run in successful
41
+ if run.configuration_id == candidate_id
42
+ and (value := getattr(run.metrics, field)) is not None
43
+ ]
44
+ base_summary, candidate_summary = summarize(name, base), summarize(name, candidate)
45
+ base_median, candidate_median = base_summary.median, candidate_summary.median
46
+ speedup = (
47
+ candidate_median / base_median
48
+ if compatible and base_median and "per_second" in name
49
+ else None
50
+ )
51
+ reduction = (
52
+ (base_median - candidate_median) / base_median
53
+ if compatible and base_median and ("seconds" in name or "bytes" in name)
54
+ else None
55
+ )
56
+ metrics[name] = {
57
+ "baseline": base_median,
58
+ "candidate": candidate_median,
59
+ "speedup": speedup,
60
+ "reduction": reduction,
61
+ }
62
+ if compatible and "per_second" in name:
63
+ base_by_run = {
64
+ (run.workload_id, run.repetition): getattr(run.metrics, field)
65
+ for run in successful
66
+ if run.configuration_id == baseline_id
67
+ }
68
+ candidate_by_run = {
69
+ (run.workload_id, run.repetition): getattr(run.metrics, field)
70
+ for run in successful
71
+ if run.configuration_id == candidate_id
72
+ }
73
+ paired_ratios = [
74
+ float(candidate_by_run[key]) / float(base_value)
75
+ for key, base_value in base_by_run.items()
76
+ if base_value and candidate_by_run.get(key) is not None
77
+ ]
78
+ interval = bootstrap_median_ci(paired_ratios)
79
+ metrics[name]["speedup_ci95_low"] = interval[0] if interval else None
80
+ metrics[name]["speedup_ci95_high"] = interval[1] if interval else None
81
+ return ComparisonResult(
82
+ compatible=compatible,
83
+ warnings=tuple(warnings),
84
+ baseline_id=baseline_id,
85
+ candidate_id=candidate_id,
86
+ metrics=metrics,
87
+ )
@@ -0,0 +1,80 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import math
5
+ import re
6
+
7
+ from adaptive_local.benchmark.schemas import EvaluationMethod, QualityResult, WorkloadManifest
8
+
9
+
10
+ def _normalized(value: str) -> str:
11
+ return " ".join(re.sub(r"[^\w\s]", " ", value.casefold()).split())
12
+
13
+
14
+ def evaluate(run_id: str, workload: WorkloadManifest, response: str) -> QualityResult:
15
+ method = workload.evaluation_method
16
+ passed: bool | None = None
17
+ invalid = False
18
+ reason = None
19
+ try:
20
+ if method == EvaluationMethod.NONE:
21
+ reason = "no deterministic evaluator for this open-text task"
22
+ elif method == EvaluationMethod.EXACT:
23
+ passed = response.strip() == str(workload.expected_output).strip()
24
+ elif method == EvaluationMethod.NORMALIZED:
25
+ passed = _normalized(response) == _normalized(str(workload.expected_output))
26
+ elif method == EvaluationMethod.NUMERIC:
27
+ numeric = re.fullmatch(r"\s*([-+]?\d+(?:\.\d+)?)\s*", response)
28
+ if numeric is None:
29
+ raise ValueError("numeric response must contain only one number")
30
+ number = float(numeric.group(1))
31
+ passed = math.isclose(
32
+ number,
33
+ float(workload.expected_output),
34
+ abs_tol=float(workload.evaluation_options.get("tolerance", 0)),
35
+ )
36
+ elif method == EvaluationMethod.MULTIPLE_CHOICE:
37
+ answer = response.strip().upper()
38
+ expected = str(workload.expected_output).strip().upper()
39
+ passed = len(answer) == 1 and answer == expected
40
+ elif method == EvaluationMethod.JSON_VALID:
41
+ json.loads(response)
42
+ passed = True
43
+ elif method in {EvaluationMethod.REQUIRED_FIELDS, EvaluationMethod.JSON_SCHEMA}:
44
+ body = json.loads(response)
45
+ required = workload.evaluation_options.get("required", workload.expected_output or {})
46
+ passed = isinstance(body, dict) and all(
47
+ body.get(key) == value for key, value in required.items()
48
+ )
49
+ elif method == EvaluationMethod.INSTRUCTION:
50
+ folded = response.casefold()
51
+ passed = all(
52
+ x.casefold() in folded for x in workload.evaluation_options.get("contains", [])
53
+ ) and not any(
54
+ x.casefold() in folded for x in workload.evaluation_options.get("forbidden", [])
55
+ )
56
+ elif method == EvaluationMethod.FACTS:
57
+ folded = response.casefold()
58
+ passed = all(str(x).casefold() in folded for x in workload.expected_output)
59
+ elif method == EvaluationMethod.TOOL_CALL:
60
+ body = json.loads(response)
61
+ passed = body.get("name") == workload.evaluation_options.get("name") and body.get(
62
+ "arguments"
63
+ ) == workload.evaluation_options.get("arguments")
64
+ except (AttributeError, TypeError, ValueError, json.JSONDecodeError) as error:
65
+ invalid = True
66
+ passed = False
67
+ reason = f"invalid output for {method.value}: {error}"
68
+ if passed is False and reason is None:
69
+ reason = "output did not satisfy the frozen deterministic evaluator"
70
+ return QualityResult(
71
+ run_id=run_id,
72
+ workload_id=workload.workload_id,
73
+ category=workload.category,
74
+ method=method,
75
+ passed=passed,
76
+ invalid=invalid,
77
+ score=None if passed is None else float(passed),
78
+ reason=reason,
79
+ response_text=response,
80
+ )
@@ -0,0 +1,142 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from pathlib import Path
5
+
6
+ from adaptive_local.benchmark.comparison import compare_configurations
7
+ from adaptive_local.benchmark.schemas import BenchmarkRun, ExperimentManifest, QualityResult
8
+ from adaptive_local.benchmark.statistics import summarize
9
+ from adaptive_local.benchmark.storage import atomic_json, read_jsonl
10
+
11
+
12
+ def build_report(directory: Path) -> str:
13
+ manifest = ExperimentManifest.model_validate_json((directory / "experiment.json").read_text())
14
+ runs = read_jsonl(directory / "runs.jsonl", BenchmarkRun)
15
+ quality = read_jsonl(directory / "quality_results.jsonl", QualityResult)
16
+ successful = [run for run in runs if run.status == "success" and run.run_kind == "warm"]
17
+ summaries = {}
18
+ for config in manifest.configurations:
19
+ selected = [run for run in successful if run.configuration_id == config.configuration_id]
20
+ summaries[config.configuration_id] = {
21
+ "request_latency_seconds": summarize(
22
+ "request_latency_seconds",
23
+ [
24
+ run.metrics.request_latency_seconds
25
+ for run in selected
26
+ if run.metrics.request_latency_seconds is not None
27
+ ],
28
+ ).model_dump(mode="json"),
29
+ "decode_tokens_per_second_runtime": summarize(
30
+ "decode_tokens_per_second_runtime",
31
+ [
32
+ run.metrics.decode_tokens_per_second_runtime
33
+ for run in selected
34
+ if run.metrics.decode_tokens_per_second_runtime is not None
35
+ ],
36
+ ).model_dump(mode="json"),
37
+ "ttft_seconds": summarize(
38
+ "ttft_seconds",
39
+ [
40
+ run.metrics.ttft_seconds
41
+ for run in selected
42
+ if run.metrics.ttft_seconds is not None
43
+ ],
44
+ ).model_dump(mode="json"),
45
+ "error_rate": 1
46
+ - len(selected)
47
+ / max(
48
+ 1,
49
+ len(
50
+ [
51
+ run
52
+ for run in runs
53
+ if run.configuration_id == config.configuration_id
54
+ and run.run_kind == "warm"
55
+ ]
56
+ ),
57
+ ),
58
+ "quality_accuracy": sum(
59
+ item.passed is True
60
+ for item in quality
61
+ if any(
62
+ run.run_id == item.run_id and run.configuration_id == config.configuration_id
63
+ for run in runs
64
+ )
65
+ )
66
+ / max(
67
+ 1,
68
+ sum(
69
+ item.passed is not None
70
+ for item in quality
71
+ if any(
72
+ run.run_id == item.run_id
73
+ and run.configuration_id == config.configuration_id
74
+ for run in runs
75
+ )
76
+ ),
77
+ ),
78
+ }
79
+ comparison = None
80
+ if len(manifest.configurations) == 2:
81
+ comparison = compare_configurations(
82
+ manifest,
83
+ runs,
84
+ manifest.configurations[0].configuration_id,
85
+ manifest.configurations[1].configuration_id,
86
+ )
87
+ summary = {
88
+ "experiment_id": manifest.experiment_id,
89
+ "run_count": len(runs),
90
+ "successful_warm_runs": len(successful),
91
+ "configurations": summaries,
92
+ "comparison": comparison.model_dump(mode="json") if comparison else None,
93
+ }
94
+ atomic_json(directory / "summary.json", summary)
95
+ lines = [
96
+ f"# Benchmark Report — {manifest.experiment_id}",
97
+ "",
98
+ f"Suite: `{manifest.suite_id}`",
99
+ f"Model: `{manifest.model.model_id}`",
100
+ f"Runtime: `{manifest.runtime.runtime}` {manifest.runtime.version or 'unknown'}",
101
+ "",
102
+ "## Results",
103
+ "",
104
+ ]
105
+ for config_id, values in summaries.items():
106
+ lines.extend(
107
+ [
108
+ f"### {config_id}",
109
+ "",
110
+ f"- Warm runs: {values['request_latency_seconds']['count']}",
111
+ f"- Median latency: {values['request_latency_seconds']['median']}",
112
+ "- Median runtime decode tok/s: "
113
+ f"{values['decode_tokens_per_second_runtime']['median']}",
114
+ f"- Median TTFT: {values['ttft_seconds']['median']}",
115
+ f"- Error rate: {values['error_rate']:.3f}",
116
+ f"- Deterministic quality accuracy: {values['quality_accuracy']:.3f}",
117
+ "",
118
+ ]
119
+ )
120
+ if comparison:
121
+ lines.extend(
122
+ [
123
+ "## Comparison",
124
+ "",
125
+ "```json",
126
+ json.dumps(comparison.model_dump(mode="json"), indent=2),
127
+ "```",
128
+ "",
129
+ ]
130
+ )
131
+ lines.extend(
132
+ [
133
+ "## Limitations",
134
+ "",
135
+ "TTFT is client-observed. Chunk intervals are not token-level ITL when chunks "
136
+ "contain multiple tokens. Runtime-reported prefill/decode metrics remain distinct. "
137
+ "Periodic memory peaks are observed peaks.",
138
+ ]
139
+ )
140
+ report = "\n".join(lines) + "\n"
141
+ (directory / "report.md").write_text(report)
142
+ return report