coderofpears commited on
Commit
37a7c5c
·
verified ·
1 Parent(s): 6f1eb44

Upload prep.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. prep.py +102 -10
prep.py CHANGED
@@ -78,13 +78,16 @@ FILE_Q = [
78
  "Read the file {path} and tell me what is on the first line.",
79
  "What is inside {path}?", "List the files in {dir}.",
80
  ]
81
- SYSTEM = ("You are clanker, a helpful assistant that can THINK and USE TOOLS. "
82
- "When you need to compute or inspect something, wrap a tool call in "
83
- "<tool name=\"...\">arguments</tool>. Available tools: calc(expr), "
84
- "python(code), read_file(path), list_dir(path), retrieve(query). After "
85
- "a tool result appears in <result>...</result>, continue and give the "
86
- "final answer. If <context>...</context> is provided, use it. You may "
87
- "use <think>...</think> to reason first.")
 
 
 
88
 
89
 
90
  def gen_synthetic(n=60000):
@@ -135,6 +138,85 @@ def gen_synthetic(n=60000):
135
  return out
136
 
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # --------------------------------------------------------------------------
139
  # 3) RAG / retrieval examples (teach <tool name="retrieve"> and <context>)
140
  # --------------------------------------------------------------------------
@@ -149,7 +231,8 @@ def gen_rag(n=40000):
149
  f"<result>{doc}</result>{a}</assistant><eos>")
150
  elif mode < 0.85:
151
  conv = (f"<bos><system>{RAG_SYSTEM}</system><user>{q}</user>"
152
- f"<assistant><context>{doc}</context>{a}</assistant><eos>")
 
153
  else:
154
  conv = (f"<bos><system>{RAG_SYSTEM}</system><user>{q}</user>"
155
  f"<assistant><think>{doc}</think>{a}</assistant><eos>")
@@ -230,6 +313,7 @@ def main():
230
  ap.add_argument("--no-wiki", action="store_true")
231
  ap.add_argument("--no-rag", action="store_true")
232
  ap.add_argument("--no-glaive", action="store_true")
 
233
  args = ap.parse_args()
234
 
235
  out_dir = args.out_dir
@@ -292,11 +376,19 @@ def main():
292
  except Exception as e:
293
  print(f"[prep] wikipedia skipped: {e}")
294
 
295
- # --- synthetic tool data ---
296
  synth = gen_synthetic(int(60_000 * scale) + 60000)
297
- n2 = pack(tok, synth, bin_path, bw_tool, SEQ_LEN)
 
298
  print(f"[prep] synthetic tool packed: {n2:,} tokens")
299
 
 
 
 
 
 
 
 
300
  # --- RAG / retrieval data ---
301
  if not args.no_rag:
302
  rag = gen_rag(int(40_000 * scale) + 40000)
 
78
  "Read the file {path} and tell me what is on the first line.",
79
  "What is inside {path}?", "List the files in {dir}.",
80
  ]
81
+ SYSTEM = ("You are clanker, a helpful assistant that can THINK, USE TOOLS, and "
82
+ "USE MEMORY. You may reason in <think>...</think> at any point, "
83
+ "interleaved with actions. Wrap tool calls in "
84
+ "<tool name=\"...\">arguments</tool>. Available tools: calc(expr), "
85
+ "python(code), read_file(path), list_dir(path), retrieve(query). After "
86
+ "a tool result appears in <result>...</result>, continue and give the "
87
+ "final answer. If <context>...</context> is provided, use it. You keep "
88
+ "facts in a secondary memory: <mem_write>KEY<mem_kv>VALUE</mem_kv> to "
89
+ "store, <mem_read>KEY</mem_read> to recall (result returns inside "
90
+ "<mem_kv>...</mem_kv>), and <mem_evict>KEY</mem_evict> to forget.")
91
 
92
 
93
  def gen_synthetic(n=60000):
 
138
  return out
139
 
140
 
141
+ # --------------------------------------------------------------------------
142
+ # 2b) Interleaved-thinking + learned-memory examples.
143
+ # Teaches the model to reason step-by-step *between* tool calls and to
144
+ # persist/recall facts via <mem_write>/<mem_read>/<mem_evict> against the
145
+ # side store (see memstore.py). Thinking is INTERLEAVED: think, act,
146
+ # think, answer -- not one big block at the start.
147
+ # --------------------------------------------------------------------------
148
+ MEM_FACTS = [
149
+ ("project:clanker", "clanker is a hybrid AR/diffusion LM with learned memory."),
150
+ ("user:name", "The user's name is Ada."),
151
+ ("user:likes", "The user likes concise answers and tool use."),
152
+ ("fact:pi", "pi is approximately 3.14159."),
153
+ ("fact:capitals", "The capital of France is Paris; of Japan is Tokyo."),
154
+ ("pref:format", "Prefer <think> reasoning before tool calls."),
155
+ ]
156
+ MEM_KEYS = [k for k, _ in MEM_FACTS]
157
+
158
+
159
+ def gen_memory(n=30000):
160
+ out = []
161
+ for _ in range(n):
162
+ key, val = random.choice(MEM_FACTS)
163
+ mode = random.random()
164
+ if mode < 0.4:
165
+ # write then read back (persistence demo)
166
+ conv = (f"<bos><system>{SYSTEM}</system>"
167
+ f"<user>Remember that {val}</user>"
168
+ f"<assistant><think>I should store this in secondary memory "
169
+ f"so I can recall it later.</think>"
170
+ f"<mem_write>{key}<mem_kv>{val}</mem_kv>"
171
+ f"<think>Stored. Now I can read it back to confirm.</think>"
172
+ f"<mem_read>{key}</mem_read><mem_kv>{val}</mem_kv>"
173
+ f"Got it -- I'll remember {val}</assistant><eos>")
174
+ elif mode < 0.75:
175
+ # read an existing fact, interleaved with reasoning
176
+ conv = (f"<bos><system>{SYSTEM}</system>"
177
+ f"<user>What do you know about {key}?</user>"
178
+ f"<assistant><think>Let me pull this from secondary memory.</think>"
179
+ f"<mem_read>{key}</mem_read><mem_kv>{val}</mem_kv>"
180
+ f"<think>That matches what I stored.</think> "
181
+ f"Based on memory: {val}</assistant><eos>")
182
+ else:
183
+ # evict
184
+ conv = (f"<bos><system>{SYSTEM}</system>"
185
+ f"<user>Forget {key}.</user>"
186
+ f"<assistant><think>I'll remove it from secondary memory.</think>"
187
+ f"<mem_evict>{key}</mem_evict>Done, I forgot {key}.</assistant><eos>")
188
+ out.append(conv)
189
+ return out
190
+
191
+
192
+ # Multi-step reasoning with INTERLEAVED think/act/think/answer.
193
+ REASON_QA = [
194
+ ("A train travels 60 km/h for 2 hours, then 90 km/h for 1 hour. Total distance?",
195
+ "60*2 + 90*1 = 120 + 90 = 210 km"),
196
+ ("If I buy 3 items at $4.50 each and a $2 tax, total cost?",
197
+ "3*4.50 = 13.50; +2 = 15.50"),
198
+ ("A rectangle is 8 by 5. Area and perimeter?",
199
+ "area 8*5=40; perimeter 2*(8+5)=26"),
200
+ ("Compound 5% on $1000 for 2 years?",
201
+ "1000*1.05^2 = 1102.50"),
202
+ ("Mix 2L at 10C with 3L at 40C, final temp?",
203
+ "(2*10+3*40)/5 = 140/5 = 28C"),
204
+ ]
205
+ def gen_interleaved(n=30000):
206
+ out = []
207
+ for _ in range(n):
208
+ q, ans = random.choice(REASON_QA)
209
+ conv = (f"<bos><system>{SYSTEM}</system>"
210
+ f"<user>{q}</user>"
211
+ f"<assistant><think>Break it into parts.</think>"
212
+ f"<tool name=\"calc\">{ans.split('=')[0].strip()}</tool>"
213
+ f"<result>{eval(ans.split('=')[0].strip())}</result>"
214
+ f"<think>That gives the first part; combine with the rest.</think> "
215
+ f"The answer is {ans}.</assistant><eos>")
216
+ out.append(conv)
217
+ return out
218
+
219
+
220
  # --------------------------------------------------------------------------
221
  # 3) RAG / retrieval examples (teach <tool name="retrieve"> and <context>)
222
  # --------------------------------------------------------------------------
 
231
  f"<result>{doc}</result>{a}</assistant><eos>")
232
  elif mode < 0.85:
233
  conv = (f"<bos><system>{RAG_SYSTEM}</system><user>{q}</user>"
234
+ f"<assistant><think>Let me check the provided context.</think>"
235
+ f"<context>{doc}</context>{a}</assistant><eos>")
236
  else:
237
  conv = (f"<bos><system>{RAG_SYSTEM}</system><user>{q}</user>"
238
  f"<assistant><think>{doc}</think>{a}</assistant><eos>")
 
313
  ap.add_argument("--no-wiki", action="store_true")
314
  ap.add_argument("--no-rag", action="store_true")
315
  ap.add_argument("--no-glaive", action="store_true")
316
+ ap.add_argument("--no-mem", action="store_true")
317
  args = ap.parse_args()
318
 
319
  out_dir = args.out_dir
 
376
  except Exception as e:
377
  print(f"[prep] wikipedia skipped: {e}")
378
 
379
+ # --- synthetic tool data (incl. interleaved reasoning) ---
380
  synth = gen_synthetic(int(60_000 * scale) + 60000)
381
+ inter = gen_interleaved(int(30_000 * scale) + 30000)
382
+ n2 = pack(tok, synth + inter, bin_path, bw_tool, SEQ_LEN)
383
  print(f"[prep] synthetic tool packed: {n2:,} tokens")
384
 
385
+ # --- learned-memory data ---
386
+ if not args.no_mem:
387
+ mem = gen_memory(int(30_000 * scale) + 30000)
388
+ nm = pack(tok, mem, bin_path, int(bw_tool * 0.6), SEQ_LEN)
389
+ print(f"[prep] memory packed: {nm:,} tokens")
390
+ n2 += nm
391
+
392
  # --- RAG / retrieval data ---
393
  if not args.no_rag:
394
  rag = gen_rag(int(40_000 * scale) + 40000)