llm-worker-rbee#

Candle-based Llama inference worker daemon

Created by: TEAM-000 (Foundation)
TEAM-009 Update: Complete rewrite using candle-transformers Llama directly


Overview#

llm-worker-rbee is a Llama inference worker with three feature-gated binaries:

TEAM-009 Implementation (Current)#

  • ✅ Uses candle-transformers::models::llama::Llama directly
  • ✅ Three binaries: CPU, CUDA, Metal
  • ✅ SafeTensors model loading with VarBuilder
  • ✅ HuggingFace tokenizers integration
  • ✅ Streaming token generation with sampling
  • ✅ Device residency logging
  • ✅ Production-ready inference in ~340 lines

Why this approach? (TEAM-008 recommendation)

  • 🚀 4-6 hours to working inference vs 20-30 hours building from scratch
  • 🎯 Production-ready Llama implementation from Candle team
  • ⚡ Optimized for GPU/CPU with GQA, RoPE scaling, quantization support
  • 🔧 Minimal code - focus on worker integration, not layer implementation

Original Plan (TEAM-000)#

  • Pure ndarray for CPU (checkpoint validation, educational value)
  • Candle kernels for CUDA acceleration (optional, performance)
  • Checkpoint-driven development

Status: TEAM-009 pivoted to use candle-transformers directly per TEAM-008 handoff


Architecture#

TEAM-009 Architecture (Current)#

┌─────────────────────────────────────────┐
│         llm-worker-rbee Worker           │
├─────────────────────────────────────────┤
│  HTTP Server (worker-http)              │
│  ├─ GET /health                         │
│  └─ POST /execute                       │
├─────────────────────────────────────────┤
│  CandleInferenceBackend (TEAM-009)      │
│  ├─ candle-transformers::Llama          │
│  ├─ tokenizers::Tokenizer              │
│  ├─ Device (CPU/CUDA/Metal)             │
│  └─ Sampling (greedy/temperature)       │
├─────────────────────────────────────────┤
│  Model Loading                          │
│  ├─ SafeTensors: VarBuilder + mmap      │
│  ├─ GGUF: Not yet implemented           │
│  └─ Config: Default 7B (TODO: parse)    │
├─────────────────────────────────────────┤
│  Compute Backend (feature-gated)        │
│  ├─ CPU: Device::Cpu                    │
│  ├─ CUDA: Device::new_cuda(idx)         │
│  └─ Metal: Device::Metal(id) (macOS)    │
└─────────────────────────────────────────┘

Three Binaries#

BinaryFeatureDeviceUse Case
llorch-cpu-candledcpuCPUx86 Linux/Windows, macOS CPU
llorch-cuda-candledcudaCUDANVIDIA GPU
llorch-metal-candledmetalMetal GPUApple Silicon GPU (macOS)

Key Differences from llorch-cpud#

Aspectllorch-cpudllm-worker-rbee (TEAM-009)
ModelGPT-2 MediumLlama (any size)
ImplementationCustom layerscandle-transformers
FormatGGUFSafeTensors (GGUF TODO)
Tokenizerworker-tokenizerHuggingFace tokenizers
BackendsCPU onlyCPU/CUDA/Metal
Code size~2000 lines~340 lines

Features#

TEAM-009 Implementation#

  • ✅ Three backends: CPU, CUDA, Metal (feature-gated)
  • ✅ SafeTensors loading: Memory-mapped for efficiency
  • ✅ Streaming generation: Token-by-token with sampling
  • ✅ Device residency: Logging to prevent RAM↔VRAM leaks
  • ✅ Worker integration: Full worker-http + worker-common support
  • ⏳ GGUF support: Deferred (use SafeTensors for now)

Original Features (TEAM-000)#

  • Checkpoint-driven development: Each component validated independently
  • Educational: Learn Llama architecture from scratch
  • Worker crates integration: Reuses 99% of existing infrastructure

Quick Start#

TEAM-009 Update: Three feature-gated binaries using candle-transformers Llama directly.

Build#

CPU-only (x86, or fallback on macOS):

bash
cd bin/llm-worker-rbee
cargo build --release --features cpu --bin llorch-cpu-candled

CUDA (NVIDIA GPU):

bash
cargo build --release --features cuda --bin llorch-cuda-candled

Metal (Apple Silicon GPU):

bash
cargo build --release --features metal --bin llorch-metal-candled

Run#

Requirements:

  • SafeTensors format model (GGUF not yet supported)
  • tokenizer.json in same directory as model
  • config.json in same directory as model

CPU:

bash
./target/release/llorch-cpu-candled \
--worker-id test-worker \
--model /path/to/llama-2-7b/ \
--port 8080 \
--callback-url http://localhost:9999

CUDA:

bash
./target/release/llorch-cuda-candled \
--worker-id test-worker \
--model /path/to/llama-2-7b/ \
--port 8080 \
--cuda-device 0 \
--callback-url http://localhost:9999

Metal:

bash
./target/release/llorch-metal-candled \
--worker-id test-worker \
--model /path/to/llama-2-7b/ \
--port 8080 \
--metal-device 0 \
--callback-url http://localhost:9999

Test#

bash
# Run all tests (CPU)
cargo test --features cpu
# Run TEAM-009 smoke tests
cargo test --test team_009_smoke --features cpu
# Run with CUDA (requires GPU)
cargo test --features cuda
# Run ignored integration test (requires model)
LLORCH_TEST_MODEL_PATH=/path/to/model cargo test test_device_residency_enforcement --features cpu -- --ignored

Development Roadmap#

Week 1: Foundation (Checkpoint 0)#

  • Project structure
  • HTTP server integration
  • Worker crates wiring
  • Compilation validation

Week 2: Core Layers#

  • Checkpoint 1: RMSNorm
  • Checkpoint 1B: RoPE
  • Checkpoint 2: Separate QKV
  • Checkpoint 3: KV Cache
  • Checkpoint 6: SwiGLU

Week 3: Full Model#

  • Checkpoint 4: Attention Scores
  • Checkpoint 5: Attention Output
  • Checkpoint 7: First Block
  • Checkpoint 8: Full Logits

Week 4: Validation & Optimization#

  • Checkpoints 9-11: Sampling
  • Checkpoint 12: End-to-End
  • CUDA kernel integration
  • Performance benchmarking

Checkpoints#

All checkpoints are documented in .specs/checkpoints/:

CheckpointComponentStatus
0Foundation Setup⬜ Not Started
1RMSNorm⬜ Not Started
1BRoPE Application⬜ Not Started
2QKV Projection⬜ Not Started
3KV Cache⬜ Not Started
4Attention Scores⬜ Not Started
5Attention Output⬜ Not Started
6SwiGLU FFN⬜ Not Started
7First Block⬜ Not Started
8Full Logits⬜ Not Started
9Selected Logits⬜ Not Started
10Argmax Sampling⬜ Not Started
11Softmax Probs⬜ Not Started
12End-to-End⬜ Not Started

Dependencies#

Worker Crates (100% reusable)#

  • worker-common: SamplingConfig, InferenceResult, startup callbacks
  • worker-http: HTTP server, SSE streaming, InferenceBackend trait
  • worker-tokenizer: BPE tokenization, GGUF support
  • worker-models: Model configs and adapters
  • worker-gguf: GGUF file format parser

Compute#

  • ndarray: CPU tensor operations (primary)
  • candle-kernels: CUDA kernels (optional, feature-gated)
  • cudarc: CUDA runtime (optional, feature-gated)

Utilities#

  • tokio: Async runtime (single-threaded!)
  • tracing: Structured logging
  • clap: CLI argument parsing
  • anyhow, thiserror: Error handling

Candle Integration#

Following CANDLE_INTEGRATION_HANDOFF.md, we use kernels only:

What We Use ✅#

  • candle-kernels: Optimized CUDA kernels
    • RmsNorm (from reduce.cu)
    • SiLU (from unary.cu)
    • Quantization (from quantized.cu)

What We DON'T Use ❌#

  • candle-core: Too much abstraction
  • candle-nn: We build our own layers
  • candle-transformers: Defeats learning purpose

Why This Approach?#

  • ✅ Best performance (optimized CUDA kernels)
  • ✅ Keep our architecture
  • ✅ Keep checkpoint validation
  • ✅ Educational value maintained
  • ✅ Minimal dependencies

Testing#

Unit Tests#

bash
cargo test --lib

Integration Tests#

bash
cargo test --test '*'

Checkpoint Validation#

bash
# Run specific checkpoint
cargo test checkpoint_01_rms_norm -- --nocapture
# Compare with llama.cpp reference
cargo test checkpoint_01_rms_norm -- --show-output

Benchmarks#

bash
cargo bench --features benchmark

Performance#

CPU (ndarray)#

  • Purpose: Validation, checkpoint testing
  • Speed: ~10-20 tokens/sec (Llama-2 7B)
  • Memory: ~7GB (Q8_0 model)

CUDA (Candle kernels)#

  • Purpose: Production inference
  • Speed: ~50-100 tokens/sec (depends on GPU)
  • Memory: ~7GB VRAM (Q8_0 model)

References#

Documentation#

  • .specs/CANDLE_INTEGRATION_HANDOFF.md: Integration strategy
  • .specs/checkpoints/: All checkpoint specifications
  • reference/candle/: Candle source code
  • reference/mistral.rs/: Reference implementation
  • llorch-cpud: GPT-2 CPU worker (predecessor)
  • worker-orcd: Production GPU worker

Team#

TEAM-000 (Foundation)

  • Mission: Build the foundation for all future workers
  • Focus: Checkpoint-driven, educational, reusable
  • Motto: "The foundation of them all"

License#

GPL-3.0-or-later


Contributing#

  1. Follow checkpoint-driven development
  2. Add TEAM-000 signatures to code changes
  3. Validate with reference implementation (llama.cpp)
  4. Keep CPU path working (CUDA is optional)
  5. Update checkpoint docs when needed

Status: 🚧 In Development
Version: 0.1.0
Last Updated: 2025-10-08

LLM Worker rbee

LLM Worker rbee

by rbee

v0.1.0

CPUCUDAMetalROCm

Install

Installation coming soon

Metadata

Author
rbee
Version
v0.1.0
Implementation
rust
Build System
cargo

Capabilities

Supported Formats
GGUFSAFETENSORS
Streaming
Supported
Batching
Not Supported

Platform Compatibility

Backends
CPUCUDAMetalROCm
Operating Systems
linuxmacoswindows
Architectures
x86_64aarch64

License

GPL-3.0-or-later