dappalumbo91 commited on
Commit
3b505dc
·
verified ·
1 Parent(s): 280bb95

Powers from the written product

Browse files

Squares 44/44. Higher exponents 45/45. Divide-back 44/44. 2^13 is 8192. 12^3 is 1728. 37^2 is 1369.

Files changed (2) hide show
  1. README.md +6 -0
  2. fluid.py +145 -0
README.md CHANGED
@@ -43,6 +43,12 @@ Far products were 225/225. The written product plus 1 was 225/225. The written p
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
 
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
+ ## Powers
47
+
48
+ `python -u fluid.py power` on an NVIDIA GeForce RTX 5070, 2026-11-10. A power multiplies the written product by the base again. Bases 2 through 45. A product that would not fit in four places is not attempted. The last written power is divided by the base, and that quotient is the previous line.
49
+
50
+ Squares were 44/44. Higher exponents were 45/45. Divide-back was 44/44. `37^2` is 1369, and `1369/37` is 37. `12^2` is 144. `12^3` wrote `728+1000=1728`, and `1728/12` is 144. `2^13` is 8192, and `8192/2` is 4096.
51
+
52
  ## Load
53
 
54
  ```python
fluid.py CHANGED
@@ -23,8 +23,14 @@ 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
@@ -408,6 +414,143 @@ def _report_thought() -> None:
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)
@@ -537,5 +680,7 @@ def main() -> None:
537
  if __name__ == "__main__":
538
  if len(sys.argv) > 1 and sys.argv[1] == "think":
539
  _report_thought()
 
 
540
  else:
541
  main()
 
23
  a later right operand. The command scores far products. It does not train
24
  and it does not write a checkpoint.
25
 
26
+ ``power`` multiplies that written product by the base again. A result that
27
+ would not fit in four places is not attempted. The last written power is
28
+ then divided by the base, and that quotient has to be the previous line.
29
+ The command does not train and it does not write a checkpoint.
30
+
31
  python -u fluid.py
32
  python -u fluid.py think
33
+ python -u fluid.py power
34
  """
35
 
36
  from __future__ import annotations
 
414
  print(f"thought product/right {div_hits}/{len(rows)}", flush=True)
415
 
416
 
417
+ def _multiply_written(
418
+ model: FluidForCausalLM,
419
+ lefts: list[list[int]],
420
+ rights: list[list[int]],
421
+ device: torch.device,
422
+ probe: set[tuple[int, int]] | None = None,
423
+ ) -> list[list[int]]:
424
+ """Multiply written answers. The next addition reads the tokens just written."""
425
+ count = len(lefts)
426
+ left_digits = [place._digits_of(tokens) for tokens in lefts]
427
+ right_digits = [place._digits_of(tokens) for tokens in rights]
428
+ partials: list[list[list[int]]] = [[] for _ in range(count)]
429
+ watched = probe or set()
430
+ for i in range(place.PLACES):
431
+ for j in range(place.PLACES):
432
+ active = [
433
+ row
434
+ for row in range(count)
435
+ if left_digits[row][i] != 0 and right_digits[row][j] != 0
436
+ ]
437
+ if not active:
438
+ continue
439
+ print(f"power mul places {i}+{j} rows {len(active)}", flush=True)
440
+ totals = [scratch._number(0) for _ in active]
441
+ addends = [scratch._number(left_digits[row][i]) for row in active]
442
+ remaining = [right_digits[row][j] for row in active]
443
+ while any(item > 0 for item in remaining):
444
+ todo = [index for index, item in enumerate(remaining) if item > 0]
445
+ before = [totals[index] for index in todo]
446
+ step_rights = [addends[index] for index in todo]
447
+ wrote = _write_lines(model, before, step_rights, scratch.OP_ADD, device)
448
+ for index, answer, left_tokens, right_tokens in zip(todo, wrote, before, step_rights):
449
+ pair = (scratch._read_number(lefts[active[index]]), scratch._read_number(rights[active[index]]))
450
+ if pair in watched:
451
+ print(
452
+ f" {scratch._read_number(left_tokens)}+{scratch._read_number(right_tokens)}={scratch._read_number(answer)}",
453
+ flush=True,
454
+ )
455
+ totals[index] = answer
456
+ remaining[index] -= 1
457
+ for index, row in enumerate(active):
458
+ shifted = place._answer(place._shift_places(place._digits_of(totals[index]), i + j))
459
+ partials[row].append(shifted)
460
+ pair = (scratch._read_number(lefts[row]), scratch._read_number(rights[row]))
461
+ if pair in watched:
462
+ print(
463
+ f" place {i}+{j} -> {scratch._read_number(shifted)} tokens {shifted}",
464
+ flush=True,
465
+ )
466
+ running = [scratch._number(0) for _ in range(count)]
467
+ width = max((len(row) for row in partials), default=0)
468
+ for index in range(width):
469
+ todo = [row for row in range(count) if index < len(partials[row])]
470
+ before = [running[row] for row in todo]
471
+ step_rights = [partials[row][index] for row in todo]
472
+ wrote = _write_lines(model, before, step_rights, scratch.OP_ADD, device)
473
+ for row, answer, left_tokens, right_tokens in zip(todo, wrote, before, step_rights):
474
+ pair = (scratch._read_number(lefts[row]), scratch._read_number(rights[row]))
475
+ if pair in watched:
476
+ print(
477
+ f" use {left_tokens}+{right_tokens}={answer} {scratch._read_number(left_tokens)}+{scratch._read_number(right_tokens)}={scratch._read_number(answer)}",
478
+ flush=True,
479
+ )
480
+ running[row] = answer
481
+ return running
482
+
483
+
484
+ def _report_power() -> None:
485
+ """Score powers built by multiplying the written line by the base again."""
486
+ if not torch.cuda.is_available():
487
+ raise SystemExit("CUDA is not available. Refusing to build the model in system RAM.")
488
+ device = torch.device("cuda")
489
+ print(torch.cuda.get_device_name(0), flush=True)
490
+ model = _load(device)
491
+ bases = list(range(2, scratch.FAR_HI + 1))
492
+ limit = 10 ** scratch.WIDTH - 1
493
+ base_tokens = [scratch._number(base) for base in bases]
494
+ running = [list(tokens) for tokens in base_tokens]
495
+ previous = [list(tokens) for tokens in base_tokens]
496
+ square_hits = square_rows = 0
497
+ higher_hits = higher_rows = 0
498
+ shown = {2, 12, 37}
499
+ probe = {(12, 12), (144, 12)}
500
+ exponent = 1
501
+ while True:
502
+ todo = []
503
+ for index, base in enumerate(bases):
504
+ current = scratch._read_number(running[index])
505
+ if current is None or current < 0 or current * base > limit:
506
+ continue
507
+ todo.append(index)
508
+ if not todo:
509
+ break
510
+ exponent += 1
511
+ print(f"power exp {exponent} rows {len(todo)}", flush=True)
512
+ wrote = _multiply_written(
513
+ model,
514
+ [running[index] for index in todo],
515
+ [base_tokens[index] for index in todo],
516
+ device,
517
+ probe,
518
+ )
519
+ for index, tokens in zip(todo, wrote):
520
+ previous[index] = running[index]
521
+ running[index] = tokens
522
+ got = scratch._read_number(tokens)
523
+ expect = bases[index] ** exponent
524
+ if exponent == 2:
525
+ square_rows += 1
526
+ square_hits += int(got == expect)
527
+ else:
528
+ higher_rows += 1
529
+ higher_hits += int(got == expect)
530
+ if got != expect:
531
+ print(f"miss ^{exponent} {bases[index]} truth {expect} pred {got}", flush=True)
532
+ if bases[index] in shown:
533
+ print(f"power {bases[index]}^{exponent} = {got} tokens {tokens}", flush=True)
534
+ print(f"power squares {square_hits}/{square_rows}", flush=True)
535
+ print(f"power higher {higher_hits}/{higher_rows}", flush=True)
536
+ print("power divide-back", flush=True)
537
+ quotients = _write_lines(model, running, base_tokens, scratch.OP_DIV, device)
538
+ back_hits = 0
539
+ for index, tokens in enumerate(quotients):
540
+ got = scratch._read_number(tokens)
541
+ expect = scratch._read_number(previous[index])
542
+ if got == expect:
543
+ back_hits += 1
544
+ else:
545
+ print(f"miss /base {bases[index]} truth {expect} pred {got}", flush=True)
546
+ if bases[index] in shown:
547
+ print(
548
+ f"power {scratch._read_number(running[index])}/{bases[index]} = {got}",
549
+ flush=True,
550
+ )
551
+ print(f"power divide-back {back_hits}/{len(bases)}", flush=True)
552
+
553
+
554
  def _chain(model: FluidForCausalLM, rows: list[tuple[int, int, int, int]], extra, device: torch.device) -> tuple[int, int]:
555
  prompts = _prompts(rows, device)
556
  first = _generate_answer(model, prompts)
 
680
  if __name__ == "__main__":
681
  if len(sys.argv) > 1 and sys.argv[1] == "think":
682
  _report_thought()
683
+ elif len(sys.argv) > 1 and sys.argv[1] == "power":
684
+ _report_power()
685
  else:
686
  main()