Skip to content

Instantly share code, notes, and snippets.

View vndee's full-sized avatar
🎯
Focusing

Duy Huynh vndee

🎯
Focusing
View GitHub Profile
def generate(
self,
prompt_tokens: List[List[int]],
max_gen_len: int,
temperature: float = 0.6,
top_p: float = 0.9,
logprobs: bool = False,
echo: bool = False,
) -> Tuple[List[List[int]], Optional[List[List[float]]]]:
# ....
import matplotlib.pyplot as plt
import numpy as np
def plot_temperature_effects(logits, temperatures):
"""
Visualize how different temperatures affect probability distribution
"""
plt.figure(figsize=(12, 6))
x = np.arange(len(logits))
def sample_next_token(logits, temperature=1.0):
"""Sample next token from language model logits"""
# Apply temperature
scaled_logits = [l/temperature for l in logits]
# Convert to probabilities with softmax
max_logit = max(scaled_logits)
exp_logits = [math.exp(l - max_logit) for l in scaled_logits]
total = sum(exp_logits)
probs = [e/total for e in exp_logits]
# For most things, Python's built-in tools work great
import random
random.seed(42)
random.random() # Gets you a random decimal
# Different parts of your program should get their own random numbers
game_rng = MersenneTwister(seed1) # For game stuff
ai_rng = MersenneTwister(seed2) # For AI stuff
# Same starting number = same "random" sequence
rng1 = MersenneTwister(42)
rng2 = MersenneTwister(42)
assert rng1.extract_number() == rng2.extract_number()
# Using operating system's secure random source
import os
true_random_bytes = os.urandom(16)
# On Linux systems
with open("/dev/random", "rb") as f:
hardware_random = f.read(16)
def sample_next_token(rng, logits, temperature=1.0):
"""Sample next token from language model logits"""
# Apply temperature
scaled_logits = [l/temperature for l in logits]
# Convert to probabilities with softmax
max_logit = max(scaled_logits)
exp_logits = [math.exp(l - max_logit) for l in scaled_logits]
total = sum(exp_logits)
probs = [e/total for e in exp_logits]
def estimate_pi(rng, n=1000000):
inside = 0
for _ in range(n):
x = uniform(rng, -1, 1)
y = uniform(rng, -1, 1)
if x*x + y*y <= 1:
inside += 1
return 4 * inside / n
mt = MersenneTwister(42)
def random_choice(rng, options, weights=None):
"""Pick something randomly from a list"""
if weights is None:
# Simple case: everything has equal chance
index = rng.extract_number() % len(options)
return options[index]
# Complex case: some things are more likely than others
# Generate random value in [0, sum(weights))
r = random_float(rng) * sum(weights)