-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathmistral_transformer_decoder.py
More file actions
253 lines (217 loc) · 9.33 KB
/
mistral_transformer_decoder.py
File metadata and controls
253 lines (217 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import keras
from keras import ops
from keras_hub.src.layers.modeling.transformer_layer_utils import (
compute_causal_mask,
)
from keras_hub.src.layers.modeling.transformer_layer_utils import (
merge_padding_and_attention_mask,
)
from keras_hub.src.models.mistral.mistral_attention import (
CachedMistralAttention,
)
from keras_hub.src.models.mistral.mistral_layer_norm import (
MistralLayerNormalization,
)
from keras_hub.src.utils.keras_utils import clone_initializer
class MistralTransformerDecoder(keras.layers.Layer):
"""A Transformer decoder layer for the Mistral backbone."""
def __init__(
self,
intermediate_dim,
num_query_heads,
num_key_value_heads,
rope_max_wavelength=10000,
rope_scaling_factor=1.0,
activation="silu",
layer_norm_epsilon=1e-5,
kernel_initializer="glorot_uniform",
sliding_window=512,
dropout=0,
**kwargs,
):
super().__init__(**kwargs)
self.intermediate_dim = intermediate_dim
self.num_query_heads = num_query_heads
self.num_key_value_heads = num_key_value_heads
self.rope_max_wavelength = rope_max_wavelength
self.rope_scaling_factor = rope_scaling_factor
self.dropout = dropout
self.sliding_window = sliding_window
self.activation = keras.activations.get(activation)
self.layer_norm_epsilon = layer_norm_epsilon
self.kernel_initializer = keras.initializers.get(kernel_initializer)
self.supports_masking = True
def build(self, decoder_sequence_shape):
self._decoder_sequence_shape = decoder_sequence_shape
self.hidden_dim = decoder_sequence_shape[-1]
# Self attention layer.
self._self_attention_layer = CachedMistralAttention(
num_query_heads=self.num_query_heads,
num_key_value_heads=self.num_key_value_heads,
rope_max_wavelength=self.rope_max_wavelength,
rope_scaling_factor=self.rope_scaling_factor,
sliding_window=self.sliding_window,
kernel_initializer=clone_initializer(self.kernel_initializer),
dropout=self.dropout,
dtype=self.dtype_policy,
name="self_attention",
)
self._self_attention_layer.build(decoder_sequence_shape)
self._self_attention_layernorm = MistralLayerNormalization(
epsilon=self.layer_norm_epsilon,
dtype=self.dtype_policy,
name="self_attention_layernorm",
)
self._self_attention_layernorm.build(decoder_sequence_shape)
self._self_attention_dropout = keras.layers.Dropout(
rate=self.dropout,
dtype=self.dtype_policy,
name="self_attention_dropout",
)
# Feedforward layers.
self._feedforward_intermediate_dense = keras.layers.Dense(
self.intermediate_dim,
kernel_initializer=clone_initializer(self.kernel_initializer),
use_bias=False,
dtype=self.dtype_policy,
name="feedforward_intermediate_dense",
)
self._feedforward_intermediate_dense.build(decoder_sequence_shape)
self._feedforward_gate_dense = keras.layers.Dense(
self.intermediate_dim,
kernel_initializer=clone_initializer(self.kernel_initializer),
use_bias=False,
dtype=self.dtype_policy,
name="feedforward_gate_dense",
)
self._feedforward_gate_dense.build(decoder_sequence_shape)
self._feedforward_output_dense = keras.layers.Dense(
self.hidden_dim,
kernel_initializer=clone_initializer(self.kernel_initializer),
use_bias=False,
dtype=self.dtype_policy,
name="feedforward_output_dense",
)
self._feedforward_output_dense.build(
self._feedforward_gate_dense.compute_output_shape(
decoder_sequence_shape
)
)
self._feedforward_layernorm = MistralLayerNormalization(
epsilon=self.layer_norm_epsilon,
dtype=self.dtype_policy,
name="feedforward_layernorm",
)
self._feedforward_layernorm.build(decoder_sequence_shape)
self.built = True
def call(
self,
decoder_sequence,
decoder_padding_mask=None,
decoder_attention_mask=None,
self_attention_cache=None,
self_attention_cache_update_index=None,
training=None,
):
self_attention_mask = self._compute_self_attention_mask(
decoder_sequence=decoder_sequence,
decoder_padding_mask=decoder_padding_mask,
decoder_attention_mask=decoder_attention_mask,
self_attention_cache=self_attention_cache,
self_attention_cache_update_index=self_attention_cache_update_index,
)
residual = decoder_sequence
x = self._self_attention_layernorm(decoder_sequence)
# Self attention block.
x = self._self_attention_layer(
hidden_states=x,
attention_mask=self_attention_mask,
cache=self_attention_cache,
cache_update_index=self_attention_cache_update_index,
)
if self_attention_cache is not None:
x, self_attention_cache = x
x = self._self_attention_dropout(x, training=training)
x = x + residual
residual = x
x = self._feedforward_layernorm(x)
gate_output = self._feedforward_gate_dense(x)
# Note that we run the activation function in full 32-bit
# precision since this is what `torch.nn.functional.silu`
# does. Internally, `torch.nn.functional.silu` converts the
# inputs to float32, computes SiLU, and converts the outputs
# back to compute dtype.
# CPU Kernel: https://github.com/pytorch/pytorch/blob/35c493f2cf9b623bfdc7e6b34dc1cb39690a7919/aten/src/ATen/native/cpu/Activation.cpp#L1221-L1235 # noqa: E501
# CUDA Kernel: https://github.com/pytorch/pytorch/blob/35c493f2cf9b623bfdc7e6b34dc1cb39690a7919/aten/src/ATen/native/cuda/ActivationSiluKernel.cu # noqa: E501
gate_output = ops.cast(gate_output, "float32")
gate_output = self.activation(gate_output)
gate_output = ops.cast(gate_output, self.compute_dtype)
x = self._feedforward_intermediate_dense(x)
x = self._feedforward_output_dense(ops.multiply(x, gate_output))
decoder_output = x + residual
if self_attention_cache is not None:
return decoder_output, self_attention_cache
return decoder_output
def _compute_self_attention_mask(
self,
decoder_sequence,
decoder_padding_mask,
decoder_attention_mask,
self_attention_cache,
self_attention_cache_update_index,
):
decoder_mask = merge_padding_and_attention_mask(
decoder_sequence, decoder_padding_mask, decoder_attention_mask
)
batch_size = ops.shape(decoder_sequence)[0]
input_length = output_length = ops.shape(decoder_sequence)[1]
# We need to handle a rectangular causal mask when doing cached
# decoding. For generative inference, `decoder_sequence` will
# generally be length 1, and `cache` will be the full generation length.
if self_attention_cache is not None:
input_length = ops.shape(self_attention_cache)[2]
cache_update_index = (
0
if self_attention_cache_update_index is None
else self_attention_cache_update_index
)
# The lower triangular attention mask
causal_mask = compute_causal_mask(
batch_size, input_length, output_length, cache_update_index
)
# Mistral uses a banded attention mask if sliding window is not None
if self.sliding_window is not None:
# Below is a workaround for `ops.triu` for Keras 2.
# TODO(tirthasheshpatel): Use `ops.triu` once Keras 2 support is
# removed.
# causal_mask = ops.triu(causal_mask, k=-self.sliding_window)
i = ops.arange(output_length)[:, None] + cache_update_index
j = ops.arange(input_length)[None, :]
causal_mask_upper = ops.cast(i < j + self.sliding_window, "int32")
causal_mask = ops.minimum(causal_mask, causal_mask_upper)
return (
ops.minimum(decoder_mask, causal_mask)
if decoder_mask is not None
else causal_mask
)
def compute_output_shape(self, decoder_sequence_shape):
return decoder_sequence_shape
def get_config(self):
config = super().get_config()
config.update(
{
"intermediate_dim": self.intermediate_dim,
"num_query_heads": self.num_query_heads,
"rope_max_wavelength": self.rope_max_wavelength,
"rope_scaling_factor": self.rope_scaling_factor,
"num_key_value_heads": self.num_key_value_heads,
"sliding_window": self.sliding_window,
"activation": keras.activations.serialize(self.activation),
"layer_norm_epsilon": self.layer_norm_epsilon,
"kernel_initializer": keras.initializers.serialize(
self.kernel_initializer
),
"dropout": self.dropout,
}
)
return config