z80ai

HarryR更新放缓

把对话语言模型压到 2-bit 量化,小到能在 8 位 Z80 处理器上跑,用 Python 训练后导出成 CP/M 的 .COM 程序,让老电脑也能聊天。

Z80-μLM is a 2-bit quantized language model small enough to run on an 8-bit Z80 processor. Train conversational models in Python, export them as CP/M .COM binaries, and chat with your vintage computer.

v0.2发布于 2026-01-18 11:17 UTC1,136star48fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
iz-cpm CHAT.COM
硬件要求按 README 说明

目标为 64KB 内存的 Z80 处理器(如 4MHz 老机器),CP/M 版需装入 TPA;ZX Spectrum 版针对 48K 机型优化;README 未提及显存或 GPU 要求,训练侧硬件未说明。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

把 2-bit 量化语言模型塞进 8 位 Z80,用 Python 训练后导出 CP/M 可执行文件

把对话语言模型压到 2-bit 量化,小到能在 8 位 Z80 处理器上跑,用 Python 训练后导出成 CP/M 的 .COM 程序,让老电脑也能聊天。

核心能力

  • 用 trigram 哈希把输入映射到 128 个桶,容忍拼写错误和词序变化
  • 权重 2-bit 量化打包,每字节存 4 个权重
  • 全部用 Z80 原生 16 位整数运算,无浮点
  • 生成约 40KB 的 .COM 文件,含推理、权重和聊天界面
  • 支持 CP/M 与 ZX Spectrum 48K 两种目标平台

容易踩的坑

需先从 GitHub Releases 下载预编译二进制,或按 TRAINING.md 自行训练导出;运行前要另装模拟器(iz-cpm 或 Fuse);ZX Spectrum 版需先 LOAD "" CODE 再 RANDOMIZE USR 32768 启动。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

1 个版本v0.2
  1. v0.2最新正式版2026-01-18 · 2026-01-18 11:17 UTC

    What's Changed

    New Contributors

    Full Changelog: https://github.com/HarryR/z80ai/commits/v0.2

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Z80-μLM: A Retrocomputing Micro Language Model

    Z80-μLM is a 'conversational AI' that generates short character-by-character sequences, with quantization-aware training (QAT) to run on a Z80 processor with 64kb of ram.

    The root behind this project was the question: how small can we go while still having personality, and can it be trained or fine-tuned easily? With easy self-hosted distribution?

    The answer is Yes! And a 40kb .com binary (including inference, weights & a chat-style UI) running on a 4MHz processor from 1976.

    It won't pass the Turing test, but it might make you smile at the green screen.

    For insight on how to best train your own model, see TRAINING.md.

    Examples

    Two pre-built examples are included:

    tinychat

    A conversational chatbot trained on casual Q&A pairs. Responds to greetings, questions about itself, and general banter with terse personality-driven answers.

    > hello
    HI
    > are you a robot
    YES
    > do you dream
    MAYBE
    

    guess

    A 20 Questions game where the model knows a secret topic and answers YES/NO/MAYBE to your questions. Guess correctly to WIN.

    GUESS.COM example

    Includes tools for generating training data with LLMs (Ollama or Claude API) and balancing class distributions.

    Quickstart

    Get running in under 5 minutes:

    1. Download pre-built binaries from GitHub Releases

    2. Install an emulator:

    PlatformCP/M (.COM files)ZX Spectrum (.TAP files)
    Linuxiz-cpmapt install fuse-emulator-gtk
    Windowsiz-cpmFuse
    macOSiz-cpmbrew install fuse-emulator

    3. Run:

    • CP/M: iz-cpm CHAT.COM
    • ZX Spectrum: fuse --tape CHAT.TAP, then LOAD "" CODE and RANDOMIZE USR 32768

    For building from source or training your own models, see TRAINING.md.

    Features

    • Trigram hash encoding: Input text is hashed into 128 buckets - typo-tolerant, word-order invariant
    • 2-bit weight quantization: Each weight is {-2, -1, 0, +1}, packed 4 per byte
    • 16-bit integer inference: All math uses Z80-native 16-bit signed arithmetic
    • ~40KB .COM file: Fits in CP/M's Transient Program Area (TPA)
    • Autoregressive generation: Outputs text character-by-character
    • No floating point: Everything is integer math with fixed-point scaling
    • Interactive chat mode: Just run CHAT with no arguments

    Platform Support

    Z80-μLM runs on multiple Z80-based platforms:

    • CP/M: Original target platform. Generates .COM files using buildz80com.py
    • ZX Spectrum 48K: Full support via buildz80tap.py. See ZX-SPECTRUM.md for details
      • Generates .TAP files for emulators or real hardware
      • Uses ZX Spectrum ROM routines for I/O
      • Memory optimized for 48K systems
      • Compatible with most ZX Spectrum emulators

    For ZX Spectrum builds, use run-zx.sh in example directories or see the ZX Spectrum guide.

    Interaction Style

    The model doesn't understand you. But somehow, it gets you.

    Your input is hashed into 128 buckets via trigram encoding - an abstract "tag cloud" representation. The model responds to the shape of your input, not the exact words:

    "hello there"  →  [bucket 23: 64, bucket 87: 32, ...]
    "there hello"  →  [bucket 23: 64, bucket 87: 32, ...]  (same!)
    "helo ther"    →  [bucket 23: 32, bucket 87: 32, ...]  (similar - typo tolerant)
    

    This is semantically powerful for short inputs, but there's a limit: longer or order-dependent sentences blur together as concepts compete for the same buckets. "Open the door and turn on the lights" will likely be too close to distinguish from "turn on the door and open the lights."

    Small Responses, Big Meaning

    A 1-2 word response can convey surprising nuance:

    • OK - acknowledged, neutral
    • WHY? - questioning your premise
    • R U? - casting existential doubt
    • MAYBE - genuine uncertainty
    • AM I? - reflecting the question back

    This isn't necessarily a limitation - it's a different mode of interaction. The terse responses force you to infer meaning from context or ask probing direct yes/no questions to see if it understands or not (e.g. 'are you a bot', 'are you human', 'am i human' displays logically consistent memorized answers)

    What It's Good At

    • Short, varied inputs with consistent categorized outputs
    • Fuzzy matching (typos, rephrasing, word order)
    • Personality through vocabulary choice
    • Running on constrained 8-bit hardware

    What It's Not

    • A chatbot that generates novel sentences
    • Something that tracks multi-turn context deeply
    • A parser that understands grammar
    • Anything approaching general intelligence

    It's small, but functional. And sometimes that's exactly what you need.

    Architecture

    • Input: 128 query trigram buckets + 128 context buckets
    • Hidden layers: Configurable depth/width, e.g., 256 → 192 → 128
    • Output: One neuron per character in charset
    • Activation: ReLU between hidden layers

    Quantization Constraints

    The Z80 is an 8-bit CPU, but we use its 16-bit register pairs (HL, DE, BC) for activations and accumulators. Weights are packed 4-per-byte (2-bit each) and unpacked into 8-bit signed values for the multiply-accumulate.

    The 16-bit accumulator gives us numerical stability (summing 256 inputs without overflow), but the model's expressiveness is still bottlenecked by the 2-bit weights, and naive training may overflow or act 'weirdly' without QAT.

    Z80 Inner Loops

    The core of inference is a tight multiply-accumulate loop. Weights are packed 4-per-byte:

    ; Unpack 2-bit weight from packed byte
    ld a, (PACKED)      ; Get packed weights
    and 03h             ; Mask bottom 2 bits
    sub 2               ; Map 0,1,2,3 → -2,-1,0,+1
    ld (WEIGHT), a
    
    ; Rotate for next weight
    ld a, (PACKED)
    rrca
    rrca
    ld (PACKED), a
    

    The multiply-accumulate handles the 4 possible weight values:

    MULADD:
        or a
        jr z, DONE       ; weight=0: skip entirely
        jp m, NEG        ; weight<0: subtract
        ; weight=+1: add activation
        ld hl, (ACC)
        add hl, de
        ld (ACC), hl
        ret
    NEG:
        cp 0FFh
        jr z, NEG1       ; weight=-1
        ; weight=-2: subtract twice
        ld hl, (ACC)
        sbc hl, de
        sbc hl, de
        ld (ACC), hl
        ret
    NEG1:
        ; weight=-1: subtract once
        ld hl, (ACC)
        sbc hl, de
        ld (ACC), hl
        ret
    

    After each layer, arithmetic right-shift by 2 to prevent overflow:

    sra h        ; Shift right arithmetic (preserves sign)
    rr l
    sra h
    rr l         ; ACC = ACC / 4
    

    That's the entire neural network: unpack weight, multiply-accumulate, shift. Repeat ~100K times per character generated.


    License: MIT or Apache-2.0 as you see fit.