home-lab/machines/grey-area/services/ollama.nix
Geir Okkenhaug Jerstad 9d8952c4ce feat: Complete Ollama CPU optimization for TaskMaster AI
- Optimize Ollama service configuration for maximum CPU performance
  - Increase OLLAMA_NUM_PARALLEL from 2 to 4 workers
  - Increase OLLAMA_CONTEXT_LENGTH from 4096 to 8192 tokens
  - Add OLLAMA_KV_CACHE_TYPE=q8_0 for memory efficiency
  - Set OLLAMA_LLM_LIBRARY=cpu_avx2 for optimal CPU performance
  - Configure OpenMP threading with 8 threads and core binding
  - Add comprehensive systemd resource limits and CPU quotas
  - Remove incompatible NUMA policy setting

- Upgrade TaskMaster AI model ecosystem
  - Main model: qwen3:4b → qwen2.5-coder:7b (specialized coding model)
  - Research model: deepseek-r1:1.5b → deepseek-r1:7b (enhanced reasoning)
  - Fallback model: gemma3:4b-it-qat → llama3.3:8b (reliable general purpose)

- Create comprehensive optimization and management scripts
  - Add ollama-optimize.sh for system optimization and benchmarking
  - Add update-taskmaster-models.sh for TaskMaster configuration management
  - Include model installation, performance testing, and system info functions

- Update TaskMaster AI configuration
  - Configure optimized models with grey-area:11434 endpoint
  - Set performance parameters for 8192 context window
  - Add connection timeout and retry settings

- Fix flake configuration issues
  - Remove nested packages attribute in packages/default.nix
  - Fix package references in modules/users/geir.nix
  - Clean up obsolete package files

- Add comprehensive documentation
  - Document complete optimization process and results
  - Include performance benchmarking results
  - Provide deployment instructions and troubleshooting guide

Successfully deployed via deploy-rs with 3-4x performance improvement estimated.
All optimizations tested and verified on grey-area server (24-core Xeon, 31GB RAM).
2025-06-18 13:08:24 +02:00

104 lines
2.9 KiB
Nix

# Ollama Service Configuration for Grey Area
#
# This service configuration deploys Ollama on the grey-area application server.
# Ollama provides local LLM hosting with an OpenAI-compatible API for development
# assistance, code review, and general AI tasks.
{
config,
lib,
pkgs,
...
}: {
# Enable Ollama service with appropriate configuration for grey-area
services.ollama = {
enable = true;
# Network configuration - localhost only for security by default
host = "0.0.0.0";
port = 11434;
# Environment variables for optimal CPU performance
environmentVariables = {
# Allow CORS from local network (adjust as needed)
OLLAMA_ORIGINS = "http://localhost,http://127.0.0.1,http://grey-area.lan,http://grey-area";
# Optimized context window for TaskMaster AI
OLLAMA_CONTEXT_LENGTH = "8192";
# CPU-optimized parallel processing
OLLAMA_NUM_PARALLEL = "4";
OLLAMA_MAX_LOADED_MODELS = "3";
# Increased queue for better throughput
OLLAMA_MAX_QUEUE = "512";
# CPU performance optimizations
OLLAMA_FLASH_ATTENTION = "1";
OLLAMA_KV_CACHE_TYPE = "q8_0"; # More memory efficient than f16
# Force specific CPU library for optimal performance
OLLAMA_LLM_LIBRARY = "cpu_avx2";
# Enable CPU optimizations
OLLAMA_CPU_HBM = "0"; # Disable unless you have high bandwidth memory
OLLAMA_OPENMP = "1"; # Enable OpenMP for parallel processing
# Disable debug for performance
OLLAMA_DEBUG = "0";
};
openFirewall = true; # Set to true if you want to allow external access
# GPU acceleration (enable if grey-area has a compatible GPU)
#enableGpuAcceleration = false; # Set to true if NVIDIA/AMD GPU available
};
# Apply resource limits and CPU optimizations using systemd overrides
systemd.services.ollama = {
serviceConfig = {
# Memory management for CPU inference
MemoryMax = "20G";
MemoryHigh = "16G";
MemorySwapMax = "4G";
# CPU optimization
CPUQuota = "800%";
CPUWeight = "100";
# I/O optimization for model loading
IOWeight = "100";
IOSchedulingClass = "1";
IOSchedulingPriority = "2";
# Process limits
LimitNOFILE = "65536";
LimitNPROC = "8192";
# Enable CPU affinity if needed (comment out if not beneficial)
# CPUAffinity = "0-7";
};
# Additional environment variables for CPU optimization
environment = {
# OpenMP threading
OMP_NUM_THREADS = "8";
OMP_PROC_BIND = "close";
OMP_PLACES = "cores";
# MKL optimizations (if available)
MKL_NUM_THREADS = "8";
MKL_DYNAMIC = "false";
# BLAS threading
OPENBLAS_NUM_THREADS = "8";
VECLIB_MAXIMUM_THREADS = "8";
};
};
# Add useful packages for AI development
environment.systemPackages = with pkgs; [
# CLI clients for testing
curl
jq
];
}