forked from keras-team/keras-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d254b02
commit 04d931d
Showing
4 changed files
with
478 additions
and
21 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
keras_nlp/models/whisper/whisper_cached_multi_head_attention.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Copyright 2023 The KerasNLP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Whisper Cached Multi-Head Attention layer.""" | ||
|
||
|
||
import string | ||
|
||
import keras_nlp | ||
from keras_nlp.backend import keras | ||
|
||
|
||
def _index_to_einsum_variable(i): | ||
"""Converts an index to a einsum variable name. | ||
We simply map indices to lowercase characters, e.g. 0 -> 'a', 1 -> 'b'. | ||
""" | ||
return string.ascii_lowercase[i] | ||
|
||
|
||
def _build_proj_equation(free_dims, bound_dims, output_dims): | ||
"""Builds an einsum equation for projections inside multi-head attention.""" | ||
input_str = "" | ||
kernel_str = "" | ||
output_str = "" | ||
bias_axes = "" | ||
letter_offset = 0 | ||
for i in range(free_dims): | ||
char = _index_to_einsum_variable(i + letter_offset) | ||
input_str += char | ||
output_str += char | ||
|
||
letter_offset += free_dims | ||
for i in range(bound_dims): | ||
char = _index_to_einsum_variable(i + letter_offset) | ||
input_str += char | ||
kernel_str += char | ||
|
||
letter_offset += bound_dims | ||
for i in range(output_dims): | ||
char = _index_to_einsum_variable(i + letter_offset) | ||
kernel_str += char | ||
output_str += char | ||
bias_axes += char | ||
equation = f"{input_str},{kernel_str}->{output_str}" | ||
|
||
return equation, bias_axes, len(output_str) | ||
|
||
|
||
def _get_output_shape(output_rank, known_last_dims): | ||
return [None] * (output_rank - len(known_last_dims)) + list(known_last_dims) | ||
|
||
|
||
class WhisperCachedMultiHeadAttention( | ||
keras_nlp.layers.CachedMultiHeadAttention | ||
): | ||
"""Whisper Cached Multi-Head Attention layer. | ||
Inherits from `keras_nlp.layers.CachedMultiHeadAttention`, and overrides the | ||
`build` method so that Q, V projection layers have bias | ||
whereas K projection layer does not. | ||
""" | ||
|
||
def build( | ||
self, | ||
query_shape, | ||
value_shape, | ||
key_shape=None, | ||
): | ||
key_shape = value_shape if key_shape is None else key_shape | ||
query_rank = len(query_shape) | ||
value_rank = len(value_shape) | ||
key_rank = len(key_shape) | ||
einsum_equation, bias_axes, output_rank = _build_proj_equation( | ||
query_rank - 1, bound_dims=1, output_dims=2 | ||
) | ||
self._query_dense = keras.layers.EinsumDense( | ||
einsum_equation, | ||
output_shape=_get_output_shape( | ||
output_rank - 1, [self._num_heads, self._key_dim] | ||
), | ||
bias_axes=bias_axes if self._use_bias else None, | ||
name="query", | ||
**self._get_common_kwargs_for_sublayer(), | ||
) | ||
self._query_dense.build(query_shape) | ||
einsum_equation, bias_axes, output_rank = _build_proj_equation( | ||
key_rank - 1, bound_dims=1, output_dims=2 | ||
) | ||
self._key_dense = keras.layers.EinsumDense( | ||
einsum_equation, | ||
output_shape=_get_output_shape( | ||
output_rank - 1, [self._num_heads, self._key_dim] | ||
), | ||
name="key", | ||
**self._get_common_kwargs_for_sublayer(), | ||
) | ||
self._key_dense.build(key_shape) | ||
einsum_equation, bias_axes, output_rank = _build_proj_equation( | ||
value_rank - 1, bound_dims=1, output_dims=2 | ||
) | ||
self._value_dense = keras.layers.EinsumDense( | ||
einsum_equation, | ||
output_shape=_get_output_shape( | ||
output_rank - 1, [self._num_heads, self._value_dim] | ||
), | ||
bias_axes=bias_axes if self._use_bias else None, | ||
name="value", | ||
**self._get_common_kwargs_for_sublayer(), | ||
) | ||
self._value_dense.build(value_shape) | ||
|
||
# Builds the attention computations for multi-head dot product | ||
# attention. These computations could be wrapped into the keras | ||
# attention layer once it supports multi-head einsum computations. | ||
self._build_attention(output_rank) | ||
self._output_dense = self._make_output_dense( | ||
query_shape, | ||
self._get_common_kwargs_for_sublayer(), | ||
"attention_output", | ||
) | ||
output_dense_input_shape = list( | ||
self._query_dense.compute_output_shape(query_shape) | ||
) | ||
output_dense_input_shape[-1] = self._value_dim | ||
self._output_dense.build(tuple(output_dense_input_shape)) | ||
self.built = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.