#![allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
use candle_core::{DType, Device, Module, Result, Tensor, D};
use candle_nn::VarBuilder;
use mistralrs_quant::{QuantMethod, QuantMethodConfig, QuantizedConfig, UnquantLinear};
use std::{collections::HashMap, sync::Arc};
use crate::{
amoe::{
AnyMoeBaseModelMixin, AnyMoeConfig, AnyMoeExpertType, AnyMoeTrainableLayer, MlpLayer,
MoeMlp,
},
attention::SdpaParams,
device_map::DeviceMapper,
get_delta_from_lora_ab,
layers::{
Activation, CausalMasker, MatMul, PhiRopeConfig, PhiRopeScalingConfig, PhiRotaryEmbedding,
RmsNorm, Sdpa,
},
layers_masker::PastKvLenCache,
paged_attention::{AttentionImplementation, ModelConfigMetadata, PagedAttention},
pipeline::{
extract_logits,
text_models_inputs_processor::{FlashParams, PagedAttentionInputMetadata},
EitherCache, IsqModel, KvCache, NormalCache, NormalLoadingMetadata, NormalModel,
},
serde_default_fn,
utils::{progress::NiceProgressBar, unvarbuilder::UnVarBuilder},
};
serde_default_fn!(bool, word_emb_default, false);
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, Default)]
pub struct Config {
pub vocab_size: usize,
pub hidden_act: Activation,
pub hidden_size: usize,
pub intermediate_size: usize,
pub num_hidden_layers: usize,
pub num_attention_heads: usize,
pub num_key_value_heads: usize,
pub rms_norm_eps: f64,
pub rope_theta: f64,
pub bos_token_id: Option<u32>,
pub eos_token_id: Option<u32>,
pub rope_scaling: Option<PhiRopeScalingConfig>,
pub max_position_embeddings: usize,
pub use_flash_attn: bool,
pub sliding_window: Option<usize>,
pub original_max_position_embeddings: usize,
pub quantization_config: Option<QuantizedConfig>,
#[serde(default = "word_emb_default")]
pub tie_word_embeddings: bool,
}
impl From<Config> for PhiRopeConfig {
fn from(val: Config) -> Self {
PhiRopeConfig {
rope_scaling: val.rope_scaling,
max_position_embeddings: val.max_position_embeddings,
original_max_position_embeddings: val.original_max_position_embeddings,
rope_theta: val.rope_theta,
head_dim: val.hidden_size / val.num_attention_heads,
}
}
}
impl Config {
pub fn head_dim(&self) -> usize {
self.hidden_size / self.num_attention_heads
}
}
struct Attention {
qkv_proj: Arc<dyn QuantMethod>,
o_proj: Arc<dyn QuantMethod>,
num_heads: usize,
num_kv_heads: usize,
head_dim: usize,
rotary_emb: Arc<PhiRotaryEmbedding>,
sliding_window: Option<usize>,
paged_attn: Option<PagedAttention>,
sdpa_params: SdpaParams,
}
impl Attention {
fn new(
rotary_emb: Arc<PhiRotaryEmbedding>,
cfg: &Config,
vb: VarBuilder,
paged_attn: Option<PagedAttention>,
) -> Result<Self> {
let num_heads = cfg.num_attention_heads;
let num_kv_heads = cfg.num_key_value_heads;
let head_dim = cfg.head_dim();
let op_size = num_heads * head_dim + 2 * num_kv_heads * head_dim;
let qkv_proj = mistralrs_quant::linear_no_bias(
cfg.hidden_size,
op_size,
&cfg.quantization_config,
vb.pp("qkv_proj"),
)?;
let o_proj = mistralrs_quant::linear_no_bias(
num_heads * head_dim,
cfg.hidden_size,
&cfg.quantization_config,
vb.pp("o_proj"),
)?;
Ok(Self {
qkv_proj,
o_proj,
rotary_emb,
num_heads,
num_kv_heads,
head_dim,
sliding_window: cfg.sliding_window,
paged_attn,
sdpa_params: SdpaParams {
n_kv_groups: num_heads / num_kv_heads,
use_flash_attn: cfg.use_flash_attn,
softcap: None,
softmax_scale: 1.0 / (head_dim as f32).sqrt(),
sliding_window: cfg.sliding_window,
},
})
}
#[allow(clippy::too_many_arguments)]
fn forward(
&self,
xs: &Tensor,
attention_mask: Option<&Tensor>,
seqlen_offsets: &[usize],
position_ids: &[usize],
kv_cache: &mut KvCache,
metadata: Option<((Tensor, Tensor), &mut PagedAttentionInputMetadata)>,
flash_params: &FlashParams,
) -> Result<Tensor> {
let (b_sz, q_len, _) = xs.dims3()?;
let original_dtype = xs.dtype();
let mut xs = xs.clone();
if let Some(t) = self.qkv_proj.quantized_act_type() {
xs = xs.to_dtype(t)?;
}
let mut qkv = MatMul.qmethod_matmul(&xs, &*self.qkv_proj)?;
if self.qkv_proj.quantized_act_type().is_some() {
qkv = qkv.to_dtype(original_dtype)?;
}
let query_pos = self.num_heads * self.head_dim;
let q = qkv.narrow(D::Minus1, 0, query_pos)?;
let k = qkv.narrow(D::Minus1, query_pos, self.num_kv_heads * self.head_dim)?;
let v = qkv.narrow(
D::Minus1,
query_pos + self.num_kv_heads * self.head_dim,
self.num_kv_heads * self.head_dim,
)?;
let (q, k, v) = if q_len != 1 {
let q = q
.reshape((b_sz, q_len, self.num_heads, self.head_dim))?
.transpose(1, 2)?;
let k = k
.reshape((b_sz, q_len, self.num_kv_heads, self.head_dim))?
.transpose(1, 2)?;
let v = v
.reshape((b_sz, q_len, self.num_kv_heads, self.head_dim))?
.transpose(1, 2)?;
(q, k, v)
} else {
let q = q.reshape((b_sz, self.num_heads, q_len, self.head_dim))?;
let k = k.reshape((b_sz, self.num_kv_heads, q_len, self.head_dim))?;
let v = v.reshape((b_sz, self.num_kv_heads, q_len, self.head_dim))?;
(q, k, v)
};
let (q, k) = self
.rotary_emb
.forward(&q, &k, seqlen_offsets, position_ids)?;
let mut attn_output = match &self.paged_attn {
Some(paged_attn) => match metadata {
Some(((key_cache, value_cache), input_metadata)) => paged_attn.forward(
&q,
&k,
&v,
attention_mask,
Some(key_cache),
Some(value_cache),
input_metadata,
None,
)?,
None => {
let mut input_metadata = PagedAttentionInputMetadata {
block_tables: None,
context_lens: None,
max_context_len: None,
slot_mappings: Tensor::new(&[0f32], q.device())?,
};
paged_attn.forward(
&q,
&k,
&v,
attention_mask,
None,
None,
&mut input_metadata,
None,
)?
}
},
_ => {
let (k, v, attn_mask) =
kv_cache.append_sliding_window(&k, &v, attention_mask, self.sliding_window)?;
Sdpa.run_attention(
&q,
&k,
&v,
attn_mask.as_ref(),
Some(flash_params),
&self.sdpa_params,
)?
}
};
if let Some(t) = self.qkv_proj.quantized_act_type() {
attn_output = attn_output.to_dtype(t)?;
}
attn_output = if attention_mask.is_some() {
attn_output.transpose(1, 2)?.reshape((b_sz, q_len, ()))?
} else {
attn_output.reshape((b_sz, q_len, ()))?
};
let mut res = MatMul.qmethod_matmul(&attn_output, &*self.o_proj)?;
if self.qkv_proj.quantized_act_type().is_some() {
res = res.to_dtype(original_dtype)?;
}
Ok(res)
}
}
#[derive(Clone)]
struct Mlp {
gate_up_proj: Arc<dyn QuantMethod>,
down_proj: Arc<dyn QuantMethod>,
act_fn: Activation,
i_size: usize,
params: Vec<usize>,
}
impl Mlp {
fn new(cfg: &Config, vb: VarBuilder) -> Result<Self> {
let hidden_size = cfg.hidden_size;
let i_size = cfg.intermediate_size;
let gate_up_proj = mistralrs_quant::linear_no_bias(
hidden_size,
2 * i_size,
&cfg.quantization_config,
vb.pp("gate_up_proj"),
)?;
let down_proj = mistralrs_quant::linear_no_bias(
i_size,
hidden_size,
&cfg.quantization_config,
vb.pp("down_proj"),
)?;
Ok(Self {
gate_up_proj,
down_proj,
act_fn: cfg.hidden_act,
i_size,
params: vec![hidden_size, i_size],
})
}
}
impl AnyMoeTrainableLayer for Mlp {}
impl MlpLayer for Mlp {
fn forward(&self, xs: &Tensor) -> Result<Tensor> {
let original_dtype = xs.dtype();
let mut xs = xs.clone();
if let Some(t) = self.gate_up_proj.quantized_act_type() {
xs = xs.to_dtype(t)?;
}
let up_states = MatMul.qmethod_matmul(&xs, &*self.gate_up_proj)?;
let gate = up_states.narrow(D::Minus1, 0, self.i_size)?;
let up_states = up_states.narrow(D::Minus1, self.i_size, self.i_size)?;
let up_states = (up_states * gate.apply(&self.act_fn))?;
let mut res = MatMul.qmethod_matmul(&up_states, &*self.down_proj)?;
if self.gate_up_proj.quantized_act_type().is_some() {
res = res.to_dtype(original_dtype)?;
}
Ok(res)
}
fn get_isq_layers(&mut self) -> Vec<&mut Arc<dyn QuantMethod>> {
vec![&mut self.gate_up_proj, &mut self.down_proj]
}
fn clone(&self) -> Box<dyn MlpLayer> {
Box::new(Clone::clone(self))
}
fn get_params(&self) -> &[usize] {
&self.params
}
fn new_added_delta(&self, deltas: Vec<Option<Tensor>>) -> Result<Box<dyn MlpLayer>> {
let new_gate_up = if let Some(ref delta) = deltas[0] {
self.gate_up_proj.add_delta_w(delta)?
} else {
self.gate_up_proj.clone()
};
let new_down = if let Some(ref delta) = deltas[1] {
self.down_proj.add_delta_w(delta)?
} else {
self.down_proj.clone()
};
Ok(Box::new(Self {
gate_up_proj: new_gate_up,
down_proj: new_down,
act_fn: self.act_fn,
i_size: self.i_size,
params: self.params.clone(),
}))
}
fn dtype_device(&self) -> (DType, Device) {
self.gate_up_proj.dtype_and_device()
}
}
struct DecoderLayer {
self_attn: Attention,
mlp: Box<dyn MlpLayer>,
input_layernorm: RmsNorm,
post_attention_layernorm: RmsNorm,
}
impl DecoderLayer {
fn new(
rotary_emb: Arc<PhiRotaryEmbedding>,
cfg: &Config,
vb: VarBuilder,
mapper: &dyn DeviceMapper,
layer_idx: usize,
loading_isq: bool,
paged_attn: Option<PagedAttention>,
) -> Result<Self> {
let self_attn = Attention::new(
rotary_emb,
cfg,
mapper.set_device(layer_idx, vb.pp("self_attn"), loading_isq),
paged_attn,
)?;
let mlp = Mlp::new(cfg, mapper.set_device(layer_idx, vb.pp("mlp"), loading_isq))?;
let input_layernorm = RmsNorm::new(
cfg.hidden_size,
cfg.rms_norm_eps,
mapper.set_device(layer_idx, vb.pp("input_layernorm"), false),
)?;
let post_attention_layernorm = RmsNorm::new(
cfg.hidden_size,
cfg.rms_norm_eps,
mapper.set_device(layer_idx, vb.pp("post_attention_layernorm"), false),
)?;
Ok(Self {
self_attn,
mlp: Box::new(mlp),
input_layernorm,
post_attention_layernorm,
})
}
#[allow(clippy::too_many_arguments)]
fn forward(
&self,
xs: &Tensor,
attention_mask: Option<&Tensor>,
seqlen_offsets: &[usize],
position_ids: &[usize],
kv_cache: &mut KvCache,
metadata: Option<((Tensor, Tensor), &mut PagedAttentionInputMetadata)>,
flash_params: &FlashParams,
) -> Result<Tensor> {
let residual = xs;
let xs = self.input_layernorm.forward(xs)?;
let xs = self.self_attn.forward(
&xs,
attention_mask,
seqlen_offsets,
position_ids,
kv_cache,
metadata,
flash_params,
)?;
let xs = (xs + residual)?;
let residual = &xs;
let xs = self
.mlp
.forward(&xs.apply(&self.post_attention_layernorm)?)?;
residual + xs
}
}
pub struct Model {
embed_tokens: candle_nn::Embedding,
layers: Vec<DecoderLayer>,
norm: RmsNorm,
lm_head: Arc<dyn QuantMethod>,
device: Device,
cache: EitherCache,
max_seq_len: usize,
mapper: Box<dyn DeviceMapper + Send + Sync>,
sliding_window: Option<usize>,
cfg: ModelConfigMetadata,
}
impl Model {
pub fn new(
cfg: &Config,
vb: VarBuilder,
_is_gptx: bool,
normal_loading_metadata: NormalLoadingMetadata,
attention_mechanism: AttentionImplementation,
) -> Result<Self> {
if let Some(ref quant_cfg) = &cfg.quantization_config {
tracing::info!(
"Using {} quantization: {}.",
quant_cfg.quant_method.to_string(),
quant_cfg.get_bits_name(&vb)
);
}
let mapper = normal_loading_metadata.mapper;
let vb_m = vb.pp("model");
let embed_tokens = candle_nn::embedding(
cfg.vocab_size,
cfg.hidden_size,
mapper.set_nm_device(vb_m.pp("embed_tokens"), false),
)?;
let mut ropes = HashMap::new();
for layer_idx in 0..cfg.num_hidden_layers {
let device = mapper
.device_for(layer_idx, false)
.unwrap_or(&normal_loading_metadata.real_device);
ropes.insert(
device.location(),
Arc::new(PhiRotaryEmbedding::new(vb.dtype(), cfg.clone(), device)?),
);
}
let mut layers = Vec::with_capacity(cfg.num_hidden_layers);
let vb_l = vb_m.pp("layers");
for layer_idx in
NiceProgressBar::<_, 'b'>(0..cfg.num_hidden_layers, "Loading repeating layers")
{
let device = mapper
.device_for(layer_idx, false)
.unwrap_or(&normal_loading_metadata.real_device);
let rotary_emb = ropes
.get(&device.location())
.expect("No RoPE for device location!")
.clone();
let paged_attn = match &attention_mechanism {
AttentionImplementation::Eager => None,
AttentionImplementation::PagedAttention => Some(PagedAttention::new(
cfg.num_attention_heads,
cfg.head_dim(),
(1.0 / (cfg.head_dim() as f64).sqrt()) as f32,
Some(cfg.num_key_value_heads),
cfg.sliding_window,
device,
None,
)?),
};
let layer = DecoderLayer::new(
rotary_emb.clone(),
cfg,
vb_l.pp(layer_idx),
&*mapper,
layer_idx,
normal_loading_metadata.loading_isq,
paged_attn,
)?;
layers.push(layer)
}
let norm = RmsNorm::new(
cfg.hidden_size,
cfg.rms_norm_eps,
mapper.set_nm_device(vb_m.pp("norm"), false),
)?;
let lm_head = if !cfg.tie_word_embeddings {
mistralrs_quant::linear_no_bias(
cfg.hidden_size,
cfg.vocab_size,
&None,
mapper.set_nm_device(vb.pp("lm_head"), normal_loading_metadata.loading_isq),
)?
} else {
Arc::new(UnquantLinear::new(QuantMethodConfig::Unquantized(
candle_nn::Linear::new(
mapper.cast_nm_device(
embed_tokens.embeddings(),
normal_loading_metadata.loading_isq,
)?,
None,
),
))?)
};
Ok(Self {
embed_tokens,
layers,
norm,
lm_head,
device: normal_loading_metadata.real_device,
cache: EitherCache::Normal(NormalCache::new(
cfg.num_hidden_layers,
cfg.max_position_embeddings,
)),
max_seq_len: cfg.max_position_embeddings,
mapper,
sliding_window: cfg.sliding_window,
cfg: ModelConfigMetadata {
num_layers: cfg.num_hidden_layers,
hidden_size: cfg.hidden_size,
num_kv_heads: cfg.num_key_value_heads,
num_attn_heads: cfg.num_attention_heads,
sliding_window: cfg.sliding_window,
head_dim: None,
},
})
}
pub fn forward(
&self,
input_ids: &Tensor,
seqlen_offsets: &[usize],
position_ids: &[usize],
context_lens: Vec<(usize, usize)>,
mut metadata: Option<(Vec<(Tensor, Tensor)>, &mut PagedAttentionInputMetadata)>,
flash_params: &FlashParams,
) -> Result<Tensor> {
let mut xs = self.embed_tokens.forward(input_ids)?;
let cache = &mut self.cache.normal().0;
let attention_mask = CausalMasker.make_sliding_window_causal_mask_matrix(
input_ids,
metadata
.as_ref()
.map(|(_, _)| &seqlen_offsets as &dyn PastKvLenCache)
.unwrap_or(cache as &dyn PastKvLenCache),
self.sliding_window,
xs.dtype(),
self.cfg.num_attn_heads,
)?;
for (i, layer) in self.layers.iter().enumerate() {
xs = self.mapper.map(xs, i)?;
xs = layer.forward(
&xs,
attention_mask
.as_ref()
.map(|m| m.to_device(xs.device()).unwrap())
.as_ref(),
seqlen_offsets,
position_ids,
&mut cache[i],
metadata
.as_mut()
.map(|(kv_cache, metadata)| (kv_cache[i].clone(), &mut **metadata)),
flash_params,
)?
}
let xs = xs.to_device(&self.device)?;
let mut xs = xs.apply(&self.norm)?;
if let Some(t) = self.lm_head.quantized_act_type() {
xs = xs.to_dtype(t)?;
}
extract_logits(&MatMul.qmethod_matmul(&xs, &*self.lm_head)?, context_lens)
}
}
impl IsqModel for Model {
fn get_layers(
&mut self,
) -> (
Vec<(&mut Arc<dyn QuantMethod>, Option<usize>)>,
&dyn DeviceMapper,
) {
let mut tensors = Vec::new();
tensors.push((&mut self.lm_head, None));
for (i, layer) in self.layers.iter_mut().enumerate() {
tensors.push((&mut layer.self_attn.qkv_proj, Some(i)));
tensors.push((&mut layer.self_attn.o_proj, Some(i)));
tensors.extend(
layer
.mlp
.get_isq_layers()
.into_iter()
.map(|m| (m, Some(i)))
.collect::<Vec<_>>(),
);
}
(tensors, &*self.mapper)
}
fn residual_tensors(&self) -> Vec<(String, Tensor)> {
let uvb = UnVarBuilder::new();
let uvb_m = uvb.pp("model");
uvb_m.pp("embed_tokens").add(&self.embed_tokens);
uvb_m.pp("norm").add(&self.norm);
for (layer_idx, layer) in self.layers.iter().enumerate() {
let uvb_l = uvb_m.pp("layers").pp(layer_idx);
uvb_l.pp("input_layernorm").add(&layer.input_layernorm);
uvb_l
.pp("post_attention_layernorm")
.add(&layer.post_attention_layernorm);
}
uvb.to_safetensors()
}
fn imatrix_names(&self) -> candle_core::Result<Vec<Option<String>>> {
let mut names = Vec::new();
names.push(None);
for i in 0..self.layers.len() {
names.push(Some(format!("blk.{i}.attn_qkv.weight")));
names.push(Some(format!("blk.{i}.attn_output.weight")));
names.push(Some(format!("blk.{i}.ffn_gate.weight")));
names.push(Some(format!("blk.{i}.ffn_up.weight")));
names.push(Some(format!("blk.{i}.ffn_down.weight")));
}
Ok(names)
}
}
impl NormalModel for Model {
fn forward(
&self,
input_ids: &Tensor,
seqlen_offsets: &[usize],
_start_offsets_kernel: Tensor,
context_lens: Vec<(usize, usize)>,
position_ids: Vec<usize>,
metadata: Option<(Vec<(Tensor, Tensor)>, &mut PagedAttentionInputMetadata)>,
flash_params: &FlashParams,
) -> Result<Tensor> {
self.forward(
input_ids,
seqlen_offsets,
&position_ids,
context_lens,
metadata,
flash_params,
)
}
fn xlora_forward(
&self,
_input_ids: &Tensor,
_input_ids_full: &Tensor,
_seqlen_offsets: &[usize],
_seqlen_offsets_full: &[usize],
_start_offsets_kernel: Tensor,
_start_offsets_kernel_full: Tensor,
_no_kv_cache: bool,
_non_granular_state: &Option<crate::xlora_models::NonGranularState>,
_context_lens: Vec<(usize, usize)>,
_position_ids: Vec<usize>,
_flash_params: &FlashParams,
_flash_params_full: &FlashParams,
) -> Result<Tensor> {
unimplemented!()
}
fn cache(&self) -> &EitherCache {
&self.cache
}
fn cache_mut(&mut self) -> &mut EitherCache {
&mut self.cache
}
fn device(&self) -> &Device {
&self.device
}
fn is_xlora(&self) -> bool {
false
}
fn max_seq_len(&self) -> usize {
self.max_seq_len
}
fn config(&self) -> &ModelConfigMetadata {
&self.cfg
}
}
impl AnyMoeBaseModelMixin for Model {
fn get_mlps(&self) -> Vec<&dyn MlpLayer> {
let mut mlps = Vec::new();
for layer in &self.layers {
mlps.push(&*layer.mlp);
}
mlps
}
fn get_mlps_mut(&mut self) -> Vec<&mut Box<dyn MlpLayer>> {
let mut mlps = Vec::new();
for layer in &mut self.layers {
mlps.push(&mut layer.mlp);
}
mlps
}
fn create_anymoe_layers(
&mut self,
additional_vbs: Vec<VarBuilder>,
config: AnyMoeConfig,
(prefix, mlp): (String, String),
mut layers: Vec<usize>,
expert_type: AnyMoeExpertType,
gate_vb: Option<VarBuilder>,
) -> Result<()> {
let mut experts: Vec<Vec<Box<dyn MlpLayer>>> = Vec::new();
if layers.is_empty() {
layers = (0..self.layers.len()).collect::<Vec<_>>();
}
for _ in 0..layers.len() {
experts.push(Vec::new());
}
for vb in additional_vbs {
let vb = vb.pp(&prefix);
for (layer, row) in experts.iter_mut().enumerate() {
if !layers.contains(&layer) {
continue;
}
let intermediate_size = self.layers[layer].mlp.get_params()[1];
let hidden_size = self.layers[layer].mlp.get_params()[0];
match expert_type {
AnyMoeExpertType::FineTuned => {
let (dtype, device) = self.layers[layer].mlp.dtype_device();
row.push(Box::new(Mlp::new(
&Config {
intermediate_size: self.layers[layer].mlp.get_params()[1],
hidden_size: self.layers[layer].mlp.get_params()[0],
..Default::default()
},
vb.pp(layer).pp(&mlp).set_dtype(dtype).set_device(device),
)?));
}
AnyMoeExpertType::LoraAdapter {
rank,
alpha,
ref target_modules,
} => {
let vb_mlp = vb.pp(layer).pp(&mlp);
let gate_up_proj_delta =
if target_modules.contains(&"gate_up_proj".to_string()) {
Some(get_delta_from_lora_ab!(
vb_mlp,
rank,
alpha,
(hidden_size, 2 * intermediate_size),
"gate_up_proj"
))
} else {
None
};
let down_proj_delta = if target_modules.contains(&"down_proj".to_string()) {
Some(get_delta_from_lora_ab!(
vb_mlp,
rank,
alpha,
(hidden_size, intermediate_size),
"down_proj"
))
} else {
None
};
row.push(
self.layers[layer]
.mlp
.new_added_delta(vec![gate_up_proj_delta, down_proj_delta])?,
);
}
}
}
}
for (layer, expert) in layers.into_iter().zip(experts) {
let mut experts_all = vec![self.layers[layer].mlp.clone()];
experts_all.extend(expert);
let (dtype, device) = self.layers[layer].mlp.dtype_device();
self.layers[layer].mlp = Box::new(MoeMlp::new(
experts_all,
config.clone(),
dtype,
&device,
layer,
gate_vb.as_ref(),
)?);
}
Ok(())
}
fn amoe_supported(&self) -> bool {
true
}
}