Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
-
"""Veda Programming
|
| 2 |
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow import keras
|
| 5 |
from tensorflow.keras import layers
|
| 6 |
import numpy as np
|
| 7 |
|
|
|
|
| 8 |
class VedaProgrammingLLM(keras.Model):
|
| 9 |
-
"""
|
| 10 |
|
| 11 |
def __init__(
|
| 12 |
-
self,
|
| 13 |
-
vocab_size: int,
|
| 14 |
-
max_length: int =
|
| 15 |
-
d_model: int = 256,
|
| 16 |
num_heads: int = 8,
|
| 17 |
-
num_layers: int = 4,
|
| 18 |
-
ff_dim: int = 512,
|
| 19 |
**kwargs
|
| 20 |
):
|
| 21 |
super().__init__(**kwargs)
|
|
@@ -27,12 +28,10 @@ class VedaProgrammingLLM(keras.Model):
|
|
| 27 |
self.num_layers = num_layers
|
| 28 |
self.ff_dim = ff_dim
|
| 29 |
|
| 30 |
-
# Embeddings
|
| 31 |
self.token_embedding = layers.Embedding(vocab_size, d_model)
|
| 32 |
self.pos_embedding = layers.Embedding(max_length, d_model)
|
| 33 |
self.dropout = layers.Dropout(0.1)
|
| 34 |
|
| 35 |
-
# Transformer layers
|
| 36 |
self.attn_layers = []
|
| 37 |
self.ffn_layers = []
|
| 38 |
self.ln1_layers = []
|
|
@@ -63,17 +62,14 @@ class VedaProgrammingLLM(keras.Model):
|
|
| 63 |
def call(self, inputs, training=False):
|
| 64 |
seq_len = tf.shape(inputs)[1]
|
| 65 |
|
| 66 |
-
# Causal mask
|
| 67 |
mask = tf.linalg.band_part(tf.ones((seq_len, seq_len)), -1, 0)
|
| 68 |
|
| 69 |
-
# Embeddings
|
| 70 |
positions = tf.range(seq_len)
|
| 71 |
x = self.token_embedding(inputs)
|
| 72 |
x = x * tf.math.sqrt(tf.cast(self.d_model, tf.float32))
|
| 73 |
x = x + self.pos_embedding(positions)
|
| 74 |
x = self.dropout(x, training=training)
|
| 75 |
|
| 76 |
-
# Transformer blocks
|
| 77 |
for i in range(self.num_layers):
|
| 78 |
attn_out = self.attn_layers[i](x, x, attention_mask=mask, training=training)
|
| 79 |
x = self.ln1_layers[i](x + attn_out)
|
|
@@ -86,79 +82,64 @@ class VedaProgrammingLLM(keras.Model):
|
|
| 86 |
def generate(
|
| 87 |
self,
|
| 88 |
prompt_tokens: list,
|
| 89 |
-
max_new_tokens: int =
|
| 90 |
temperature: float = 0.7,
|
| 91 |
top_k: int = 50,
|
| 92 |
top_p: float = 0.9,
|
| 93 |
-
repetition_penalty: float = 1.2,
|
| 94 |
stop_tokens: list = None
|
| 95 |
) -> list:
|
| 96 |
-
"""Generate
|
| 97 |
-
|
| 98 |
generated = list(prompt_tokens)
|
| 99 |
|
| 100 |
-
for
|
| 101 |
-
# Use last max_length tokens
|
| 102 |
context = generated[-self.max_length:]
|
| 103 |
input_tensor = tf.constant([context], dtype=tf.int32)
|
| 104 |
|
| 105 |
-
# Get logits
|
| 106 |
logits = self(input_tensor, training=False)
|
| 107 |
next_logits = logits[0, -1, :].numpy().astype(np.float64)
|
| 108 |
|
| 109 |
-
# Apply repetition penalty
|
| 110 |
if repetition_penalty != 1.0:
|
| 111 |
-
for token_id in set(generated[-
|
| 112 |
if 0 <= token_id < len(next_logits):
|
| 113 |
if next_logits[token_id] > 0:
|
| 114 |
next_logits[token_id] /= repetition_penalty
|
| 115 |
else:
|
| 116 |
next_logits[token_id] *= repetition_penalty
|
| 117 |
|
| 118 |
-
# Apply temperature
|
| 119 |
next_logits = next_logits / max(temperature, 0.1)
|
| 120 |
|
| 121 |
-
# Apply top-k filtering
|
| 122 |
if top_k > 0 and top_k < len(next_logits):
|
| 123 |
indices_to_remove = next_logits < np.partition(next_logits, -top_k)[-top_k]
|
| 124 |
next_logits[indices_to_remove] = -np.inf
|
| 125 |
|
| 126 |
-
# Apply top-p (nucleus) filtering
|
| 127 |
if top_p < 1.0:
|
| 128 |
sorted_indices = np.argsort(next_logits)[::-1]
|
| 129 |
sorted_logits = next_logits[sorted_indices]
|
| 130 |
|
| 131 |
-
|
| 132 |
-
max_logit = np.max(sorted_logits[sorted_logits > -np.inf])
|
| 133 |
exp_logits = np.exp(sorted_logits - max_logit)
|
| 134 |
probs = exp_logits / (np.sum(exp_logits) + 1e-10)
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
sorted_indices_to_remove = cumulative_probs > top_p
|
| 140 |
-
sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].copy()
|
| 141 |
-
sorted_indices_to_remove[0] = False
|
| 142 |
-
|
| 143 |
-
indices_to_remove = sorted_indices[sorted_indices_to_remove]
|
| 144 |
-
next_logits[indices_to_remove] = -np.inf
|
| 145 |
|
| 146 |
-
# Convert to probabilities
|
| 147 |
max_logit = np.max(next_logits[next_logits > -np.inf]) if np.any(next_logits > -np.inf) else 0
|
| 148 |
exp_logits = np.exp(next_logits - max_logit)
|
| 149 |
exp_logits[next_logits == -np.inf] = 0
|
| 150 |
probs = exp_logits / (np.sum(exp_logits) + 1e-10)
|
| 151 |
|
| 152 |
-
# Ensure valid distribution
|
| 153 |
probs = np.clip(probs, 0, 1)
|
| 154 |
prob_sum = np.sum(probs)
|
| 155 |
if prob_sum > 0:
|
| 156 |
probs = probs / prob_sum
|
| 157 |
else:
|
| 158 |
-
# Fallback to uniform
|
| 159 |
probs = np.ones_like(probs) / len(probs)
|
| 160 |
|
| 161 |
-
# Sample
|
| 162 |
try:
|
| 163 |
next_token = np.random.choice(len(probs), p=probs)
|
| 164 |
except ValueError:
|
|
@@ -166,10 +147,8 @@ class VedaProgrammingLLM(keras.Model):
|
|
| 166 |
|
| 167 |
generated.append(int(next_token))
|
| 168 |
|
| 169 |
-
|
| 170 |
-
if next_token == 0: # PAD
|
| 171 |
break
|
| 172 |
-
|
| 173 |
if stop_tokens and next_token in stop_tokens:
|
| 174 |
break
|
| 175 |
|
|
|
|
| 1 |
+
"""Veda Programming Assistant Model"""
|
| 2 |
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow import keras
|
| 5 |
from tensorflow.keras import layers
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
|
| 9 |
class VedaProgrammingLLM(keras.Model):
|
| 10 |
+
"""Conversational Programming Assistant LLM"""
|
| 11 |
|
| 12 |
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
vocab_size: int,
|
| 15 |
+
max_length: int = 512,
|
| 16 |
+
d_model: int = 256,
|
| 17 |
num_heads: int = 8,
|
| 18 |
+
num_layers: int = 4,
|
| 19 |
+
ff_dim: int = 512,
|
| 20 |
**kwargs
|
| 21 |
):
|
| 22 |
super().__init__(**kwargs)
|
|
|
|
| 28 |
self.num_layers = num_layers
|
| 29 |
self.ff_dim = ff_dim
|
| 30 |
|
|
|
|
| 31 |
self.token_embedding = layers.Embedding(vocab_size, d_model)
|
| 32 |
self.pos_embedding = layers.Embedding(max_length, d_model)
|
| 33 |
self.dropout = layers.Dropout(0.1)
|
| 34 |
|
|
|
|
| 35 |
self.attn_layers = []
|
| 36 |
self.ffn_layers = []
|
| 37 |
self.ln1_layers = []
|
|
|
|
| 62 |
def call(self, inputs, training=False):
|
| 63 |
seq_len = tf.shape(inputs)[1]
|
| 64 |
|
|
|
|
| 65 |
mask = tf.linalg.band_part(tf.ones((seq_len, seq_len)), -1, 0)
|
| 66 |
|
|
|
|
| 67 |
positions = tf.range(seq_len)
|
| 68 |
x = self.token_embedding(inputs)
|
| 69 |
x = x * tf.math.sqrt(tf.cast(self.d_model, tf.float32))
|
| 70 |
x = x + self.pos_embedding(positions)
|
| 71 |
x = self.dropout(x, training=training)
|
| 72 |
|
|
|
|
| 73 |
for i in range(self.num_layers):
|
| 74 |
attn_out = self.attn_layers[i](x, x, attention_mask=mask, training=training)
|
| 75 |
x = self.ln1_layers[i](x + attn_out)
|
|
|
|
| 82 |
def generate(
|
| 83 |
self,
|
| 84 |
prompt_tokens: list,
|
| 85 |
+
max_new_tokens: int = 200,
|
| 86 |
temperature: float = 0.7,
|
| 87 |
top_k: int = 50,
|
| 88 |
top_p: float = 0.9,
|
| 89 |
+
repetition_penalty: float = 1.2,
|
| 90 |
stop_tokens: list = None
|
| 91 |
) -> list:
|
| 92 |
+
"""Generate response"""
|
|
|
|
| 93 |
generated = list(prompt_tokens)
|
| 94 |
|
| 95 |
+
for _ in range(max_new_tokens):
|
|
|
|
| 96 |
context = generated[-self.max_length:]
|
| 97 |
input_tensor = tf.constant([context], dtype=tf.int32)
|
| 98 |
|
|
|
|
| 99 |
logits = self(input_tensor, training=False)
|
| 100 |
next_logits = logits[0, -1, :].numpy().astype(np.float64)
|
| 101 |
|
|
|
|
| 102 |
if repetition_penalty != 1.0:
|
| 103 |
+
for token_id in set(generated[-100:]):
|
| 104 |
if 0 <= token_id < len(next_logits):
|
| 105 |
if next_logits[token_id] > 0:
|
| 106 |
next_logits[token_id] /= repetition_penalty
|
| 107 |
else:
|
| 108 |
next_logits[token_id] *= repetition_penalty
|
| 109 |
|
|
|
|
| 110 |
next_logits = next_logits / max(temperature, 0.1)
|
| 111 |
|
|
|
|
| 112 |
if top_k > 0 and top_k < len(next_logits):
|
| 113 |
indices_to_remove = next_logits < np.partition(next_logits, -top_k)[-top_k]
|
| 114 |
next_logits[indices_to_remove] = -np.inf
|
| 115 |
|
|
|
|
| 116 |
if top_p < 1.0:
|
| 117 |
sorted_indices = np.argsort(next_logits)[::-1]
|
| 118 |
sorted_logits = next_logits[sorted_indices]
|
| 119 |
|
| 120 |
+
max_logit = np.max(sorted_logits[sorted_logits > -np.inf]) if np.any(sorted_logits > -np.inf) else 0
|
|
|
|
| 121 |
exp_logits = np.exp(sorted_logits - max_logit)
|
| 122 |
probs = exp_logits / (np.sum(exp_logits) + 1e-10)
|
| 123 |
|
| 124 |
+
cumulative = np.cumsum(probs)
|
| 125 |
+
remove_mask = cumulative > top_p
|
| 126 |
+
remove_mask[1:] = remove_mask[:-1].copy()
|
| 127 |
+
remove_mask[0] = False
|
| 128 |
|
| 129 |
+
next_logits[sorted_indices[remove_mask]] = -np.inf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
|
|
|
| 131 |
max_logit = np.max(next_logits[next_logits > -np.inf]) if np.any(next_logits > -np.inf) else 0
|
| 132 |
exp_logits = np.exp(next_logits - max_logit)
|
| 133 |
exp_logits[next_logits == -np.inf] = 0
|
| 134 |
probs = exp_logits / (np.sum(exp_logits) + 1e-10)
|
| 135 |
|
|
|
|
| 136 |
probs = np.clip(probs, 0, 1)
|
| 137 |
prob_sum = np.sum(probs)
|
| 138 |
if prob_sum > 0:
|
| 139 |
probs = probs / prob_sum
|
| 140 |
else:
|
|
|
|
| 141 |
probs = np.ones_like(probs) / len(probs)
|
| 142 |
|
|
|
|
| 143 |
try:
|
| 144 |
next_token = np.random.choice(len(probs), p=probs)
|
| 145 |
except ValueError:
|
|
|
|
| 147 |
|
| 148 |
generated.append(int(next_token))
|
| 149 |
|
| 150 |
+
if next_token == 0 or next_token == 3:
|
|
|
|
| 151 |
break
|
|
|
|
| 152 |
if stop_tokens and next_token in stop_tokens:
|
| 153 |
break
|
| 154 |
|