| Crates.io | micro_cartan_attn |
| lib.rs | micro_cartan_attn |
| version | 0.2.0 |
| created_at | 2025-08-01 04:38:26.115223+00 |
| updated_at | 2025-08-01 14:39:31.552663+00 |
| description | Complete Cartan matrix attention mechanisms with proper Lie algebra structures |
| homepage | |
| repository | https://github.com/ruvnet/ruv-FANN |
| max_upload_size | |
| id | 1776094 |
| size | 166,103 |
Cartan matrix-constrained attention mechanisms for semantic coherence
This crate implements attention mechanisms that maintain Cartan matrix constraints for preserving orthogonal semantic relationships in neural networks. It provides structured attention patterns based on Lie algebra principles.
Add this to your Cargo.toml:
[dependencies]
micro_cartan_attn = { path = "../micro_cartan_attn" }
micro_core = { path = "../micro_core" }
Basic Cartan-constrained attention mechanism:
use micro_cartan_attn::{CartanAttention, AttentionConfig};
use micro_core::{RootVector, CartanMatrix};
// Create Cartan matrix (identity by default)
let cartan_matrix = CartanMatrix::default();
// Configure attention
let config = AttentionConfig {
orthogonalize_output: true,
regularize_attention: true,
temperature: 1.0,
normalize_input: true,
};
// Create attention mechanism
let mut attention = CartanAttention::with_config(cartan_matrix, config)?;
// Apply attention to vector sequence
let vectors = vec![
RootVector::from_array([1.0, 0.0, /* ... 30 more zeros ... */]),
RootVector::from_array([0.0, 1.0, /* ... 30 more zeros ... */]),
];
let attended = attention.apply_attention(&vectors)?;
let weights = attention.attention_weights()?;
Multi-head attention with different Cartan constraint types:
use micro_cartan_attn::MultiHeadCartanAttention;
// Create different Cartan matrices for each head
let matrices = vec![
CartanMatrix::default(), // Identity
CartanMatrix::default(), // Another identity (placeholder)
];
let mut multi_head = MultiHeadCartanAttention::new(matrices)?;
// Set custom output weights
multi_head.set_output_weights(vec![0.6, 0.4])?;
// Apply multi-head attention
let output = multi_head.apply_attention(&input_vectors)?;
The attention mechanism enforces compliance with Cartan matrix relationships:
// Attention scoring based on Cartan compliance
for (i, vector) in vectors.iter().enumerate() {
let mut score = 0.0;
// Score based on inner product compliance
for (j, other_vector) in vectors.iter().enumerate() {
if i != j {
let actual_inner = vector.dot(other_vector);
let target_inner = cartan_matrix.entry(i, j);
let compliance = (-((actual_inner - target_inner).powi(2))).exp();
score += compliance;
}
}
// Score based on norm compliance (Cartan normalization)
let norm = vector.magnitude();
let target_norm = (2.0_f32).sqrt(); // โจฮฑแตข, ฮฑแตขโฉ = 2
let norm_compliance = (-((norm - target_norm).powi(2))).exp();
score += norm_compliance;
}
use micro_cartan_attn::AttentionConfig;
let config = AttentionConfig {
orthogonalize_output: true, // Apply orthogonalization after attention
regularize_attention: true, // Apply regularization during attention
temperature: 1.0, // Temperature for softmax
normalize_input: true, // Normalize input vectors
};
[features]
default = ["gram-schmidt"]
std = ["serde?/std", "nalgebra/std"]
serde = ["dep:serde", "dep:serde_json"]
gram-schmidt = [] # Gram-Schmidt orthogonalization (placeholder)
training = [] # Training-time features (placeholder)
simd = [] # SIMD optimizations (not implemented)
analysis = [] # Analysis tools (not implemented)
The current implementation focuses on correctness over performance:
| Operation | Time (ฮผs) | Notes |
|---|---|---|
| CartanAttention (2 vectors) | ~5-15 | Basic compliance scoring |
| MultiHead (4 heads) | ~20-60 | 4x single head cost |
| Orthogonalization | ~10-30 | Placeholder implementation |
# Run unit tests
cargo test
# Test with micro_core integration
cargo test --features integration-tests
# Test serialization
cargo test --features serde
Current tests cover:
Missing tests:
Current examples demonstrate:
Planned examples:
This is part of a research project. Contributions welcome for:
Licensed under either of:
at your option.
micro_core: Core types and mathematical structuresmicro_routing: Dynamic routing using attention mechanismsmicro_metrics: Performance monitoring and analysismicro_swarm: High-level orchestration with attention-based coordinationPart of the rUv-FANN Semantic Cartan Matrix system - Research implementation of Cartan matrix-inspired attention mechanisms.