Skip to content

Formal: Python/native classifier + per-line malloc attribution - #1082

Merged
emeryberger merged 1 commit into
masterfrom
formal-classifier-and-perline-malloc
Jul 1, 2026
Merged

Formal: Python/native classifier + per-line malloc attribution#1082
emeryberger merged 1 commit into
masterfrom
formal-classifier-and-perline-malloc

Conversation

@emeryberger

Copy link
Copy Markdown
Member

Closes two more open gaps from the correctness map. Two new Lean modules (sorry-free, standard axioms only; full lake build green — 8575 jobs; now 16 modules / 133 theorems).

Python/native classifier — PythonNativeClassifier.lean

Models the 4-way heuristic that splits each CPU sample's time between Python and native (scalene_cpu_profiler.py:251-341). Separates the theorem from the heuristic:

  • charge_total / classified_conservesevery branch conserves the sample's CPU budget (python_time + c_time) across all (line, python|native) buckets. The classification only moves a fixed budget; it never invents or drops time.
  • split_atCall / split_together / split_splitToCall — the native/Python split each branch decides.
  • charge_nonneg — every bucket charge ≥ 0.
  • classify_total — the branch selector is a total function (exactly one branch fires for any input).
  • branchA_exact_if_in_call — conditional correctness: if the sample truly landed in native code, branch A is exact. Which branch is right per sample (the CALL-opcode/deferral heuristic) stays an engineering hypothesis — stated, not hidden.

Per-line malloc attribution — PerLineMallocAttribution.lean

The Python reader's per-line bookkeeping (scalene_memory_profiler.py:336-360), distinct from the footprint total in #1080.

  • perline_conserves — Σ over lines of memory_malloc_samples[line] = total_memory_malloc_samples, so malloc[line]/total (the renderer's n_usage_fraction) is a genuine fraction.
  • python_le_malloc — per line 0 ≤ memory_python_samples ≤ memory_malloc_samples (from python_fraction ∈ [0,1]). This derives the bound Attribution.lean assumed and that scalene_json.py's n_python_fraction = python/malloc ∈ [0,1] relies on.
  • highwater_ge_current / highwater_monotone — the per-line high-water peak dominates the running footprint and never decreases.

Docs

STATUS.md updated (CPU classifier row ❌→✅ for conservation / ⚠️ for branch-choice; new per-line attribution row), README gains §15 + §16 and the top roundup reflects both, HANDOFF module table + next-steps updated.

Formal-only; no production code touched.

Closes two more HANDOFF gaps.

PythonNativeClassifier.lean (scalene_cpu_profiler.py:251-341): the 4-way
Python-vs-native time classifier. Separates the theorem from the heuristic:
- charge_total / classified_conserves: EVERY branch conserves the sample's CPU
  budget (python_time + c_time) across (line, python|native) buckets -- the
  classification only moves a fixed budget, never invents/loses time.
- split_atCall / split_together / split_splitToCall: the native/Python split
  each branch decides.
- charge_nonneg: every bucket charge >= 0 (from python_time,c_time >= 0).
- classify_total: the branch selector is total (exactly one branch fires).
- branchA_exact_if_in_call: conditional correctness -- IF the sample truly
  landed in native, branch A is exact. Which branch is right (the CALL-opcode
  heuristic) stays an engineering hypothesis, stated not hidden.

PerLineMallocAttribution.lean (scalene_memory_profiler.py:336-360): the Python
reader's per-line bookkeeping, distinct from the footprint total.
- perline_conserves: Sum of memory_malloc_samples[line] = total_memory_malloc_
  samples, so malloc[line]/total is a genuine fraction.
- python_le_malloc: memory_python_samples[line] <= memory_malloc_samples[line]
  (python_fraction in [0,1]) -- DERIVES the bound Attribution.lean assumed and
  scalene_json.py's n_python_fraction relies on.
- highwater_ge_current / highwater_monotone: the per-line peak dominates the
  running footprint and never decreases.

No sorry; standard axioms only. Full lake build green (8575 jobs). 16 modules /
133 theorems. STATUS.md, README (§15/§16 + roundup), HANDOFF updated.
@emeryberger
emeryberger merged commit 7934cac into master Jul 1, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant