|
1 | 1 | import keras |
2 | | -from keras import backend |
3 | 2 |
|
4 | 3 | from keras_hub.src.layers.modeling.cached_multi_head_attention import ( |
5 | 4 | CachedMultiHeadAttention, |
@@ -29,42 +28,16 @@ def _rotate_half(x): |
29 | 28 | Returns: |
30 | 29 | Tensor: A tensor of shape `[..., 2*d]` with the two halves rotated. |
31 | 30 | """ |
32 | | - # Conditional for Tensorflow backend. |
33 | | - if backend.backend() == "tensorflow": |
34 | | - x_shape = keras.ops.shape(x) |
35 | | - last_dim = x_shape[-1] |
36 | | - d = last_dim // 2 |
37 | | - x_shape_tensor = keras.ops.convert_to_tensor(x_shape) |
38 | | - new_shape = keras.ops.concatenate( |
39 | | - [x_shape_tensor[:-1], keras.ops.convert_to_tensor([d, 2])], axis=0 |
40 | | - ) |
41 | | - x = keras.ops.reshape(x, new_shape) |
42 | | - x1 = x[..., 0] |
43 | | - x2 = x[..., 1] |
44 | | - x_rotated = keras.ops.stack([-x2, x1], axis=-1) |
45 | | - x_rotated = keras.ops.reshape(x_rotated, x_shape) |
46 | | - return x_rotated |
47 | | - |
48 | | - # Conditional for PyTorch and JAX backends. |
49 | | - if backend.backend() == "torch" or backend.backend() == "jax": |
50 | | - x_shape = keras.ops.shape(x) |
51 | | - x_shape_tuple = tuple( |
52 | | - int(keras.ops.convert_to_numpy(dim).item()) for dim in x_shape |
53 | | - ) |
54 | | - last_dim = x_shape_tuple[-1] |
55 | | - d = last_dim // 2 |
56 | | - new_shape = x_shape_tuple[:-1] + (d, 2) |
57 | | - x = keras.ops.reshape(x, new_shape) |
58 | | - x1 = x[..., 0] |
59 | | - x2 = x[..., 1] |
60 | | - x_rotated = keras.ops.stack([-x2, x1], axis=-1) |
61 | | - x_rotated = keras.ops.reshape(x_rotated, x_shape_tuple) |
62 | | - return x_rotated |
63 | | - |
64 | | - else: |
65 | | - raise NotImplementedError( |
66 | | - "Backend not supported. Please use TensorFlow, PyTorch, or JAX." |
67 | | - ) |
| 31 | + x_shape = keras.ops.shape(x) |
| 32 | + last_dim = x_shape[-1] |
| 33 | + d = last_dim // 2 |
| 34 | + new_shape = x_shape[:-1] + (d, 2) |
| 35 | + x = keras.ops.reshape(x, new_shape) |
| 36 | + x1 = x[..., 0] |
| 37 | + x2 = x[..., 1] |
| 38 | + x_rotated = keras.ops.stack([-x2, x1], axis=-1) |
| 39 | + x_rotated = keras.ops.reshape(x_rotated, x_shape) |
| 40 | + return x_rotated |
68 | 41 |
|
69 | 42 |
|
70 | 43 | def _apply_rotary_pos_emb(t, freqs): |
@@ -241,24 +214,10 @@ def build(self, query_shape, value_shape, key_shape=None): |
241 | 214 | self.built = True |
242 | 215 |
|
243 | 216 | def _compute_causal_mask(self, query, value=None, for_cache=False): |
244 | | - if backend.backend() == "torch" or backend.backend() == "jax": |
245 | | - q_seq_length = int( |
246 | | - keras.ops.convert_to_numpy(keras.ops.shape(query)[1]).item() |
247 | | - ) |
248 | | - v_seq_length = ( |
249 | | - int( |
250 | | - keras.ops.convert_to_numpy(keras.ops.shape(value)[1]).item() |
251 | | - ) |
252 | | - if value is not None |
253 | | - else q_seq_length |
254 | | - ) |
255 | | - elif backend.backend() == "tensorflow": |
256 | | - if for_cache: |
257 | | - assert value is not None |
258 | | - v_seq_length = keras.ops.shape(value)[1] |
259 | | - else: |
260 | | - v_seq_length = keras.ops.shape(query)[1] |
261 | | - q_seq_length = keras.ops.shape(query)[1] |
| 217 | + q_seq_length = keras.ops.shape(query)[1] |
| 218 | + v_seq_length = ( |
| 219 | + keras.ops.shape(value)[1] if value is not None else q_seq_length |
| 220 | + ) |
262 | 221 | n_rows = v_seq_length if for_cache else q_seq_length |
263 | 222 | ones_mask = keras.ops.ones((1, n_rows, v_seq_length), dtype="int32") |
264 | 223 | row_index = keras.ops.cumsum(ones_mask, axis=-2) |
|
0 commit comments