Constrained Aligned Sampling Algorithms
Project description
Constrained aligned sampling algorithms for language models via CARS, MCMC, and rejection sampling variants.
Installation
Prerequisites
- Python 3.12+
- CUDA-compatible GPU (recommended)
Install from source
# Clone the repository
git clone https://github.com/LargeLorisModels/casa.git
cd casa
# Create virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate
# Install package in editable mode
pip install -e .
Using uv (faster)
# Install uv if you haven't
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create environment and install
uv venv
source .venv/bin/activate
uv pip install -e .
Quick Start
from casa import LLM, Grammar, CARS
llm = LLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
grammar_str = """
start: CHARACTER " " ACTION " " LOCATION "."
CHARACTER: "a dragon" | "a knight" | "a wizard"
ACTION: "discovered" | "protected" | "enchanted"
LOCATION: "the castle" | "the forest" | "the treasure"
"""
prompt = "Once upon a time,"
grammar = Grammar.from_string(grammar_str, llm.tokenizer)
sampler = CARS(llm, grammar, max_new_tokens=32, verbose=True)
results = sampler.sample(prompt, n_samples=10, max_attempts=100)
if results:
print("\nGenerated samples,")
for i, result in enumerate(results, 1):
print(f" {i}. {prompt} {result.text}")
else:
print("Failed to generate any samples")
Example Output
With verbose=True, you'll see the rejection samplers performance in real-time. Running above code,
Sample 01/10: ████████████████████████████████████████ 78 attempts
Sample 02/10: ████████ 12 attempts
Sample 03/10: █ 2 attempts
Sample 04/10: █ 1 attempts
Sample 05/10: █ 1 attempts
...
Generated samples:
1. Once upon a time, a dragon enchanted the castle.
2. Once upon a time, a dragon enchanted the forest.
3. Once upon a time, a dragon enchanted the forest.
...
Available Samplers
- CARS: Constrained Adaptive Rejection Sampling
- MCMC: Markov Chain Monte Carlo sampling. Avaliable variants,
- Uniform - Randomly resamples from any position. Balances exploration with structural preservation.
- Priority - Resample higher perplexity regions first. Targets uncertain tokens for refinement.
- Restart - Generates from scratch. Independent proposals via importance sampling.
- ARS: Adaptive Rejection Sampling
- RSFT: Rejection Sampling with First Token constraints
- RS: Basic Rejection Sampling
Running Tests
# Run the example
python tests/test_cars.py
# Or other samplers
python tests/test_mcmc.py
References
CASA implements the following algorithms from:
-
RS, ARS, RSFT, CARS
Constrained Adaptive Rejection Sampling
Preprint | arXiv:2510.01902 -
MCMC - Uniform, Priority, Restart
Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective
NeurIPS 2026 | arXiv:2506.05754
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file casa_lm-0.1.0.tar.gz.
File metadata
- Download URL: casa_lm-0.1.0.tar.gz
- Upload date:
- Size: 130.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79be29fadc3cc0ba0bf364e3770a92dd7c4d9d314787027b35cb0a471176fed8
|
|
| MD5 |
37ae479479336b6a27a25274af827444
|
|
| BLAKE2b-256 |
fc79ec26b214aced6e5b3c236fc6fd8dae0fa70db86d26f7452d8f49f43cbd8e
|
File details
Details for the file casa_lm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: casa_lm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e7547fece3a6967a5bd10e4536e1202552b9cbdc2f106e62718b8e1a6cbd265
|
|
| MD5 |
9f57202ef53a16513a544abde9be8ded
|
|
| BLAKE2b-256 |
bcb0de6519225ee98767c359a3cf41be8df2cb63b150e9a349cc3f5678746496
|