Skip to content

HDL Export and Equivalence Verification

This page walks through lowering a trained model to Verilog / VHDL and confirming that it is bit-for-bit identical to the original model. For the background (why we go through the IR), see Architecture overview.

Three-way equivalence — what is guaranteed

Section titled “Three-way equivalence — what is guaranteed”

“The generated HDL is probably right” is not worth burning to silicon. This library runs the same circuit through three independent paths and verifies that they agree on every vector.

PyTorch hard eval ⇔ ir/simulate.py (numpy golden) ⇔ HDL simulation
PathImplementationRole
PyTorch hard evalthe torch model after model.eval()truth on the training side
simulateir/simulate.py (bit-exact in numpy)golden model (the reference for comparison)
HDL simulationiverilog (Verilog) / ghdl (VHDL)truth about the generated artifact

Only when all three agree can you say “the HDL is equivalent to the original model”.

Rather than theory first, the fastest route is to run the self-verifying, executable samples. Both finish in under a minute, and the assert at the end checks the expected result for you.

ターミナルウィンドウ
# Fully connected DLGN: train → IR → Verilog → iverilog cross-check
uv run python examples/04_export_hdl.py
# Logic-gate convolutional CNN: IR unroll (lowering) → Verilog/VHDL → cross-check
uv run python examples/05_cnn_export_hdl.py
SampleContentsExpected result
examples/04_export_hdl.pytrained model → IR → Verilog + iverilog cross-checkIR predictions = labels, cross-check ALLPASS
examples/05_cnn_export_hdl.pyCNN → IR unroll → Verilog/VHDL + cross-check100% match with simulate predictions, cross-check ALLPASS

As 05 shows, a model containing convolutions must go through lowering (expanding conv2d / or_pool2d into a series of lut_layers) before HDL generation. The HDL generator explicitly rejects an IR that contains native conv nodes.

iverilog (Verilog / fine to install locally)

Section titled “iverilog (Verilog / fine to install locally)”
ターミナルウィンドウ
brew install icarus-verilog # macOS
sudo apt-get install -y iverilog # Debian/Ubuntu

Verification runs as long as both iverilog and vvp are on the PATH. On machines without them, tests marked hdl are skipped automatically, so nothing breaks locally.

Section titled “ghdl (VHDL / local installation not recommended)”

On machines without ghdl, tests marked hdl_vhdl are skipped automatically.

These are defined in tests/hdl/ under two markers. The default addopts excludes them, so they run only when you ask for them explicitly.

MarkerTargetRequired simulator
hdlVerilog equivalenceiverilog
hdl_vhdlVHDL equivalenceghdl
ターミナルウィンドウ
make hdl # = pytest -m "hdl or hdl_vhdl"
uv run pytest -m hdl -q # Verilog only (skipped if iverilog is missing)
uv run pytest -m hdl_vhdl -q # VHDL only (skipped if ghdl is missing)

Generating and cross-checking with a script

Section titled “Generating and cross-checking with a script”
ターミナルウィンドウ
# Generate Verilog from a checkpoint/IR and cross-check against the golden (simulate)
uv run python scripts/export_hdl.py model.json -o out/ --verify
# Generate VHDL at the same time and cross-check (the VHDL side runs only where ghdl is installed)
uv run python scripts/export_hdl.py model.json -o out/ --format both --verify

--verify runs all the way through equivalence verification (stage 7). If the corresponding simulator is not installed, only that language’s cross-check is skipped and a message says so on stdout (it is not an error).

ターミナルウィンドウ
uv run griffith export-hdl model.json --lang verilog -o out/model.v
uv run griffith export-hdl model.json --lang vhdl -o out/model.vhdl

griffith export-hdl performs IR → HDL conversion only and does no simulator cross-checking. If you need the cross-check too, use scripts/export_hdl.py --verify.

SymptomCauseFix
HDL generation rejects the IRNative nodes such as conv2d / or_pool2d remainRun lowering (stage 5) first. examples/05 shows how
validate emits warningsConstructs that cannot be expressed in HDL (nodes returning real values, etc.) are mixed inMove the layer in question to the logic side, or cut before it and export
No VHDL tests run at all locallyghdl is not installedExpected (automatic skip). Verify via the container path
Accuracy does not match torchYou are comparing against soft (training-time) outputAlways compare against hard eval with model.eval() (soft/hard in DLGN)