jgalego commited on
Commit
6e3660f
·
verified ·
1 Parent(s): c70e94d

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +34 -10
  2. example.py +34 -0
README.md CHANGED
@@ -63,23 +63,47 @@ A run, from an ELF to the HALT token:
63
 
64
  ## Usage
65
 
 
 
66
  ```bash
67
- pip install "chipformer @ https://github.com/JGalego/olmputer/archive/defd633c754fedd4c4ab7ff1cd9d109a9a5effcf.tar.gz#subdirectory=experimental/riscv" torch==2.14.1 transformers==5.19.0
68
  ```
69
 
70
  ```python
71
- import torch
72
- from transformers import OlmoHybridForCausalLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
 
74
  from chipformer.q35.ir import OLMO
75
- from chipformer.rv import core, firmware, ref
76
  from chipformer.rv.programs import program
 
77
 
78
- c = core.build(OLMO) # the circuit: the program's snapshot is written into the model's cache
79
- model = OlmoHybridForCausalLM.from_pretrained("jgalego/olmputer-tiny", dtype=torch.bfloat16).cuda()
80
-
81
- trace = firmware.run(model, c, ref.Machine.from_program(program("fib")))
82
- print(trace == ref.Machine.from_program(program("fib")).run()[1:])
 
 
 
 
 
 
83
  ```
84
 
85
- `firmware.run` loads the program's ROM, RAM and registers into the linear-attention layers' recurrent states after a one-token prefix, starts the CPU with GO and decodes greedily until HALT.
 
63
 
64
  ## Usage
65
 
66
+ [`example.py`](example.py) runs fibonacci(25) on olmo-tiny: greedy `generate` runs the CPU, and the result is read from the trace the model wrote. It is a [uv script](https://docs.astral.sh/uv/guides/scripts/) that declares its own dependencies:
67
+
68
  ```bash
69
+ uv run https://huggingface.co/jgalego/olmputer-tiny/resolve/main/example.py
70
  ```
71
 
72
  ```python
73
+ # /// script
74
+ # requires-python = ">=3.12"
75
+ # dependencies = [
76
+ # "chipformer @ https://github.com/JGalego/olmputer/archive/98639dff4c84a18542e3ca12a286a4df563b3a27.tar.gz#subdirectory=experimental/riscv", # pylint: disable=line-too-long
77
+ # "huggingface_hub",
78
+ # "torch==2.14.1",
79
+ # "transformers==5.19.0",
80
+ # ]
81
+ # ///
82
+ """Run a RISC-V program on olmo-tiny: fibonacci(25), computed by the computer in its weights.
83
+
84
+ The program's code and registers are written into the model's Gated DeltaNet states; greedy
85
+ generate then runs the CPU, every generated token being the next token of its execution trace.
86
+ The program ends with mv a0, t0 (fibonacci(25)), li a7, 93 and the exit ecall, so the result is
87
+ the value of the second-to-last value record the model wrote.
88
+ """
89
 
90
+ import torch
91
  from chipformer.q35.ir import OLMO
92
+ from chipformer.rv import core, firmware, ref, spec
93
  from chipformer.rv.programs import program
94
+ from transformers import OlmoHybridForCausalLM
95
 
96
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
97
+ MODEL = OlmoHybridForCausalLM.from_pretrained("jgalego/olmputer-tiny", dtype=torch.bfloat16)
98
+ MODEL = MODEL.to(DEVICE).eval()
99
+ CIRCUIT = core.build(OLMO) # the CPU as a circuit: it writes the program into the model's cache
100
+
101
+ TRACE = firmware.run(MODEL, CIRCUIT, ref.Machine.from_program(program("fib")))
102
+ VALUES = [spec.decode_value(TRACE[:i + 1]) for i, t in enumerate(TRACE)
103
+ if spec.family(t)[0] in ("F0", "F1", "F2")]
104
+ print("fibonacci(25) =", VALUES[-2])
105
+ print(f"{len(TRACE)} trace tokens on {DEVICE}, identical to the reference machine:",
106
+ TRACE == ref.Machine.from_program(program("fib")).run()[1:])
107
  ```
108
 
109
+ A GPU takes under a minute; a CPU works too, more slowly. `firmware.run` loads the program's ROM, RAM and registers into the linear-attention layers' recurrent states after a one-token prefix, starts the CPU with GO and decodes greedily until HALT.
example.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.12"
3
+ # dependencies = [
4
+ # "chipformer @ https://github.com/JGalego/olmputer/archive/98639dff4c84a18542e3ca12a286a4df563b3a27.tar.gz#subdirectory=experimental/riscv", # pylint: disable=line-too-long
5
+ # "huggingface_hub",
6
+ # "torch==2.14.1",
7
+ # "transformers==5.19.0",
8
+ # ]
9
+ # ///
10
+ """Run a RISC-V program on olmo-tiny: fibonacci(25), computed by the computer in its weights.
11
+
12
+ The program's code and registers are written into the model's Gated DeltaNet states; greedy
13
+ generate then runs the CPU, every generated token being the next token of its execution trace.
14
+ The program ends with mv a0, t0 (fibonacci(25)), li a7, 93 and the exit ecall, so the result is
15
+ the value of the second-to-last value record the model wrote.
16
+ """
17
+
18
+ import torch
19
+ from chipformer.q35.ir import OLMO
20
+ from chipformer.rv import core, firmware, ref, spec
21
+ from chipformer.rv.programs import program
22
+ from transformers import OlmoHybridForCausalLM
23
+
24
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
25
+ MODEL = OlmoHybridForCausalLM.from_pretrained("jgalego/olmputer-tiny", dtype=torch.bfloat16)
26
+ MODEL = MODEL.to(DEVICE).eval()
27
+ CIRCUIT = core.build(OLMO) # the CPU as a circuit: it writes the program into the model's cache
28
+
29
+ TRACE = firmware.run(MODEL, CIRCUIT, ref.Machine.from_program(program("fib")))
30
+ VALUES = [spec.decode_value(TRACE[:i + 1]) for i, t in enumerate(TRACE)
31
+ if spec.family(t)[0] in ("F0", "F1", "F2")]
32
+ print("fibonacci(25) =", VALUES[-2])
33
+ print(f"{len(TRACE)} trace tokens on {DEVICE}, identical to the reference machine:",
34
+ TRACE == ref.Machine.from_program(program("fib")).run()[1:])