| Crates.io | ds-r1-rs |
| lib.rs | ds-r1-rs |
| version | 0.1.1 |
| created_at | 2025-08-29 15:29:31.49868+00 |
| updated_at | 2025-08-29 15:55:30.157548+00 |
| description | A DeepSeek R1-inspired reasoning model prototype in Rust |
| homepage | https://github.com/k5602/ds-r1_rs |
| repository | https://github.com/k5602/ds-r1_rs |
| max_upload_size | |
| id | 1816023 |
| size | 812,448 |
A Rust implementation of a DeepSeek R1βinspired reasoning model focused on clarity, testability, and strong engineering practices. This project is designed to be an impressive portfolio piece: it includes a modular transformer architecture, reasoning-aware inference, evaluation harness, examples, comprehensive tests, and CI.
Highlights:
Prerequisites: Rust (stable), Cargo.
Build:
cargo build
Run CLI:
# Help (shows available commands)
cargo run
Core commands:
# Show default model configuration
cargo run -- config
# Show version and build info
cargo run -- version
# Run basic checks and smoke tests
cargo run -- test
# Generate text from a prompt (uses simple model forward)
cargo run -- generate "Explain Rust ownership in simple terms"
# Evaluate reasoning benchmarks (math, logic, programming, general)
cargo run -- eval
# Export evaluation results as JSON (for dashboards)
cargo run -- eval --json > results.json
# Save current model weights (full)
cargo run -- save-weights ckpt.json
# Save only lm_head parameters; exclude embeddings
cargo run -- save-weights ckpt.json --include lm_head --exclude embeddings
# Save a small demo-size checkpoint (size-conscious)
cargo run -- save-weights ckpt-small.json --demo-small
# Load weights and generate deterministically (temperature=0)
cargo run -- load-weights ckpt.json "Explain Rust ownership"
# Load only lm_head from checkpoint, allowing missing others
cargo run -- load-weights ckpt.json --allow-missing --include lm_head "Explain Rust ownership"
Run examples:
cargo run --example config_demo
cargo run --example generation_demo
cargo run --example math_solver_demo
cargo run --example training_demo
Tests + checks:
# Unit + doc tests
cargo test
# Integration tests (CLI)
cargo test --test cli_integration
# Optional heavier integration tests
cargo test --test cli_integration -- --ignored
# Lints/format
cargo clippy --all-targets -- -D warnings
cargo fmt --all -- --check
# Benchmarks (Criterion)
cargo bench --bench decoding -- --warm-up-time 0.5 --measurement-time 10
A ready-to-use devcontainer is provided at .devcontainer/devcontainer.json for reproducible development with VS Code or compatible editors.
Requirements:
Usage:
Common commands inside the devcontainer:
# Run unit + integration tests
cargo test
cargo test --test cli_integration
cargo test --test cli_integration -- --ignored
# Lints/format
cargo clippy --all-targets -- -D warnings
cargo fmt --all -- --check
# Benchmarks (Criterion)
cargo bench --bench decoding -- --warm-up-time 0.5 --measurement-time 10
Notes:
A minimal Dockerfile is included for reproducible builds and tests.
Build the image:
docker build -t ds-r1-rs:latest .
Run the CLI:
# Print version
docker run --rm ds-r1-rs:latest version
# Show default config
docker run --rm ds-r1-rs:latest config
# Generate text
docker run --rm ds-r1-rs:latest generate "Explain Rust ownership"
Mount the project and run from source (optional):
# Evaluate and export JSON results using the container runtime
docker run --rm -v "$PWD":/work -w /work ds-r1-rs:latest ds-r1-rs eval --json
Run tests inside the container:
# Use the built toolchain to run tests against your mounted workspace
docker run --rm -v "$PWD":/work -w /work ds-r1-rs:latest bash -lc "cargo test --all --release --locked"
Tip:
βββ ds-r1_rs/ # Rust crate
β βββ Cargo.toml
β βββ src/
β β βββ main.rs # CLI: config/version/test/generate/eval
β β βββ lib.rs # Public crate API & re-exports
β β βββ model/ # Core model components
β β β βββ config.rs # Model configuration & validation
β β β βββ transformer.rs# Transformer stack + LM head (implemented)
β β β βββ attention.rs # Standard attention + MLA + Linear
β β β βββ layers.rs # Pre-norm TransformerLayer, FFN (SwiGLU), LayerNorm
β β β βββ embeddings.rs # Token + Rotary embeddings
β β β βββ moe.rs # Mixture of Experts (router, experts, load balancing)
β β βββ inference/ # Inference & reasoning
β β β βββ engine.rs # InferenceEngine + high-level solve/explain APIs
β β β βββ generation.rs # Text generation & configs (KV cache placeholder)
β β β βββ sampling.rs # Greedy/temperature/top-k sampling
β β β βββ reasoning.rs # <think> parsing, states, analysis
β β β βββ math_solver.rs# Structured math solver utilities
β β β βββ code_analyzer.rs
β β βββ training/ # Training infrastructure (supervised + RL scaffolding)
β β β βββ data.rs # Datasets + loaders + synthetic generator
β β β βββ loss.rs # CrossEntropy + metrics
β β β βββ optimizer.rs # Adam optimizer
β β β βββ trainer.rs # BasicTrainer + RLTrainer (REINFORCE scaffolding)
β β βββ utils/ # Errors, math, tokenizer, evaluation harness
β βββ examples/ # Ready-to-run demos
β βββ generation_demo.rs
β βββ math_solver_demo.rs
β βββ training_demo.rs
β βββ config_demo.rs
βββ .github/workflows/ci.yml # CI: build, lint, test, examples, coverage
[seq_len * vocab_size]Programmatic usage:
use ds_r1_rs::{
model::{ModelConfig, DeepSeekR1Model},
inference::engine::InferenceEngine,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build model with validated config
let config = ModelConfig::default();
let model = DeepSeekR1Model::new(config)?;
// Inference engine with default generation configs
let mut engine = InferenceEngine::new(model)?;
// Basic generation
let text = engine.generate_text("The Rust language is")?;
println!("Generated: {}", text);
// Reasoning-aware generation
let reasoning = engine.generate_with_reasoning("Explain ownership in Rust")?;
println!("Steps: {:?}", reasoning.thinking_chain);
println!("Answer: {}", reasoning.final_answer);
Ok(())
}
CLI usage (quick):
cargo run -- config
cargo run -- generate "List 3 benefits of static typing"
cargo run -- eval
cargo run -- eval --json > results.json
cargo run -- save-weights ckpt.json
cargo run -- load-weights ckpt.json "Explain Rust ownership"
You can save/load checkpoints in JSON v1 format. Partial save/load is supported via name-prefix filters. For size-conscious artifacts, use the demo-small model configuration.
Examples:
# Full save
cargo run -- save-weights ckpt.json
# Partial save/load only lm_head.*
cargo run -- save-weights ckpt.json --include lm_head
cargo run -- load-weights ckpt.json --allow-missing --include lm_head "Your prompt"
# Size-conscious small artifact
cargo run -- save-weights ckpt-small.json --demo-small
cargo run -- load-weights ckpt-small.json --demo-small "Your prompt"
# Deterministic generation (temperature=0 applied automatically in load-weights flow)
cargo run -- load-weights ckpt.json "Explain Rust ownership"
This prototype uses special thinking tokens and a reasoning state machine to parse and structure βthoughtsβ during generation:
<think> ... </think> sections.ReasoningEngine tracks states (Normal/Thinking/Answering), captures steps, and produces a ReasoningOutput.EvaluationHarness aggregates metrics (accuracy, clarity, verification presence) across curated benchmarks and reports performance and breakdowns.[seq_len * vocab_size] for simple integration with training and demos.Use:
cargo run -- eval
This runs curated reasoning benchmarks via the EvaluationHarness:
Metrics reported:
Whatβs next (post v0.1)
GitHub Actions workflow runs on PRs and main:
This is a research/education project. Issues and PRs are welcome. Please:
MIT β see the crate manifest for details.
Made with insistence by Khaled.