Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.ruff_cache
.vscode
*.a
.DS_Store
.DS_Store
.idea
76 changes: 76 additions & 0 deletions mistralrs-core/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,82 @@ impl Qwen2VLRotaryEmbedding {
}
}

#[derive(Debug, Clone)]
pub struct Qwen2_5VLRotaryEmbedding {
inv_freq: Tensor,
mrope_section: Vec<usize>,
}

impl Qwen2_5VLRotaryEmbedding {
pub fn new(
base: f32,
head_dim: usize,
device: &Device,
mrope_section: Vec<usize>,
) -> Result<Self> {
let inv_freq: Vec<_> = (0..head_dim)
.step_by(2)
.map(|i| 1f32 / base.powf(i as f32 / head_dim as f32))
.collect();
let inv_freq_len = inv_freq.len();
let inv_freq = Tensor::from_vec(inv_freq, (inv_freq_len,), device)?.to_dtype(DType::F32)?;
Ok(Self {
inv_freq,
mrope_section,
})
}

/// (cos, sin)
pub fn compute_cos_sin(&self, position_ids: &Tensor, dtype: DType) -> Result<(Tensor, Tensor)> {
let inv_freq_expanded =
self.inv_freq
.reshape((1, 1, (), 1))?
.repeat((3, position_ids.dim(1)?, 1, 1))?;
let position_ids_expanded = position_ids.unsqueeze(2)?;
let freqs = inv_freq_expanded
.matmul(&position_ids_expanded.to_dtype(inv_freq_expanded.dtype())?)?
.transpose(2, 3)?;
let cos = freqs.cos()?;
let sin = freqs.sin()?;

let cos = Tensor::cat(
&cos.split(&self.mrope_section, D::Minus1)?
.into_iter()
.enumerate()
.map(|(i, m)| m.i(i % 3))
.collect::<Result<Vec<_>>>()?,
D::Minus1,
)?
.squeeze(0)?
.to_dtype(dtype)?
.contiguous()?;
let sin = Tensor::cat(
&sin.split(&self.mrope_section, D::Minus1)?
.into_iter()
.enumerate()
.map(|(i, m)| m.i(i % 3))
.collect::<Result<Vec<_>>>()?,
D::Minus1,
)?
.squeeze(0)?
.to_dtype(dtype)?
.contiguous()?;

Ok((cos, sin))
}

pub fn forward(
&self,
(cos, sin): &(Tensor, Tensor),
q: &mut Tensor,
k: &mut Tensor,
) -> Result<()> {
*q = candle_nn::rotary_emb::rope(&q.contiguous()?, cos, sin)?;
*k = candle_nn::rotary_emb::rope(&k.contiguous()?, cos, sin)?;
Ok(())
}
}

#[derive(Debug, Clone)]
pub struct DeepSeekV2RotaryEmbedding {
sin: Tensor,
Expand Down
3 changes: 2 additions & 1 deletion mistralrs-core/src/pipeline/loaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub use normal_loaders::{
use tracing::{info, warn};
pub use vision_loaders::{
Idefics2Loader, Idefics3Loader, LLaVALoader, LLaVANextLoader, MiniCpmOLoader, Phi3VLoader,
Phi4MMLoader, Qwen2VLLoader, VLlamaLoader, VisionLoaderType, VisionModel, VisionModelLoader,
Phi4MMLoader, Qwen2VLLoader, Qwen2_5VLLoader, VLlamaLoader, VisionLoaderType, VisionModel,
VisionModelLoader,
};

pub use diffusion_loaders::{
Expand Down
Loading
Loading