use alloc::vec::Vec;
use burn::{
Tensor,
tensor::{
DType,
Device,
FloatDType,
TensorData,
linalg,
},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RotaryTableDTypePolicy {
pub round_to: Option<FloatDType>,
pub keep_in: FloatDType,
}
impl RotaryTableDTypePolicy {
pub fn init(
table_dtype: Option<FloatDType>,
supports: impl Fn(DType) -> bool,
) -> Self {
let (round_to, keep_in) = match table_dtype {
Some(d @ (FloatDType::BF16 | FloatDType::F16)) => (
Some(d),
if supports(d.into()) {
d
} else {
FloatDType::F32
},
),
Some(FloatDType::F64) if supports(DType::F64) => (None, FloatDType::F64),
_ => (None, FloatDType::F32),
};
Self { round_to, keep_in }
}
}
fn f32_tensor(
values: Vec<f32>,
device: &Device,
) -> Tensor<1> {
let n = values.len();
Tensor::from_data(TensorData::new(values, [n]), (device, DType::F32))
}
pub fn inverse_frequency_table(
base: usize,
head_dim: usize,
device: &Device,
) -> Tensor<1> {
let exponents = (0..head_dim)
.step_by(2)
.map(|d| -(d as f32) / head_dim as f32)
.collect();
f32_tensor(vec![base as f32], device).powf(f32_tensor(exponents, device))
}
pub fn positional_frequency_table(
seq_len: usize,
base: usize,
head_dim: usize,
device: &Device,
) -> Tensor<2> {
frequency_table(seq_len, inverse_frequency_table(base, head_dim, device))
}
pub fn frequency_table(
seq_len: usize,
inv_freq: Tensor<1>,
) -> Tensor<2> {
let t = f32_tensor((0..seq_len).map(|p| p as f32).collect(), &inv_freq.device());
linalg::outer::<1, 2, _>(t, inv_freq)
}
#[cfg(test)]
mod tests {
#[test]
fn test_rotary_table_dtype_policy() {
let all = |_: DType| true;
let no_half_no_f64 = |d: DType| !matches!(d, DType::BF16 | DType::F16 | DType::F64);
let policy = |table_dtype, supports: &dyn Fn(DType) -> bool| {
let p = RotaryTableDTypePolicy::init(table_dtype, supports);
(p.round_to, p.keep_in)
};
assert_eq!(policy(None, &all), (None, FloatDType::F32));
assert_eq!(
policy(Some(FloatDType::BF16), &all),
(Some(FloatDType::BF16), FloatDType::BF16)
);
assert_eq!(
policy(Some(FloatDType::BF16), &no_half_no_f64),
(Some(FloatDType::BF16), FloatDType::F32)
);
assert_eq!(
policy(Some(FloatDType::F16), &no_half_no_f64),
(Some(FloatDType::F16), FloatDType::F32)
);
assert_eq!(policy(Some(FloatDType::F64), &all), (None, FloatDType::F64));
assert_eq!(
policy(Some(FloatDType::F64), &no_half_no_f64),
(None, FloatDType::F32)
);
}
use burn::tensor::Tolerance;
use serial_test::serial;
use super::*;
use crate::support::testing::{
DeviceMemoryGuard,
performance_device,
};
#[test]
#[serial]
fn test_inverse_frequency_table() {
let device = performance_device();
let _memory = DeviceMemoryGuard::new(&device);
let base = 10000;
let head_dim = 4;
let base_f = base as f32;
let head_dim_f = head_dim as f32;
inverse_frequency_table(base, head_dim, &device)
.to_data()
.assert_approx_eq(
&Tensor::<1>::from_data(
[
1.0 / base_f.powf(0.0 / head_dim_f),
1.0 / base_f.powf(2.0 / head_dim_f),
],
&device,
)
.to_data(),
Tolerance::<f32>::default(),
);
}
#[test]
#[serial]
fn test_frequency_matrix() {
let device = performance_device();
let _memory = DeviceMemoryGuard::new(&device);
let base = 10000;
let head_dim = 4;
let base_f = base as f32;
let head_dim_f = head_dim as f32;
positional_frequency_table(3, base, head_dim, &device)
.to_data()
.assert_approx_eq(
&Tensor::<2>::from_data(
[
[0.0, 0.0],
[
1.0 / base_f.powf(0.0 / head_dim_f),
1.0 / base_f.powf(2.0 / head_dim_f),
],
[
2.0 / base_f.powf(0.0 / head_dim_f),
2.0 / base_f.powf(2.0 / head_dim_f),
],
],
&device,
)
.to_data(),
Tolerance::<f32>::default(),
);
}
}