Views
No views yet
Gemma2-9b-it), it was not developed as a generative tool — its primary use is to compute the perplexity score of Guarani documents. Lower perplexity may indicate text that is more predictable by the model and more similar to the reference high-quality corpus.princeton-nlp/gemma-2-9b-it-SimPOguaran-ia/gntweetsGemma2ForCausalLM42358416143362560008192 tokensfloat16tokenizer.json and tokenizer_config.jsongeneration_config.jsonchat_template.jinja112e-50.01100paged_adamw_8bitlinear6bf162048936 records (1916928 tokens)117 records (239616 tokens)117 records (239616 tokens)princeton-nlp/gemma-2-9b-it-SimPOguaran-ia/gntweets1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import math
4
5model_id = 'guaran-ia/gntweets-lm'
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(model_id)
8model.eval()
9
10def perplexity(text: str) -> float:
11 inputs = tokenizer(text, return_tensors='pt')
12 with torch.no_grad():
13 outputs = model(**inputs, labels=inputs['input_ids'])
14 loss = outputs.loss
15 return math.exp(loss.item())
16
17text = "Your Guarani text here."
18print(f"Perplexity: {perplexity(text):.4f}")1import torch, math
2
3def perplexity_sliding(text: str, model, tokenizer, max_len: int = 8192, stride: int = 4096):
4 """Compute perplexity over long text by slicing into overlapping chunks.
5
6 - `max_len` should be <= model.config.max_position_embeddings (8192).
7 - `stride` controls overlap; larger overlap gives smoother per-token averaging.
8 """
9 enc = tokenizer(text, return_tensors='pt')['input_ids'][0]
10 n = enc.size(0)
11 if n == 0:
12 return float('nan')
13
14 total_nll = 0.0
15 total_tokens = 0
16 start = 0
17 while start < n:
18 end = min(start + max_len, n)
19 input_ids = enc[start:end].unsqueeze(0)
20 with torch.no_grad():
21 outputs = model(input_ids, labels=input_ids)
22 # outputs.loss is the average NLL for the chunk
23 loss = outputs.loss.item()
24 chunk_len = end - start
25 total_nll += loss * chunk_len
26 total_tokens += chunk_len
27 if end == n:
28 break
29 start += stride
30
31 avg_nll = total_nll / total_tokens
32 return math.exp(avg_nll)
33
34# Example usage:
35text = open('some_guarani.txt', encoding='utf-8').read()
36tokenizer.model_max_length = 8192
37print(f"Perplexity (sliding): {perplexity_sliding(text, model, tokenizer):.4f}")LICENSE file in this directory for the full license text.