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