-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathflash_mistral.rs
More file actions
460 lines (387 loc) · 15.4 KB
/
Copy pathflash_mistral.rs
File metadata and controls
460 lines (387 loc) · 15.4 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use crate::flash_attn::flash_attn_varlen;
use crate::layers::{get_cos_sin, get_inv_freqs, index_select, HiddenAct, Linear, RMSNorm};
use crate::models::{MistralConfig, Model};
use candle::{DType, Device, IndexOp, Result, Tensor};
use candle_nn::{Embedding, Module, VarBuilder};
use candle_rotary::apply_rotary_inplace;
use text_embeddings_backend_core::{Batch, ModelType, Pool};
struct MistralAttention {
qkv_linear: Linear,
o_proj: Linear,
window_size_left: Option<usize>,
use_bidirectional_attention: bool,
num_attention_heads: usize,
num_key_value_heads: usize,
attention_head_size: usize,
softmax_scale: f32,
span: tracing::Span,
}
impl MistralAttention {
pub fn load(vb: VarBuilder, config: &MistralConfig) -> Result<Self> {
let window_size_left = config.sliding_window;
let use_bidirectional_attention = config.use_bidirectional_attention.unwrap_or(false);
let num_attention_heads = config.num_attention_heads;
let attention_head_size = config.hidden_size / config.num_attention_heads;
let num_key_value_heads = config.num_key_value_heads;
let hidden_size = config.hidden_size;
let query_weight = vb.pp("q_proj").get((hidden_size, hidden_size), "weight")?;
let key_weight = vb.pp("k_proj").get(
(num_key_value_heads * attention_head_size, hidden_size),
"weight",
)?;
let value_weight = vb.pp("v_proj").get(
(num_key_value_heads * attention_head_size, hidden_size),
"weight",
)?;
let qkv_weight = Tensor::cat(&[&query_weight, &key_weight, &value_weight], 0)?;
let qkv_linear = Linear::new(qkv_weight, None, None);
let o_proj_weight = vb.pp("o_proj").get((hidden_size, hidden_size), "weight")?;
let o_proj = Linear::new(o_proj_weight, None, None);
let softmax_scale = (1. / (attention_head_size as f64).sqrt()) as f32;
Ok(Self {
qkv_linear,
o_proj,
window_size_left,
use_bidirectional_attention,
num_attention_heads,
num_key_value_heads,
attention_head_size,
softmax_scale,
span: tracing::span!(tracing::Level::TRACE, "attention"),
})
}
pub fn forward(
&self,
hidden_states: &Tensor,
cu_seqlens: &Tensor,
cos: &Tensor,
sin: &Tensor,
max_s: usize,
) -> Result<Tensor> {
let _enter = self.span.enter();
let qkv = self.qkv_linear.forward(hidden_states)?;
// Reshape to [tokens, heads, head_size]
let mut new_qkv_shape = qkv.dims().to_vec();
new_qkv_shape.pop();
new_qkv_shape.push(self.num_attention_heads + 2 * self.num_key_value_heads);
new_qkv_shape.push(self.attention_head_size);
let qkv = qkv.reshape(new_qkv_shape)?;
// Split qkv tensor
let q = qkv.narrow(1, 0, self.num_attention_heads)?;
let k = qkv.narrow(1, self.num_attention_heads, self.num_key_value_heads)?;
let v = qkv.narrow(
1,
self.num_attention_heads + self.num_key_value_heads,
self.num_key_value_heads,
)?;
apply_rotary_inplace(&q, &k, &cos, &sin, true)?;
let attention = flash_attn_varlen(
&q,
&k,
&v,
None,
cu_seqlens,
cu_seqlens,
max_s,
max_s,
self.softmax_scale,
!self.use_bidirectional_attention,
self.window_size_left,
None,
)?;
let attention = attention.flatten_from(candle::D::Minus2)?;
self.o_proj.forward(&attention)
}
}
struct MistralMLP {
gate_up_proj: Linear,
down_proj: Linear,
act: HiddenAct,
intermediate_size: usize,
span: tracing::Span,
}
impl MistralMLP {
pub fn load(vb: VarBuilder, config: &MistralConfig) -> Result<Self> {
let intermediate_size = config.intermediate_size;
let gate_proj_weight = vb
.pp("gate_proj")
.get((intermediate_size, config.hidden_size), "weight")?;
let up_proj_weight = vb
.pp("up_proj")
.get((intermediate_size, config.hidden_size), "weight")?;
let gate_up_proj_weight = Tensor::cat(&[&gate_proj_weight, &up_proj_weight], 0)?;
let gate_up_proj = Linear::new(gate_up_proj_weight, None, None);
let down_proj_weight = vb
.pp("down_proj")
.get((config.hidden_size, intermediate_size), "weight")?;
let down_proj = Linear::new(down_proj_weight, None, None);
Ok(Self {
gate_up_proj,
down_proj,
intermediate_size,
act: config.hidden_act.clone(),
span: tracing::span!(tracing::Level::TRACE, "mlp"),
})
}
pub fn forward(&self, hidden_states: &Tensor) -> Result<Tensor> {
let _enter = self.span.enter();
let gate_up_states = self.gate_up_proj.forward(hidden_states)?;
let gate_states = gate_up_states.narrow(1, 0, self.intermediate_size)?;
let up_states = gate_up_states.narrow(1, self.intermediate_size, self.intermediate_size)?;
let gate_states = self.act.forward(&gate_states)?;
let r = self.down_proj.forward(&(gate_states * up_states)?);
r
}
}
struct MistralLayer {
attention: MistralAttention,
mlp: MistralMLP,
input_layer_norm: RMSNorm,
post_attention_layer_norm: RMSNorm,
span: tracing::Span,
}
impl MistralLayer {
pub fn load(vb: VarBuilder, config: &MistralConfig) -> Result<Self> {
let attention = MistralAttention::load(vb.pp("self_attn"), config)?;
let mlp = MistralMLP::load(vb.pp("mlp"), config)?;
let input_layer_norm = RMSNorm::load(
vb.pp("input_layernorm"),
config.hidden_size,
config.rms_norm_eps,
)?;
let post_attention_layer_norm = RMSNorm::load(
vb.pp("post_attention_layernorm"),
config.hidden_size,
config.rms_norm_eps,
)?;
Ok(Self {
attention,
mlp,
input_layer_norm,
post_attention_layer_norm,
span: tracing::span!(tracing::Level::TRACE, "layer"),
})
}
pub fn forward(
&self,
hidden_states: &Tensor,
residual: Option<&Tensor>,
cu_seqlens: &Tensor,
cos: &Tensor,
sin: &Tensor,
max_s: usize,
) -> Result<(Tensor, Tensor)> {
let _enter = self.span.enter();
let (normed_hidden_states, res) = self.input_layer_norm.forward(hidden_states, residual)?;
let attn_output =
self.attention
.forward(&normed_hidden_states, cu_seqlens, cos, sin, max_s)?;
let (normed_attn_res_output, attn_res) = self
.post_attention_layer_norm
.forward(&attn_output, Some(&res))?;
let mlp_output = self.mlp.forward(&normed_attn_res_output)?;
Ok((mlp_output, attn_res))
}
}
pub struct FlashMistralModel {
embeddings: Embedding,
layers: Vec<MistralLayer>,
norm: RMSNorm,
cos_cache: Tensor,
sin_cache: Tensor,
pool: Pool,
pub device: Device,
span: tracing::Span,
}
impl FlashMistralModel {
pub fn load(vb: VarBuilder, config: &MistralConfig, model_type: ModelType) -> Result<Self> {
match vb.device() {
Device::Cuda(_) => {}
_ => candle::bail!("FlashMistral requires Cuda"),
}
if vb.dtype() != DType::F16 {
candle::bail!("FlashMistral requires DType::F16")
}
let pool = match model_type {
ModelType::Classifier => {
candle::bail!("`classifier` model type is not supported for Mistral")
}
ModelType::Embedding(pool) => pool,
};
let embeddings = Embedding::new(
vb.pp("embed_tokens")
.get((config.vocab_size, config.hidden_size), "weight")?,
config.hidden_size,
);
let layers = (0..config.num_hidden_layers)
.map(|index| MistralLayer::load(vb.pp(format!("layers.{index}")), config))
.collect::<Result<Vec<_>>>()?;
let norm = RMSNorm::load(vb.pp("norm"), config.hidden_size, config.rms_norm_eps)?;
// NOTE: https://github.com/huggingface/transformers/pull/39847
let rope_theta = match config.rope_theta {
Some(rope_theta) => rope_theta,
None => match &config.rope_parameters {
Some(rope_parameters) => rope_parameters.rope_theta,
None => candle::bail!("Neither `rope_theta` nor `rope_parameters.rope_theta` are defined in the `config.json`"),
},
};
let inv_freqs = get_inv_freqs(
layers[0].attention.attention_head_size,
rope_theta,
vb.device(),
config.rope_scaling.as_ref(),
)?;
let (cos_cache, sin_cache) = get_cos_sin(
config.max_position_embeddings,
&inv_freqs,
vb.dtype(),
false,
)?;
Ok(Self {
embeddings,
layers,
norm,
cos_cache,
sin_cache,
pool,
device: vb.device().clone(),
span: tracing::span!(tracing::Level::TRACE, "model"),
})
}
pub fn forward(&self, batch: Batch) -> Result<(Option<Tensor>, Option<Tensor>)> {
let _enter = self.span.enter();
let batch_size = batch.cumulative_seq_lengths.len() - 1;
let shape = batch.input_ids.len();
// Create Cuda tensors
let input_ids = Tensor::from_vec(batch.input_ids, shape, &self.device)?;
let position_ids = Tensor::from_vec(batch.position_ids, shape, &self.device)?;
let cu_seqlens = Tensor::from_vec(
batch.cumulative_seq_lengths.clone(),
batch_size + 1,
&self.device,
)?;
let mut hidden_states = self.embeddings.forward(&input_ids)?;
let cos = index_select(&self.cos_cache, &position_ids, 0)?;
let sin = index_select(&self.sin_cache, &position_ids, 0)?;
let mut residual = None;
for layer in &self.layers {
let (h, r) = layer.forward(
&hidden_states,
residual.as_ref(),
&cu_seqlens,
&cos,
&sin,
batch.max_length as usize,
)?;
hidden_states = h;
residual = Some(r);
}
let (outputs, _) = self.norm.forward(&hidden_states, residual.as_ref())?;
let has_pooling_requests = !batch.pooled_indices.is_empty();
let has_raw_requests = !batch.raw_indices.is_empty();
let pooled_embeddings = if has_pooling_requests {
match self.pool {
// CLS and LastToken pooling
Pool::Cls | Pool::LastToken => {
if batch_size > 1 {
// Get token indices form cu_seqlens
let mut indices = match self.pool {
Pool::Cls => cu_seqlens.narrow(0, 0, batch_size)?,
Pool::LastToken => {
let end = cu_seqlens.narrow(0, 1, batch_size)?;
(&end - &end.ones_like()?)?
}
_ => unreachable!(),
};
// If raw_indices is empty, we don't need to do anything with
// the pooled_indices
if has_raw_requests {
// We need the pooled indices to select the correct cls indices
let pooled_indices = Tensor::from_vec(
batch.pooled_indices.clone(),
batch.pooled_indices.len(),
&self.device,
)?;
// Only select indices that requires pooling
indices = index_select(&indices, &pooled_indices, 0)?
}
// Select tokens
Some(index_select(&outputs, &indices, 0)?)
} else {
Some(
match self.pool {
Pool::Cls => outputs.i(0)?,
Pool::LastToken => {
outputs.i(batch.cumulative_seq_lengths[1] as usize - 1)?
}
_ => unreachable!(),
}
.unsqueeze(0)?,
)
}
}
// Mean pooling
Pool::Mean => {
if batch_size > 1 {
// for each request that requires pooling
let results: Result<Vec<Tensor>> = batch
.pooled_indices
.into_iter()
.map(|i| {
let i = i as usize;
let start = batch.cumulative_seq_lengths[i];
let len = batch.cumulative_seq_lengths[i + 1] - start;
// Mean
let embeddings = outputs.narrow(0, start as usize, len as usize)?;
embeddings.sum_keepdim(0)? / (len as f64)
})
.collect();
// Concatenate all results
Some(Tensor::cat(&results?, 0)?)
} else {
Some((outputs.sum_keepdim(0)? / (batch.max_length as f64))?)
}
}
Pool::Splade => {
unreachable!();
}
}
} else {
None
};
let raw_embeddings = if has_raw_requests {
if batch_size > 1 && has_pooling_requests {
// Create indexing vector for the embeddings
let mut final_indices: Vec<u32> = Vec::with_capacity(shape);
for i in batch.raw_indices.into_iter() {
let i = i as usize;
// Get start/end token index of this specific member of the batch
let start = batch.cumulative_seq_lengths[i];
let end = batch.cumulative_seq_lengths[i + 1];
for j in start..end {
// Add indices for the tokens of this specific member of the batch
final_indices.push(j);
}
}
let final_indices_length = final_indices.len();
let final_indices =
Tensor::from_vec(final_indices, final_indices_length, &self.device)?;
// Select the tokens with final indices
Some(index_select(&outputs, &final_indices, 0)?)
} else {
Some(outputs)
}
} else {
None
};
Ok((pooled_embeddings, raw_embeddings))
}
}
impl Model for FlashMistralModel {
fn is_padded(&self) -> bool {
false
}
fn embed(&self, batch: Batch) -> Result<(Option<Tensor>, Option<Tensor>)> {
self.forward(batch)
}
}