index tts stash save
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::{Result, anyhow};
|
||||
use candle_core::{D, DType, Device, IndexOp, Tensor};
|
||||
use candle_transformers::models::deepseek2::SplitOp;
|
||||
|
||||
@@ -158,6 +158,68 @@ pub fn glm_asr_apply_rotary_pos_emb(
|
||||
Ok((q_embed, k_embed))
|
||||
}
|
||||
|
||||
pub fn roformer_rotate(x: &Tensor) -> Result<Tensor> {
|
||||
let dims = x.dims();
|
||||
let last_dim = dims
|
||||
.last()
|
||||
.ok_or(anyhow!("Input tensor must have at least one dimension"))?;
|
||||
if last_dim % 2 != 0 {
|
||||
return Err(anyhow!(
|
||||
"Last dimension size must be even, got {}",
|
||||
last_dim
|
||||
));
|
||||
}
|
||||
let new_dims: Vec<usize> = dims[..dims.len() - 1]
|
||||
.iter()
|
||||
.copied()
|
||||
.chain([last_dim / 2, 2])
|
||||
.collect();
|
||||
let x_reshape = x.reshape(new_dims)?;
|
||||
let x_chunks = x_reshape.chunk(2, D::Minus1)?;
|
||||
let x1 = &x_chunks[0];
|
||||
let x2 = &x_chunks[1];
|
||||
// let x1 = x_reshape.narrow(D::Minus1, 0, 1)?;
|
||||
// let x2 = x_reshape.narrow(D::Minus1, 1, 1)?;
|
||||
let x2_neg = x2.affine(-1.0, 0.0)?;
|
||||
let rotate_x = Tensor::cat(&[&x2_neg, x1], D::Minus1)?;
|
||||
Ok(rotate_x.flatten(D::Minus2, D::Minus1)?)
|
||||
}
|
||||
|
||||
pub fn apply_rotary_pos_emb_roformer(
|
||||
q: &Tensor,
|
||||
k: &Tensor,
|
||||
cos: &Tensor,
|
||||
sin: &Tensor,
|
||||
tof32: bool,
|
||||
) -> Result<(Tensor, Tensor)> {
|
||||
let mut cos = cos.clone();
|
||||
let mut sin = sin.clone();
|
||||
if cos.rank() == 2 {
|
||||
// (seq_len, head_dim) -> (1, 1, seq_len, head_dim)
|
||||
cos = cos.unsqueeze(0)?.unsqueeze(0)?;
|
||||
sin = sin.unsqueeze(0)?.unsqueeze(0)?;
|
||||
}
|
||||
if cos.rank() == 3 {
|
||||
// (bs, seq_len, head_dim) -> (bs, 1, seq_len, head_dim)
|
||||
cos = cos.unsqueeze(1)?;
|
||||
sin = sin.unsqueeze(1)?;
|
||||
}
|
||||
let orig_dtype = q.dtype();
|
||||
let q = if tof32 { &q.to_dtype(DType::F32)? } else { q };
|
||||
let k = if tof32 { &k.to_dtype(DType::F32)? } else { k };
|
||||
let cos = cos.to_dtype(q.dtype())?;
|
||||
let sin = sin.to_dtype(q.dtype())?;
|
||||
let q_embed = q
|
||||
.broadcast_mul(&cos)?
|
||||
.add(&roformer_rotate(q)?.broadcast_mul(&sin)?)?
|
||||
.to_dtype(orig_dtype)?;
|
||||
let k_embed = k
|
||||
.broadcast_mul(&cos)?
|
||||
.add(&roformer_rotate(k)?.broadcast_mul(&sin)?)?
|
||||
.to_dtype(orig_dtype)?;
|
||||
Ok((q_embed, k_embed))
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Qwen2_5VLTextRotaryEmbedding {
|
||||
inv_freq: Vec<f32>,
|
||||
|
||||
Reference in New Issue
Block a user