Delete indigo/model_keras.py with huggingface_hub
Browse files- indigo/model_keras.py +0 -61
indigo/model_keras.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
import tensorflow as tf
|
| 2 |
-
from keras import layers
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
class PositionEmbedding(layers.Layer):
|
| 6 |
-
def __init__(self, block_size, **kwargs):
|
| 7 |
-
super().__init__(**kwargs)
|
| 8 |
-
self.block_size = block_size
|
| 9 |
-
|
| 10 |
-
def build(self, input_shape):
|
| 11 |
-
self.pos_emb = self.add_weight(
|
| 12 |
-
name="pos_emb", shape=(self.block_size, input_shape[-1]), initializer="random_normal"
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
def call(self, x):
|
| 16 |
-
T = tf.shape(x)[1]
|
| 17 |
-
return x + self.pos_emb[tf.newaxis, :T, :]
|
| 18 |
-
|
| 19 |
-
def get_config(self):
|
| 20 |
-
config = super().get_config()
|
| 21 |
-
config["block_size"] = self.block_size
|
| 22 |
-
return config
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def build_gpt(vocab_size, block_size, n_layer=4, n_head=4, n_embd=128, dropout=0.1, name="indigo"):
|
| 26 |
-
tokens = tf.keras.Input(shape=(None,), dtype="int64", name="tokens")
|
| 27 |
-
x = layers.Embedding(vocab_size, n_embd, name="tok_emb")(tokens)
|
| 28 |
-
x = PositionEmbedding(block_size, name="pos_emb")(x)
|
| 29 |
-
x = layers.Dropout(dropout)(x)
|
| 30 |
-
for i in range(n_layer):
|
| 31 |
-
xn = layers.LayerNormalization(epsilon=1e-5, name=f"ln1_{i}")(x)
|
| 32 |
-
attn = layers.MultiHeadAttention(
|
| 33 |
-
num_heads=n_head, key_dim=n_embd // n_head, dropout=dropout, name=f"attn_{i}"
|
| 34 |
-
)
|
| 35 |
-
x = x + attn(xn, xn, use_causal_mask=True)
|
| 36 |
-
xn = layers.LayerNormalization(epsilon=1e-5, name=f"ln2_{i}")(x)
|
| 37 |
-
h = layers.Dense(4 * n_embd, activation="gelu", name=f"fc_{i}")(xn)
|
| 38 |
-
h = layers.Dense(n_embd, name=f"proj_{i}")(h)
|
| 39 |
-
h = layers.Dropout(dropout)(h)
|
| 40 |
-
x = x + h
|
| 41 |
-
x = layers.LayerNormalization(epsilon=1e-5, name="ln_f")(x)
|
| 42 |
-
logits = layers.Dense(vocab_size, use_bias=False, name="head")(x)
|
| 43 |
-
return tf.keras.Model(tokens, logits, name=name)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def generate(model, idx, max_new_tokens, block_size, temperature=1.0, top_k=None):
|
| 47 |
-
for _ in range(max_new_tokens):
|
| 48 |
-
idx_cond = idx[:, -block_size:]
|
| 49 |
-
logits = model(idx_cond, training=False)[:, -1, :]
|
| 50 |
-
logits = logits / max(temperature, 1e-8)
|
| 51 |
-
if top_k is not None:
|
| 52 |
-
k = min(top_k, int(logits.shape[-1]))
|
| 53 |
-
vals, _ = tf.math.top_k(logits, k=k)
|
| 54 |
-
logits = tf.where(
|
| 55 |
-
logits < vals[:, -1:],
|
| 56 |
-
tf.fill(tf.shape(logits), tf.float32.min),
|
| 57 |
-
logits,
|
| 58 |
-
)
|
| 59 |
-
next_id = tf.random.categorical(logits, num_samples=1, dtype=tf.int64)
|
| 60 |
-
idx = tf.concat([idx, next_id], axis=1)
|
| 61 |
-
return idx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|