Spaces:
Sleeping
Sleeping
File size: 5,186 Bytes
54ad1e5 | 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | from __future__ import annotations
import os
os.environ["KERAS_BACKEND"] = "jax"
import numpy as np
import jax
import keras
from veylon_model import create_llm
from tokenizer import TokenizerWrapper
from config import (
CONTEXT,
vocab_size,
D_MODEL,
numberoflayers,
numberofheads,
d_Latent,
ffn_mult,
num_kv_heads,
swa_window,
)
# ============================================================
# Runtime info
# ============================================================
print(f"Backend: {keras.backend.backend()}")
print(f"JAX devices: {jax.devices()}")
keras.mixed_precision.set_global_policy("mixed_bfloat16")
# ============================================================
# Load tokenizer
# ============================================================
tokenizer = TokenizerWrapper("tokenizer.model")
assert tokenizer.vocab_size == vocab_size, (
f"Tokenizer vocab ({tokenizer.vocab_size}) "
f"!= config vocab ({vocab_size})"
)
print(f"Tokenizer vocab size: {tokenizer.vocab_size}")
# ============================================================
# Build model (must exactly match training)
# ============================================================
print("Building model...")
model = create_llm(
vocab_size=vocab_size,
d_model=D_MODEL,
n_layers=numberoflayers,
n_heads=numberofheads,
d_latent=d_Latent,
ffn_mult=ffn_mult,
max_seq_len=CONTEXT,
use_moe=False,
num_kv_heads=num_kv_heads,
swa_window=swa_window,
)
# Warmup with EXACT training/inference shape
dummy = np.zeros((1, CONTEXT), dtype=np.int32)
_ = model(dummy, training=False)
print("✓ Model built successfully")
# ============================================================
# Load weights
# ============================================================
WEIGHTS_PATH = "veylon_final.weights.h5"
print(f"Loading weights from: {WEIGHTS_PATH}")
model.load_weights(WEIGHTS_PATH)
print("✓ Weights loaded successfully")
# ============================================================
# Sampling settings
# ============================================================
MAX_NEW_TOKENS = 64
TEMPERATURE = 0.8
TOP_K = 50
def sample_from_logits(
logits: np.ndarray,
temperature: float = 0.8,
top_k: int = 50,
) -> int:
"""
NumPy-only sampling to avoid JAX/readonly array issues.
"""
logits = np.array(logits, dtype=np.float32, copy=True)
if temperature > 0:
logits = logits / float(max(temperature, 1e-8))
if top_k > 0:
k = min(int(top_k), logits.shape[-1])
row = logits[0]
top_indices = np.argpartition(row, -k)[-k:]
filtered = np.full_like(row, -np.inf)
filtered[top_indices] = row[top_indices]
logits[0] = filtered
row = logits[0]
row = row - np.max(row)
probs = np.exp(row)
probs = probs / probs.sum()
return int(np.random.choice(len(probs), p=probs))
# ============================================================
# Generation loop
# ============================================================
while True:
prompt = input("\nEnter your prompt (or 'exit'): ").strip()
if prompt.lower() in {"exit", "quit"}:
break
tokens = tokenizer.encode(
prompt,
add_bos=True,
add_eos=False,
)
if len(tokens) == 0:
tokens = [tokenizer.bos_id if hasattr(tokenizer, "bos_id") else 1]
tokens = tokens[-CONTEXT:]
print("\nGenerating...\n")
# Prompt prefill (one-time)
prompt_ids = np.array([tokens], dtype=np.int32)
logits, cache_k, cache_v = model.generate_step(
prompt_ids,
cache_k=None,
cache_v=None,
cache_pos=0,
)
next_token = sample_from_logits(
np.array(logits[:, -1, :], dtype=np.float32, copy=True),
temperature=TEMPERATURE,
top_k=TOP_K,
)
tokens.append(next_token)
if next_token != tokenizer.eos_id and len(tokens) < CONTEXT:
# After prefill, we are decoding token-by-token.
cache_pos = len(prompt_ids[0])
for _ in range(MAX_NEW_TOKENS - 1):
next_input = np.array([[next_token]], dtype=np.int32)
logits, cache_k, cache_v = model.generate_step(
next_input,
cache_k=cache_k,
cache_v=cache_v,
cache_pos=cache_pos,
)
cache_pos += 1
next_token = sample_from_logits(
np.array(logits[:, -1, :], dtype=np.float32, copy=True),
temperature=TEMPERATURE,
top_k=TOP_K,
)
tokens.append(next_token)
if next_token == tokenizer.eos_id:
break
if len(tokens) >= CONTEXT:
print("\n[Context limit reached]")
break
generated_text = tokenizer.decode(tokens)
print("\n" + "=" * 60)
print("Veylon Alpha")
print("=" * 60)
print(generated_text)
print("=" * 60) |