Cuttle 🦀
一个基于CPU的大语言模型推理引擎,使用纯Rust实现,专门优化支持Qwen3-0.6B模型。
✨ 特性
- 🦀 纯Rust实现: 无Python依赖,高性能CPU推理
- 🤖 Qwen3-0.6B支持: 专门优化支持Qwen3-0.6B模型
- 🌐 中英文双语: 支持中英文双语文本生成
- 📦 自动下载: 自动模型下载功能
- 💻 命令行界面: 易于使用的CLI工具
- 🔧 灵活配置: 可配置的推理参数和分词系统
- 📊 性能监控: 内置性能分析和基准测试
🏗️ Architecture
Cuttle adopts a modular design with the following main components:
- Tensor Module (
tensor): High-performance tensor operations using pure Rust - Model Module (
model): Transformer architecture implementation - Tokenizer Module (
tokenizer): Text tokenization and encoding - Inference Engine (
inference): Complete inference pipeline - Utils Module (
utils): Performance monitoring and utility functions
📦 安装和构建
系统要求
- Rust 1.70+
- 内存: 建议4GB以上
- 存储: 约2GB用于模型文件
- 网络: 首次下载模型需要网络连接
从源码构建
# 克隆仓库
# 调试构建
# 发布构建(推荐用于实际使用)
# 安装命令行工具
🚀 快速开始
1. 下载Qwen3-0.6B模型
# 下载Qwen3-0.6B模型文件到assets目录
# 强制重新下载(如果文件已存在)
2. 文本生成
# 中文文本生成
# 英文文本生成
# 交互式模式
# 自定义参数
3. 查看模型信息
# 显示模型信息
💻 Programming Interface
Basic Usage
use ;
// Create model configuration
let config = default;
let model = new?;
// Create tokenizer
let mut tokenizer = create_default_tokenizer;
let texts = vec!;
tokenizer.build_vocab?;
// Create inference engine
let engine = new;
// Generate text
let response = engine.generate?;
println!;
Custom Inference Configuration
let inference_config = InferenceConfig ;
let engine = with_config;
Batch Processing
let prompts = vec!;
let responses = engine.generate_batch?;
for in prompts.iter.zip
Tensor Operations
use Tensor;
// Create tensors
let a = randn?;
let b = randn?;
// Matrix multiplication
let c = a.matmul?;
// Activation function
let activated = c.gelu;
// Softmax
let probs = activated.softmax?;
⚙️ Configuration
Model Configuration (config.json)
配置选项
--max-length: 最大生成长度 (默认: 512)--temperature: 温度参数,控制随机性 (默认: 1.0)--top-p: Top-p采样参数 (默认: 0.9)--top-k: Top-k采样参数 (默认: 50)--interactive: 交互式模式--force: 强制重新下载模型
📊 Performance Benchmarks
Run benchmarks:
# Run all benchmarks
# Run specific benchmarks
Performance Optimization Tips
- Compilation Optimization: Use
--releasemode - Pure Rust Implementation: No external BLAS dependencies required
- Parallel Processing: Utilize Rayon for parallel computation
- Memory Management: Avoid unnecessary memory allocations
🧪 Testing
# Run unit tests
# Run integration tests
# Run documentation tests
📚 API Documentation
Generate and view API documentation:
🛠️ Development
项目结构
cuttle/
├── src/
│ ├── lib.rs # 库入口
│ ├── main.rs # 命令行工具
│ ├── model.rs # 模型定义
│ ├── inference.rs # 推理引擎
│ ├── tensor.rs # 张量运算
│ ├── tokenizer.rs # 分词器
│ ├── downloader.rs # 模型下载器
│ ├── error.rs # 错误处理
│ └── utils.rs # 工具函数
├── assets/ # 模型文件存储目录
│ └── qwen3-0.6b/ # Qwen3-0.6B模型文件
├── examples/ # 示例代码
├── benches/ # 性能测试
├── tests/ # 集成测试
├── Cargo.toml # 项目配置
└── README.md # 项目文档
🤖 Qwen3-0.6B模型配置
- 参数量: 0.6B
- 词汇表大小: 151,936
- 隐藏层维度: 1,024
- 层数: 28
- 注意力头数: 16
- 键值头数: 8 (GQA)
- 支持语言: 中文、英文等多语言
📝 使用示例
中文文本生成
英文文本生成
交互式对话
Contributing Guidelines
- Fork the project
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Create a Pull Request
Code Style
- Use
rustfmtto format code - Use
clippyfor code linting - Write comprehensive documentation and tests
# Format code
# Code linting
🔧 Troubleshooting
Common Issues
Q: Compilation errors
A: Ensure you have the latest Rust toolchain:
# Update Rust
# Use Rust 2024 edition
Q: Slow inference speed
A: Check the following optimization options:
- Compile with
--releasemode - Adjust batch processing size
- Use smaller models for testing
- Enable parallel processing
Q: High memory usage
A: Try the following approaches:
- Reduce model size
- Lower batch processing size
- Use smaller sequence lengths
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- rayon - Parallel computing framework
- serde - Serialization framework
- clap - Command line argument parsing
- tokio - Asynchronous runtime
🔗 Related Links
Cuttle - Power your AI inference with Rust 🦀✨