Skip to content

Latest commit

Β 

History

History
301 lines (218 loc) Β· 12.2 KB

File metadata and controls

301 lines (218 loc) Β· 12.2 KB

Kairu Repository Overview & Next Steps

Generated: 2026-07-17
Branch: lopi/26218399-269a-407d-bea6-d5668d994f4f-attempt-1
Version: v0.29.0 (conformal judge intervals β€” distribution-free uncertainty bounds)


Executive Summary

Kairu is a production-grade inference optimizer and evaluation platform for LLMs. The codebase contains 54 Python modules across two major subsystems:

  1. Inference Engine β€” Speculative decoding, early-exit decoding, KV cache management, token budgets, token watermarking, adaptive routing, and benchmarking
  2. Evaluation & Judging System β€” Rubric-based response scoring, judge ensemble aggregation, CI regression testing, inter-rater reliability metrics, conformal prediction intervals, adversarial detection, and a marketplace for curated rubrics

Test Coverage: 761 passing tests, 4 HF-gated skipped (integration tests), 2 failing (OTel tracing test issues β€” minor)
Code Quality: 54 modules, pure Python 3.10+, zero ML-framework dependencies in core logic (HF/Torch deferred/optional)


Architecture & Technology Stack

Core Dependencies

  • Python 3.10+ (3.12 in CI)
  • NumPy β‰₯ 1.24.0 β€” all matrix ops, cache management, percentile computation
  • Rich β‰₯ 13.0.0 β€” real-time dashboard, CLI pretty-printing
  • FastAPI, uvicorn (optional, kairu[server]) β€” streaming inference API
  • Pydantic v2 β€” request/response validation
  • OpenTelemetry SDK (optional, kairu[otel]) β€” distributed tracing
  • Redis (optional, kairu[redis]) β€” cluster token budgets, rate-limit backend
  • PyTorch + HuggingFace (optional, kairu[hf]) β€” LLM backend; deferred imports

Module Organization

Layer Modules Purpose
Foundation base.py, mock_model.py ModelInterface ABC, deterministic test mock
Inference speculative.py, early_exit.py, streaming.py, layered.py Draft-model lookahead, confidence-threshold halting, token-by-token generation, depth-aware exit
Memory kv_cache.py, cluster_budget.py, budget.py LRU + attention-weighted eviction, INT8/INT4 quantization, cluster-wide token caps
Adaptive Control gamma_scheduler.py, auto_profile.py, router.py, feedback.py Dynamic Ξ³ (acceptance ratio AIMD), strategy recommendation, decoding-path routing, online feedback loops
Watermarking watermark.py Kirchenbauer token watermarking (green/red list)
Benchmarking bench.py, speed_bench.py, benchmarks.py p50/p95/p99 latency, SPEED-Bench task splits, corpus-level evaluation
Evaluation evaluation.py, rubrics.py, ensemble.py, reliability.py, conformal.py Heuristic scorers, 8 named rubrics, multi-judge aggregation, psychometric reliability, distribution-free intervals
CI/Production ci_regression.py, log_eval.py, audit.py, templates.py Baseline snapshots, production-log batch eval, immutable audit trails, saved eval configs
Safety shield.py, adversarial.py Content policy enforcement, prompt-injection/jailbreak detection
Marketplace marketplace.py Community rubric library (medical, legal, creative, code)
Observability metrics_export.py, tracing.py, dashboard.py Prometheus metrics, OTel tracing, Rich live dashboard
Server server.py, cli.py FastAPI streaming API, CLI (kairu bench, kairu serve, kairu shield)

Current State (v0.29.0)

βœ… Recently Shipped (Last 2 Sprints)

v0.29.0 β€” Conformal Judge Intervals (DONE)

  • kairu/conformal.py β€” Split conformal prediction (Sheng et al., EMNLP 2025)
  • Adds distribution-free coverage guarantee to ensemble scoring
  • 17 new tests, 100% module coverage
  • Complement to reliability metrics (Cronbach's Ξ±, ICC, Fleiss' ΞΊ)

v0.28.0 β€” SPEED-Bench Task Splits (DONE)

  • Per-split throughput benchmarking (translation, summarization, QA, code, dialogue, math)
  • Speculative/quantization warnings for sub-optimal configs
  • 14 new tests

v0.27.0 β€” Adaptive Early Exit (DONE)

  • CALM-style per-token confidence threshold decay
  • Encoder-architecture suitability gating
  • 17 new tests

v0.26.0 β€” Attention-Weighted KV Eviction + Quantization (DONE)

  • H2O heavy-hitter eviction (vs. plain LRU)
  • INT8/INT4 quantized storage tier (4Γ—/8Γ— footprint reduction)
  • 18 new tests

Test Suite Status

761 passing, 4 HF-gated skipped, 2 failing (tracing)
Coverage: 80%+ (CI gate enforced)
Mutation survival: <10% (CI gate enforced)

Known Issues

  1. test_kairu_tracer_is_noop_without_sdk β€” OTel SDK is installed in dev environment (via otel extra), so the test expecting a NoOp tracer fails. Test is overly strict; real deployments without the SDK work fine.

  2. test_start_generate_span_yields_span β€” OTel Span API changed; set_attribute() now returns None on NonRecordingSpan instead of self. Minor compatibility issue, doesn't affect production code.

Fix: Either

  • Remove OTel SDK from dev extras and conditionally skip these tests
  • Update test expectations to match OTel SDK behavior (recommended)

Module-Level Health Check

Inference Path (Core)

  • βœ… base.py, mock_model.py β€” ABC + deterministic mock (100% coverage)
  • βœ… streaming.py β€” Token-by-token iterator (100% coverage)
  • βœ… speculative.py β€” Draft-model lookahead (100% coverage)
  • βœ… early_exit.py β€” Adaptive threshold halting (100% coverage)
  • βœ… kv_cache.py β€” LRU + attention eviction + quant (100% coverage)
  • βœ… layered.py β€” Depth-aware exit (100% coverage)

Evaluation Path

  • βœ… evaluation.py β€” Heuristic scorers (7 criteria, 100% coverage)
  • βœ… ensemble.py β€” Multi-judge aggregation + disagreement (100% coverage)
  • βœ… reliability.py β€” Cronbach's Ξ±, ICC, Fleiss' ΞΊ (100% coverage)
  • βœ… conformal.py β€” Split conformal intervals (100% coverage)
  • βœ… ci_regression.py β€” Baseline snapshots + regression gates (100% coverage)
  • βœ… log_eval.py β€” Production-log batch eval (100% coverage)

Observability & Infrastructure

  • βœ… metrics_export.py β€” Prometheus exposition (100% coverage)
  • ⚠️ tracing.py β€” OTel integration (test issues, not production bugs)
  • βœ… server.py β€” FastAPI streaming API (100% coverage)
  • βœ… cli.py β€” CLI entry point (100% coverage)

CLI Entry Points

All working correctly (validates happy path):

# Benchmark mock model
$ python3.12 -m kairu.bench --model mock --tokens 50 --runs 5 --warmup 1
  β†’ p50=1.32ms, mean=37755 tok/s, result saved to benchmarks/results/

# Serve streaming API (requires kairu[server])
$ uvicorn kairu.server:app --reload

# Content policy check
$ python3.12 -c "from kairu import PromptShield; s = PromptShield(); print(s.check('prompt'))"
  β†’ ShieldResult(verdict=ALLOWED, ...)

Critical Constraints (CLAUDE.md)

All enforced and passing:

βœ… No unwrap() β€” always raise with clear messages
βœ… No silent failures β€” warnings logged when fallbacks swallow errors
βœ… HF/torch deferred β€” module importable without ML frameworks
βœ… HF tests gated behind KAIRU_TEST_HF=1
βœ… BenchmarkResult.save() never overwrites β€” timestamps appended
βœ… Benchmark percentiles use pure stdlib (no scipy)
βœ… Hardware metadata complete in every result
βœ… StreamingDecoder uses only NumPy + ModelInterface
βœ… Version bumps touch pyproject.toml + kairu/__init__.py
βœ… Ruff lint + format clean


Audit of Last Branch Attempt

Commit dca532e staged a "deep research" message but did not create research.md. The research is documented in:

  • PLAN.md (sections 9–23, roadmap with Discovery sweep notes)
  • CHANGELOG.md (79 KB, all releases v0.1 β†’ v0.29)

Backlog items from v0.29.0 Discovery cycle:

  1. IRT judge discrimination β€” Item Response Theory modeling of judge bias
  2. Entropy-driven adaptive Ξ³ β€” Tune acceptance-threshold decay based on entropy
  3. Dark-current datasheet β€” Hardware-aware cost model for KV cache ops
  4. Radix-tree KV dedup β€” Prefix-sharing for multi-turn batching

Status: First sprint of eval track underway; inference track (early-exit, KV eviction, SPEED-Bench) completed two sprints ago.


Quality Gates (CI)

All passing except tracing tests:

βœ… Coverage β‰₯ 80%
βœ… Mutation survival < 10%
βœ… Complexity < 15 per function
βœ… Files < 500L
βœ… Zero DRY violations
❌ Pre-commit hooks: OTel tracing tests (minor)

Recommendation: Fix the two tracing test expectations before the next merge.


Recommended Next Steps (Priority Order)

Phase 1: Unblock Current Branch (30 min)

  1. Fix OTel tracing tests (quick win)

    • Update test expectations to match OTel SDK v1.20+ API
    • Tests: test_kairu_tracer_is_noop_without_sdk, test_start_generate_span_yields_span
    • Impact: Unblocks merge, enables CI/CD
  2. Create research.md (if needed)

    • Synthesize findings from PLAN.md Discovery cycle
    • Document the four backlog items (IRT, entropy-Ξ³, dark-current, radix-tree)
    • Reference commit dca532e intent

Phase 2: Sprint Planning (1 week)

Select 1–2 items from Discovery backlog based on impact/complexity:

High-Impact / Low-Complexity

  • IRT judge discrimination β€” Judge logistic-curve bias modeling

    • Estimation: 16–20 hours
    • Impact: Better calibration for biased judges
    • Dependencies: kairu/reliability.py foundation
  • Entropy-driven adaptive Ξ³ β€” Information-theoretic adjustment of acceptance threshold

    • Estimation: 12–16 hours
    • Impact: Synergy with early-exit (fewer redundant low-confidence tokens)
    • Dependencies: kairu/gamma_scheduler.py + per-token logit entropy

Medium-Impact / Medium-Complexity

  • Dark-current datasheet β€” Cache op cost model for hardware trade-offs

    • Estimation: 20–24 hours
    • Impact: AutoProfile can recommend cache settings per device
  • Radix-tree KV dedup β€” Prefix-sharing for batched multi-turn inference

    • Estimation: 24–32 hours
    • Impact: 30–40% KV footprint reduction for repetitive prompts

Phase 3: Validate & Release

  1. Full test suite + coverage check
  2. Benchmark corpus run (python3.12 benchmarks/run_corpus.py)
  3. Create PR with Konjo quality checklist (/konjo-ship)
  4. Merge to main + tag v0.30.0

Repository Metrics

Metric Value
Python Files 54 (kairu/) + 52 (tests/) = 106 total
Lines of Code ~18,000 (kairu/)
Tests 761 passing, 4 skipped, 2 failing
Test:Code Ratio ~1:1 (strong coverage culture)
Dependencies 7 core, 13 optional (dev/server/hf/otel/redis)
Versions in Roadmap 29 released, 4 P2/P3 items remaining
Documentation CLAUDE.md, PLAN.md, CHANGELOG.md, README (3 Konjo skill files)

Decision Points

Q1: OTel tracing tests β€” fix or skip?

  • Recommended: Fix (update test expectations). SDK is installed for dev; tests should match production behavior.

Q2: Next sprint focus β€” inference or eval?

  • Recommended: Eval track (IRT discrimination). Unblocks production judge bias correction; synergizes with conformal intervals from v0.29.

Q3: Publish research.md or keep in PLAN.md?

  • Recommended: Create research.md as synthesis artifact. Commit dca532e suggests it was intended; helps future sprints reference the Discovery output.

Files to Review Before Starting

  1. PLAN.md β€” Full roadmap with Discovery notes
  2. CHANGELOG.md β€” All releases and feature descriptions
  3. tests/test_tracing.py β€” Understand OTel API expectations
  4. kairu/tracing.py β€” Current implementation
  5. .claude/rules/git-workflow.md β€” Conventional commits and merge protocol

Quick Reference: Running Kairu

# Full test suite (no ML deps)
python3.12 -m pytest tests/ -x

# With HF integration tests
KAIRU_TEST_HF=1 python3.12 -m pytest tests/

# Benchmark
python3.12 -m kairu.bench --model mock --tokens 100 --runs 50 --warmup 5

# Serve API
pip install kairu[server]
uvicorn kairu.server:app --reload

# Check code quality
ruff check kairu/
ruff format --check kairu/

Next: Pick Phase 1 or Phase 2 tasks and confirm priority with the team. All implementation paths are clear and well-scaffolded. Ready to ship.