How a ternary language model ended up generating text on a hobbyist Zynq board, with the matrix multiplications and attention running on circuits written by hand. One entry per stretch of work, written at the time. Wrong turns and later corrections are left in.
三値の言語モデルを、PYNQ-Z1 の FPGA に自分で書いた回路で動かすまでの記録です。 区切りごとにその時点で書いたもので、途中の誤りと後からの訂正もそのまま残してあります。
To run it yourself, see the repository README.
What a Zynq chip is, why memory bandwidth rather than multipliers sets the speed limit, and the first measurement of how fast the fabric can read DDR.
Zynq とは何か、なぜ速さを決めるのが掛け算器ではなく帯域なのか、そして PL から DDR を読む速さを初めて測った話。FPGA 初心者向けの入口。
Separating two different limits — the datapath width on the fabric side and the DDR itself — and finding the real ceiling with two HP ports.
PL 側の道幅と DDR そのものという二つの天井を切り分け、HP ポート 2 本で本当の上限を見つけるまで。
Computing right up to the bandwidth wall with an INT8 multiplier array, then getting 4.8× more weights through the same wall by going ternary — with zero DSP slices. An update adds the bad news: attention on the CPU dominates.
INT8 の掛け算器で壁まで計算し、三値にして同じ壁から 4.8 倍の重みを取り出すまで。DSP は 0 個。追記で、CPU に残した attention が全体を支配すると分かる。
Attention as a two-phase circuit. Softmax turns out to cost no bandwidth at all, and all four circuits sit at the DDR wall. Along the way, the way bandwidth had been measured since stage 1 turns out to be wrong.
attention を 2 相の回路にする。softmax は帯域を 1 バイトも食わず、4 本の回路がすべて DDR の壁に張り付く。途中で、段階1 からの帯域の測り方が間違っていたと分かる。
Rounding a normal model to ternary breaks it, so the project switches to a model trained ternary. Stream headers, a rebuilt attention core, scatter-gather DMA for the KV cache — and text generated on the board, with logits bit-identical to a Mac. 7.3 tok/s at 512 tokens of context.
普通のモデルを三値に丸めたら壊れたので、三値で学習したモデルに乗り換える。見出し付きストリーム、attention の作り直し、KV を流す SG 転送を経て、ボードで文章を生成。Mac と logits がビット単位で一致し、文脈 512 で 7.3 tok/s。