| Crates.io | entrenar |
| lib.rs | entrenar |
| version | 0.5.6 |
| created_at | 2025-11-21 07:44:33.040607+00 |
| updated_at | 2026-01-22 09:04:53.788934+00 |
| description | Training & Optimization library with autograd, LoRA, quantization, and model merging |
| homepage | |
| repository | https://github.com/paiml/entrenar |
| max_upload_size | |
| id | 1943220 |
| size | 102,653,624 |
Entrenar (Spanish: "to train") provides everything needed to train neural networks in Rust:
Part of the PAIML Stack, built on trueno for SIMD-accelerated operations.
# From crates.io
cargo install entrenar
# From source
git clone https://github.com/paiml/entrenar
cd entrenar
cargo install --path .
Add to your Cargo.toml:
[dependencies]
entrenar = "0.2"
use entrenar::train::{Trainer, TrainConfig, Batch, MSELoss, EarlyStopping};
use entrenar::optim::Adam;
use entrenar::Tensor;
fn main() {
// Model parameters
let params = vec![Tensor::zeros(784 * 128, true)];
let optimizer = Adam::new(0.001, 0.9, 0.999, 1e-8);
// Create trainer with callbacks
let mut trainer = Trainer::new(params, Box::new(optimizer), TrainConfig::default());
trainer.set_loss(Box::new(MSELoss));
trainer.add_callback(EarlyStopping::new(5, 0.001));
// Train
let result = trainer.train(100, || batches.clone(), |x| model.forward(x));
println!("Final loss: {:.4}", result.final_loss);
}
# train.yaml
model:
path: base-model.gguf
data:
train: train.parquet
batch_size: 8
optimizer:
name: adamw
lr: 0.0001
lora:
rank: 64
alpha: 16
training:
epochs: 10
grad_clip: 1.0
entrenar train train.yaml
Tape-based automatic differentiation with verified gradients:
use entrenar::autograd::{matmul, softmax, layer_norm, attention};
let y = matmul(&x, &w); // Matrix multiplication
let s = softmax(&logits); // Softmax activation
let n = layer_norm(&x, &gamma, &beta); // Layer normalization
let a = attention(&q, &k, &v); // Scaled dot-product attention
use entrenar::optim::{SGD, Adam, AdamW, CosineScheduler};
let sgd = SGD::new(0.01, 0.9);
let adam = Adam::new(0.001, 0.9, 0.999, 1e-8);
let adamw = AdamW::new(0.001, 0.9, 0.999, 1e-8, 0.01);
// Learning rate scheduling
let scheduler = CosineScheduler::new(0.001, 0.0001, 100);
Parameter-efficient fine-tuning with up to 99.75% parameter reduction:
use entrenar::lora::{LoRALayer, QLoRALayer, LoRAConfig};
// Standard LoRA
let lora = LoRALayer::new(4096, 4096, 16, 32.0);
// QLoRA: 4-bit base + FP16 adapters
// 7B model: 28GB -> 3.5GB memory
let qlora = QLoRALayer::new(base_weights, 16, 32.0);
use entrenar::quant::{FakeQuantize, PTQCalibrator, GGUFQuantizer};
// QAT with straight-through estimator
let fq = FakeQuantize::new(8, true);
// Post-training quantization
let calibrator = PTQCalibrator::percentile(0.999);
// GGUF export (llama.cpp compatible)
let quantizer = GGUFQuantizer::q4_0();
use entrenar::merge::{TiesMerge, DareMerge, SlerpMerge};
// TIES: Trim + Sign Election
let merged = TiesMerge::new(0.2).merge(&models, &weights);
// DARE: Dropout + Rescale
let merged = DareMerge::new(0.9).merge(&base, &finetuned);
// SLERP: Spherical interpolation
let merged = SlerpMerge::new().merge(&a, &b, 0.5);
use entrenar::distill::{DistillationLoss, EnsembleDistiller};
// Temperature-scaled KD loss
let kd = DistillationLoss::new(4.0, 0.7);
let loss = kd.compute(&student, &teacher, &labels);
// Multi-teacher ensemble
let ensemble = EnsembleDistiller::weighted(&[0.5, 0.3, 0.2]);
use entrenar::train::{
EarlyStopping, CheckpointCallback, ProgressCallback,
MonitorCallback, ExplainabilityCallback, ExplainMethod,
};
trainer.add_callback(EarlyStopping::new(5, 0.001));
trainer.add_callback(CheckpointCallback::new("./checkpoints"));
trainer.add_callback(ProgressCallback::new(10));
trainer.add_callback(MonitorCallback::new()); // NaN/Inf detection
// Feature importance tracking
trainer.add_callback(
ExplainabilityCallback::new(ExplainMethod::PermutationImportance)
.with_top_k(10)
);
Toyota Way-inspired quality monitoring:
use entrenar::monitor::{MetricsCollector, DriftDetector, AndonSystem};
let mut collector = MetricsCollector::new();
let mut drift = DriftDetector::new(10);
let mut andon = AndonSystem::new();
// Automatic drift detection and Andon alerts
if let DriftStatus::Drift(z) = drift.check(loss) {
andon.warning(format!("Loss drift: z={:.2}", z));
}
cargo run --example training_loop # Basic training
cargo run --example explainability # Feature attribution
cargo run --example distillation # Knowledge distillation
cargo run --example merge_models # Model merging
cargo run --example model_io # Save/load models
cargo run --example cli_bench # Latency benchmarking
cargo run --example cli_audit # Bias detection
cargo run --example cli_monitor # Drift detection (PSI)
# Training
entrenar train config.yaml --epochs 10
# Model operations
entrenar quantize model.safetensors --bits 4 --output model_q4.json
entrenar merge model1.safetensors model2.safetensors --method ties
# Benchmarking & Monitoring
entrenar bench config.yaml --warmup 5 --iterations 100
entrenar inspect model.safetensors -v
entrenar audit predictions.parquet --type bias --threshold 0.8
entrenar monitor data.parquet --threshold 0.2
# Shell completions
entrenar completion bash > ~/.local/share/bash-completion/completions/entrenar
entrenar/
├── autograd/ Tape-based automatic differentiation
├── optim/ SGD, Adam, AdamW, schedulers
├── lora/ LoRA, QLoRA fine-tuning
├── quant/ QAT, PTQ, GGUF quantization
├── merge/ TIES, DARE, SLERP merging
├── distill/ Knowledge distillation
├── train/ Trainer, callbacks, metrics
├── monitor/ Real-time monitoring, Andon
├── config/ Declarative YAML config
└── io/ Model persistence
| Metric | Value |
|---|---|
| Tests | 2155 passing |
| Coverage | >90% |
| Property Tests | 200K+ iterations |
| Gradient Checking | Finite difference validated |
| Mutation Testing | >80% kill rate |
| Library | Purpose | Version |
|---|---|---|
| trueno | SIMD tensor operations | 0.7.3 |
| entrenar | Training & optimization | 0.2.3 |
| aprender | ML algorithms & explainability | 0.14.0 |
| realizar | GGUF inference | 0.2.1 |
Contributions welcome! Please follow the PAIML quality standards:
cargo testcargo clippy -- -D warnings && cargo fmt --checkMIT License - see LICENSE for details.