meme-core 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.
@@ -0,0 +1,190 @@
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 the 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 discussing 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 usage,
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 reasonable and customary use in describing the
141
+ origin of the Work and 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 to 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 Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Abel
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,257 @@
1
+ Metadata-Version: 2.5
2
+ Name: meme-core
3
+ Version: 0.1.0
4
+ Summary: Memory-Compute Pareto analysis core: guarded memory strategies and solver-based Best-of-N
5
+ Project-URL: Homepage, https://github.com/abel/meme-llm
6
+ Project-URL: Documentation, https://github.com/abel/meme-llm#readme
7
+ Project-URL: Repository, https://github.com/abel/meme-llm
8
+ Project-URL: Issues, https://github.com/abel/meme-llm/issues
9
+ Project-URL: Changelog, https://github.com/abel/meme-llm/blob/main/CHANGELOG.md
10
+ Author-email: Abel <abel@example.com>
11
+ License-Expression: Apache-2.0
12
+ License-File: LICENSE
13
+ Keywords: best-of-n,compute,constraint-solver,inference,llm,memory,pareto,small-language-models,structured-memory,test-time
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: License :: OSI Approved :: Apache Software License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
24
+ Requires-Python: >=3.10
25
+ Provides-Extra: dev
26
+ Requires-Dist: black>=23.0; extra == 'dev'
27
+ Requires-Dist: mypy>=1.0; extra == 'dev'
28
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
29
+ Requires-Dist: pytest>=7.0; extra == 'dev'
30
+ Requires-Dist: ruff>=0.1; extra == 'dev'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # meme-core
34
+
35
+ > **Memory-Compute Pareto analysis core** — guarded memory strategies and solver-based Best-of-N for small language models
36
+
37
+ [![PyPI version](https://img.shields.io/pypi/v/meme-core.svg)](https://pypi.org/project/meme-core/)
38
+ [![Python](https://img.shields.io/pypi/pyversions/meme-core.svg)](https://pypi.org/project/meme-core/)
39
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
40
+
41
+ ## What is this?
42
+
43
+ **meme-core** extracts two key techniques from the [MEME project](https://github.com/abel/meme-llm) as a standalone Python library:
44
+
45
+ ### 1. Guarded Memory Strategies (`guard`)
46
+
47
+ Structured memory reliably **hurts** pure-retrieval tasks by 30–50pp across every model tested. The guard removes this penalty entirely by falling back to plain prose when there is nothing to structure.
48
+
49
+ **Key insight (MEME v3.7):**
50
+ - Without guard: M3 on TriviaQA drops 60% (from 100%) — model relies on shattered question fragments
51
+ - With guard (M3G/M3Q): Returns to 100% — prose fallback prevents damage
52
+
53
+ **Authority framing matters:**
54
+ - **Block-authoritative** (M*G): Best for constraint-satisfaction tasks (ZebraLogic)
55
+ - **Question-authoritative** (M*Q): Best for retrieval tasks (TriviaQA, HotpotQA)
56
+
57
+ See [`guard`](src/meme_core/guard/) for implementation.
58
+
59
+ ### 2. Solver-based Best-of-N (`solver_bon`)
60
+
61
+ Best-of-N without a verifier has no selection criterion and degenerates to a random sample. Using a constraint solver as the reward model makes BoN deterministic and maximally effective for constraint-satisfaction tasks.
62
+
63
+ **Key insight (MEME v3.9):**
64
+ - For tasks with verified unique solutions (ZebraLogic, GSM8K-Hard), the constraint solver IS the ground truth
65
+ - No LLM call needed for verification — deterministic, reproducible, perfect accuracy
66
+
67
+ See [`solver_bon`](src/meme_core/solver_bon/) for implementation.
68
+
69
+ ## Installation
70
+
71
+ ```bash
72
+ pip install meme-core
73
+ ```
74
+
75
+ Or from source:
76
+
77
+ ```bash
78
+ pip install -e .
79
+ ```
80
+
81
+ ## Quick Start
82
+
83
+ ### Guarded Memory
84
+
85
+ ```python
86
+ from meme_core.guard import GuardedMemory, Problem
87
+
88
+ # Create a problem (e.g., from JSONL data)
89
+ problem = Problem(
90
+ id=1,
91
+ question="Who is the engineer? There are 5 people: Alice, Bob, Carol, Dan, Eve.",
92
+ expected="Alice",
93
+ constraints=[
94
+ "Alice is the engineer",
95
+ "Bob is the doctor",
96
+ "Carol is the teacher",
97
+ "Dan is not the engineer",
98
+ "Eve is the lawyer"
99
+ ],
100
+ structure_kind="constraints" # Key: tells guard what material exists
101
+ )
102
+
103
+ # Use a guarded strategy
104
+ # M3G: Block-authoritative (recommended for constraint tasks)
105
+ # M3Q: Question-authoritative (recommended for retrieval tasks)
106
+ strategy = GuardedMemory(strategy="M3G")
107
+ prompt = strategy.render(problem)
108
+
109
+ print(prompt)
110
+ # Output includes entity-grouped facts with "Using ONLY" authority claim
111
+ ```
112
+
113
+ **When structure_kind="none", the guard falls back to plain prose:**
114
+
115
+ ```python
116
+ problem = Problem(
117
+ id=2,
118
+ question="What is the capital of France?",
119
+ expected="Paris",
120
+ constraints=[],
121
+ structure_kind="none" # No structure → prose fallback
122
+ )
123
+
124
+ strategy = GuardedMemory(strategy="M3G")
125
+ prompt = strategy.render(problem)
126
+ # Returns plain M0-style prose, not empty scaffold
127
+ ```
128
+
129
+ ### Best-of-N with Constraint Solver
130
+
131
+ ```python
132
+ from meme_core.solver_bon import best_of_n, ConstraintSolver, Problem
133
+
134
+ problem = Problem(
135
+ id=1,
136
+ question="Who is the engineer?",
137
+ expected="lawyer",
138
+ aliases=["advocate", "attorney"] # Acceptable variants
139
+ )
140
+
141
+ # Candidate answers from N model samples at temperature 0.7
142
+ answers = ["doctor", "lawyer", "engineer", "lawyer"]
143
+
144
+ # Select best using ground-truth constraint solver
145
+ winner, scores = best_of_n(answers, problem, use_solver=True)
146
+
147
+ print(f"Winner: {winner}") # "lawyer"
148
+ print(f"Scores: {[s.confidence for s in scores]}") # [0, 100, 0, 100]
149
+ ```
150
+
151
+ ## Memory Strategies
152
+
153
+ | Strategy | Description | Use Case |
154
+ |----------|-------------|----------|
155
+ | `M0` | Raw prose question (baseline) | Control / pure retrieval |
156
+ | `M1` | Full context + enumerate constraints | General purpose |
157
+ | `M2` | Structured bullet constraints | Compression |
158
+ | `M3` | Entity-grouped facts (auto-extracted) | Entity-centric reasoning |
159
+ | `M4` | Knowledge-graph triples | Relationship reasoning |
160
+ | `M2G/M3G/M4G` | Guarded + block-authoritative | Constraint tasks (recommended) |
161
+ | `M2Q/M3Q/M4Q` | Guarded + question-authoritative | Retrieval tasks (recommended) |
162
+
163
+ ## The `structure_kind` Field
164
+
165
+ Problems declare what auxiliary material they provide, which determines guard behavior:
166
+
167
+ | structure_kind | Example Task | Guard Behavior |
168
+ |----------------|--------------|----------------|
169
+ | `constraints` | ZebraLogic | Organize constraints into structured view |
170
+ | `quantities` | GSM8K-Hard | Organize numeric givens |
171
+ | `options` | ARC | Organize answer choices |
172
+ | `facts` | HotpotQA | Organize facts for chaining |
173
+ | `none` | TriviaQA, HumanEval | **Fall back to prose** (no scaffold) |
174
+
175
+ ## Architecture
176
+
177
+ ```
178
+ meme_core/
179
+ ├── __init__.py # Package entry point
180
+ ├── guard/
181
+ │ ├── __init__.py # Guard module exports
182
+ │ ├── core.py # GuardedMemory class, render_prompt, Problem
183
+ │ └── templates.py # Prompt templates
184
+ └── solver_bon/
185
+ ├── __init__.py # Solver BoN exports
186
+ └── core.py # ConstraintSolver, best_of_n, normalize_for_comparison
187
+ ```
188
+
189
+ ## Example: gemma4:26b Performance Improvement
190
+
191
+ A real example showing how meme-core's `solver_bon` improves gemma4:26b accuracy:
192
+
193
+ ```bash
194
+ python3 examples/gemma4_26b_improvement.py
195
+ ```
196
+
197
+ **Output:**
198
+
199
+ ```
200
+ 📊 gemma4:26b Accuracy Comparison: Greedy vs. Best-of-N
201
+ ----------------------------------------------------------------------
202
+ Task Greedy BoN Δ Notes
203
+ ----------------------------------------------------------------------
204
+ ZebraLogic (hard) 70% 90% +20pp 🎯 (major gain!)
205
+ HumanEval 90% 100% +10pp 🎯
206
+ GSM8K-Hard 90% 90% 0pp
207
+ ARC 100% 100% 0pp (saturated)
208
+ ----------------------------------------------------------------------
209
+ Average 87.5% 95.0% +7.5pp
210
+ ```
211
+
212
+ **Key insight:** Best-of-N without a verifier degenerates to random sampling. Using a constraint solver as the reward model makes answer selection deterministic and maximally effective.
213
+
214
+ ## Key Results (from MEME project)
215
+
216
+ ### Guard removes retrieval penalty completely
217
+
218
+ | Task | M0 | M3 (unguarded) | M3G | M3Q |
219
+ |------|-----|-----------------|-----|-----|
220
+ | TriviaQA | 100% | 60% | **100%** | **100%** |
221
+ | HotpotQA | 90% | 60% | **90%** | **90%** |
222
+ | GSM8K-Hard | 70% | 70% | **80%** | 70% |
223
+ | ARC | 90% | 100% | 90% | **100%** |
224
+
225
+ Cross-vendor验证: llama3.2:3b on TriviaQA 40% → **90%**; phi3.5 70% → **100%**
226
+
227
+ ### Guarded strategies are robust across model families
228
+
229
+ | Model | TriviaQA (M3→M3G) | HotpotQA (M3→M3G) |
230
+ |-------|---------------------|---------------------|
231
+ | qwen3:1.7b | +40pp | +30pp |
232
+ | qwen3:8b | +30pp | +30pp |
233
+ | llama3.2:3b | +50pp | — |
234
+ | phi3.5 | +30pp | — |
235
+
236
+ ## Citation
237
+
238
+ If you use meme-core in your research, please cite:
239
+
240
+ ```bibtex
241
+ @misc{meme2026,
242
+ title = {Memory is the Cheap Axis: Pareto Analysis of Test-Time Memory
243
+ and Compute for Small Language Models},
244
+ author = {Abel},
245
+ year = {2026},
246
+ note = {Preprint, https://github.com/abel/meme-llm}
247
+ }
248
+ ```
249
+
250
+ ## References
251
+
252
+ - [MEME Project (meme-llm)](https://github.com/abel/meme-llm) — Full research project with data, code, and results
253
+ - [Paper (arXiv)](https://arxiv.org/abs/XXXX.XXXXX) — Full methodology and results (TODO: update with actual link)
254
+
255
+ ## License
256
+
257
+ Apache 2.0 — see [LICENSE](LICENSE) for details.
@@ -0,0 +1,225 @@
1
+ # meme-core
2
+
3
+ > **Memory-Compute Pareto analysis core** — guarded memory strategies and solver-based Best-of-N for small language models
4
+
5
+ [![PyPI version](https://img.shields.io/pypi/v/meme-core.svg)](https://pypi.org/project/meme-core/)
6
+ [![Python](https://img.shields.io/pypi/pyversions/meme-core.svg)](https://pypi.org/project/meme-core/)
7
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
8
+
9
+ ## What is this?
10
+
11
+ **meme-core** extracts two key techniques from the [MEME project](https://github.com/abel/meme-llm) as a standalone Python library:
12
+
13
+ ### 1. Guarded Memory Strategies (`guard`)
14
+
15
+ Structured memory reliably **hurts** pure-retrieval tasks by 30–50pp across every model tested. The guard removes this penalty entirely by falling back to plain prose when there is nothing to structure.
16
+
17
+ **Key insight (MEME v3.7):**
18
+ - Without guard: M3 on TriviaQA drops 60% (from 100%) — model relies on shattered question fragments
19
+ - With guard (M3G/M3Q): Returns to 100% — prose fallback prevents damage
20
+
21
+ **Authority framing matters:**
22
+ - **Block-authoritative** (M*G): Best for constraint-satisfaction tasks (ZebraLogic)
23
+ - **Question-authoritative** (M*Q): Best for retrieval tasks (TriviaQA, HotpotQA)
24
+
25
+ See [`guard`](src/meme_core/guard/) for implementation.
26
+
27
+ ### 2. Solver-based Best-of-N (`solver_bon`)
28
+
29
+ Best-of-N without a verifier has no selection criterion and degenerates to a random sample. Using a constraint solver as the reward model makes BoN deterministic and maximally effective for constraint-satisfaction tasks.
30
+
31
+ **Key insight (MEME v3.9):**
32
+ - For tasks with verified unique solutions (ZebraLogic, GSM8K-Hard), the constraint solver IS the ground truth
33
+ - No LLM call needed for verification — deterministic, reproducible, perfect accuracy
34
+
35
+ See [`solver_bon`](src/meme_core/solver_bon/) for implementation.
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ pip install meme-core
41
+ ```
42
+
43
+ Or from source:
44
+
45
+ ```bash
46
+ pip install -e .
47
+ ```
48
+
49
+ ## Quick Start
50
+
51
+ ### Guarded Memory
52
+
53
+ ```python
54
+ from meme_core.guard import GuardedMemory, Problem
55
+
56
+ # Create a problem (e.g., from JSONL data)
57
+ problem = Problem(
58
+ id=1,
59
+ question="Who is the engineer? There are 5 people: Alice, Bob, Carol, Dan, Eve.",
60
+ expected="Alice",
61
+ constraints=[
62
+ "Alice is the engineer",
63
+ "Bob is the doctor",
64
+ "Carol is the teacher",
65
+ "Dan is not the engineer",
66
+ "Eve is the lawyer"
67
+ ],
68
+ structure_kind="constraints" # Key: tells guard what material exists
69
+ )
70
+
71
+ # Use a guarded strategy
72
+ # M3G: Block-authoritative (recommended for constraint tasks)
73
+ # M3Q: Question-authoritative (recommended for retrieval tasks)
74
+ strategy = GuardedMemory(strategy="M3G")
75
+ prompt = strategy.render(problem)
76
+
77
+ print(prompt)
78
+ # Output includes entity-grouped facts with "Using ONLY" authority claim
79
+ ```
80
+
81
+ **When structure_kind="none", the guard falls back to plain prose:**
82
+
83
+ ```python
84
+ problem = Problem(
85
+ id=2,
86
+ question="What is the capital of France?",
87
+ expected="Paris",
88
+ constraints=[],
89
+ structure_kind="none" # No structure → prose fallback
90
+ )
91
+
92
+ strategy = GuardedMemory(strategy="M3G")
93
+ prompt = strategy.render(problem)
94
+ # Returns plain M0-style prose, not empty scaffold
95
+ ```
96
+
97
+ ### Best-of-N with Constraint Solver
98
+
99
+ ```python
100
+ from meme_core.solver_bon import best_of_n, ConstraintSolver, Problem
101
+
102
+ problem = Problem(
103
+ id=1,
104
+ question="Who is the engineer?",
105
+ expected="lawyer",
106
+ aliases=["advocate", "attorney"] # Acceptable variants
107
+ )
108
+
109
+ # Candidate answers from N model samples at temperature 0.7
110
+ answers = ["doctor", "lawyer", "engineer", "lawyer"]
111
+
112
+ # Select best using ground-truth constraint solver
113
+ winner, scores = best_of_n(answers, problem, use_solver=True)
114
+
115
+ print(f"Winner: {winner}") # "lawyer"
116
+ print(f"Scores: {[s.confidence for s in scores]}") # [0, 100, 0, 100]
117
+ ```
118
+
119
+ ## Memory Strategies
120
+
121
+ | Strategy | Description | Use Case |
122
+ |----------|-------------|----------|
123
+ | `M0` | Raw prose question (baseline) | Control / pure retrieval |
124
+ | `M1` | Full context + enumerate constraints | General purpose |
125
+ | `M2` | Structured bullet constraints | Compression |
126
+ | `M3` | Entity-grouped facts (auto-extracted) | Entity-centric reasoning |
127
+ | `M4` | Knowledge-graph triples | Relationship reasoning |
128
+ | `M2G/M3G/M4G` | Guarded + block-authoritative | Constraint tasks (recommended) |
129
+ | `M2Q/M3Q/M4Q` | Guarded + question-authoritative | Retrieval tasks (recommended) |
130
+
131
+ ## The `structure_kind` Field
132
+
133
+ Problems declare what auxiliary material they provide, which determines guard behavior:
134
+
135
+ | structure_kind | Example Task | Guard Behavior |
136
+ |----------------|--------------|----------------|
137
+ | `constraints` | ZebraLogic | Organize constraints into structured view |
138
+ | `quantities` | GSM8K-Hard | Organize numeric givens |
139
+ | `options` | ARC | Organize answer choices |
140
+ | `facts` | HotpotQA | Organize facts for chaining |
141
+ | `none` | TriviaQA, HumanEval | **Fall back to prose** (no scaffold) |
142
+
143
+ ## Architecture
144
+
145
+ ```
146
+ meme_core/
147
+ ├── __init__.py # Package entry point
148
+ ├── guard/
149
+ │ ├── __init__.py # Guard module exports
150
+ │ ├── core.py # GuardedMemory class, render_prompt, Problem
151
+ │ └── templates.py # Prompt templates
152
+ └── solver_bon/
153
+ ├── __init__.py # Solver BoN exports
154
+ └── core.py # ConstraintSolver, best_of_n, normalize_for_comparison
155
+ ```
156
+
157
+ ## Example: gemma4:26b Performance Improvement
158
+
159
+ A real example showing how meme-core's `solver_bon` improves gemma4:26b accuracy:
160
+
161
+ ```bash
162
+ python3 examples/gemma4_26b_improvement.py
163
+ ```
164
+
165
+ **Output:**
166
+
167
+ ```
168
+ 📊 gemma4:26b Accuracy Comparison: Greedy vs. Best-of-N
169
+ ----------------------------------------------------------------------
170
+ Task Greedy BoN Δ Notes
171
+ ----------------------------------------------------------------------
172
+ ZebraLogic (hard) 70% 90% +20pp 🎯 (major gain!)
173
+ HumanEval 90% 100% +10pp 🎯
174
+ GSM8K-Hard 90% 90% 0pp
175
+ ARC 100% 100% 0pp (saturated)
176
+ ----------------------------------------------------------------------
177
+ Average 87.5% 95.0% +7.5pp
178
+ ```
179
+
180
+ **Key insight:** Best-of-N without a verifier degenerates to random sampling. Using a constraint solver as the reward model makes answer selection deterministic and maximally effective.
181
+
182
+ ## Key Results (from MEME project)
183
+
184
+ ### Guard removes retrieval penalty completely
185
+
186
+ | Task | M0 | M3 (unguarded) | M3G | M3Q |
187
+ |------|-----|-----------------|-----|-----|
188
+ | TriviaQA | 100% | 60% | **100%** | **100%** |
189
+ | HotpotQA | 90% | 60% | **90%** | **90%** |
190
+ | GSM8K-Hard | 70% | 70% | **80%** | 70% |
191
+ | ARC | 90% | 100% | 90% | **100%** |
192
+
193
+ Cross-vendor验证: llama3.2:3b on TriviaQA 40% → **90%**; phi3.5 70% → **100%**
194
+
195
+ ### Guarded strategies are robust across model families
196
+
197
+ | Model | TriviaQA (M3→M3G) | HotpotQA (M3→M3G) |
198
+ |-------|---------------------|---------------------|
199
+ | qwen3:1.7b | +40pp | +30pp |
200
+ | qwen3:8b | +30pp | +30pp |
201
+ | llama3.2:3b | +50pp | — |
202
+ | phi3.5 | +30pp | — |
203
+
204
+ ## Citation
205
+
206
+ If you use meme-core in your research, please cite:
207
+
208
+ ```bibtex
209
+ @misc{meme2026,
210
+ title = {Memory is the Cheap Axis: Pareto Analysis of Test-Time Memory
211
+ and Compute for Small Language Models},
212
+ author = {Abel},
213
+ year = {2026},
214
+ note = {Preprint, https://github.com/abel/meme-llm}
215
+ }
216
+ ```
217
+
218
+ ## References
219
+
220
+ - [MEME Project (meme-llm)](https://github.com/abel/meme-llm) — Full research project with data, code, and results
221
+ - [Paper (arXiv)](https://arxiv.org/abs/XXXX.XXXXX) — Full methodology and results (TODO: update with actual link)
222
+
223
+ ## License
224
+
225
+ Apache 2.0 — see [LICENSE](LICENSE) for details.