Program.eval and Program.depths take exponential time when executed

Aperta
#1,294 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva

Direzione di ricerca

Start with Program.eval in Cslib/Computability/Circuit/Program.lean (around lines 311-320), then Program.depths (around lines 351-358), since both re-evaluate earlier gates through the prior function. Time a chain of shared gates with #eval to reproduce the blow-up, using the probe linked in the issue. Done means a linear-time evaluation with a lemma tying it to the structural definition, and a timing check on a much larger gate count.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Cslib.Circuits.Program.eval (Cslib/Computability/Circuit/Program.lean:311-320, commit 18852ce9)
is defined as

  | .gate p line =>
      let prior := p.eval i x
      Fin.lastCases (line.eval i x prior) prior

In compiled code (#eval, programs, native_decide) this is exponential in the number of gates on any
program with shared gates: prior is a function, so each lookup re-evaluates the earlier gates, and
Fin.lastCases receives the last-gate value as a strict argument, so it is computed even for lookups of
earlier gates. Program.depths (lines 351-358) has the same shape.

Reproduction (one binary operation interpreted as Nat addition; gate k = add(gate k-1, gate k-2)):

last gate index Program.eval Program.depths
14 0.32 s 0.42 s
16 1.9 s 2.5 s
18 11.3 s 14.3 s
20 64.6 s 84.5 s
22 > 120 s > 120 s

GitHub-hosted ubuntu-24.04 runner, 2 vCPU, Lean v4.37.0-rc1, one process per size; a second machine shows
the same x2.4 per gate. Probe file and run script: https://github.com/ryananderson60/fairtally/tree/main/docs/probes/cslib-program-eval

Nothing in the repository executes this on large inputs today (the tests check 2-gate circuits by rfl
and decide), so CI is unaffected. Suggested fix: a one-pass Program.evalVector : Vector U gateCount
(one push per gate), a lemma (p.evalVector i x)[j] = p.eval i x j, and
@[csimp] theorem eval_eq_evalFast : @Program.eval = @Program.evalFast, leaving the structural
definition for proofs; the same for depths. About 65 lines including a test. A sketch compiles against
this commit and evaluates 10,001 gates in 0.16 s.

Happy to open a PR with the fix if this approach is welcome.

Written with Claude Code (AI-assisted), directed by Ryan Anderson; measurements reproduced on two machines before posting.

Lingua principale
Lean
Stelle
847
Fork
234
Merge medio
2g 6h
PR unite (30g)
51

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/cslib

Tutte le issue di leanprover/cslib

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.