flatrun 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. flatrun-0.1.0/LICENSE +201 -0
  2. flatrun-0.1.0/PKG-INFO +442 -0
  3. flatrun-0.1.0/README.md +402 -0
  4. flatrun-0.1.0/pyproject.toml +98 -0
  5. flatrun-0.1.0/setup.cfg +4 -0
  6. flatrun-0.1.0/setup.py +87 -0
  7. flatrun-0.1.0/src/flatrun/__init__.py +110 -0
  8. flatrun-0.1.0/src/flatrun/backend/__init__.py +32 -0
  9. flatrun-0.1.0/src/flatrun/backend/_populate.py +26 -0
  10. flatrun-0.1.0/src/flatrun/backend/base.py +133 -0
  11. flatrun-0.1.0/src/flatrun/backend/gguf.py +570 -0
  12. flatrun-0.1.0/src/flatrun/backend/multi.py +103 -0
  13. flatrun-0.1.0/src/flatrun/backend/registry.py +93 -0
  14. flatrun-0.1.0/src/flatrun/backend/safetensor.py +299 -0
  15. flatrun-0.1.0/src/flatrun/cli.py +2100 -0
  16. flatrun-0.1.0/src/flatrun/core/__init__.py +23 -0
  17. flatrun-0.1.0/src/flatrun/core/tensor.py +359 -0
  18. flatrun-0.1.0/src/flatrun/dequant/__init__.py +74 -0
  19. flatrun-0.1.0/src/flatrun/dequant/gguf.py +432 -0
  20. flatrun-0.1.0/src/flatrun/dequant/loader.py +125 -0
  21. flatrun-0.1.0/src/flatrun/dequant/mlx.py +123 -0
  22. flatrun-0.1.0/src/flatrun/model/__init__.py +25 -0
  23. flatrun-0.1.0/src/flatrun/model/huggingface.py +193 -0
  24. flatrun-0.1.0/src/flatrun/model/manifest.py +180 -0
  25. flatrun-0.1.0/src/flatrun/model/qwen2.py +2302 -0
  26. flatrun-0.1.0/src/flatrun/model/sampling.py +197 -0
  27. flatrun-0.1.0/src/flatrun/runtime/__init__.py +58 -0
  28. flatrun-0.1.0/src/flatrun/runtime/backend.py +323 -0
  29. flatrun-0.1.0/src/flatrun/runtime/executor.py +162 -0
  30. flatrun-0.1.0/src/flatrun/runtime/kv_cache.py +240 -0
  31. flatrun-0.1.0/src/flatrun/runtime/memory.py +268 -0
  32. flatrun-0.1.0/src/flatrun/runtime/runtime.py +239 -0
  33. flatrun-0.1.0/src/flatrun/runtime/scheduler.py +334 -0
  34. flatrun-0.1.0/src/flatrun/tests/__init__.py +9 -0
  35. flatrun-0.1.0/src/flatrun/tests/conftest.py +49 -0
  36. flatrun-0.1.0/src/flatrun/tests/test_backend.py +323 -0
  37. flatrun-0.1.0/src/flatrun/tests/test_cli_thinking.py +52 -0
  38. flatrun-0.1.0/src/flatrun/tests/test_debug.py +419 -0
  39. flatrun-0.1.0/src/flatrun/tests/test_dequant.py +372 -0
  40. flatrun-0.1.0/src/flatrun/tests/test_gguf.py +284 -0
  41. flatrun-0.1.0/src/flatrun/tests/test_layer_spec.py +126 -0
  42. flatrun-0.1.0/src/flatrun/tests/test_live_thinking.py +216 -0
  43. flatrun-0.1.0/src/flatrun/tests/test_qwen2.py +521 -0
  44. flatrun-0.1.0/src/flatrun/tests/test_runtime.py +118 -0
  45. flatrun-0.1.0/src/flatrun/tests/test_safetensor.py +102 -0
  46. flatrun-0.1.0/src/flatrun/tests/test_sampling.py +144 -0
  47. flatrun-0.1.0/src/flatrun/tests/test_scheduler.py +241 -0
  48. flatrun-0.1.0/src/flatrun/tests/test_smollm2_hf.py +114 -0
  49. flatrun-0.1.0/src/flatrun/tests/test_tensor.py +74 -0
  50. flatrun-0.1.0/src/flatrun/tests/test_tokenizer.py +417 -0
  51. flatrun-0.1.0/src/flatrun/tokenizer/__init__.py +17 -0
  52. flatrun-0.1.0/src/flatrun/tokenizer/bpe.py +750 -0
  53. flatrun-0.1.0/src/flatrun/utils/__init__.py +34 -0
  54. flatrun-0.1.0/src/flatrun/utils/debug.py +883 -0
  55. flatrun-0.1.0/src/flatrun/utils/errors.py +51 -0
  56. flatrun-0.1.0/src/flatrun/utils/memory.py +98 -0
  57. flatrun-0.1.0/src/flatrun/utils/profiler.py +254 -0
  58. flatrun-0.1.0/src/flatrun/utils/types.py +147 -0
  59. flatrun-0.1.0/src/flatrun.egg-info/PKG-INFO +442 -0
  60. flatrun-0.1.0/src/flatrun.egg-info/SOURCES.txt +76 -0
  61. flatrun-0.1.0/src/flatrun.egg-info/dependency_links.txt +1 -0
  62. flatrun-0.1.0/src/flatrun.egg-info/entry_points.txt +3 -0
  63. flatrun-0.1.0/src/flatrun.egg-info/not-zip-safe +1 -0
  64. flatrun-0.1.0/src/flatrun.egg-info/requires.txt +13 -0
  65. flatrun-0.1.0/src/flatrun.egg-info/top_level.txt +2 -0
  66. flatrun-0.1.0/src/flatrun_native/_C.cpp +488 -0
  67. flatrun-0.1.0/src/flatrun_native/__init__.py +23 -0
  68. flatrun-0.1.0/src/flatrun_native/bench/bench_e2e_python_vs_native.py +230 -0
  69. flatrun-0.1.0/src/flatrun_native/bench/bench_native_gemm.py +152 -0
  70. flatrun-0.1.0/src/flatrun_native/kernels/q4_k.h +514 -0
  71. flatrun-0.1.0/src/flatrun_native/kernels/q6_k.h +263 -0
  72. flatrun-0.1.0/src/flatrun_native/kernels/q8_0.h +265 -0
  73. flatrun-0.1.0/src/flatrun_native/python/__init__.py +227 -0
  74. flatrun-0.1.0/src/flatrun_native/tests/test_e2e_backends.py +228 -0
  75. flatrun-0.1.0/src/flatrun_native/tests/test_native_backend.py +346 -0
  76. flatrun-0.1.0/src/flatrun_native/tests/test_q4_k_parity.py +140 -0
  77. flatrun-0.1.0/src/flatrun_native/tests/test_q6_k_parity.py +173 -0
  78. flatrun-0.1.0/src/flatrun_native/tests/test_q8_0_parity.py +138 -0
flatrun-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of tracking and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by, or
173
+ claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 judotens <judotens@flatseek.io>
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
200
+ implied. See the License for the specific language governing
201
+ permissions and limitations under the License.
flatrun-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,442 @@
1
+ Metadata-Version: 2.4
2
+ Name: flatrun
3
+ Version: 0.1.0
4
+ Summary: The streaming inference runtime for open-weight LLMs.
5
+ Author-email: Judotens Budiarto <judotens@flatseek.io>
6
+ License: MIT
7
+ Project-URL: Homepage, https://flatseek.io
8
+ Project-URL: Documentation, https://flatseek.io/docs
9
+ Project-URL: Repository, https://github.com/flatseek/flatrun
10
+ Project-URL: Issues, https://github.com/flatseek/flatrun/issues
11
+ Project-URL: Flatlens, https://github.com/flatseek/flatlens
12
+ Project-URL: Flatvec, https://github.com/flatseek/flatvec
13
+ Project-URL: Flatask, https://github.com/flatseek/flatask
14
+ Project-URL: Flattune, https://github.com/flatseek/flattune
15
+ Project-URL: Benchmarks, https://github.com/flatseek/flatbench
16
+ Keywords: llm,inference,runtime,mmap,safetensors,memory-efficient,gguf,quantization,streaming
17
+ Classifier: Development Status :: 3 - Alpha
18
+ Classifier: Intended Audience :: Developers
19
+ Classifier: Intended Audience :: Science/Research
20
+ Classifier: License :: OSI Approved :: MIT License
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Programming Language :: Python :: 3.13
25
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
26
+ Requires-Python: >=3.10
27
+ Description-Content-Type: text/markdown
28
+ License-File: LICENSE
29
+ Requires-Dist: numpy>=1.26.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=8.0.0; extra == "dev"
32
+ Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
33
+ Requires-Dist: mypy>=1.8.0; extra == "dev"
34
+ Requires-Dist: ruff>=0.2.0; extra == "dev"
35
+ Provides-Extra: safetensors
36
+ Requires-Dist: safetensors>=0.4.0; extra == "safetensors"
37
+ Provides-Extra: native
38
+ Requires-Dist: pybind11>=2.10; extra == "native"
39
+ Dynamic: license-file
40
+
41
+ <div align="center">
42
+
43
+ <img src="logo.svg" alt="Flatrun" width="64" height="64">
44
+
45
+ # Flatrun
46
+
47
+ **The streaming inference runtime for open-weight LLMs.**
48
+
49
+ <p align="center">
50
+ <em>
51
+ Run GGUF, SafeTensors, and MLX models layer-by-layer directly from storage,
52
+ enabling inference on models larger than available system RAM.
53
+ </em>
54
+ </p>
55
+
56
+ [![Python](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/downloads/)
57
+ [![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)](LICENSE)
58
+ [![Tests](https://github.com/flatseek/flatrun/workflows/Test/badge.svg)](https://github.com/flatseek/flatrun/actions)
59
+ [![PyPI version](https://img.shields.io/pypi/v/flatrun.svg)](https://pypi.org/project/flatrun/)
60
+
61
+
62
+ **GitHub:** https://github.com/flatseek/flatrun
63
+ &nbsp;&middot;&nbsp;
64
+ **Organization:** https://github.com/flatseek
65
+
66
+ <br>
67
+
68
+ **Part of the Flatseek ecosystem**
69
+
70
+ [Flatseek](https://github.com/flatseek/flatseek) (Keyword Search) •
71
+ [Flatvec](https://github.com/flatseek/flatvec) (Vector Search) •
72
+ [Flatask](https://github.com/flatseek/flatask) (RAG Runtime) •
73
+ [Flatweight](https://github.com/flatseek/flatweight) (AI Model Storage) •
74
+ [Flattune](https://github.com/flatseek/flattune) (LLM Fine-Tuning) •
75
+ [Flatlens](https://github.com/flatseek/flatlens) (Data Visualization)
76
+
77
+ </div>
78
+
79
+ ---
80
+ ## Try It
81
+
82
+ Download a sample GGUF model from Hugging Face and start an interactive chat:
83
+
84
+ ```bash
85
+ wget https://huggingface.co/lmstudio-community/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf -O smoll.gguf
86
+
87
+ flatrun chat --model smoll.gguf
88
+ ```
89
+
90
+ Example session:
91
+
92
+ ```text
93
+ Detected format: gguf
94
+ Building tokenizer from GGUF metadata (SmolLM2-135M-Instruct-Q4_K_M.gguf) ...
95
+ Tokenizer vocab: 49152
96
+ Chat template: Qwen2 ChatML
97
+ Loaded model in 0.53 s; layers=30
98
+
99
+ Chat mode (max_new=128/turn, history=True).
100
+ Type your message; Ctrl-D (EOF) or 'exit' to quit.
101
+
102
+ You: where is paris?
103
+
104
+ Assistant:
105
+ Paris is the capital and largest city of France. It is located in the
106
+ north-central part of the country along the Seine River and is one of
107
+ Europe's major cultural, historical, and economic centers.
108
+
109
+ ```
110
+ ---
111
+ # Overview
112
+
113
+ **A pure Python and NumPy runtime for AI inference research and optimization.**
114
+
115
+ Flatrun is an experimental inference runtime built to explore how large language models can execute **without loading an entire checkpoint into memory**.
116
+
117
+ Instead of treating a model as a single monolithic file, Flatrun streams weights layer-by-layer directly from disk. Only the layer currently being executed resides in memory while the remaining weights stay memory-mapped on storage.
118
+
119
+ This streaming architecture allows models significantly larger than available system RAM to run on commodity hardware, trading throughput for dramatically lower memory usage.
120
+
121
+ ---
122
+ # Why Flatrun?
123
+
124
+ Flatrun is designed for developers and researchers who want to understand how LLM inference works.
125
+
126
+ It provides a transparent streaming runtime where every stage—from tensor loading to KV cache management and forward execution—can be inspected, profiled, modified, and optimized.
127
+
128
+ ---
129
+ # Highlights
130
+
131
+ - Streaming layer-by-layer execution
132
+ - Constant-memory architecture
133
+ - Pure Python + NumPy runtime
134
+ - Optional native C++ acceleration
135
+ - GGUF, SafeTensors, and MLX support
136
+ - Q1/Q4/Q5/Q6/Q8 quantization
137
+ - Python API and interactive chat
138
+ - Layer profiler and memory tracing
139
+ ---
140
+
141
+ # Supported Models
142
+
143
+ | Family | GGUF | SafeTensors | MLX |
144
+ |---------|:---:|:-----------:|:---:|
145
+ | Llama 1 / 2 / 3 | ✅ | ✅ | — |
146
+ | Qwen2 | ✅ | ✅ | — |
147
+ | Qwen2.5 | ✅ | ✅ | — |
148
+ | Qwen2.5 Coder | ✅ | ✅ | — |
149
+ | Qwen3 | ✅ | ✅ | — |
150
+ | Qwen3.5 | ✅ | — | — |
151
+ | SmolLM2 | ✅ | ✅ | — |
152
+ | Gemma 3 | — | — | ✅ |
153
+ | Bonsai Q1_0 | ✅ | — | — |
154
+
155
+ Support for additional architectures is continuously expanding.
156
+
157
+ ---
158
+
159
+ # Installation
160
+
161
+ Install from PyPI:
162
+
163
+ ```bash
164
+ pip install flatrun
165
+ ```
166
+ Or clone + editable install:
167
+
168
+
169
+ ```bash
170
+ git clone https://github.com/flatseek/flatrun.git
171
+
172
+ cd flatrun
173
+
174
+ make install
175
+ ```
176
+
177
+ Requirements:
178
+
179
+ - Python 3.10+
180
+ - NumPy
181
+
182
+ ---
183
+
184
+ # Quick Start
185
+
186
+ Run a GGUF model:
187
+
188
+ ```bash
189
+ flatrun \
190
+ --model model.gguf \
191
+ --prompt "The capital of France is" \
192
+ --max-new 32
193
+ ```
194
+
195
+ Interactive chat:
196
+
197
+ ```bash
198
+ flatrun chat \
199
+ --model model.gguf
200
+ ```
201
+
202
+ Run a SafeTensors checkpoint:
203
+
204
+ ```bash
205
+ flatrun \
206
+ --model /path/to/model \
207
+ --prompt "Explain quantum computing."
208
+ ```
209
+
210
+ ---
211
+
212
+ ## Backend Selection
213
+
214
+ Flatrun provides two interchangeable execution backends.
215
+
216
+ ### Python Backend (Default)
217
+
218
+ The reference runtime is implemented entirely in pure Python and NumPy, making every stage of inference easy to inspect, profile, and modify.
219
+
220
+ ```bash
221
+ flatrun \
222
+ --backend python \
223
+ --model model.gguf \
224
+ --prompt "Hello"
225
+ ```
226
+
227
+ This is the default backend, so `--backend python` can be omitted.
228
+
229
+ ### Native Backend
230
+
231
+ For higher performance, Flatrun can execute performance-critical operations using the native C++ backend located in `src/flatrun_native`.
232
+
233
+ ```bash
234
+ flatrun \
235
+ --backend native \
236
+ --model model.gguf \
237
+ --prompt "Hello"
238
+ ```
239
+
240
+ The native backend preserves the same streaming execution pipeline while accelerating compute-intensive operations such as quantized GEMM and dequantization.
241
+
242
+ Backend options:
243
+
244
+ - `--backend python` *(default)* — Pure Python and NumPy runtime.
245
+ - `--backend native` — Native C++ accelerated runtime.
246
+
247
+ ---
248
+
249
+ # Python API
250
+
251
+ Flatrun is fully scriptable. The same runtime that powers the CLI is
252
+ exposed as a Python library — pick a local model file, hand it a
253
+ prompt, and read the logits back. Nothing in the public surface
254
+ requires downloading a model from a hub.
255
+
256
+ ## Full minimal example
257
+
258
+ ```python
259
+ from flatrun import KVCache, StreamingExecutor, load_huggingface
260
+ from flatrun.model import make_qwen2_forwarder
261
+ from flatrun.model.sampling import Sampler
262
+ from flatrun.runtime.backend import get_backend
263
+ from flatrun.tokenizer import auto_load
264
+
265
+ # 1. Open a local GGUF
266
+ loaded = load_huggingface("/Users/me/models/qwen3-0.6b-q4_k_m.gguf")
267
+
268
+ # 2. Tokenize
269
+ tokenizer = auto_load("/Users/me/models/qwen3-0.6b-q4_k_m.gguf")
270
+ prompt = "The capital of France is"
271
+ tokens = list(tokenizer.encode(prompt))
272
+
273
+ # 3. Build the executor
274
+ backend = get_backend("python")
275
+ forwarder = make_qwen2_forwarder(
276
+ loaded.config, enable_dequant_cache=True, backend=backend,
277
+ )
278
+ kv_cache = KVCache(capacity=4096)
279
+ scheduler = loaded.runtime.build_scheduler(loaded.manifest.layers)
280
+ executor = StreamingExecutor(scheduler, forwarder, kv_cache=kv_cache)
281
+
282
+ # 4. Prefill + decode
283
+ sampler = Sampler(temperature=0.0001, top_k=1)
284
+ step = executor.step(tokens)
285
+ next_id = sampler.sample(step.last_hidden[-1])
286
+ out = list(tokens) + [next_id]
287
+ for _ in range(32):
288
+ step = executor.step([next_id])
289
+ next_id = sampler.sample(step.last_hidden[-1])
290
+ out.append(next_id)
291
+
292
+ # 5. Decode
293
+ print(tokenizer.decode(out))
294
+ ```
295
+
296
+ ---
297
+ # CLI
298
+
299
+ ```bash
300
+ flatrun run
301
+ flatrun chat
302
+ ```
303
+
304
+ Supports:
305
+
306
+ - Text generation and sampling
307
+ - Python and native backends
308
+ - Runtime and cache configuration
309
+ - Quantization overrides
310
+ - Layer selection
311
+ - Profiling and debugging
312
+
313
+ See `flatrun run --help` for the complete command reference.
314
+
315
+ ---
316
+ # How It Works
317
+
318
+ ```text
319
+ Model on Disk
320
+
321
+
322
+ Load Layer
323
+
324
+
325
+ Execute
326
+
327
+
328
+ Update KV
329
+
330
+
331
+ Release Layer
332
+
333
+
334
+ Next Layer
335
+ ```
336
+
337
+ Instead of loading an entire checkpoint into memory, Flatrun streams one decoder layer at a time. Only the active layer and KV cache remain resident in memory, keeping peak RAM nearly constant regardless of model size.
338
+
339
+ ---
340
+ # Performance
341
+
342
+ Flatrun prioritizes memory efficiency over maximum throughput.
343
+
344
+ Typical performance on modern Apple Silicon:
345
+
346
+ | Model | Peak RAM | Speed |
347
+ |--------|----------:|------:|
348
+ | SmolLM2-360M Q8 | ~400 MB | ~40 tok/s |
349
+ | Qwen3-0.6B Q4 | ~512 MB | ~12 tok/s |
350
+ | Qwen3-14B Q4 | ~2–30 GB | ~0.7–1.5 tok/s |
351
+
352
+ Performance depends on:
353
+
354
+ - Model size
355
+ - Quantization
356
+ - Storage bandwidth
357
+ - Cache configuration
358
+
359
+ Profile every stage of execution using:
360
+
361
+ ```bash
362
+ flatrun \
363
+ --profile-detailed \
364
+ --profile-save profile.json
365
+ ```
366
+
367
+ ---
368
+
369
+ # Native Backend
370
+
371
+ While the reference runtime is intentionally written in pure Python, Flatrun also includes an optional native backend located in `src/flatrun_native`.
372
+
373
+ The native backend accelerates performance-critical components using C++ while preserving the exact same streaming architecture and runtime API.
374
+
375
+ Current focus includes:
376
+
377
+ - Quantized GEMM kernels (fused dequant + matmul)
378
+ - Per-quant coverage: **Q4_K**, **Q6_K**, **Q8_0**
379
+ - SIMD vectorization (ARM NEON 128-bit intrinsics)
380
+ - Multi-threaded row-parallel matmul (std::thread pool, one per call)
381
+ - Batched matmul API for prefill (no per-token Python overhead)
382
+ - Reduced Python overhead via pybind11 marshalling
383
+
384
+ The native backend is selected with `--backend native`. When the C++
385
+ extension is not built (no pybind11 toolchain available), or when a
386
+ specific kernel rejects an unusual layout, the call falls back to
387
+ the numpy path transparently — `--backend native` is always safe.
388
+
389
+ Each supported quant type has a parity test in
390
+ `src/flatrun_native/tests/`:
391
+
392
+ | Quant | Test | Tolerance |
393
+ |-------|------|----------:|
394
+ | Q4_K | `test_q4_k_parity.py` | ~3e-6 max rel |
395
+ | Q6_K | `test_q6_k_parity.py` | ~3e-4 max rel |
396
+ | Q8_0 | `test_q8_0_parity.py` | ~2e-5 max rel |
397
+
398
+ See [`docs/native.md`](docs/native.md) for the kernel design notes
399
+ and the multi-threading / batched-API rationale.
400
+
401
+ ---
402
+
403
+ # Research Focus
404
+
405
+ Flatrun serves as a platform for exploring new ideas in AI runtime design.
406
+
407
+ Current research areas include:
408
+
409
+ - Streaming inference
410
+ - Layer scheduling
411
+ - Memory-aware execution
412
+ - Quantized inference
413
+ - Dequantization optimization
414
+ - KV cache architectures
415
+ - Native kernel acceleration
416
+ - Runtime instrumentation
417
+ - Pure NumPy performance
418
+
419
+ The codebase is intentionally designed to be readable, modular, and easy to modify, making it suitable for experimentation, benchmarking, and runtime research.
420
+
421
+ ---
422
+
423
+ # Limitations
424
+
425
+ Current limitations include:
426
+
427
+ - CPU-only execution
428
+ - Pure NumPy reference runtime
429
+ - No CUDA backend
430
+ - No Metal kernels
431
+ - Lower throughput than GPU runtimes
432
+ - Some architectures are still under active development
433
+
434
+ Flatrun is built primarily for experimentation, research, and runtime innovation rather than large-scale production serving.
435
+
436
+ ---
437
+
438
+ # License
439
+
440
+ Apache License 2.0
441
+
442
+ See `LICENSE`.