How to use from
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/contrastive-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/contrastive-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/contrastive-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/contrastive-search",
		"messages": [
			{
				"role": "user",
				"content": "What is the capital of France?"
			}
		]
	}'
Quick Links

Description

Implementation of Contrastive Search, a decoding strategy that jointly optimizes model confidence and a degeneration penalty to produce fluent, coherent, and low-repetition text. At each step, the model considers the top-k candidate tokens and selects the one maximizing:

score(v) = (1 - alpha) * p(v | context) - alpha * max_cosine_similarity(h_v, H_context)

where alpha is the trade-off between confidence and the cosine-similarity-based penalty.

This strategy typically:

  • Reduces repetition compared to greedy/beam search
  • Preserves semantic coherence better than pure sampling

Base model

  • Qwen/Qwen2.5-0.5B-Instruct (example)

Model compatibility

  • Decoder and encoder-decoder transformer models for causal LM

Additional Arguments

  • top_k (int): Number of candidate tokens to consider each step (e.g., 4)
  • penalty_alpha (float): Weight of the degeneration penalty (e.g., 0.6)

Tips:

  • Larger top_k explores more candidates but increases compute
  • penalty_alpha in [0.3, 0.8] often works well; 0.0 reduces to greedy

Output Type changes

(none) โ€” returns the same structure as standard transformers generation


Example usage

from transformers import AutoModelForCausalLM, AutoTokenizer, infer_device

device = infer_device()

model_id = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto").to(device)

inputs = tokenizer(["DeepMind Company is"], return_tensors="pt").to(device)

# Contrastive search
gen_out = model.generate(
    **inputs,
    custom_generate="contrastive_search",
    penalty_alpha=0.6,
    top_k=4,
    max_new_tokens=128,
    trust_remote_code=True,
)

print(tokenizer.batch_decode(gen_out, skip_special_tokens=True))
Downloads last month
109
Safetensors
Model size
0.8B params
Tensor type
BF16
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Collection including transformers-community/contrastive-search