File size: 740 Bytes
5bb6ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import evaluate

from config import ModelArgs
from model import Llama

import evaluate

# Load the perplexity metric
perplexity = evaluate.load("perplexity")


def compute_perplexity(model_name, text):

    

    results = perplexity.compute(predictions=[text], model_id=model_name)
    
    return results["perplexities"][0]

# Example Usage
llama = Llama(device=ModelArgs.device, embeddings_dims=ModelArgs.embeddings_dims, no_of_decoder_layers=ModelArgs.no_of_decoder_layers, block_size=ModelArgs.block_size, vocab_size=ModelArgs.vocab_size, dropout=ModelArgs.dropout)
llama = llama.to(ModelArgs.device)

text = "This is an example sentence for perplexity calculation."

ppl = compute_perplexity(llama, text)
print(f"Perplexity: {ppl}")