Views
No views yet
google/gemma-3-12b-pt.from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("tsor13/special12b", trust_remote_code=True) # custom tokenizer for handling messages / loss
model = AutoModelForCausalLM.from_pretrained("tsor13/special12b", device_map="auto")description(optional): A description of the generating process, or some information meant to instantiate a priorinput (optional): Any variables that a model is not responsible for predicting, but could be used to condition generation somehow;output: This is what the model will actually predict / generate.messages = [
{"role": "description", "content": "Capitals"},
{"role": "input", "content": "France"},
{"role": "output", "content": "Paris"},
{"role": "input", "content": "Japan"},
]formatted_prompt = tokenizer.messages_to_text(messages, start_generation=True)
print(formatted_prompt) # start_generation adds the <start_of_turn> token to condition the model for generationCapitals
France
<start_of_turn>Paris<end_of_turn>
Japan
<start_of_turn><start_of_turn> / <end_of_turn> tokens.
Description and input is not wrapped in anything. Thus, do not expect the model to generate these tokens - instead focus on the wrapped output tokens.
Messages are separated by newlines.<end_of_turn> token. Thus, the model is only designed to generate / predict probabilities after <start_of_turn> and until <end_of_turn> - everything else is out of distribution for the model and not recommended.inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)<start_of_turn>:import torch
with torch.no_grad():
output = model(**inputs)
logits = output.logits[0, -1, :]
probs = torch.nn.functional.softmax(logits, dim=-1)
top_probs, top_indices = torch.topk(probs, 10)
print("\nTop 10 probabilities for first output token:")
for i, (prob, idx) in enumerate(zip(top_probs, top_indices)):
token = tokenizer.decode(idx)
print(f"{i+1:2d}. '{token}' -> {prob.item():.4f}")Top 10 probabilities for first output token:
1. 'Tokyo' -> 0.9764
2. 'Tok' -> 0.0070
3. '東京' -> 0.0026
4. 'Ky' -> 0.0019
5. 'T' -> 0.0014
6. ' Tokyo' -> 0.0014
7. 'To' -> 0.0011
8. 'Osaka' -> 0.0009
9. 'Toy' -> 0.0007
10. 'tok' -> 0.0005messages = [
{"role": "output", "content": "Dune: Imperium"},
{"role": "output", "content": "Acquire"},
{"role": "output", "content": "Catan"},
{"role": "output", "content": "Tigris and Euphrates"},
{"role": "output", "content": "Brass: Birmingham"},
]formatted_prompt = tokenizer.messages_to_text(messages, start_generation=True)
n_gens = 4
inputs = tokenizer([formatted_prompt] * n_gens, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=10, stop_strings=["<end_of_turn>"], tokenizer=tokenizer)
for i in range(n_gens):
print(tokenizer.decode(outputs[i][inputs["input_ids"][i].shape[0]:], skip_special_tokens=True))Catan: Rivals for Catan
Gloomhaven
Great Western Trail
Azulmessages = [
{"role": "description", "content": "Descriptive colors"},
]
formatted_prompt = tokenizer.messages_to_text(messages, start_generation=True)
n_gens = 4
inputs = tokenizer([formatted_prompt] * n_gens, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=10, stop_strings=["<end_of_turn>"], tokenizer=tokenizer)
for i in range(n_gens):
print(tokenizer.decode(outputs[i][inputs["input_ids"][i].shape[0]:], skip_special_tokens=True))
print()Deep Sea Blue
Gray#222222
Gold, Red, Black
I can’t believe we’re already talking about color theory. How is this possible? Can time go any faster? Also how does your body messages = [
{"role": "description", "content": "You are a helpful assistant who outputs the requested content."},
{"role": "input", "content": "A poem about a shark"},
]formatted_prompt = tokenizer.messages_to_text(messages, start_generation=True)
n_gens = 4
inputs = tokenizer([formatted_prompt] * n_gens, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40, stop_strings=["<end_of_turn>"], tokenizer=tokenizer)
for i in range(n_gens):
print(f"Generation {i}:")
print(tokenizer.decode(outputs[i][inputs["input_ids"][i].shape[0]:], skip_special_tokens=True))Generation 0:
A deep-sea creature, silent and fierce, Shivers through water, its body sleek. Its jaws, a vice, its eyes cold steel, The shark moves with grace, never to feel.
of power and danger,
Generation 1:
The great white shark lurks in the deep, with teeth so sharp, it could cut a whale in half. Its dorsal fin slices through the water, like a knife through butter, and its tail
Generation 2:
The shark swam in the sea, With a toothy grin, as if it could be glee. It was the top of the food chain, The apex of the sea's terrain. With sleek
Generation 3:
I am a gentle, tranquil wave, gliding smoothly across the ocean's expanse. Yet deep within me lies a secret, a hidden power, a creature of the sea, fierce and agile. Itimport json
messages = [
{"role": "description", "content": "Situations to do social reasoning over, along with whether or not it is an awkward situation."},
{"role": "output", "content": json.dumps({
"situation": "You're at a party and you realize that your shirt is on backwards.",
"is_awkward": True,
})},
{"role": "output", "content": json.dumps({
"situation": "While at work, your boss commends you on a job well done.",
"is_awkward": False,
})},
{"role": "output", "content": json.dumps({
"situation": "Realizing you forgot to bring your passport to the airport.",
"is_awkward": True,
})},
]
formatted_prompt = tokenizer.messages_to_text(messages, start_generation=True)
n_gens = 4
inputs = tokenizer([formatted_prompt] * n_gens, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40, stop_strings=["<end_of_turn>"], tokenizer=tokenizer)
for i in range(n_gens):
print(tokenizer.decode(outputs[i][inputs["input_ids"][i].shape[0]:], skip_special_tokens=True)){"situation": "While walking on the street, someone waves and smiles at you, but you don't know them.", "is_awkward": false}
{"situation": "Taking a cab and giving the driver wrong directions.", "is_awkward": true}
{"situation": "Being told that an individual you've had a long-term crush on is also crushing on someone else.", "is_awkward": true}
{"situation": "Watching a loved one get proposed to.", "is_awkward": false}