use crate::compressive::{CompressiveSpectralDecoder, CompressiveSpectralEncoder};
use crate::holotensor::HoloFragment;
use crate::svd_compression::{SvdCompressedWeight, SvdDecoder, SvdEncoder};
use haagenti_core::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompressionMethod {
Svd,
#[default]
Dct,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TensorType {
AttentionQuery,
AttentionKey,
AttentionValue,
AttentionOutput,
AttentionQkv,
Mlp,
Embedding,
LayerNorm,
OutputHead,
Bias,
Unknown,
}
impl TensorType {
pub fn from_name(name: &str) -> Self {
let name_lower = name.to_lowercase();
if name_lower.contains(".bias") || name_lower.ends_with("_bias") {
return Self::Bias;
}
if name_lower.contains("layernorm")
|| name_lower.contains("layer_norm")
|| name_lower.contains("ln_")
|| name_lower.contains("_ln")
|| name_lower.contains("norm.weight")
{
return Self::LayerNorm;
}
if name_lower.contains("embed_tokens")
|| name_lower.contains("wte")
|| name_lower.contains("word_embed")
|| name_lower.contains("token_embed")
|| name_lower.contains("position_embed")
{
return Self::Embedding;
}
if name_lower.contains("lm_head") || name_lower.contains("output.weight") {
return Self::OutputHead;
}
if name_lower.contains("qkv") || name_lower.contains("c_attn") {
return Self::AttentionQkv;
}
if name_lower.contains("q_proj")
|| name_lower.contains(".wq.")
|| name_lower.contains("query")
{
return Self::AttentionQuery;
}
if name_lower.contains("k_proj")
|| name_lower.contains(".wk.")
|| name_lower.contains("key")
{
return Self::AttentionKey;
}
if name_lower.contains("v_proj")
|| name_lower.contains(".wv.")
|| name_lower.contains("value")
{
return Self::AttentionValue;
}
if name_lower.contains("o_proj")
|| name_lower.contains(".wo.")
|| name_lower.contains("dense")
{
return Self::AttentionOutput;
}
if name_lower.contains("mlp.")
|| name_lower.contains("feed_forward")
|| name_lower.contains("ffn")
|| name_lower.contains(".fc1")
|| name_lower.contains(".fc2")
|| name_lower.contains("up_proj")
|| name_lower.contains("down_proj")
|| name_lower.contains("gate_proj")
|| name_lower.contains("w1.")
|| name_lower.contains("w2.")
|| name_lower.contains("w3.")
{
return Self::Mlp;
}
Self::Unknown
}
pub fn recommended_method(&self) -> CompressionMethod {
match self {
Self::LayerNorm | Self::Bias => CompressionMethod::None,
Self::AttentionQuery
| Self::AttentionKey
| Self::AttentionValue
| Self::AttentionOutput
| Self::AttentionQkv
| Self::Mlp
| Self::Embedding
| Self::OutputHead
| Self::Unknown => CompressionMethod::Dct,
}
}
pub fn recommended_retention(&self, base_retention: f32) -> f32 {
match self {
Self::LayerNorm | Self::Bias => 1.0,
Self::Embedding => (base_retention * 1.2).min(1.0),
Self::OutputHead => (base_retention * 1.1).min(1.0),
Self::AttentionQuery
| Self::AttentionKey
| Self::AttentionValue
| Self::AttentionOutput
| Self::AttentionQkv => base_retention,
Self::Mlp => (base_retention * 0.9).max(0.1),
Self::Unknown => base_retention,
}
}
}
#[derive(Debug, Clone)]
pub enum HybridCompressedWeight {
Svd(SvdCompressedWeight),
Dct {
width: usize,
height: usize,
fragments: Vec<HoloFragment>,
},
None {
width: usize,
height: usize,
data: Vec<f32>,
},
}
impl HybridCompressedWeight {
pub fn method(&self) -> CompressionMethod {
match self {
Self::Svd(_) => CompressionMethod::Svd,
Self::Dct { .. } => CompressionMethod::Dct,
Self::None { .. } => CompressionMethod::None,
}
}
pub fn storage_bytes(&self) -> usize {
match self {
Self::Svd(svd) => svd.storage_bytes(),
Self::Dct { fragments, .. } => fragments.iter().map(|f| f.data.len()).sum(),
Self::None { data, .. } => data.len() * 4,
}
}
pub fn original_bytes(&self) -> usize {
match self {
Self::Svd(svd) => svd.original_bytes(),
Self::Dct { width, height, .. } => width * height * 4,
Self::None { data, .. } => data.len() * 4,
}
}
pub fn compression_ratio(&self) -> f32 {
let orig = self.original_bytes();
let compressed = self.storage_bytes();
if compressed == 0 {
0.0
} else {
orig as f32 / compressed as f32
}
}
}
#[derive(Debug, Clone)]
pub struct HybridEncoder {
base_retention: f32,
num_fragments: u16,
auto_select: bool,
}
impl Default for HybridEncoder {
fn default() -> Self {
Self::new(0.30)
}
}
impl HybridEncoder {
pub fn new(base_retention: f32) -> Self {
Self {
base_retention: base_retention.clamp(0.01, 1.0),
num_fragments: 8,
auto_select: true,
}
}
pub fn with_num_fragments(mut self, n: u16) -> Self {
self.num_fragments = n.max(1);
self
}
pub fn with_auto_select(mut self, auto: bool) -> Self {
self.auto_select = auto;
self
}
pub fn compress_auto(
&self,
data: &[f32],
rows: usize,
cols: usize,
tensor_name: &str,
) -> Result<HybridCompressedWeight> {
let tensor_type = TensorType::from_name(tensor_name);
let method = tensor_type.recommended_method();
let retention = tensor_type.recommended_retention(self.base_retention);
self.compress_with_method(data, rows, cols, method, retention)
}
pub fn compress(
&self,
data: &[f32],
rows: usize,
cols: usize,
method: CompressionMethod,
) -> Result<HybridCompressedWeight> {
self.compress_with_method(data, rows, cols, method, self.base_retention)
}
pub fn compress_with_method(
&self,
data: &[f32],
rows: usize,
cols: usize,
method: CompressionMethod,
retention: f32,
) -> Result<HybridCompressedWeight> {
if data.len() != rows * cols {
return Err(Error::corrupted("data size mismatch"));
}
match method {
CompressionMethod::None => Ok(HybridCompressedWeight::None {
width: cols,
height: rows,
data: data.to_vec(),
}),
CompressionMethod::Svd => {
let encoder = SvdEncoder::new(retention);
let compressed = encoder.compress(data, rows, cols)?;
Ok(HybridCompressedWeight::Svd(compressed))
}
CompressionMethod::Dct => {
let encoder = CompressiveSpectralEncoder::new(self.num_fragments, retention);
let fragments = encoder.encode_2d(data, cols, rows)?;
Ok(HybridCompressedWeight::Dct {
width: cols,
height: rows,
fragments,
})
}
}
}
pub fn classify_tensor(&self, name: &str) -> TensorType {
TensorType::from_name(name)
}
}
#[derive(Debug, Clone, Default)]
pub struct HybridDecoder {
svd_decoder: SvdDecoder,
}
impl HybridDecoder {
pub fn new() -> Self {
Self {
svd_decoder: SvdDecoder::new(),
}
}
pub fn decompress(&self, compressed: &HybridCompressedWeight) -> Result<Vec<f32>> {
match compressed {
HybridCompressedWeight::None { data, .. } => Ok(data.clone()),
HybridCompressedWeight::Svd(svd) => self.svd_decoder.decompress(svd),
HybridCompressedWeight::Dct { fragments, .. } => {
let mut decoder = CompressiveSpectralDecoder::new();
decoder.add_essentials(&fragments[0])?;
for frag in &fragments[1..] {
decoder.add_detail(frag)?;
}
decoder.reconstruct()
}
}
}
}
#[derive(Debug, Clone, Default)]
pub struct HybridCompressionStats {
pub tensors_processed: usize,
pub svd_count: usize,
pub dct_count: usize,
pub none_count: usize,
pub total_original_bytes: usize,
pub total_compressed_bytes: usize,
pub type_counts: std::collections::HashMap<String, usize>,
}
impl HybridCompressionStats {
pub fn compression_ratio(&self) -> f32 {
if self.total_compressed_bytes == 0 {
0.0
} else {
self.total_original_bytes as f32 / self.total_compressed_bytes as f32
}
}
pub fn record(&mut self, compressed: &HybridCompressedWeight, tensor_type: TensorType) {
self.tensors_processed += 1;
self.total_original_bytes += compressed.original_bytes();
self.total_compressed_bytes += compressed.storage_bytes();
match compressed.method() {
CompressionMethod::Svd => self.svd_count += 1,
CompressionMethod::Dct => self.dct_count += 1,
CompressionMethod::None => self.none_count += 1,
}
let type_name = format!("{:?}", tensor_type);
*self.type_counts.entry(type_name).or_insert(0) += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tensor_type_classification() {
assert_eq!(
TensorType::from_name("model.layers.0.self_attn.q_proj.weight"),
TensorType::AttentionQuery
);
assert_eq!(
TensorType::from_name("model.layers.0.self_attn.k_proj.weight"),
TensorType::AttentionKey
);
assert_eq!(
TensorType::from_name("model.layers.0.self_attn.v_proj.weight"),
TensorType::AttentionValue
);
assert_eq!(
TensorType::from_name("model.layers.0.self_attn.o_proj.weight"),
TensorType::AttentionOutput
);
assert_eq!(
TensorType::from_name("model.layers.0.mlp.gate_proj.weight"),
TensorType::Mlp
);
assert_eq!(
TensorType::from_name("model.layers.0.mlp.up_proj.weight"),
TensorType::Mlp
);
assert_eq!(
TensorType::from_name("model.layers.0.mlp.down_proj.weight"),
TensorType::Mlp
);
assert_eq!(
TensorType::from_name("model.embed_tokens.weight"),
TensorType::Embedding
);
assert_eq!(
TensorType::from_name("model.layers.0.input_layernorm.weight"),
TensorType::LayerNorm
);
assert_eq!(
TensorType::from_name("lm_head.weight"),
TensorType::OutputHead
);
assert_eq!(
TensorType::from_name("model.layers.0.self_attn.q_proj.bias"),
TensorType::Bias
);
}
#[test]
fn test_recommended_method() {
assert_eq!(
TensorType::AttentionQuery.recommended_method(),
CompressionMethod::Dct
);
assert_eq!(TensorType::Mlp.recommended_method(), CompressionMethod::Dct);
assert_eq!(
TensorType::LayerNorm.recommended_method(),
CompressionMethod::None
);
assert_eq!(
TensorType::Bias.recommended_method(),
CompressionMethod::None
);
}
#[test]
fn test_hybrid_encoder_auto() {
let encoder = HybridEncoder::new(0.30);
let data: Vec<f32> = (0..64).map(|i| (i as f32 * 0.1).sin()).collect();
let compressed = encoder
.compress_auto(&data, 8, 8, "model.layers.0.self_attn.q_proj.weight")
.unwrap();
assert_eq!(compressed.method(), CompressionMethod::Dct);
let compressed = encoder
.compress_auto(&data, 8, 8, "model.layers.0.input_layernorm.weight")
.unwrap();
assert_eq!(compressed.method(), CompressionMethod::None);
}
#[test]
fn test_hybrid_roundtrip_dct() {
let encoder = HybridEncoder::new(0.50);
let decoder = HybridDecoder::new();
let data: Vec<f32> = (0..64).map(|i| (i as f32 * 0.1).sin()).collect();
let compressed = encoder
.compress(&data, 8, 8, CompressionMethod::Dct)
.unwrap();
let reconstructed = decoder.decompress(&compressed).unwrap();
assert_eq!(reconstructed.len(), data.len());
}
#[test]
fn test_hybrid_roundtrip_svd() {
let encoder = HybridEncoder::new(0.50);
let decoder = HybridDecoder::new();
let data: Vec<f32> = (0..64).map(|i| (i as f32 * 0.1).sin()).collect();
let compressed = encoder
.compress(&data, 8, 8, CompressionMethod::Svd)
.unwrap();
let reconstructed = decoder.decompress(&compressed).unwrap();
assert_eq!(reconstructed.len(), data.len());
}
#[test]
fn test_hybrid_roundtrip_none() {
let encoder = HybridEncoder::new(0.50);
let decoder = HybridDecoder::new();
let data: Vec<f32> = (0..64).map(|i| (i as f32 * 0.1).sin()).collect();
let compressed = encoder
.compress(&data, 8, 8, CompressionMethod::None)
.unwrap();
let reconstructed = decoder.decompress(&compressed).unwrap();
assert_eq!(reconstructed, data);
}
#[test]
fn test_compression_stats() {
let encoder = HybridEncoder::new(0.30);
let mut stats = HybridCompressionStats::default();
let data: Vec<f32> = (0..64).map(|i| i as f32).collect();
let compressed1 = encoder
.compress_auto(&data, 8, 8, "model.layers.0.self_attn.q_proj.weight")
.unwrap();
stats.record(&compressed1, TensorType::AttentionQuery);
let compressed2 = encoder
.compress_auto(&data, 8, 8, "model.layers.0.input_layernorm.weight")
.unwrap();
stats.record(&compressed2, TensorType::LayerNorm);
assert_eq!(stats.tensors_processed, 2);
assert_eq!(stats.dct_count, 1);
assert_eq!(stats.none_count, 1);
}
#[test]
fn test_recommended_retention() {
let base = 0.30;
assert_eq!(TensorType::LayerNorm.recommended_retention(base), 1.0);
assert!(TensorType::Mlp.recommended_retention(base) < base);
assert!(TensorType::Embedding.recommended_retention(base) > base);
}
}