adyoi commited on
Commit
ac2b9c4
·
verified ·
1 Parent(s): 382bae9

Upload indigotf/model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. indigotf/model.py +16 -11
indigotf/model.py CHANGED
@@ -43,19 +43,24 @@ def build_gpt(vocab_size, block_size, n_layer=4, n_head=4, n_embd=128, dropout=0
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
 
43
  return tf.keras.Model(tokens, logits, name=name)
44
 
45
 
46
+ @tf.function(reduce_retracing=True)
47
+ def _langkah(model, idx_cond, temperature, top_k):
48
+ logits = model(idx_cond, training=False)[:, -1, :]
49
+ logits = logits / max(temperature, 1e-8)
50
+ if top_k is not None:
51
+ k = min(top_k, int(logits.shape[-1]))
52
+ vals, _ = tf.math.top_k(logits, k=k)
53
+ logits = tf.where(
54
+ logits < vals[:, -1:],
55
+ tf.fill(tf.shape(logits), tf.float32.min),
56
+ logits,
57
+ )
58
+ return tf.random.categorical(logits, num_samples=1, dtype=tf.int64)
59
+
60
+
61
  def generate(model, idx, max_new_tokens, block_size, temperature=1.0, top_k=None):
62
  for _ in range(max_new_tokens):
63
  idx_cond = idx[:, -block_size:]
64
+ next_id = _langkah(model, idx_cond, temperature, top_k)
 
 
 
 
 
 
 
 
 
 
65
  idx = tf.concat([idx, next_id], axis=1)
66
  return idx