use super::*;
use crate::device::test_pool::get_test_device;
#[tokio::test]
async fn test_sparse_attention_basic() {
let device = get_test_device().await;
let batch = 1;
let heads = 2;
let seq = 8;
let dim = 4;
let q = Tensor::from_vec_on(
vec![0.5; batch * heads * seq * dim],
vec![batch, heads, seq, dim],
device.clone(),
)
.await
.unwrap();
let k = q.clone();
let v = q.clone();
let output = q.sparse_attention(&k, &v, 2).unwrap();
assert_eq!(output.shape(), &[batch, heads, seq, dim]);
let data = output.to_vec().unwrap();
assert!(data.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_sparse_attention_stride_1() {
let device = get_test_device().await;
let batch = 1;
let heads = 1;
let seq = 4;
let dim = 4;
let q = Tensor::from_vec_on(
vec![1.0; batch * heads * seq * dim],
vec![batch, heads, seq, dim],
device.clone(),
)
.await
.unwrap();
let k = q.clone();
let v = q.clone();
let output = q.sparse_attention(&k, &v, 1).unwrap();
assert_eq!(output.shape(), &[batch, heads, seq, dim]);
let data = output.to_vec().unwrap();
assert!(data.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_sparse_attention_large_stride() {
let device = get_test_device().await;
let batch = 2;
let heads = 4;
let seq = 16;
let dim = 8;
let q = Tensor::from_vec_on(
vec![0.5; batch * heads * seq * dim],
vec![batch, heads, seq, dim],
device.clone(),
)
.await
.unwrap();
let k = q.clone();
let v = q.clone();
let output = q.sparse_attention(&k, &v, 4).unwrap();
assert_eq!(output.shape(), &[batch, heads, seq, dim]);
let data = output.to_vec().unwrap();
assert!(data.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_sparse_attention_long_sequence() {
let device = get_test_device().await;
let batch = 2;
let heads = 8;
let seq = 64; let dim = 16;
let q = Tensor::from_vec_on(
vec![0.5; batch * heads * seq * dim],
vec![batch, heads, seq, dim],
device.clone(),
)
.await
.unwrap();
let k = q.clone();
let v = q.clone();
let output = q.sparse_attention(&k, &v, 8).unwrap();
assert_eq!(output.shape(), &[batch, heads, seq, dim]);
}