#![cfg(feature = "gguf-export")]
use std::path::Path;
use crate::error::{BitNetError, Result};
use crate::layer::BitLinear;
use crate::quantization::dequantize_weights;
pub fn export_gguf<P: AsRef<Path>>(
layer: &BitLinear,
output_path: P,
layer_name: &str,
) -> Result<()> {
use candle_core::Device;
let device = Device::Cpu;
let weights = dequantize_weights(layer.quantized_weight(), &device)?;
let nf4_quantized = qlora_rs::quantize_nf4(&weights, 64)
.map_err(|e| BitNetError::Serialization(e.to_string()))?;
let metadata = qlora_rs::GgufMetadata {
model_name: format!("bitnet-{}", layer_name),
model_type: "bitnet".to_string(),
model_size: nf4_quantized.numel(),
};
qlora_rs::export_gguf(&[(layer_name, &nf4_quantized)], Some(metadata), output_path)
.map_err(|e| BitNetError::Serialization(e.to_string()))?;
Ok(())
}