Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add perplexity example #313

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/perplexity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
from datasets import load_dataset
from transformers import AutoTokenizer
from aphrodite import LLM, SamplingParams

# Load the wikitext2 dataset.
dataset = load_dataset('wikitext', 'wikitext-2-raw-v1')

# Get the first 2000 elements from the 'train' split.
prompts = dataset['train']['text'][:2000]

model_id = "mistralai/Mistral-7B-Instruct-v0.2"
# Create a tokenizer.
tokenizer = AutoTokenizer.from_pretrained(model_id)

# Tokenize the prompts and discard or truncate any prompts longer than 2048 tokens.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_length is different below, maybe you meant 4096?

tokenized_prompts = [tokenizer.encode(prompt, truncation=True,
max_length=4096) for prompt in prompts]

# Detokenize the prompts.
detokenized_prompts = [tokenizer.decode(tokens
) for tokens in tokenized_prompts]

# Create a sampling params object.
sampling_params = SamplingParams(
temperature=0.0,
ignore_eos=True,
max_tokens=10,
skip_special_tokens=False,
spaces_between_special_tokens=False,
logprobs=1,
prompt_logprobs=1,
)

# Create an LLM.
llm = LLM(model=model_id)

# Generate texts from the detokenized prompts.
outputs = llm.generate(detokenized_prompts, sampling_params)

# Calculate the perplexity.
all_logprobs = []
for output in outputs:
all_logprobs.extend([next(iter(lp.values())) for lp in output.prompt_logprobs[1:]])

all_logprobs = np.array([lp.logprob for lp in all_logprobs])
# NOTE: we need to divide by 2 to match the perplexity results
# for the same model on llama.cpp. I'm unsure if this
# approach to ppx measurement is correct.
perplexity = (np.exp(-all_logprobs.mean())) / 2
print(f"Perplexity: {perplexity}")
Loading