Skip to main content

A library for advanced LLM sampling techniques

Project description

Samplers

A Python library for advanced LLM sampling techniques, providing a collection of sophisticated sampling methods for language models.

Features

  • Temperature Scaling
  • Top-K Sampling
  • Top-P (Nucleus) Sampling
  • Min-P Sampling
  • Anti-Slop Sampling
  • XTC (Exclude Top Choices) Sampling

Installation

From PyPI

pip install samplers

From Source

  1. Clone the repository:
git clone https://github.com/iantimmis/samplers.git
cd samplers
  1. Create and activate a virtual environment (recommended):
# Using venv
python -m venv .venv
source .venv/bin/activate  # On Unix/macOS
# or
.venv\Scripts\activate  # On Windows

# Using uv (recommended)
uv venv
source .venv/bin/activate  # On Unix/macOS
# or
.venv\Scripts\activate  # On Windows
  1. Install the package in development mode:
# Using pip
pip install -e .

# Using uv (recommended)
uv pip install -e .

Development

Running Tests

The test suite uses pytest. To run the tests:

# Install test dependencies
pip install pytest

# Run all tests
python -m pytest tests/

# Run tests with verbose output
python -m pytest tests/ -v

# Run a specific test file
python -m pytest tests/test_temperature.py

Usage

from samplers import TemperatureSampler, TopKSampler, TopPSampler, MinPSampler, AntiSlopSampler, XTCSampler

# Initialize a sampler
sampler = TemperatureSampler(temperature=0.7)

# Use with a HuggingFace model
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")

# Generate text with the sampler
input_text = "Once upon a time"
input_ids = tokenizer.encode(input_text, return_tensors="pt")
output_ids = sampler.sample(model, input_ids)
generated_text = tokenizer.decode(output_ids[0])

Available Samplers

Temperature Scaling

Adjusts the "sharpness" of the probability distribution:

  • Low temperature (<1.0): More deterministic, picks high-probability tokens
  • High temperature (>1.0): More random, flatter distribution

Top-K Sampling

Considers only the 'k' most probable tokens, filtering out unlikely ones.

Top-P (Nucleus) Sampling

Selects the smallest set of tokens whose cumulative probability exceeds threshold 'p'.

Min-P Sampling

Dynamically adjusts the sampling pool size based on the probability of the most likely token.

Anti-Slop

Down-weights probabilities at word & phrase level, using backtracking to retry with adjusted probabilities.

XTC (Exclude Top Choices)

Enhances creativity by nudging the model away from its most predictable choices.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_samplers-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_samplers-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file llm_samplers-0.1.0.tar.gz.

File metadata

  • Download URL: llm_samplers-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for llm_samplers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2fecdfa3244811a6614a0083c0186f5ee5dc11a63ebc91c984023e4ef63c1d83
MD5 aa355f7c5d45724a7f3ce51621249782
BLAKE2b-256 25731aeec7e3e9860ce80519e9509c8898179bcb4fadbfa2d489866a4a3283d0

See more details on using hashes here.

File details

Details for the file llm_samplers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llm_samplers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for llm_samplers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 729053695606ed1ea4923e4428531bd8192ec37e0d09fdf28e326063f2166b09
MD5 2af587c314dc4f0d9d629b783ce7feb3
BLAKE2b-256 cca0eed6acc548c5c9932d08e8479abdd930bcede0dc4804aacae8fc316ebb36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page