Skip to content

Commit f83a7fe

Browse files
authored
Fix dynamic shape handling in Moonshine multi-head attention layer. (#2742)
1 parent a5c9e54 commit f83a7fe

2 files changed

Lines changed: 30 additions & 67 deletions

File tree

keras_hub/src/models/d_fine/d_fine_loss.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,14 @@ def compute_ddf_loss_fn():
619619
mask_flat = keras.ops.reshape(mask_expanded, (-1,))
620620
loss_match_local1 = keras.ops.cond(
621621
keras.ops.any(mask_flat),
622-
lambda: keras.ops.sum(
623-
loss_match_local
624-
* keras.ops.cast(mask_flat, loss_match_local.dtype)
625-
)
626-
/ keras.ops.sum(
627-
keras.ops.cast(mask_flat, loss_match_local.dtype)
622+
lambda: (
623+
keras.ops.sum(
624+
loss_match_local
625+
* keras.ops.cast(mask_flat, loss_match_local.dtype)
626+
)
627+
/ keras.ops.sum(
628+
keras.ops.cast(mask_flat, loss_match_local.dtype)
629+
)
628630
),
629631
lambda: keras.ops.convert_to_tensor(
630632
0.0, dtype=loss_match_local.dtype
@@ -633,12 +635,14 @@ def compute_ddf_loss_fn():
633635
neg_mask_flat = keras.ops.logical_not(mask_flat)
634636
loss_match_local2 = keras.ops.cond(
635637
keras.ops.any(neg_mask_flat),
636-
lambda: keras.ops.sum(
637-
loss_match_local
638-
* keras.ops.cast(neg_mask_flat, loss_match_local.dtype)
639-
)
640-
/ keras.ops.sum(
641-
keras.ops.cast(neg_mask_flat, loss_match_local.dtype)
638+
lambda: (
639+
keras.ops.sum(
640+
loss_match_local
641+
* keras.ops.cast(neg_mask_flat, loss_match_local.dtype)
642+
)
643+
/ keras.ops.sum(
644+
keras.ops.cast(neg_mask_flat, loss_match_local.dtype)
645+
)
642646
),
643647
lambda: keras.ops.convert_to_tensor(
644648
0.0, dtype=loss_match_local.dtype

keras_hub/src/models/moonshine/moonshine_multi_head_attention.py

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import keras
2-
from keras import backend
32

43
from keras_hub.src.layers.modeling.cached_multi_head_attention import (
54
CachedMultiHeadAttention,
@@ -29,42 +28,16 @@ def _rotate_half(x):
2928
Returns:
3029
Tensor: A tensor of shape `[..., 2*d]` with the two halves rotated.
3130
"""
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
6841

6942

7043
def _apply_rotary_pos_emb(t, freqs):
@@ -241,24 +214,10 @@ def build(self, query_shape, value_shape, key_shape=None):
241214
self.built = True
242215

243216
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+
)
262221
n_rows = v_seq_length if for_cache else q_seq_length
263222
ones_mask = keras.ops.ones((1, n_rows, v_seq_length), dtype="int32")
264223
row_index = keras.ops.cumsum(ones_mask, axis=-2)

0 commit comments

Comments
 (0)