Spaces:
Runtime error
Runtime error
File size: 2,312 Bytes
70ea05e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import numpy as np
def evaluate_perplexity(model_name, revision="main", test_text=None):
"""
Evaluate perplexity on a fixed piece of text.
Args:
model_name: Hugging Face model identifier
revision: Model revision/commit hash
test_text: Text to evaluate perplexity on (default if None)
Returns:
float: Perplexity score (lower is better)
"""
# Default test text if none provided
if test_text is None:
test_text = """Artificial intelligence has transformed the way we live and work, bringing both opportunities and challenges.
From autonomous vehicles to language models that can engage in human-like conversation, AI technologies are becoming increasingly
sophisticated. However, with this advancement comes the responsibility to ensure these systems are developed and deployed ethically,
with careful consideration for privacy, fairness, and transparency. The future of AI will likely depend on how well we balance innovation
with these important social considerations."""
# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained(
model_name,
revision=revision,
torch_dtype=torch.float16,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name, revision=revision)
# Tokenize the text
inputs = tokenizer(test_text, return_tensors="pt")
# Move to same device as model
inputs = {k: v.to(model.device) for k, v in inputs.items()}
# Calculate loss
with torch.no_grad():
outputs = model(**inputs, labels=inputs["input_ids"])
loss = outputs.loss
# Calculate perplexity
perplexity = torch.exp(loss).item()
return perplexity
def create_perplexity_result(model_name, revision, precision, perplexity_score):
"""
Create a result file in the expected format.
"""
return {
"config": {
"model_dtype": f"torch.{precision}",
"model_name": model_name,
"model_sha": revision,
},
"results": {
"perplexity": {
"perplexity": perplexity_score,
}
}
} |