Keras on PyTorch: compiled generation 11.6x faster
Traced why Keras on PyTorch ran about 9x slower than plain PyTorch, and merged the fixes upstream, one root cause each.
- Performance and inference
- Jul 2026 – Oct 2026
- Merged
- 2 min read


Keras runs on PyTorch, but on small models it ran about 9x slower than the same model in plain PyTorch. I traced the cost to Python work Keras repeated on every call, opened keras#22561 with the root causes, and landed the fixes as small pull requests, one cause each. Each was timed on the commit just before and just after its merge.
Problem
Most of each call went to Keras's own Python: building traceback details that were only needed when an op failed,
re-setting layer state, checking dtypes and inputs. torch.compile could not help either. Graph breaks split every
forward pass and compiled steps kept recompiling, so compiling a Keras model made generation slower than running it
eagerly.
Approach
- Counted every Python call with cProfile and every
torch.compilegraph break withtorch._dynamo.explain, on the issue's two models: a small CNN and a two-layer transformer language model. - Split one large fix into small pull requests, one root cause each, each with tests and a before-and-after benchmark.
- Three merges made
torch.compilework with Keras: one removed the graph break in attention, one made the PyTorch tree code traceable, and one stopped compiled steps from recompiling. - Most fixes changed shared code (
Layer.__call__, op tracebacks, dtype handling, input checks), so the JAX, TensorFlow and NumPy backends gained too.
Results
By the progress report of October 18, 15 pull requests were merged. Between Keras master on July 18 and on October 15, compiled 32-token generation got 11.6x faster and eager generation 1.21x faster. Graph breaks per forward pass fell from 20 to 0, and Python calls per forward pass fell 12 to 23% on all four backends. The progress report times every pull request on its own; its interactive version on DEV is under Links.
What did not work
The eager gap did not close: eager Keras on PyTorch still takes 8.45x the time of plain PyTorch on this benchmark, so the issue stays open. When the report was posted, two more fixes were in review. Training, CPU timing and larger models were not measured.
Charts from the report
From the progress update on keras#22561, posted 18 Oct 2026.












More in performance and inference
- GLM-5.3-Flash on one GPU2 Sep 2026
Served a 181 GiB mixture-of-experts LLM from one 96 GiB GPU and took decoding from 10.8 to 15.0 tokens/s shipped, 16.5 at peak. · Independent · Published
- One call from a Keras model to a phone: LiteRT exportDec 2025 – Jul 2026
Built the LiteRT export path in Keras and KerasHub: a trained model to a phone-ready file in one call, on TensorFlow and PyTorch. · Google · Merged
- End-to-end UI tests for XProfAug 2026 – now
Wrote the end-to-end UI test suite for XProf, the open-source ML profiler, with 107 browser tests and a side-by-side check of two builds. · Google · Shipped
- Llama 3.1 in KerasHub, and Gemma 3 fixesApr 2025 – Jul 2026
Implemented Llama 3.1 in KerasHub and fixed Gemma 3 bugs, including a preprocessor crash across Gemma 3, 3n and 4. · Google · Merged