Commit aa47f06
committed
MLX: bound the lazy graph so a method's peak memory is not its whole graph
MLX is lazy: Interpreter::dispatch only builds graph nodes, and nothing is
materialized until MLXBackend::execute calls async_eval on the method outputs.
For a long instruction chain that means every intermediate in the method is
live at the same instant.
Whisper-small encode is 495 instructions built before a single byte is
evaluated. Measured with mlx::core::get_{active,peak,cache}_memory on macOS:
peak 1105.6 MB against 94.8 MB of steady-state active memory. On iOS that peak
lands in the app's footprint and is what makes the model unusable there
(#22513).
Evaluate the live per-execution tensors once the intermediates produced since
the last barrier exceed a byte budget. Each barrier costs a GPU sync, so the
cost tracks the NUMBER of barriers; budgeting bytes rather than counting
instructions puts them only in the methods that actually allocate. Whisper-small
at 512 MB takes 12 barriers in encode and 0 in decode.
iPhone 16, whisper-small int8 through the full pipeline, medians of interleaved
rounds:
peak MB peak-loaded pipeline ms encode decode x10
no barrier 1194.4 763.7 885.2 463.2 472.6
byte budget 512MB 692.8 261.0 831.3 420.5 410.4
1.72x lower peak, 2.9x lower execute-phase footprint, and 1.06x faster than the
unbounded path.
macOS, medians of 4 interleaved rounds x 10 executions, round 1 discarded:
peak MB (off -> 512) speed vs off
small encode 1105.6 -> 350.1 1.05x
small decode 258.0 -> 258.0 0.97x
tiny encode 550.4 -> 343.2 1.07x
tiny decode 61.8 -> 61.8 0.93x
SmolLM2 fwd 1196.5 -> 496.8 1.00x
Outputs bit-identical to the unbounded path on whisper tiny/base/small x
{encode, decode} and on two MLX LLMs. ET_MLX_EVAL_BUDGET_MB overrides the
budget; 0 restores the previous behaviour.1 parent 7863536 commit aa47f06
1 file changed
Lines changed: 63 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
1933 | 1936 | | |
1934 | 1937 | | |
1935 | 1938 | | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
1936 | 1952 | | |
1937 | 1953 | | |
1938 | 1954 | | |
| |||
1945 | 1961 | | |
1946 | 1962 | | |
1947 | 1963 | | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
1948 | 1985 | | |
1949 | 1986 | | |
1950 | 1987 | | |
| |||
1957 | 1994 | | |
1958 | 1995 | | |
1959 | 1996 | | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
1960 | 2023 | | |
1961 | 2024 | | |
1962 | 2025 | | |
| |||
0 commit comments