dappalumbo91 commited on
Commit
280bb95
·
verified ·
1 Parent(s): 87f7afa

Written product steps through generate

Browse files

Far products 225/225. Written product plus 1 is 225/225. Written product divided by the right factor is 225/225. 37*37 ends 469+900=1369.

Files changed (2) hide show
  1. README.md +8 -0
  2. fluid.py +164 -1
README.md CHANGED
@@ -35,6 +35,14 @@ The pass counter printed `passes kept 725 rewritten 415`. Those two counts are t
35
 
36
  These far counts are the two-pass generate. They are not a claim that the learned sweep, scored on its own, writes every far row.
37
 
 
 
 
 
 
 
 
 
38
  ## Load
39
 
40
  ```python
 
35
 
36
  These far counts are the two-pass generate. They are not a claim that the learned sweep, scored on its own, writes every far row.
37
 
38
+ ## Written steps
39
+
40
+ `python -u fluid.py think` on an NVIDIA GeForce RTX 5070, 2026-11-10. Each addition inside a product is one `generate` call. The next prompt starts with the tokens that call just wrote. A place shift is applied to those tokens, and the shifted tokens are a later right operand.
41
+
42
+ Far products were 225/225. The written product plus 1 was 225/225. The written product divided by the right factor was 225/225.
43
+
44
+ `37*37` wrote `42+7=49`, then place `0+0` as 49. The partial sums were `0+49=49`, `49+210=259`, `259+210=469`, and `469+900=1369`. The `49` on the next line is the token list `[2, 20, 15, 11, 11]` that the previous line wrote. The written 1369 plus 1 is 1370. The written 1369 divided by 37 is 37.
45
+
46
  ## Load
47
 
48
  ```python
fluid.py CHANGED
@@ -17,12 +17,20 @@ The first pass is the learned place forward. When its tokens disagree with
17
  the place-value chain, a second pass writes the chain's tokens. The chain
18
  is the offset already in the model. Weights load from ``weights/place/factor.pt``.
19
 
 
 
 
 
 
 
20
  python -u fluid.py
 
21
  """
22
 
23
  from __future__ import annotations
24
 
25
  import shutil
 
26
 
27
  import torch
28
  import torch.nn.functional as F
@@ -248,6 +256,158 @@ def _generate_answer(model: FluidForCausalLM, prompts: torch.Tensor) -> torch.Te
248
  return drafted[:, prompts.shape[1] :]
249
 
250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  def _chain(model: FluidForCausalLM, rows: list[tuple[int, int, int, int]], extra, device: torch.device) -> tuple[int, int]:
252
  prompts = _prompts(rows, device)
253
  first = _generate_answer(model, prompts)
@@ -375,4 +535,7 @@ def main() -> None:
375
 
376
 
377
  if __name__ == "__main__":
378
- main()
 
 
 
 
17
  the place-value chain, a second pass writes the chain's tokens. The chain
18
  is the offset already in the model. Weights load from ``weights/place/factor.pt``.
19
 
20
+ ``think`` writes a product as one addition at a time. Each line is a
21
+ ``generate`` call. The next line's left side is the tokens that call just
22
+ wrote. A place shift is applied to those tokens, and the shifted tokens are
23
+ a later right operand. The command scores far products. It does not train
24
+ and it does not write a checkpoint.
25
+
26
  python -u fluid.py
27
+ python -u fluid.py think
28
  """
29
 
30
  from __future__ import annotations
31
 
32
  import shutil
33
+ import sys
34
 
35
  import torch
36
  import torch.nn.functional as F
 
256
  return drafted[:, prompts.shape[1] :]
257
 
258
 
259
+ def _line_prompts(
260
+ lefts: list[list[int]],
261
+ rights: list[list[int]],
262
+ op: int,
263
+ device: torch.device,
264
+ ) -> torch.Tensor:
265
+ """One equation prompt. The left side is an answer already written."""
266
+ if len(lefts) != len(rights) or not lefts:
267
+ raise RuntimeError("thought lines are empty")
268
+ rows = []
269
+ for left, right in zip(lefts, rights):
270
+ if len(left) != ANSWER_LEN or len(right) != ANSWER_LEN:
271
+ raise RuntimeError("thought side is not an answer")
272
+ rows.append(left + [op] + right + [EQ])
273
+ return torch.tensor(rows, dtype=torch.long, device=device)
274
+
275
+
276
+ def _write_lines(
277
+ model: FluidForCausalLM,
278
+ lefts: list[list[int]],
279
+ rights: list[list[int]],
280
+ op: int,
281
+ device: torch.device,
282
+ ) -> list[list[int]]:
283
+ """Generate one line. The prompt's left tokens are the previous answer."""
284
+ prompts = _line_prompts(lefts, rights, op, device)
285
+ if prompts[:, :ANSWER_LEN].tolist() != lefts:
286
+ raise RuntimeError("prompt does not start with the written answer")
287
+ if prompts[:, 6 : 6 + ANSWER_LEN].tolist() != rights:
288
+ raise RuntimeError("prompt right side drifted")
289
+ return _generate_answer(model, prompts).tolist()
290
+
291
+
292
+ def _thought_products(
293
+ model: FluidForCausalLM,
294
+ pairs: list[tuple[int, int]],
295
+ device: torch.device,
296
+ ) -> list[list[int]]:
297
+ """Multiply by writing each addition and reading those tokens back."""
298
+ count = len(pairs)
299
+ left_digits = [place._operand_digits(left) for left, _right in pairs]
300
+ right_digits = [place._operand_digits(right) for _left, right in pairs]
301
+ partials: list[list[list[int]]] = [[] for _ in range(count)]
302
+ probe = (37, 37)
303
+ for i in range(place.PLACES):
304
+ for j in range(place.PLACES):
305
+ active = [
306
+ row
307
+ for row in range(count)
308
+ if left_digits[row][i] != 0 and right_digits[row][j] != 0
309
+ ]
310
+ if not active:
311
+ continue
312
+ totals = [scratch._number(0) for _ in active]
313
+ addends = [scratch._number(left_digits[row][i]) for row in active]
314
+ remaining = [right_digits[row][j] for row in active]
315
+ step = 0
316
+ while any(item > 0 for item in remaining):
317
+ todo = [index for index, item in enumerate(remaining) if item > 0]
318
+ print(f"thought pair {i}+{j} step {step} rows {len(todo)}", flush=True)
319
+ before = [totals[index] for index in todo]
320
+ rights = [addends[index] for index in todo]
321
+ wrote = _write_lines(model, before, rights, scratch.OP_ADD, device)
322
+ for index, answer, left_tokens, right_tokens in zip(todo, wrote, before, rights):
323
+ if pairs[active[index]] == probe:
324
+ print(
325
+ f" {scratch._read_number(left_tokens)}+{scratch._read_number(right_tokens)}={scratch._read_number(answer)}",
326
+ flush=True,
327
+ )
328
+ totals[index] = answer
329
+ remaining[index] -= 1
330
+ step += 1
331
+ for index, row in enumerate(active):
332
+ shifted = place._answer(place._shift_places(place._digits_of(totals[index]), i + j))
333
+ partials[row].append(shifted)
334
+ if pairs[row] == probe:
335
+ print(
336
+ f" place {i}+{j} -> {scratch._read_number(shifted)} tokens {shifted}",
337
+ flush=True,
338
+ )
339
+ running = [scratch._number(0) for _ in range(count)]
340
+ width = max((len(row) for row in partials), default=0)
341
+ for index in range(width):
342
+ todo = [row for row in range(count) if index < len(partials[row])]
343
+ print(f"thought sum partial {index} rows {len(todo)}", flush=True)
344
+ before = [running[row] for row in todo]
345
+ rights = [partials[row][index] for row in todo]
346
+ wrote = _write_lines(model, before, rights, scratch.OP_ADD, device)
347
+ for row, answer, left_tokens, right_tokens in zip(todo, wrote, before, rights):
348
+ if pairs[row] == probe:
349
+ print(
350
+ f" use {left_tokens}+{right_tokens}={answer} {scratch._read_number(left_tokens)}+{scratch._read_number(right_tokens)}={scratch._read_number(answer)}",
351
+ flush=True,
352
+ )
353
+ running[row] = answer
354
+ return running
355
+
356
+
357
+ def _report_thought() -> None:
358
+ """Score far products whose steps are generate calls on the previous line."""
359
+ if not torch.cuda.is_available():
360
+ raise SystemExit("CUDA is not available. Refusing to build the model in system RAM.")
361
+ device = torch.device("cuda")
362
+ print(torch.cuda.get_device_name(0), flush=True)
363
+ model = _load(device)
364
+ rows = [row for row in scratch._banks()["far"] if row[0] == scratch.OP_MUL]
365
+ pairs = [(left, right) for _op, left, right, _value in rows]
366
+ written = _thought_products(model, pairs, device)
367
+ hits = 0
368
+ for row, tokens in zip(rows, written):
369
+ got = scratch._read_number(tokens)
370
+ if got == row[3]:
371
+ hits += 1
372
+ else:
373
+ print(f"miss * {row[1]} {row[2]} truth {row[3]} pred {got}", flush=True)
374
+ if (row[1], row[2]) == (37, 37):
375
+ print(f"thought 37*37 = {got} tokens {tokens}", flush=True)
376
+ print(f"thought far * {hits}/{len(rows)}", flush=True)
377
+ plus = _write_lines(model, written, [scratch._number(1) for _ in rows], scratch.OP_ADD, device)
378
+ plus_hits = 0
379
+ for row, tokens, base in zip(rows, plus, written):
380
+ if scratch._read_number(tokens) == row[3] + 1:
381
+ plus_hits += 1
382
+ else:
383
+ print(
384
+ f"miss *+1 {row[1]} {row[2]} truth {row[3] + 1} pred {scratch._read_number(tokens)}",
385
+ flush=True,
386
+ )
387
+ if (row[1], row[2]) == (37, 37):
388
+ print(
389
+ f"thought {scratch._read_number(base)}+1 = {scratch._read_number(tokens)}",
390
+ flush=True,
391
+ )
392
+ print(f"thought product+1 {plus_hits}/{len(rows)}", flush=True)
393
+ divided = _write_lines(
394
+ model,
395
+ written,
396
+ [scratch._number(right) for _op, _left, right, _value in rows],
397
+ scratch.OP_DIV,
398
+ device,
399
+ )
400
+ div_hits = 0
401
+ for row, tokens in zip(rows, divided):
402
+ if scratch._read_number(tokens) == row[1]:
403
+ div_hits += 1
404
+ else:
405
+ print(f"miss */right {row[1]} {row[2]} truth {row[1]} pred {scratch._read_number(tokens)}", flush=True)
406
+ if (row[1], row[2]) == (37, 37):
407
+ print(f"thought product/37 = {scratch._read_number(tokens)}", flush=True)
408
+ print(f"thought product/right {div_hits}/{len(rows)}", flush=True)
409
+
410
+
411
  def _chain(model: FluidForCausalLM, rows: list[tuple[int, int, int, int]], extra, device: torch.device) -> tuple[int, int]:
412
  prompts = _prompts(rows, device)
413
  first = _generate_answer(model, prompts)
 
535
 
536
 
537
  if __name__ == "__main__":
538
+ if len(sys.argv) > 1 and sys.argv[1] == "think":
539
+ _report_thought()
540
+ else:
541
+ main()