rustensor 0.1.0

Small Rust-native checkpoint format for model metadata and tensor weights.
Documentation
//! Tensor weight layer definitions.
//!
//! This module defines supported tensor data, tensor dtype metadata and
//! validation rules for weight layers.
use crate::error::{Result, RustensorError};
use serde::{Deserialize, Serialize};

pub const MAX_DIMENSIONS: usize = 16;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TensorDType {
    F32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum TensorData {
    F32(Vec<f32>),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct WeightLayer {
    pub name: String,
    pub shape: Vec<u64>,
    pub dtype: TensorDType,
    pub data: TensorData,
}

impl WeightLayer {
    pub fn f32(name: impl Into<String>, shape: Vec<u64>, data: Vec<f32>) -> Result<Self> {
        let layer = Self {
            name: name.into(),
            shape,
            dtype: TensorDType::F32,
            data: TensorData::F32(data),
        };

        layer.validate()?;
        Ok(layer)
    }

    pub fn validate(&self) -> Result<()> {
        if self.name.trim().is_empty() {
            return Err(RustensorError::InvalidLayerName);
        }

        self.validate_shape()?;
        self.validate_data()?;

        Ok(())
    }

    fn validate_shape(&self) -> Result<()> {
        if self.shape.is_empty() {
            return Err(RustensorError::InvalidTensorShape {
                reason: "shape must contain at least one dimension".to_string(),
            });
        }

        if self.shape.len() > MAX_DIMENSIONS {
            return Err(RustensorError::InvalidTensorShape {
                reason: format!("shape has too many dimensions: {}", self.shape.len()),
            });
        }

        if self.shape.contains(&0) {
            return Err(RustensorError::InvalidTensorShape {
                reason: "shape dimensions must be greater than zero".to_string(),
            });
        }

        self.expected_element_count()?;
        Ok(())
    }

    fn validate_data(&self) -> Result<()> {
        match &self.data {
            TensorData::F32(values) => {
                if self.dtype != TensorDType::F32 {
                    return Err(RustensorError::InvalidTensorData {
                        reason: "dtype does not match tensor data".to_string(),
                    });
                }

                let expected = self.expected_element_count()?;
                let found = values.len();

                if found != expected {
                    return Err(RustensorError::InvalidTensorDataLength { expected, found });
                }

                if values.iter().any(|value| !value.is_finite()) {
                    return Err(RustensorError::InvalidTensorData {
                        reason: "F32 tensor data must not contain NaN or infinity".to_string(),
                    });
                }
            }
        }

        Ok(())
    }

    fn expected_element_count(&self) -> Result<usize> {
        let product = self.shape.iter().try_fold(1u64, |acc, dimension| {
            acc.checked_mul(*dimension)
                .ok_or_else(|| RustensorError::InvalidTensorShape {
                    reason: "shape element count overflow".to_string(),
                })
        })?;

        usize::try_from(product).map_err(|_| RustensorError::InvalidTensorShape {
            reason: "shape element count does not fit in usize".to_string(),
        })
    }
}