moe-l2 0.2.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.
moe_l2-0.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yalund
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
moe_l2-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,192 @@
1
+ Metadata-Version: 2.2
2
+ Name: moe-l2
3
+ Version: 0.2.0
4
+ Summary: MoE inference L2 hot-cache scheduler — run large MoE models on consumer GPUs
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/yalund/moe-l2
7
+ Project-URL: Source, https://github.com/yalund/moe-l2
8
+ Keywords: moe,mixture-of-experts,llm,inference,caching,ollama,llama.cpp
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
+ Classifier: Topic :: System :: Hardware :: Hardware Drivers
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: httpx>=0.27
24
+ Requires-Dist: gguf>=0.10
25
+ Provides-Extra: predictor
26
+ Requires-Dist: sentence-transformers>=3.0; extra == "predictor"
27
+
28
+ # moe-l2
29
+
30
+ **Run large MoE models on consumer GPUs.** A transparent proxy that predicts which experts your prompt needs, preloads them into a shared-memory LRU cache, so you can run 16 GB+ models on 8 GB GPUs.
31
+
32
+ ## How it works
33
+
34
+ MoE models have many "experts" but only activate a few per token. moe-l2 predicts your prompt's domain (codegen, math, chinese_tech, etc.) and preloads the relevant experts into an mmap'd LRU cache before they're needed.
35
+
36
+ ```
37
+ user → moe-l2 proxy (localhost:11435)
38
+ ├── predict domain
39
+ ├── preload domain experts → /dev/shm/moe_l2/
40
+ └── forward to ollama (localhost:11434)
41
+ ```
42
+
43
+ ## Quick start
44
+
45
+ ```bash
46
+ pip install moe-l2 # keyword-only predictor (zero extra deps)
47
+ pip install moe-l2[predictor] # hybrid: keyword + semantic embedding
48
+ moe-l2 start --model model.gguf --l2-size 4GB
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ ### 1. L2 proxy (recommended)
54
+
55
+ Start the transparent proxy — sits between your client and ollama:
56
+
57
+ ```bash
58
+ moe-l2 start --model /models/DeepSeek-V2-Lite.Q4_K_M.gguf --l2-size 4GB
59
+ ```
60
+
61
+ All default ollama tools work through it (curl, open-webui, langchain):
62
+
63
+ ```bash
64
+ # streaming
65
+ curl http://localhost:11435/api/chat -d '{
66
+ "model":"qwen3:4b",
67
+ "messages":[{"role":"user","content":"write a Python script"}],
68
+ "stream":true
69
+ }'
70
+
71
+ # blocking
72
+ curl http://localhost:11435/api/chat -d '{
73
+ "model":"qwen3:4b",
74
+ "messages":[{"role":"user","content":"hello"}],
75
+ "stream":false
76
+ }'
77
+ ```
78
+
79
+ ### 2. Monitor cache stats
80
+
81
+ ```bash
82
+ moe-l2 stats --port 11435
83
+ ```
84
+
85
+ Example output:
86
+ ```
87
+ moe-l2 cache stats
88
+ requests: 47
89
+ hits: 42 (89.4%)
90
+ misses: 5
91
+ slots_used: 32/48 (66.7%)
92
+ memory: 456 MB (68.3% of 668 MB)
93
+ ```
94
+
95
+ ### 3. Use as a library
96
+
97
+ ```python
98
+ from moe_l2 import predict, predict_hybrid, domain_to_expert_ids
99
+ from moe_l2.cache import L2Cache
100
+
101
+ # Predict domain (zero-dependency mode)
102
+ domain = predict("print hello world") # → "codegen"
103
+
104
+ # Hybrid mode (falls back to semantic embedding)
105
+ from moe_l2 import enable_semantic
106
+ enable_semantic()
107
+ domain = predict_hybrid("deploy nginx on ubuntu") # → "chinese_tech"
108
+
109
+ # Setup L2 cache
110
+ cache = L2Cache(
111
+ model_path="/models/model.gguf",
112
+ slots_per_layer=48 # auto-calculated from --l2-size
113
+ )
114
+ expert_map = load_mapping()
115
+ cache.preload_domain("codegen", expert_map)
116
+
117
+ # Stats
118
+ cache.stats() # → {"hits": ..., "misses": ..., ...}
119
+ ```
120
+
121
+ ## Supported domains
122
+
123
+ | Domain | Examples |
124
+ |--------|---------|
125
+ | `codegen` | Python, JS, bash, API design |
126
+ | `debug` | error logs, stack traces, crash analysis |
127
+ | `math` | algebra, calculus, equations |
128
+ | `logic` | reasoning, puzzles, proofs |
129
+ | `general_qa` | general knowledge, facts, explanations |
130
+ | `chinese_tech` | 中文技术内容, NAS, 部署, 教程 |
131
+ | `creative_write` | storytelling, poetry, marketing |
132
+ | `translate` | translation between languages |
133
+
134
+ ## Architecture
135
+
136
+ ```
137
+ ┌────────────────────────────────────────────────┐
138
+ │ Ollama Client (user-facing) │
139
+ │ curl / open-webui / langchain / any tool │
140
+ └──────────┬─────────────────────────────────────┘
141
+ │ POST to :11435
142
+ ┌──────────▼─────────────────────────────────────┐
143
+ │ moe-l2 proxy │
144
+ │ ┌─────────────────────────────────────────┐ │
145
+ │ │ Domain Predictor │ │
146
+ │ │ ┌──────────┐ ┌───────────────────┐ │ │
147
+ │ │ │ Keywords │ → │ Semantic (opt) │ │ │
148
+ │ │ │ ~210词 │ │ all-MiniLM-L6-v2 │ │ │
149
+ │ │ └──────────┘ └───────────────────┘ │ │
150
+ │ │ ↓ domain │ │
151
+ │ │ ┌──────────────────────────────────┐ │ │
152
+ │ │ │ L2 Cache (LRU + mmap /dev/shm/) │ │ │
153
+ │ │ │ preload domain experts async │ │ │
154
+ │ │ └──────────────────────────────────┘ │ │
155
+ │ └─────────────────────────────────────────┘ │
156
+ │ │ POST to :11434 (transparent) │
157
+ └──────────┬─────────────────────────────────────┘
158
+
159
+ ┌──────────▼─────────────────────────────────────┐
160
+ │ Ollama / llama.cpp │
161
+ │ MoE inference with hot-cached experts │
162
+ └────────────────────────────────────────────────┘
163
+ ```
164
+
165
+ ## CLI reference
166
+
167
+ | Command | Description |
168
+ |---------|-------------|
169
+ | `moe-l2 start --model <path> --l2-size <size>` | Start proxy + cache |
170
+ | `moe-l2 stats --port <port>` | Show live cache stats |
171
+ | `moe-l2 stop --port <port>` | Stop proxy |
172
+
173
+ Options:
174
+ - `--model auto`: scan `/opt/data/models/*.gguf`
175
+ - `--l2-size 4GB` / `--l2-size 512MB`: target cache size
176
+ - `--port 11435` (default)
177
+
178
+ ## Project status
179
+
180
+ **Phase 2** — core components complete (2026-07-30):
181
+ - ✅ Domain predictor (keyword + optional semantic)
182
+ - ✅ L2 cache (mmap LRU, thread-safe, async preload)
183
+ - ✅ GGUF weight reader (direct memmap from .gguf)
184
+ - ✅ Transparent proxy (HTTP/SSE forwarding, predict+preload)
185
+ - ✅ CLI (start/stats with auto model detection)
186
+ - ✅ GPU end-to-end pipeline verified (DS-V2-Lite on RTX 4090 24GB, Dec 2026)
187
+ - 🔲 llama.cpp C++ integration (direct mmap from L2 cache) — Phase 3
188
+ - 🔲 PyPI v0.2.0 release
189
+
190
+ ## License
191
+
192
+ MIT
moe_l2-0.2.0/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # moe-l2
2
+
3
+ **Run large MoE models on consumer GPUs.** A transparent proxy that predicts which experts your prompt needs, preloads them into a shared-memory LRU cache, so you can run 16 GB+ models on 8 GB GPUs.
4
+
5
+ ## How it works
6
+
7
+ MoE models have many "experts" but only activate a few per token. moe-l2 predicts your prompt's domain (codegen, math, chinese_tech, etc.) and preloads the relevant experts into an mmap'd LRU cache before they're needed.
8
+
9
+ ```
10
+ user → moe-l2 proxy (localhost:11435)
11
+ ├── predict domain
12
+ ├── preload domain experts → /dev/shm/moe_l2/
13
+ └── forward to ollama (localhost:11434)
14
+ ```
15
+
16
+ ## Quick start
17
+
18
+ ```bash
19
+ pip install moe-l2 # keyword-only predictor (zero extra deps)
20
+ pip install moe-l2[predictor] # hybrid: keyword + semantic embedding
21
+ moe-l2 start --model model.gguf --l2-size 4GB
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ### 1. L2 proxy (recommended)
27
+
28
+ Start the transparent proxy — sits between your client and ollama:
29
+
30
+ ```bash
31
+ moe-l2 start --model /models/DeepSeek-V2-Lite.Q4_K_M.gguf --l2-size 4GB
32
+ ```
33
+
34
+ All default ollama tools work through it (curl, open-webui, langchain):
35
+
36
+ ```bash
37
+ # streaming
38
+ curl http://localhost:11435/api/chat -d '{
39
+ "model":"qwen3:4b",
40
+ "messages":[{"role":"user","content":"write a Python script"}],
41
+ "stream":true
42
+ }'
43
+
44
+ # blocking
45
+ curl http://localhost:11435/api/chat -d '{
46
+ "model":"qwen3:4b",
47
+ "messages":[{"role":"user","content":"hello"}],
48
+ "stream":false
49
+ }'
50
+ ```
51
+
52
+ ### 2. Monitor cache stats
53
+
54
+ ```bash
55
+ moe-l2 stats --port 11435
56
+ ```
57
+
58
+ Example output:
59
+ ```
60
+ moe-l2 cache stats
61
+ requests: 47
62
+ hits: 42 (89.4%)
63
+ misses: 5
64
+ slots_used: 32/48 (66.7%)
65
+ memory: 456 MB (68.3% of 668 MB)
66
+ ```
67
+
68
+ ### 3. Use as a library
69
+
70
+ ```python
71
+ from moe_l2 import predict, predict_hybrid, domain_to_expert_ids
72
+ from moe_l2.cache import L2Cache
73
+
74
+ # Predict domain (zero-dependency mode)
75
+ domain = predict("print hello world") # → "codegen"
76
+
77
+ # Hybrid mode (falls back to semantic embedding)
78
+ from moe_l2 import enable_semantic
79
+ enable_semantic()
80
+ domain = predict_hybrid("deploy nginx on ubuntu") # → "chinese_tech"
81
+
82
+ # Setup L2 cache
83
+ cache = L2Cache(
84
+ model_path="/models/model.gguf",
85
+ slots_per_layer=48 # auto-calculated from --l2-size
86
+ )
87
+ expert_map = load_mapping()
88
+ cache.preload_domain("codegen", expert_map)
89
+
90
+ # Stats
91
+ cache.stats() # → {"hits": ..., "misses": ..., ...}
92
+ ```
93
+
94
+ ## Supported domains
95
+
96
+ | Domain | Examples |
97
+ |--------|---------|
98
+ | `codegen` | Python, JS, bash, API design |
99
+ | `debug` | error logs, stack traces, crash analysis |
100
+ | `math` | algebra, calculus, equations |
101
+ | `logic` | reasoning, puzzles, proofs |
102
+ | `general_qa` | general knowledge, facts, explanations |
103
+ | `chinese_tech` | 中文技术内容, NAS, 部署, 教程 |
104
+ | `creative_write` | storytelling, poetry, marketing |
105
+ | `translate` | translation between languages |
106
+
107
+ ## Architecture
108
+
109
+ ```
110
+ ┌────────────────────────────────────────────────┐
111
+ │ Ollama Client (user-facing) │
112
+ │ curl / open-webui / langchain / any tool │
113
+ └──────────┬─────────────────────────────────────┘
114
+ │ POST to :11435
115
+ ┌──────────▼─────────────────────────────────────┐
116
+ │ moe-l2 proxy │
117
+ │ ┌─────────────────────────────────────────┐ │
118
+ │ │ Domain Predictor │ │
119
+ │ │ ┌──────────┐ ┌───────────────────┐ │ │
120
+ │ │ │ Keywords │ → │ Semantic (opt) │ │ │
121
+ │ │ │ ~210词 │ │ all-MiniLM-L6-v2 │ │ │
122
+ │ │ └──────────┘ └───────────────────┘ │ │
123
+ │ │ ↓ domain │ │
124
+ │ │ ┌──────────────────────────────────┐ │ │
125
+ │ │ │ L2 Cache (LRU + mmap /dev/shm/) │ │ │
126
+ │ │ │ preload domain experts async │ │ │
127
+ │ │ └──────────────────────────────────┘ │ │
128
+ │ └─────────────────────────────────────────┘ │
129
+ │ │ POST to :11434 (transparent) │
130
+ └──────────┬─────────────────────────────────────┘
131
+
132
+ ┌──────────▼─────────────────────────────────────┐
133
+ │ Ollama / llama.cpp │
134
+ │ MoE inference with hot-cached experts │
135
+ └────────────────────────────────────────────────┘
136
+ ```
137
+
138
+ ## CLI reference
139
+
140
+ | Command | Description |
141
+ |---------|-------------|
142
+ | `moe-l2 start --model <path> --l2-size <size>` | Start proxy + cache |
143
+ | `moe-l2 stats --port <port>` | Show live cache stats |
144
+ | `moe-l2 stop --port <port>` | Stop proxy |
145
+
146
+ Options:
147
+ - `--model auto`: scan `/opt/data/models/*.gguf`
148
+ - `--l2-size 4GB` / `--l2-size 512MB`: target cache size
149
+ - `--port 11435` (default)
150
+
151
+ ## Project status
152
+
153
+ **Phase 2** — core components complete (2026-07-30):
154
+ - ✅ Domain predictor (keyword + optional semantic)
155
+ - ✅ L2 cache (mmap LRU, thread-safe, async preload)
156
+ - ✅ GGUF weight reader (direct memmap from .gguf)
157
+ - ✅ Transparent proxy (HTTP/SSE forwarding, predict+preload)
158
+ - ✅ CLI (start/stats with auto model detection)
159
+ - ✅ GPU end-to-end pipeline verified (DS-V2-Lite on RTX 4090 24GB, Dec 2026)
160
+ - 🔲 llama.cpp C++ integration (direct mmap from L2 cache) — Phase 3
161
+ - 🔲 PyPI v0.2.0 release
162
+
163
+ ## License
164
+
165
+ MIT
@@ -0,0 +1,28 @@
1
+ """moe-l2: MoE inference L2 hot-cache scheduler."""
2
+
3
+ from .predictor import (
4
+ DOMAINS,
5
+ domain_to_expert_ids,
6
+ enable_semantic,
7
+ get_backbone_experts,
8
+ get_layer_specificity,
9
+ get_preload_set,
10
+ is_semantic_available,
11
+ load_mapping,
12
+ predict,
13
+ predict_hybrid,
14
+ )
15
+
16
+ __version__ = "0.2.0"
17
+ __all__ = [
18
+ "DOMAINS",
19
+ "load_mapping",
20
+ "predict",
21
+ "predict_hybrid",
22
+ "domain_to_expert_ids",
23
+ "get_preload_set",
24
+ "get_backbone_experts",
25
+ "get_layer_specificity",
26
+ "enable_semantic",
27
+ "is_semantic_available",
28
+ ]