Skip to content

Add logits support to whisper backbone #2134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions keras_hub/src/models/whisper/whisper_backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,13 @@ def get_config(self):
}
)
return config

def logits(self, *args, **kwargs):
result = self(*args, **kwargs)
token_embedding = None
for embedding_type in self.decoder_embeddings.weights:
if "token_embedding" in embedding_type.path:
token_embedding = embedding_type
return keras.ops.matmul(
result["decoder_sequence_output"], keras.ops.transpose(token_embedding)
)
21 changes: 18 additions & 3 deletions keras_hub/src/models/whisper/whisper_backbone_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def test_smallest_preset(self):
"decoder_token_ids": ops.array(
[[50257, 50362, 464, 2068, 7586, 21831, 13, 50256, 50256]]
),
"decoder_padding_mask": ops.array(
[[1, 1, 1, 1, 1, 1, 1, 1, 0]]
),
"decoder_padding_mask": ops.array([[1, 1, 1, 1, 1, 1, 1, 1, 0]]),
},
expected_output_shape={
"encoder_sequence_output": (1, 1500, 384),
Expand All @@ -89,6 +87,23 @@ def test_smallest_preset(self):
},
)

@pytest.mark.large
def test_logits(self):
backbone_cls = WhisperBackbone.from_preset("whisper_tiny_en")
input_data = {
"encoder_features": ops.ones((1, 3000, 80)),
"decoder_token_ids": ops.array(
[[50257, 50362, 464, 2068, 7586, 21831, 13, 50256, 50256]]
),
"decoder_padding_mask": ops.array([[1, 1, 1, 1, 1, 1, 1, 1, 1]]),
}
logits = backbone_cls.logits(input_data)
self.assertEqual(logits.shape, (1, 9, 51864))
self.assertAllEqual(
ops.argmax(ops.squeeze(logits, axis=0), axis=-1),
[50361, 357, 50256, 395, 263, 50256, 50256, 50256, 50256],
)

@pytest.mark.extra_large
def test_all_presets(self):
for preset in WhisperBackbone.presets:
Expand Down
Loading