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

Abierto
#1,294 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Lean
Estrellas
847
Forks
234
Merge medio
2 d 6 h
PR fusionados (30 d)
51

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de leanprover/cslib

Todos los issues de leanprover/cslib

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.