Instructions to use transformers-community/constrained-beam-search with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use transformers-community/constrained-beam-search with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="transformers-community/constrained-beam-search") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("transformers-community/constrained-beam-search") model = AutoModelForCausalLM.from_pretrained("transformers-community/constrained-beam-search") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use transformers-community/constrained-beam-search with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "transformers-community/constrained-beam-search" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "transformers-community/constrained-beam-search", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/transformers-community/constrained-beam-search
- SGLang
How to use transformers-community/constrained-beam-search with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "transformers-community/constrained-beam-search" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "transformers-community/constrained-beam-search", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "transformers-community/constrained-beam-search" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "transformers-community/constrained-beam-search", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use transformers-community/constrained-beam-search with Docker Model Runner:
docker model run hf.co/transformers-community/constrained-beam-search
Support Transformers v5 cache handling
This PR was created to address the cached-decoding regression reported in transformers-community/constrained-beam-search discussion #2, where deterministic constrained beam search behaves correctly with use_cache=False but produces degenerate output with use_cache=True.
The issue appears to be the same Transformers v5 cache-handling compatibility problem that was previously reported and fixed in transformers-community/group-beam-search: https://huggingface.co/transformers-community/group-beam-search/discussions/4
I pushed a fix for the cached-decoding issue reported in https://huggingface.co/transformers-community/constrained-beam-search/discussions/2.
The generation loop now passes next_sequence_length and is_first_iteration into prepare_inputs_for_generation, matching the Transformers v5 cache protocol. This follows the same compatibility pattern that was already accepted for transformers-community/group-beam-search: https://huggingface.co/transformers-community/group-beam-search/discussions/4
The reason for the change is that the KV cache should be an optimization only: with deterministic constrained beam search, use_cache=True should not change the generated result compared with use_cache=False.
I tested the PR in Colab with openai-community/gpt2, force_words_ids, num_beams=4, and do_sample=False.
Transformers 5 test
python: 3.12.13
torch: 2.11.0+cu128
transformers: 5.12.0
cuda: True
model: openai-community/gpt2
custom_generate: /content/constrained-beam-search/
device: cuda:0
use_cache=False:
ids: [25, 198, 198, 16, 13, 5765, 262, 6121, 364, 287, 262, 976, 835, 345, 561, 779, 257, 3218, 5408, 13, 198, 198, 17, 13, 5765, 262, 6121, 364, 287, 11059]
text: ':\n\n1. Use the transformers in the same way you would use a regular expression.\n\n2. Use the transformers in translation'
use_cache=True:
ids: [25, 198, 198, 16, 13, 5765, 262, 6121, 364, 287, 262, 976, 835, 345, 561, 779, 257, 3218, 5408, 13, 198, 198, 17, 13, 5765, 262, 6121, 364, 287, 11059]
text: ':\n\n1. Use the transformers in the same way you would use a regular expression.\n\n2. Use the transformers in translation'
Transformers 4 compatibility test
python: 3.12.12
torch: 2.9.0+cu126
transformers: 4.57.6
cuda: True
model: openai-community/gpt2
custom_generate: /content/constrained-beam-search/
device: cuda:0
use_cache=False:
ids: [25, 198, 198, 16, 13, 5765, 262, 6121, 364, 287, 262, 976, 835, 345, 561, 779, 257, 3218, 5408, 13, 198, 198, 17, 13, 5765, 262, 6121, 364, 287, 11059]
text: ':\n\n1. Use the transformers in the same way you would use a regular expression.\n\n2. Use the transformers in translation'
use_cache=True:
ids: [25, 198, 198, 16, 13, 5765, 262, 6121, 364, 287, 262, 976, 835, 345, 561, 779, 257, 3218, 5408, 13, 198, 198, 17, 13, 5765, 262, 6121, 364, 287, 11059]
text: ':\n\n1. Use the transformers in the same way you would use a regular expression.\n\n2. Use the transformers in translation'
So the PR fixes the Transformers v5 cached-decoding regression and keeps the same deterministic output under Transformers 4.57.6.
Yep, same issue and thanks for the PR