# sadadhaidari/zybo-weight-memory architecture

> Architecture diagram of the GitHub repository sadadhaidari/zybo-weight-memory, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sadadhaidari/zybo-weight-memory
- Repository: https://github.com/sadadhaidari/zybo-weight-memory

## Overview

This repository demonstrates lossless BF16 weight compression for language-model inference: Python packs weights, C++ restores them on the PC, and a Zybo FPGA stores and restores packed groups over JTAG. The main user-to-result path is loading model embeddings, optionally round-tripping them through the board, then comparing CPU inference outputs and measurements. Hardware RTL and some experiment modules are described by the README or tree but are not present in the supplied excerpts; arrows are limited to documented or excerpt-supported relationships.

Main entry points: [`hardware/board.py`](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/board.py) (FPGA block store), [`software/native_model.py`](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/native_model.py) (Native model runner), [`experiments/stress_qwen.py`](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/stress_qwen.py) (Qwen stress runs), [`experiments/live_inference.py`](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/live_inference.py) (Live board inference)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_software["Software compression and inference"]
  node_codec["BF16 exponent codec<br/>[exponent_codec.py]"]
  node_archive["Exponent archive prep"]
  node_gguf["GGUF tensor reader<br/>[gguf_blocks.py]"]
  node_native["Native model runner<br/>[native_model.py]"]
  node_restore["C++ weight restore<br/>[rqc.cpp]"]
  node_memory["Memory budgeting<br/>[memory_budget.py]"]
  node_stock["Stock model runner<br/>[stock_native.py]"]
end

subgraph group_hardware["FPGA board access"]
  node_board["FPGA block store<br/>[board.py]"]
  node_cable["JTAG cable transport<br/>[cable.py]"]
  node_program["FPGA bitstream loader<br/>[program.py]"]
end

subgraph group_experiments["Physical inference experiments"]
  node_experiment_paths["Experiment paths<br/>[paths.py]"]
  node_physical["Physical weight sampling<br/>[physical.py]"]
  node_inference["Embedding inference<br/>[inference.py]"]
  node_stress["Qwen stress runs<br/>[stress_qwen.py]"]
  node_live["Live board inference<br/>[live_inference.py]"]
end

subgraph group_evidence["Measurement and validation"]
  node_board_verify["Board readback checks<br/>[verify_board.py]"]
  node_early_bench["Baseline model benchmark<br/>[bench_model.py]"]
  node_summaries["Board measurement summaries<br/>[summarize_board.py]"]
end

node_engineer(("Engineer"))

node_engineer -->|"samples weights"| node_physical
node_engineer -->|"runs inference"| node_stress
node_codec -->|"packs for"| node_restore
node_restore -.->|"restores weights"| node_native
node_gguf -->|"reads model tensors"| node_inference
node_native -->|"runs CPU forward"| node_inference
node_board -->|"supplies embeddings"| node_inference
node_gguf -->|"reads model tensors"| node_stress
node_native -->|"runs CPU inference"| node_stress
node_board -->|"reads embeddings"| node_stress
node_physical -->|"uses board access"| node_board
node_physical -->|"uses readback checks"| node_board_verify
node_live -->|"checks and saves"| node_physical
node_live -->|"reads model tensors"| node_gguf
node_live -->|"runs CPU inference"| node_native
node_live -->|"reuses test cases"| node_stress
node_board -->|"sends JTAG commands"| node_cable
node_board -->|"checks FPGA identity"| node_program
node_program -->|"loads bitstream over"| node_cable
node_board_verify -->|"checks readback"| node_board
node_early_bench -->|"benchmarks runner"| node_stock
node_memory -.->|"budgets runtime"| node_native
node_summaries -->|"uses locations"| node_experiment_paths

click node_codec "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/exponent_codec.py"
click node_archive "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/prepare_exponent_archive.py"
click node_gguf "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/gguf_blocks.py"
click node_native "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/native_model.py"
click node_restore "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/rqc.cpp"
click node_memory "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/memory_budget.py"
click node_stock "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/stock_native.py"
click node_experiment_paths "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/paths.py"
click node_physical "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/physical.py"
click node_inference "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/inference.py"
click node_stress "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/stress_qwen.py"
click node_live "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/live_inference.py"
click node_board "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/board.py"
click node_cable "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/cable.py"
click node_program "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/program.py"
click node_board_verify "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/verify_board.py"
click node_early_bench "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/early/bench_model.py"
click node_summaries "https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/summarize_board.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_codec,node_archive,node_gguf,node_native,node_restore,node_memory,node_stock toneBlue
class node_board,node_cable,node_program toneAmber
class node_experiment_paths,node_physical,node_inference,node_stress,node_live toneMint
class node_board_verify,node_early_bench,node_summaries toneRose
class node_engineer toneIndigo
```

## Components

### Software compression and inference

- [BF16 exponent codec](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/exponent_codec.py): `software/exponent_codec.py`
- [Exponent archive prep](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/prepare_exponent_archive.py): `software/prepare_exponent_archive.py`
- [GGUF tensor reader](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/gguf_blocks.py): `software/gguf_blocks.py`
- [Native model runner](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/native_model.py): `software/native_model.py`
- [C++ weight restore](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/rqc.cpp): `software/rqc.cpp`
- [Memory budgeting](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/memory_budget.py): `software/memory_budget.py`
- [Stock model runner](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/stock_native.py): `software/stock_native.py`

### FPGA board access

- [FPGA block store](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/board.py): `hardware/board.py`
- [JTAG cable transport](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/cable.py): `hardware/cable.py`
- [FPGA bitstream loader](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/program.py): `hardware/program.py`

### Physical inference experiments

- [Experiment paths](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/paths.py): `experiments/paths.py`
- [Physical weight sampling](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/physical.py): `experiments/physical.py`
- [Embedding inference](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/inference.py): `experiments/inference.py`
- [Qwen stress runs](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/stress_qwen.py): `experiments/stress_qwen.py`
- [Live board inference](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/live_inference.py): `experiments/live_inference.py`

### Measurement and validation

- [Board readback checks](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/hardware/verify_board.py): `hardware/verify_board.py`
- [Baseline model benchmark](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/software/early/bench_model.py): `software/early/bench_model.py`
- [Board measurement summaries](https://github.com/sadadhaidari/zybo-weight-memory/blob/main/experiments/summarize_board.py): `experiments/summarize_board.py`

### Other components

- **Engineer**

## Connections

- Engineer → Physical weight sampling: samples weights (evidence: `README.md`)
- Engineer → Qwen stress runs: runs inference (evidence: `README.md`)
- BF16 exponent codec → C++ weight restore: packs for (evidence: `README.md`)
- C++ weight restore → Native model runner: restores weights
- GGUF tensor reader → Embedding inference: reads model tensors (evidence: `experiments/inference.py`)
- Native model runner → Embedding inference: runs CPU forward (evidence: `experiments/inference.py`)
- FPGA block store → Embedding inference: supplies embeddings (evidence: `experiments/inference.py`)
- GGUF tensor reader → Qwen stress runs: reads model tensors (evidence: `experiments/stress_qwen.py`)
- Native model runner → Qwen stress runs: runs CPU inference (evidence: `experiments/stress_qwen.py`)
- FPGA block store → Qwen stress runs: reads embeddings (evidence: `experiments/stress_qwen.py`)
- Physical weight sampling → FPGA block store: uses board access (evidence: `experiments/physical.py`)
- Physical weight sampling → Board readback checks: uses readback checks (evidence: `experiments/physical.py`)
- Live board inference → Physical weight sampling: checks and saves (evidence: `experiments/live_inference.py`)
- Live board inference → GGUF tensor reader: reads model tensors (evidence: `experiments/live_inference.py`)
- Live board inference → Native model runner: runs CPU inference (evidence: `experiments/live_inference.py`)
- Live board inference → Qwen stress runs: reuses test cases (evidence: `experiments/live_inference.py`)
- FPGA block store → JTAG cable transport: sends JTAG commands (evidence: `hardware/board.py`)
- FPGA block store → FPGA bitstream loader: checks FPGA identity (evidence: `hardware/board.py`)
- FPGA bitstream loader → JTAG cable transport: loads bitstream over (evidence: `hardware/program.py`)
- Board readback checks → FPGA block store: checks readback (evidence: `hardware/verify_board.py`)
- Baseline model benchmark → Stock model runner: benchmarks runner (evidence: `software/early/bench_model.py`)
- Memory budgeting → Native model runner: budgets runtime
- Board measurement summaries → Experiment paths: uses locations (evidence: `experiments/summarize_board.py`)
