olmputer / example.py
jgalego's picture
Upload folder using huggingface_hub
fa0f2cf verified
Raw History Blame Contribute Delete
2.3 kB
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "chipformer @ https://github.com/JGalego/olmputer/archive/98639dff4c84a18542e3ca12a286a4df563b3a27.tar.gz#subdirectory=experimental/riscv", # pylint: disable=line-too-long
# "huggingface_hub",
# "torch==2.14.1",
# "transformers==5.19.0",
# ]
# ///
"""Ask Olmputer a question and print the conversation.
OLMo writes a calc call. At </function_calls> the chip layers run the calculator firmware and
write the environment turn with the exact value, and OLMo reads it and answers. The CPU's trace
tokens stay inside the chip layers, so this decodes with chipformer's RvChat rather than
generate(), on its Triton executor when there is a GPU. Every token the computer writes is checked
against a reference RISC-V emulator as it is generated, and the whole conversation is replayed
through the circuit at the end. The question is the first argument.
"""
import sys
import urllib.request
from pathlib import Path
import torch
from chipformer.rv import bridge, elf, ref
from chipformer.rv.chat import RvChat, replay
from huggingface_hub import snapshot_download
QUESTION = sys.argv[1] if len(sys.argv) > 1 else "Use calc: what is 123456789 * 987654321?"
FIRMWARE = Path("calc-olmo.elf")
if not FIRMWARE.exists():
urllib.request.urlretrieve("https://raw.githubusercontent.com/JGalego/olmputer/"
"98639dff4c84a18542e3ca12a286a4df563b3a27/experimental/riscv/"
"tests/data/rv/calc-olmo.elf", FIRMWARE)
BOT = RvChat(snapshot_download("jgalego/olmputer"), fast=False,
executor=torch.cuda.is_available())
IDS = BOT.prompt_ids([{"role": "user", "content": QUESTION}])
MACHINE, REFERENCE = (ref.Machine.from_program(elf.program(FIRMWARE)) for _ in range(2))
bridge.park(MACHINE) # the firmware waits for a call
bridge.park(REFERENCE)
LOCK = bridge.Session(BOT.T, REFERENCE)
LOCK.history(IDS)
RESULT = BOT.generate(IDS, MACHINE, 600, lock=LOCK)
print(BOT.text_of(RESULT["tokens"]))
print(f"\n{RESULT['text_tokens']} text tokens, {RESULT['chip_tokens']} trace tokens, "
f"{RESULT['seconds']:.1f} s; checked against the reference: {RESULT['lock_error'] is None}")
print("replayed through the circuit:", replay(BOT, IDS, RESULT["tokens"], FIRMWARE))