Skip to main content

llmppl

llmppl is a Python package for calculating text perplexity using various language models, including GPT-3.5, Llama 2, RWKV, and Mixtral.

Installation

You can install this package via pip:

pip install llmppl

You need to install PyTorch first based on your system configuration. Use the following command to install PyTorch with CUDA 12.4 using pip or conda:

conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia

Usage

Here are some examples of how to use llmppl to calculate text perplexity.

GPT-3.5 Turbo

export OPENAI_API_KEY='YOUROPENAIAPIKEY'

from llmppl import GPTLogProb

model = GPTLogProb(model='gpt-3.5-turbo-instruct')
text = "The quick brown fox jumps over the lazy dog."
logprobs, tokens = model.get_logprobs(text)
print("Log probabilities:", logprobs)

Llama 2

from llmppl import Llama2PPL

llama = Llama2PPL(model_name="meta-llama/Llama-2-7b-hf")
text = "The quick brown fox jumps over the lazy dog."
ppl = llama.calculate_ppl(text)
print(f"Perplexity: {ppl}")

RWKV

from llmppl import RWKVPPL

rwkv = RWKVPPL(model_name="RWKV/rwkv-raven-7b")
text = "The quick brown fox jumps over the lazy dog."
ppl = rwkv.calculate_ppl(text)
print(f"Perplexity: {ppl}")

Perplexity Calculation for Core Models

The llmppl package supports perplexity (PPL) calculation using various language model types including Masked Language Models (MLM), Causal Language Models (CLM), and Encoder-Decoder models. Here are examples of how to use these models for perplexity calculation.

Masked Language Model (MLM) Perplexity

from llmppl import MLMPPL

mlm = MLMPPL(model_name='bert-base-uncased')
text = "The quick brown fox jumps over the lazy dog."
ppl = mlm.calculate_ppl(text)
print(f"Perplexity (MLM): {ppl}")

Causal Language Model (CLM) Perplexity

from llmppl import DecoderPPL

decoder = DecoderPPL(model_name='gpt2')
text = "The quick brown fox jumps over the lazy dog."
ppl = decoder.calculate_ppl(text)
print(f"Perplexity (CLM): {ppl}")

Encoder-Decoder Model Perplexity

from llmppl import EncoderDecoderPPL

enc_dec = EncoderDecoderPPL(model_name='t5-small')
input_text = "translate English to German: The quick brown fox jumps over the lazy dog."
output_text = "Der schnelle braune Fuchs springt über den faulen Hund."
ppl = enc_dec.calculate_ppl(input_text, output_text)
print(f"Perplexity (Encoder-Decoder): {ppl}")

General Perplexity Calculation via LLMPPL

from llmppl import LLMPPL

# Masked Language Model (MLM)
text = "The quick brown fox jumps over the lazy dog."
ppl = LLMPPL.get_perplexity(text, model_type='mlm', model_name='bert-base-uncased')
print(f"Perplexity (MLM): {ppl}")

# Causal Language Model (CLM)
ppl = LLMPPL.get_perplexity(text, model_type='clm', model_name='gpt2')
print(f"Perplexity (CLM): {ppl}")

# Encoder-Decoder Model
input_text = "translate English to German: The quick brown fox jumps over the lazy dog."
output_text = "Der schnelle braune Fuchs springt über den faulen Hund."
ppl = LLMPPL.get_perplexity(input_text, output_text, model_type='enc-dec', model_name='t5-small')
print(f"Perplexity (Encoder-Decoder): {ppl}")

Dependencies

This project requires the following Python packages:

  • torch==2.5.0
  • transformers==4.45.2
  • openai==0.28.0
  • tqdm==4.66.5
  • sentencepiece==0.2.0
  • bitsandbytes==0.44.1
  • accelerate==1.0.1
  • protobuf==5.28.2
  • tiktoken==0.8.0

Contributing

Contributions are welcome! If you have ideas or find bugs, feel free to submit an issue or a pull request.

Citation

If this package is helpful to your work, please consider citing the following paper:

Xu, Z., & Sheng, V. S. (2024, March). Detecting AI-Generated Code Assignments Using Perplexity of Large Language Models. In Proceedings of the AAAI Conference on Artificial Intelligence (Vol. 38, No. 21, pp. 23155-23162).

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for llmppl 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmppl 0.1.3
File Size Uploaded Provenance
llmppl-0.1.3.tar.gz 10.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llmppl 0.1.3
File Interpreter ABI Platform
llmppl-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.7 kB

Release files / llmppl-0.1.3.tar.gz

Download URL llmppl-0.1.3.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ccb6275dbf3be0c56379a4bd71e4b6c1910ed553e888d32cf49cfeae3a2cb3bc
BLAKE2b-256 checksum
How to use checksums
a12aa544c60ce0de1b1c24ce816371f402af5aa3806c9ac54f0f8558ed18a950
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release files / llmppl-0.1.3-py3-none-any.whl

Download URL llmppl-0.1.3-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51b11ebb03352ce92fa6d0eb80b1b868956c778ff8ac018d7df95a2cdc171267
BLAKE2b-256 checksum
How to use checksums
14466ffa2a00ca6a4418d59f384eef1acc95a5fd0e8f684d44980b9ea5b70f82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Security for llmppl 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page