Skip to content

Repository files navigation

Federated Learning System

License: MIT Python 3.10+ Framework: Flower

A scalable, fault-tolerant Federated Learning implementation using Flower framework. Designed to support 10+ concurrent clients with comprehensive error handling, configuration management, and monitoring.

Quick StartDocumentationInstallationContributing

Table of Contents

Features

Scalability

  • Supports 10+ concurrent clients
  • Configurable client/server parameters
  • Efficient data partitioning
  • Load-balanced communication

Reliability

  • Automatic client reconnection with exponential backoff
  • Comprehensive error handling
  • Resource cleanup and management
  • Connection timeout handling

Configuration

  • Environment variable support
  • Per-component configuration classes
  • Default sensible values
  • Easy distributed setup

Monitoring & Logging

  • Structured logging with rotation
  • Per-client and per-round logging
  • Metrics aggregation and tracking
  • Performance statistics

Production Ready

  • Type hints throughout
  • Docstring documentation
  • Exception hierarchy
  • Best practices implementation

Architecture

┌─────────────────────────────────────────────────────────┐
│                    Federated Server                     │
│        (Aggregation, Strategy, Metrics Tracking)        │
└──────────────┬────────────────────────────────────────── │
               │
      ┌────────┴────────┬──────────────┬──────────────┐
      │                 │              │              │
   Client 0          Client 1      Client 2  ...  Client N
   (MNIST)           (MNIST)       (MNIST)         (MNIST)

Installation

Clone Repository

git clone https://github.com/YOUR_USERNAME/federated-learning-system.git
cd federated-learning-system

Prerequisites

  • Python 3.10+
  • CUDA 11.8+ (optional, for GPU support)

Setup

# Clone or download the project
cd /path/to/project

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On Linux/Mac:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# For GPU support (optional)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Quick Start

1. Start the Server

# Terminal 1: Start the federated learning server
python server.py

Expected output:

2024-01-10 14:30:45 [INFO    ] federated_learning - Starting Flower Server...
2024-01-10 14:30:45 [INFO    ] federated_learning -   Address: 0.0.0.0:8080
2024-01-10 14:30:45 [INFO    ] federated_learning -   Rounds: 5

2. Start Clients

# Terminal 2: Start client 0
python client.py --partition-id 0 --num-partitions 3

# Terminal 3: Start client 1
python client.py --partition-id 1 --num-partitions 3

# Terminal 4: Start client 2
python client.py --partition-id 2 --num-partitions 3

3. Monitor Training

The server will automatically start training once minimum clients are connected:

2024-01-10 14:31:20 [INFO    ] federated_learning - Round 1: Training completed...
2024-01-10 14:31:40 [INFO    ] federated_learning - ROUND 1 EVALUATION RESULTS
                                Loss      : 0.3245
                                Accuracy  : 0.9145 (91.45%)
                                Clients   : 3

Configuration

Environment Variables

Create a .env file in the project root:

# Server Configuration
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
NUM_ROUNDS=5
MIN_FIT_CLIENTS=2
MIN_EVALUATE_CLIENTS=2
MIN_AVAILABLE_CLIENTS=2
SERVER_TIMEOUT=600

# Client Configuration
SERVER_HOST=127.0.0.1
SERVER_PORT=8080
BATCH_SIZE=64
NUM_WORKERS=2
DEVICE=auto
RETRY_ATTEMPTS=3
RETRY_DELAY=5

# Data Configuration
DATASET_PATH=./data
SHUFFLE=true
PIN_MEMORY=true

# Training Configuration
EPOCHS_PER_ROUND=1
LEARNING_RATE=1.0
OPTIMIZER=Adadelta
WEIGHT_DECAY=0.0

Command-Line Arguments

Server

python server.py

Environment variables control all server settings.

Client

python client.py \
  --partition-id 0 \
  --num-partitions 10 \
  --server 192.168.1.100:8080 \
  --batch-size 32 \
  --num-workers 4 \
  --log-level DEBUG \
  --log-file logs/client_0.log

Options:

  • --partition-id: Client ID (required)
  • --num-partitions: Total clients (default: 10)
  • --server: Server address (default: 127.0.0.1:8080)
  • --batch-size: Training batch size (default: 64)
  • --num-workers: Data loading workers (default: 2)
  • --log-level: DEBUG|INFO|WARNING|ERROR|CRITICAL (default: INFO)
  • --log-file: Log file path (optional)

Scaling to 10+ Clients

Single Machine (Testing)

# Terminal 1: Server
python server.py

# Terminal 2-11: Clients (10 clients)
for i in {0..9}; do
  gnome-terminal -- python client.py --partition-id $i --num-partitions 10
done

Distributed Setup

Server Machine:

# Set SERVER_HOST to machine IP (e.g., 192.168.1.100)
export SERVER_HOST=0.0.0.0
python server.py

Client Machines:

# For each client machine
export SERVER_HOST=192.168.1.100  # Server IP
python client.py \
  --partition-id $CLIENT_ID \
  --num-partitions 10

Performance Tuning

For 10+ clients:

# Server settings (.env)
MIN_AVAILABLE_CLIENTS=10
MIN_FIT_CLIENTS=8
MIN_EVALUATE_CLIENTS=8
SERVER_TIMEOUT=1200

# Client settings
BATCH_SIZE=32
NUM_WORKERS=4
RETRY_ATTEMPTS=5
RETRY_DELAY=10

Project Structure

project/
├── client.py              # Production client with error handling
├── server.py              # Production server with aggregation
├── model.py               # Neural network model
├── utils.py               # Data loading and training utilities
├── config.py              # Centralized configuration management
├── logger.py              # Logging configuration
├── exceptions.py          # Custom exception hierarchy
├── requirements.txt       # Python dependencies
├── .env.example           # Environment template
├── README.md              # This file
└── logs/                  # Generated log files
    ├── server.log
    ├── client_0.log
    └── client_1.log

Error Handling

The system handles common errors:

Client Errors

Error Cause Resolution
ConnectionError Cannot reach server Check server is running and address is correct
DataLoadingError Dataset download failed Check internet connection and disk space
DeviceError GPU initialization failed Falls back to CPU automatically
TrainingError Training failed Check model and data
TimeoutError Operation took too long Increase SERVER_TIMEOUT

Server Errors

Error Cause Resolution
ServerError Aggregation failed Check client data validity
ConfigurationError Invalid config Verify parameters match clients

All errors are logged with full stack traces in log files.

Logging

Log Levels

  • DEBUG: Detailed internal state
  • INFO: Training progress and key events
  • WARNING: Recoverable issues
  • ERROR: Failures requiring intervention
  • CRITICAL: System-wide failures

Log Files

# Server logs
tail -f logs/server.log

# Client logs
tail -f logs/client_0.log

# Watch all logs
tail -f logs/*.log

Monitoring Metrics

The server tracks and reports:

  • Per-Round Metrics

    • Aggregated loss
    • Aggregated accuracy
    • Number of participating clients
    • Failure count
  • Overall Statistics

    • Best accuracy achieved
    • Best round
    • Training time
================================================================================
ROUND 1: Training completed with 10 clients (0 failures)
================================================================================
Loss      : 0.5342
Accuracy  : 0.8754 (87.54%)
Clients   : 10
================================================================================

Performance

Baseline Metrics (Single GPU, 10 clients)

  • Per-Round Time: ~30-60 seconds
  • Throughput: ~600 samples/second
  • Memory: ~2GB GPU + 500MB per client process

Optimization Tips

  1. Increase batch size for better GPU utilization
  2. Use pin_memory=true for faster data transfer
  3. Reduce num_workers if CPU-bound
  4. Enable mixed precision training (future enhancement)

Troubleshooting

Clients cannot connect to server

# Check server is running
netstat -an | grep 8080

# Check firewall if on different machines
# Linux: sudo ufw allow 8080
# Windows: Check Windows Defender Firewall

Out of memory errors

# Reduce batch size
python client.py --batch-size 32

# Or reduce number of workers
python client.py --num-workers 1

Slow training

# Enable GPU
export CUDA_VISIBLE_DEVICES=0

# Increase batch size
python client.py --batch-size 128

# Reduce epochs per round (in .env)
EPOCHS_PER_ROUND=1

Advanced Usage

Custom Data Partitioning

Edit utils.load_data() to implement custom partitioning strategy.

Custom Model Architecture

Edit model.Net class with your architecture.

Custom Training Algorithm

Extend CustomFedAvg in server.py to implement custom aggregation.

Testing

# Run unit tests
pytest tests/ -v

# With coverage
pytest tests/ --cov=. --cov-report=html

Security Considerations

For production:

  1. Enable SSL/TLS for client-server communication
  2. Add authentication for client identity verification
  3. Encrypt model parameters in transit
  4. Implement differential privacy for sensitive data
  5. Add rate limiting on server

Contributing

We welcome contributions! Please follow these guidelines:

Code Style

  1. Format Code: Use Black formatter

    black *.py
  2. Lint Code: Use Flake8

    flake8 *.py
  3. Type Checking: Use mypy

    mypy *.py
  4. Requirements:

    • Follow PEP 8 style guide
    • Add type hints to all functions
    • Include comprehensive docstrings
    • Add unit tests for new features

Steps to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests and formatters
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

License

MIT License - for details, see the LICENSE file

Support

For issues and questions:

  1. Check logs for detailed error messages
  2. Ensure all dependencies are installed: pip install -r requirements.txt
  3. Verify configuration in .env file
  4. Try with minimum setup (1-2 clients) first
  5. Open an Issue on GitHub

References

About

Production-ready Federated Learning system using Flower and PyTorch with scalable client-server architecture, monitoring, logging, and fault tolerance.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages