{"id":30699959,"url":"https://github.com/eth-sri/constrained-diffusion","last_synced_at":"2025-09-29T17:42:02.541Z","repository":{"id":310012763,"uuid":"1035168952","full_name":"eth-sri/constrained-diffusion","owner":"eth-sri","description":"Constrained Decoding of Diffusion LLMs with Context-Free Grammars.","archived":false,"fork":false,"pushed_at":"2025-08-27T15:41:07.000Z","size":4848,"stargazers_count":18,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-27T20:41:55.429Z","etag":null,"topics":["constrained-decoding","diffusion","diffusion-model","fill-in-the-middle","llm","llms","llms-benchmarking","multi-region-infilling"],"latest_commit_sha":null,"homepage":"https://constrained-diffusion.ai","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eth-sri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-09T19:56:44.000Z","updated_at":"2025-08-27T15:41:10.000Z","dependencies_parsed_at":"2025-08-15T07:16:58.792Z","dependency_job_id":null,"html_url":"https://github.com/eth-sri/constrained-diffusion","commit_stats":null,"previous_names":["eth-sri/constrained-diffusion"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eth-sri/constrained-diffusion","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eth-sri%2Fconstrained-diffusion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eth-sri%2Fconstrained-diffusion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eth-sri%2Fconstrained-diffusion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eth-sri%2Fconstrained-diffusion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eth-sri","download_url":"https://codeload.github.com/eth-sri/constrained-diffusion/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eth-sri%2Fconstrained-diffusion/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273279811,"owners_count":25077318,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constrained-decoding","diffusion","diffusion-model","fill-in-the-middle","llm","llms","llms-benchmarking","multi-region-infilling"],"created_at":"2025-09-02T11:43:23.607Z","updated_at":"2025-09-29T17:42:02.532Z","avatar_url":"https://github.com/eth-sri.png","language":"Rust","readme":"\u003cdiv align=\"center\"\u003e\u003ch1\u003eConstrained Decoding of Diffusion LLMs\u003cbr\u003e with Context-Free Grammars\u003c/h1\u003e\u003c/div\u003e\n\n[![arXiv](https://img.shields.io/badge/arXiv-2508.10111-b31b1b.svg)](https://arxiv.org/abs/2508.10111)\n![Python Versions](https://img.shields.io/badge/Python-3.11%20%7C%203.12%20%7C%203.13-blue)\n![Rust Version](https://img.shields.io/badge/rust-2021-orange)\n[![Python Tests](https://github.com/eth-sri/constrained-diffusion/actions/workflows/python-tests.yml/badge.svg)](https://github.com/eth-sri/constrained-diffusion/actions/workflows/python-tests.yml)\n[![Rustformlang Tests](https://github.com/eth-sri/constrained-diffusion/actions/workflows/rustformlang-tests.yml/badge.svg)](https://github.com/eth-sri/constrained-diffusion/actions/workflows/rustformlang-tests.yml)\n[![Regex DFA Tests](https://github.com/eth-sri/constrained-diffusion/actions/workflows/regex-dfa-tests.yml/badge.svg)](https://github.com/eth-sri/constrained-diffusion/actions/workflows/regex-dfa-tests.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n\nThis repository contains the implementation of [Constrained Decoding of Diffusion LLMs with Context-Free Grammars](https://arxiv.org/abs/2508.10111), including techniques for multi-region constrained generation. Our method guarantees syntactic correctness while improving functional correctness by up to 7%.\n\n## 🚀 Overview\n\nWe present the first generalized method for constrained decoding of multi-region infilling and out-of-order generation models. Our approach:\n\n- **Works with SOTA diffusion LLMs** like LLaDA, Dream-Coder and DiffuCoder for non-autoregressive generation\n- **Also works for Fill-in-the-Middle (FIM) and Multi-Region Infilling (MRI) models** like StarCoder, DeepSeek Coder, and CodeGemma\n- **Supports multiple constraint languages** through context-free grammars (examples provided are JSON Schema, C++, and SMILES)\n- **Guarantees syntactic correctness** wrt. the grammar \n- **Improves functional correctness** by up to 7% with minimal computational overhead\n\n## 📦 Installation\n\n### Prerequisites\n- [Python](https://www.python.org/) 3.11+ \n- [Rust](https://www.rust-lang.org/) (for building the formal language library)\n- CUDA-compatible GPU (for inference)\n\n### Setup\n\nWe recommend using a virtual environment to avoid conflicts with other Python packages.\n\n0. **Clone the repository and set up virtual enviroment:**\n```bash\ngit clone https://github.com/eth-sri/constrained-diffusion.git\ncd constrained-diffusion\npython3 -m venv venv\nsource venv/bin/activate\n```\n\n1. **Build and install Rust bindings:**\n```bash\ncd rustformlang_bindings\npip install maturin\nmaturin build --release\npip install .\ncd ..\n```\n\n2. **Install the main package:**\n```bash\npip install -e .\n```\n\n\n4. **Verify installation:**\n```bash\npytest tests\n```\n\n\n## 🔧 Usage \u0026 Demo\n\nCheck out [`example.py`](example.py) for a complete example of how to use the constrained decoding mechanism.\nIn general, you want to first load a model and then load a constraint language, such as C++ or JSON Schema. The example below shows abbreviated code on how to use the `GSAI-ML/LLaDA-8B-Instruct` model with a C++ constraint.\nReplace the model name with any diffusion LLM of your choice, such as `apple/DiffuCoder-7B-Instruct`.\n\n```bash\npython3 example.py\n```\n\nThis is a visualization of our constrained decoding mechanism on output similar to that created by LLaDA 7b.\n\n\u003e ![LLaDA 7B Inference](./docs/static/images/animation/words_grid_animation_constrained_dark.gif)\n\n\n## 📁 Project Structure\n\n```\n├── constrained_diffusion/           # Main package\n│   ├── constrain_utils.py            # Constraint generation utilities\n│   ├── cfgs/                         # Context-free grammar definitions\n│   └── eval/                         # Evaluation frameworks\n│       ├── dllm/                     # Evaluation framework for DLLMs\n│       └── mri/                      # Evaluation framework for Multi-Region Infilling\n├── rustformlang/                     # Rust formal language library\n├── rustformlang_bindings/            # Python bindings for Rust library\n├── eval/                             # Evaluation scripts and results\n│   ├── dllm/                         # DLLM task evaluations\n│   ├── mri/                          # Multi-Region infilling evaluations\n│   └── figures/                      # Result visualization\n├── benchmark_generation/             # Benchmark generation tools\n└── docs/                             # Project website\n```\n\n## 🧪 Evaluation\n\n### Datasets\n\nWe run MRI and diffusion LLMs on the following datasets:\n\n| Dataset | Setting | Description                                           | Download |\n|---------|---------|-------------------------------------------------------|----------|\n| C++     | MRI     | C++ code generation tasks with multi-region infilling | [🤗 HuggingFace](https://huggingface.co/datasets/eth-sri/HumanEval-MRI-Cpp)    |\n| C++     | DLM     | C++ code generation tasks with diffusion LLMs         | [🤗 HuggingFace](https://huggingface.co/datasets/zai-org/humaneval-x) |\n| JSON    | DLM     | Data extraction, following a JSON Schema              | [🤗 HuggingFace](https://huggingface.co/datasets/eth-sri/json-mode-eval-extended) |\n| SMILES  | DLM     | Chemical compound representation in SMILES            | [🤗 HuggingFace](https://huggingface.co/datasets/eth-sri/smiles-eval)      |\n\n\u003e You can download the results of our evaluation using the following link: [Download Results](https://files.sri.inf.ethz.ch/constrained-diffusion/results.zip).\n\u003e Unzip the file in the `results/` directory to access the evaluation results.\n\n\n### Running Inference\n\nFor the MRI models, we provide an execution harness for the C++ HumanEval multi-region dataset.\nTo execute task 11 on the 1-region dataset with constraints and traces enabled, use the following command:\n```bash\npython3 -m constrained_diffusion.eval.mri.generic_inference \\\n  --max-tokens 256 \\\n  --model_name deepseek-ai/deepseek-coder-6.7b-base \\\n  --seed 0 \\\n  --temp 1 \\\n  --dataset-name HumanEval/MRI/cpp/1 \\\n  --constrained True \\\n  --trace True \\\n  --task_id /11_ \n```\n\nFor the diffusion LLMs, use the following command for the JSON dataset.\n```bash\npython3 -m constrained_diffusion.eval.dllm.generic_inference \\\n  --max-tokens 256 \\\n  --model_name apple/DiffuCoder-7B-Instruct \\\n  --seed 0 \\\n  --temp 0.2 \\\n  --dataset-name jsonschema \\\n  --steps 32 \\\n  --constrained True \\\n  --trace True \\\n  --task_id _37\n```\n\nA general orchestration script for all experiments in the main paper is provided in `eval/mri/run_mri.py` and `eval/dllm/run_dllm.py`.\nThe results are stored in the `results/` directory, with each configuration's results in a separate file.\n\n### Running Evaluation\n\nEvaluation of result correctness is decoupled from the inference step. The following assumes that the inference step above was executed correctly and results lie in `results`.\n\n\u003e Note: For SMILES evaluation, you need to install `rdkit`and `partialsmiles`: `pip install rdkit partialsmiles`\n\nMake sure to have sufficient memory and CPU cores available, as the evaluation scripts can be memory-intensive.\n```bash\n# Evaluate all files in the results folder\nbash eval/check_all_individually.sh results/*\n```\n\n### More details\n\nYou  can find more details on the evaluation scripts, for example on how to reproduce the figures from the paper, in the README in the `eval/` directory: [README](eval/README.md).\n\n## 🤝 Contributing\n\nWe welcome contributions! When contributing, please make sure to activate pre-commit hooks to ensure code quality and consistency. You can install pre-commit hooks with:\n\n```bash\npip install pre-commit\npre-commit install\n```\n\n### Adding New Constraint Languages\n\n1. Define the grammar in `constrained_outoforder/cfgs/`\n2. Implement lexical mapping in `check_lex_map.py`\n3. Add tests in `tests/test_cfgs/`\n4. Update documentation\n \n### Adding New Evaluation Tasks\n\n1. [Create a new constraint language](#adding-new-constraint-languages)\n2. Implement a dataset in `constrained_outoforder/eval/[dllm|mri]/datasets/your_task.py`\n4. Register the dataset using `register_dataset()`\n3. Add evaluation logic in `eval/[dllm|mri]/your_task/checker.py`\n\n### Adding a New Model\n\n1. Implement the model in `constrained_outoforder/eval/[dllm|mri]/models/your_model.py`\n2. Register the model using `register_model()`\n\n## 📄 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## 🔗 Links\n\n- **Paper**: [arXiv:2508.10111](https://arxiv.org/abs/2508.10111)\n- **Project Website**: [Constrained Decoding Paper Website + Demo](https://constrained-diffusion.ai)\n- **Rustformlang README**: [Rustformlang Docs](rustformlang/)\n\n## 📚 Citation\n\nIf you use this work in your research, please cite:\n\n```bibtex\n@article{mundler2025constraineddiffusion,\n    title={Constrained Decoding of Diffusion LLMs with Context-Free Grammars}, \n    author={Niels Mündler and Jasper Dekoninck and Martin Vechev},\n    year={2025},\n    eprint={2508.10111},\n    archivePrefix={arXiv},\n    url={https://arxiv.org/abs/2508.10111}\n}\n```\n\nThis work was done by the [Secure, Reliable and Intelligent Systems Lab](https://sri.inf.ethz.ch/) at [ETH Zurich](https://ethz.ch).\n","funding_links":[],"categories":["Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feth-sri%2Fconstrained-diffusion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feth-sri%2Fconstrained-diffusion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feth-sri%2Fconstrained-diffusion/lists"}