VeriGen: A Large Language Model for Verilog Code Generation

Shailja Thakur, Baleegh Ahmad, Hammond Pearce, Benjamin Tan, Brendan Dolan-Gavitt, Ramesh Karri, Siddharth Garg

TL;DR

VeriGen is a specialized Large Language Model (LLM) framework designed to automate the generation of Verilog code for digital hardware design. By fine-tuning open-source models on a massive, curated corpus of hardware descriptions and textbooks, the researchers created a 16-billion parameter model that rivals—and sometimes outperforms—commercial giants like GPT-3.5-turbo in functional hardware generation.

What Problem Does This Paper Address?

While software engineering has been transformed by AI coding assistants like GitHub Copilot, hardware design remains a bottleneck. Writing Verilog (a Hardware Description Language or HDL) is notoriously time-consuming and error-prone. Unlike standard software, Verilog describes physical circuits where concurrency, timing, and resource constraints are paramount.

Existing general-purpose LLMs often struggle with Verilog because the language represents a tiny fraction of their training data. Furthermore, previous attempts to build Verilog-specific models relied on small, synthetic datasets that didn't generalize well to real-world engineering problems. The authors of VeriGen set out to bridge this gap by creating a robust training pipeline and a comprehensive evaluation framework for hardware-centric AI.

Methods

The researchers adopted a fine-tuning approach, taking five pre-existing "base" models (ranging from 345 million to 16 billion parameters) and specializing them for hardware.

The technical heavy lifting involved:

  • Distributed Training: Using DeepSpeed and ZeRO-3 optimization to handle the 16B parameter model across multiple A100 GPUs.
  • Prompt Engineering: Testing different levels of prompt detail (Low, Medium, High) and "system prompts" to see how instructions influence the quality of the generated hardware.
  • Functional Verification: Unlike software where you just "run" code, hardware needs to be simulated. The team built an automated pipeline that takes LLM output, compiles it using Icarus Verilog or Vivado, and runs it against test benches to ensure the logic is actually correct.

Datasets

The paper introduces what is likely the largest Verilog training corpus to date (~400 MB), consisting of:

  1. GitHub Corpus: ~50,000 de-duplicated .v files scraped from public repositories.
  2. Verilog Books Corpus: Text extracted from 70 Verilog textbooks using OCR and custom cleaning scripts to capture "idiomatic" and well-explained code examples.

For evaluation, they used two benchmarks:

  • Problem Set I: 17 hand-designed challenges ranging from simple gates to complex Finite State Machines (FSMs).
  • Problem Set II: 181 problems derived from HDLBits, a popular platform for learning digital logic.

Key Insights and Ideas

  • The "Textbook" Advantage: One of the most striking findings was that adding textbook data (CodeGen-2B-FT++) significantly improved performance over models trained only on GitHub code. Textbooks provide the "why" and "how" that raw, often messy GitHub code lacks, leading to more structured and "idiomatic" Verilog.
  • Specialization Beats Scale: A fine-tuned 16B parameter model (VeriGen) was able to compete with GPT-3.5-turbo, which is estimated to be significantly larger. This suggests that for niche domains like hardware, a smaller, "in-house" model can be more efficient and effective than a massive general-purpose one.
  • The Syntax Leap: Fine-tuning resulted in a 41% improvement in generating syntactically correct code compared to the base models. This is crucial because even a single syntax error renders a hardware design useless.
  • Prompt Sensitivity: Commercial models like GPT-3.5 are highly sensitive to "system prompts." Telling the model to act specifically as a "Verilog autocomplete engine" boosted its performance by over 30% in some categories.

Key Results

  • Performance: The fine-tuned CodeGen-16B model achieved a 1.1% overall increase in functional correctness over GPT-3.5-turbo on the custom problem set.
  • Complexity Handling: On medium-complexity problems, VeriGen solved up to 74% of tasks, nearly matching GPT-4’s 75%.
  • Efficiency: VeriGen (16B) demonstrated an inference time of approximately 2 seconds, compared to 10 seconds for GPT-4, making it much more viable for real-time developer feedback loops.
  • Open Source: The authors released their training scripts and model checkpoints, providing a foundation for the hardware community to build upon.

Limitations and Future Work

Despite the progress, "spatial" and highly complex logic remains a hurdle. All models, including VeriGen and GPT-4, struggled with problems like Conway’s Game of Life, which requires a deep understanding of 2D grid neighbors and toroidal logic. The models often failed to account for "edge cases" in grid boundaries.

Future work will likely focus on:

  • Hybrid Training: Combining Verilog with natural language explanations more deeply.
  • Bug Localization: Improving the model's ability to not just write code, but identify and fix errors in existing designs.
  • Larger Context Windows: Allowing the model to "see" more of a large-scale hardware project to maintain consistency across multiple modules.

Summary updated

BibTeX
@article{thakur2024verigen,
  author = {Shailja Thakur and Baleegh Ahmad and Hammond Pearce and Benjamin Tan and Brendan Dolan-Gavitt and Ramesh Karri and Siddharth Garg},
  title = {{VeriGen: A Large Language Model for Verilog Code Generation}},
  journal = {ACM Trans. Design Autom. Electr. Syst.},
  year = {2024},
  doi = {10.1145/3643681},
}
Figure