Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,38 +33,33 @@ class RotaryEmbedding(keras.layers.Layer):
|
|
| 33 |
self.dim = dim
|
| 34 |
self.max_len = max_len
|
| 35 |
self.theta = theta
|
|
|
|
| 36 |
|
| 37 |
def build(self, input_shape):
|
| 38 |
-
#
|
| 39 |
-
inv_freq = 1.0 / (self.theta ** (np.arange(0, self.dim, 2, dtype=np.float32) / self.dim))
|
| 40 |
-
t = np.arange(self.max_len, dtype=np.float32)
|
| 41 |
-
freqs = np.outer(t, inv_freq)
|
| 42 |
-
emb = np.concatenate([freqs, freqs], axis=-1)
|
| 43 |
-
|
| 44 |
-
# Create as non-trainable weights instead of tf.constant
|
| 45 |
-
self.cos_cached = self.add_weight(
|
| 46 |
-
name="cos_cached",
|
| 47 |
-
shape=(self.max_len, self.dim),
|
| 48 |
-
initializer=keras.initializers.Constant(np.cos(emb)),
|
| 49 |
-
trainable=False,
|
| 50 |
-
dtype=tf.float32
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
self.sin_cached = self.add_weight(
|
| 54 |
-
name="sin_cached",
|
| 55 |
-
shape=(self.max_len, self.dim),
|
| 56 |
-
initializer=keras.initializers.Constant(np.sin(emb)),
|
| 57 |
-
trainable=False,
|
| 58 |
-
dtype=tf.float32
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
super().build(input_shape)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def rotate_half(self, x):
|
| 64 |
x1, x2 = tf.split(x, 2, axis=-1)
|
| 65 |
return tf.concat([-x2, x1], axis=-1)
|
| 66 |
|
| 67 |
def call(self, q, k):
|
|
|
|
|
|
|
|
|
|
| 68 |
seq_len = tf.shape(q)[2]
|
| 69 |
dtype = q.dtype
|
| 70 |
cos = tf.cast(self.cos_cached[:seq_len, :], dtype)[None, None, :, :]
|
|
|
|
| 33 |
self.dim = dim
|
| 34 |
self.max_len = max_len
|
| 35 |
self.theta = theta
|
| 36 |
+
self.built_cache = False
|
| 37 |
|
| 38 |
def build(self, input_shape):
|
| 39 |
+
# Use the ORIGINAL training code - compute cache on first call, not in build
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
super().build(input_shape)
|
| 41 |
|
| 42 |
+
def _build_cache(self):
|
| 43 |
+
"""Build RoPE cache on first forward pass"""
|
| 44 |
+
if not self.built_cache:
|
| 45 |
+
inv_freq = 1.0 / (self.theta ** (tf.range(0, self.dim, 2, dtype=tf.float32) / self.dim))
|
| 46 |
+
t = tf.range(self.max_len, dtype=tf.float32)
|
| 47 |
+
freqs = tf.einsum("i,j->ij", t, inv_freq)
|
| 48 |
+
emb = tf.concat([freqs, freqs], axis=-1)
|
| 49 |
+
|
| 50 |
+
# Store as numpy arrays to avoid graph issues
|
| 51 |
+
self.cos_cached = tf.constant(np.cos(emb.numpy()), dtype=tf.float32)
|
| 52 |
+
self.sin_cached = tf.constant(np.sin(emb.numpy()), dtype=tf.float32)
|
| 53 |
+
self.built_cache = True
|
| 54 |
+
|
| 55 |
def rotate_half(self, x):
|
| 56 |
x1, x2 = tf.split(x, 2, axis=-1)
|
| 57 |
return tf.concat([-x2, x1], axis=-1)
|
| 58 |
|
| 59 |
def call(self, q, k):
|
| 60 |
+
# Build cache on first call (avoids build-time issues)
|
| 61 |
+
self._build_cache()
|
| 62 |
+
|
| 63 |
seq_len = tf.shape(q)[2]
|
| 64 |
dtype = q.dtype
|
| 65 |
cos = tf.cast(self.cos_cached[:seq_len, :], dtype)[None, None, :, :]
|