Skip to content

Latest commit

Β 

History

History
361 lines (291 loc) Β· 14.8 KB

SETUP_DOCUMENTATION.md

File metadata and controls

361 lines (291 loc) Β· 14.8 KB

SFT-Play Project Setup Documentation

This document provides a comprehensive overview of the complete project setup and automation system created for the SFT-Play supervised fine-tuning template.

πŸ“‹ Project Overview

SFT-Play is a plug-and-play Supervised Fine-Tuning template designed for small GPUs (8GB+) with comprehensive automation, multiple backend support, and memory-efficient checkpointing.

πŸ—‚οΈ Files Created/Modified

Core Project Files

  1. .gitignore - Comprehensive ignore patterns for ML projects

    • Python-specific ignores (bytecode, virtual environments)
    • ML-specific ignores (data directories, model outputs, adapters)
    • OS and IDE-specific ignores
    • Smart exceptions for essential project files
  2. LICENSE - MIT License for open source distribution

  3. pyproject.toml - Modern Python project configuration

    • Complete dependency specification
    • Optional dependencies (dev tools, wandb, unsloth, trackio)
    • Project metadata and scripts
    • Development tool configurations (black, isort, pytest, mypy)
  4. README.md - Comprehensive project documentation

    • Project description and use cases
    • Complete folder structure explanation
    • Backend-specific configuration guides
    • XFormers compatibility solutions
    • Installation and quickstart guides
    • Configuration examples
    • Automation documentation
    • Comprehensive troubleshooting section

Backend Configuration System

  1. configs/run_bnb.yaml - BitsAndBytes backend configuration

    • Optimized for stability and broad compatibility
    • fp16 precision settings
    • Separate output directory (outputs/run-bnb)
    • Comprehensive safety settings
  2. configs/run_unsloth.yaml - Unsloth backend configuration

    • Optimized for speed (with automatic fallback)
    • bf16 precision settings
    • Separate output directory (outputs/run-unsloth)
    • XFormers compatibility handling
  3. configs/run_bnb.yaml - BitsAndBytes backend configuration (default)

    • Optimized for stability and broad compatibility
    • Uses fp16 precision for maximum compatibility
    • Default configuration used by Makefile
  4. configs/run_unsloth.yaml - Unsloth backend configuration (optional)

    • Optimized for speed with automatic fallback
    • Uses bf16 precision for better performance
    • Requires compatible CUDA setup

Enhanced Training System

  1. scripts/train.py - Enhanced training script with safety features
    • Backend stamping: Creates backend.json for run tracking
    • Resume protection: Prevents cross-backend resume conflicts
    • Automatic fallback: Unsloth β†’ BitsAndBytes when XFormers issues occur
    • Precision validation: Ensures compatible precision settings
    • Configuration mismatch fixes: Handles evaluation_strategy vs eval_strategy
    • XFormers protection: Multiple layers of compatibility safeguards

Automation System

  1. Makefile - Complete automation with backend-specific commands

    • Installation management (auto-detects uv or pip)
    • Backend-specific training commands (train-bnb, train-unsloth)
    • TensorBoard integration for all backends
    • Directory setup and data processing pipeline
    • Enhanced environment variable handling
    • Customizable variables and error handling
  2. workflows/quick_start.sh - Interactive setup script

    • Colored output for better UX
    • Automatic sample data creation
    • Error handling and validation
    • Step-by-step guidance
    • Prerequisites checking
  3. workflows/batch_process.sh - Batch processing automation

    • Multiple dataset processing
    • Different configuration support
    • Experiment management
    • Template for YAML-based configuration

πŸš€ Key Features Implemented

1. Backend Separation System

  • BitsAndBytes Backend: Stable, broad compatibility across hardware configurations
  • Unsloth Backend: Faster training with automatic fallback for compatibility issues
  • Backend Stamping: Each run creates backend.json to track configuration
  • Resume Protection: Prevents accidental resume across different backends
  • Automatic Fallback: Seamless fallback from Unsloth to BitsAndBytes when needed

2. XFormers Compatibility Resolution

  • Issue Identification: Resolved NotImplementedError: No operator found for memory_efficient_attention_backward
  • Multi-layer Protection: Environment variables, model configuration, and script-level safeguards
  • Automatic Detection: System detects XFormers incompatibility and handles gracefully
  • User Guidance: Clear warnings and recommendations for optimal performance

3. Configuration System Enhancements

  • Training Arguments Fix: Resolved evaluation_strategy vs eval_strategy mismatch
  • Precision Validation: Ensures only one precision type (bf16 or fp16) is enabled
  • GPU Auto-detection: Automatically selects optimal precision for Ada GPUs
  • Backend-specific Configs: Optimized settings for each backend

4. Multi-Level Installation Support

  • pip: Traditional Python package management
  • uv: Modern, fast Python package manager
  • Auto-detection: Makefile automatically chooses the best available option

5. Model Caching

  • Automatic Downloads: Models are downloaded from Hugging Face Hub on first use
  • Local Caching: Models are stored locally to avoid re-downloading
  • Offline Support: Works offline after the first download

6. Comprehensive Automation

  • One-command setup: ./workflows/quick_start.sh
  • Backend-specific commands: make train-bnb, make train-unsloth
  • Granular control: Individual Makefile commands
  • Batch processing: Multiple datasets and experiments
  • Pipeline automation: Complete data processing workflows

7. Smart Directory Management

  • Auto-creation: All necessary directories created automatically
  • Git-friendly: .gitkeep files maintain structure
  • Clean separation: Raw, processed, styled, and rendered data
  • Backend isolation: Separate output directories prevent conflicts

8. Error Handling & Validation

  • Prerequisites checking: Validates environment before execution
  • Graceful failures: Clear error messages and recovery suggestions
  • Interactive prompts: User-friendly decision points
  • Compatibility checks: Automatic detection and resolution of common issues

9. Flexible Configuration

  • Variable overrides: Customize commands with environment variables
  • Multiple configs: Support for different configuration files
  • Style customization: Easy prompt modification
  • Backend switching: Easy migration between different backends

πŸ“ Directory Structure Created

sft-play/
β”œβ”€β”€ .gitignore                    # Comprehensive ignore patterns
β”œβ”€β”€ LICENSE                       # MIT License
β”œβ”€β”€ README.md                     # Complete documentation
β”œβ”€β”€ pyproject.toml               # Modern Python project config
β”œβ”€β”€ Makefile                     # Automation commands
β”œβ”€β”€ workflows/                   # Automation scripts
β”‚   β”œβ”€β”€ quick_start.sh          # Interactive setup
β”‚   └── batch_process.sh        # Batch processing
β”œβ”€β”€ scripts/
β”‚   └── utils/
β”‚       └── model_store.py      # Model caching and downloading
β”œβ”€β”€ configs/                     # Configuration files
β”œβ”€β”€ data/                        # Data directories
β”‚   β”œβ”€β”€ raw/                    # Raw input data
β”‚   β”œβ”€β”€ processed/              # Structured chat data
β”‚   β”œβ”€β”€ processed_with_style/   # Style-enhanced data
β”‚   └── rendered/               # Template-rendered data
β”œβ”€β”€ scripts/                     # Core functionality scripts
β”œβ”€β”€ chat_templates/              # Jinja templates
β”œβ”€β”€ env/                         # Environment configs
β”œβ”€β”€ outputs/                     # Training outputs
└── adapters/                    # LoRA adapter storage

πŸ› οΈ Automation Commands

Makefile Commands

# Setup
make install                     # Install dependencies
make setup-dirs                  # Create directories
make help                        # Show all commands

# Data Pipeline
make process                     # Process raw data
make style                       # Apply style prompts
make render                      # Render templates
make full-pipeline              # Complete data processing

# Training & Evaluation
make train                       # Start training
make eval                        # Run evaluation
make infer                       # Run inference

# Model Management
make download-model              # Pre-download a model
make merge                       # Merge LoRA adapters

# Utilities
make clean                       # Clean generated files

# Customization
make style STYLE="Custom prompt"
make train CONFIG=custom_config.yaml

Workflow Scripts

# Interactive setup
./workflows/quick_start.sh

# Batch processing
./workflows/batch_process.sh

🎯 User Experience Improvements

For Beginners

  • One-command setup: Complete project initialization
  • Sample data creation: Automatic demo data generation
  • Interactive guidance: Step-by-step prompts
  • Clear documentation: Comprehensive README with examples

For Advanced Users

  • Granular control: Individual command execution
  • Customization options: Variable overrides and custom configs
  • Batch processing: Multiple dataset handling
  • Professional tooling: Complete development environment

For All Users

  • Error prevention: Validation and prerequisite checking
  • Clean output: Colored, structured terminal output
  • Flexible installation: Support for both pip and uv
  • Memory efficiency: Smart gitignore and directory management

πŸ”§ Technical Implementation Details

Makefile Features

  • PHONY targets: Prevents conflicts with file names
  • Variable support: Customizable CONFIG and STYLE variables
  • Error handling: Proper exit codes and validation
  • Cross-platform: Works on Linux, macOS, and Windows (with make)

Shell Script Features

  • Set -e: Exit on any error for safety
  • Colored output: Enhanced user experience
  • Function organization: Modular, reusable code
  • Input validation: Checks for required files and directories

Python Integration

  • Modern packaging: pyproject.toml with complete metadata
  • Development tools: Integrated linting, formatting, and testing
  • Optional dependencies: Modular installation options
  • Script entry points: Easy command-line access

πŸ“Š Project Benefits

  1. Reduced Setup Time: From hours to minutes
  2. Lower Barrier to Entry: Beginners can start immediately
  3. Professional Standards: Industry-standard tooling and practices
  4. Maintainability: Clean structure and comprehensive documentation
  5. Scalability: Batch processing and experiment management
  6. Flexibility: Multiple installation and usage options

πŸŽ‰ Success Metrics

  • Zero-config startup: Works out of the box
  • Complete automation: End-to-end pipeline automation
  • Professional quality: Production-ready structure
  • Comprehensive documentation: Self-explanatory setup
  • Multi-user support: Beginner to advanced user workflows

πŸ› Issues Resolved

1. Training Arguments Mismatch Error

Problem: ValueError: --load_best_model_at_end requires the save and eval strategy to match

  • Root Cause: Configuration used evaluation_strategy but script looked for eval_strategy
  • Solution: Enhanced config key handling to support both formats
  • Implementation: Added flexible key detection in train.py

2. XFormers Compatibility Issues

Problem: NotImplementedError: No operator found for memory_efficient_attention_backward

  • Root Cause: Unsloth + XFormers incompatibility on certain GPU/CUDA configurations
  • Affected Systems: RTX 4060 + CUDA 12.6, tensor format BMGHK not supported
  • Solution: Multi-layer protection and automatic fallback system
  • Implementation:
    • Environment variables set before imports
    • Model attention implementation forced to SDPA
    • Automatic fallback to BitsAndBytes
    • Clear user warnings and guidance

3. Backend Configuration Conflicts

Problem: Users could accidentally resume training with wrong backend

  • Root Cause: No tracking of which backend was used for previous runs
  • Solution: Backend stamping system
  • Implementation: Creates backend.json with run metadata

4. Precision Setting Conflicts

Problem: Both bf16 and fp16 could be enabled simultaneously

  • Root Cause: No validation of precision settings
  • Solution: Precision validation and auto-detection
  • Implementation: GPU-specific precision selection and conflict resolution

5. Environment Variable Timing Issues

Problem: XFormers environment variables not set early enough

  • Root Cause: Variables set after imports had already occurred
  • Solution: Move environment variable setting to script start
  • Implementation: Set variables before any other imports

πŸ›‘οΈ Safety Features Implemented

Backend Safety

  • Backend Stamping: Tracks which backend was used for each run
  • Resume Protection: Prevents cross-backend resume conflicts
  • Automatic Fallback: Graceful degradation when compatibility issues occur
  • Clear Warnings: User-friendly messages explaining what's happening

Configuration Safety

  • Precision Validation: Ensures only one precision type is enabled
  • Strategy Matching: Validates evaluation and save strategies match
  • GPU Detection: Automatically selects optimal settings for hardware
  • Error Prevention: Catches common configuration mistakes

Environment Safety

  • Early Protection: Environment variables set before any imports
  • Multiple Layers: Script, model, and environment-level protections
  • Compatibility Checks: Automatic detection of known issues
  • Graceful Handling: Clear error messages and recovery paths

πŸ”„ Next Steps for Users

  1. Clone the repository
  2. Run ./workflows/quick_start.sh for immediate setup
  3. Add your data to data/raw/
  4. Choose your backend:
    • make train-bnb-tb for maximum stability (recommended)
    • make train-unsloth-tb for speed (with automatic fallback)
  5. Monitor progress with built-in TensorBoard
  6. Evaluate and deploy your fine-tuned model

🎯 Recommended Usage Patterns

For Maximum Stability

make train-bnb-tb        # BitsAndBytes with TensorBoard

For Speed (with Safety)

make train-unsloth-tb    # Unsloth with automatic fallback

For Custom Configurations

make train CONFIG=configs/my_config.yaml

This setup transforms SFT-Play from a collection of scripts into a professional, production-ready machine learning toolkit with comprehensive automation, robust error handling, and extensive documentation.