Skip to content

yagmurtncr/ModelDistributedInference

Repository files navigation

Secure Model-Distributed LLM Inference (MDI-LLM)

Proof of Concept implementation of secure distributed large language model inference with:

  • ✅ Model partitioning across multiple nodes
  • ✅ Recurrent pipeline parallelism
  • ✅ Encrypted inter-node communication (AES-GCM)
  • ✅ Commitment-based verification
  • ✅ Probabilistic proof checking
  • ✅ HMAC message authentication

Installation

pip install -r requirements.txt

Quick Start

Simple Demo (Single Process)

cd examples
python run_demo_simple.py

This runs a simplified version showing the security features without multiprocessing.

Full Distributed Demo (3 Nodes)

Recommended - Thread-based (more reliable):

cd examples
python demo_simple_distributed.py

Experimental - Process-based:

cd examples
python demo.py  # May have serialization issues

This runs the complete system with 3 nodes, demonstrating:

  • Secure node-to-node communication
  • Distributed inference with GPT-2
  • Pipeline parallelism
  • Real-time verification

Note: If you encounter connection errors, use demo_simple_distributed.py which is more stable.

Architecture

flowchart LR
    S["Starter Node (:5000)<br/>embeddings · block 0–1 · LM head"]
    N1["Secondary Node 1 (:5001)<br/>blocks 2–7"]
    N2["Secondary Node 2 (:5002)<br/>blocks 8–11"]
    S -->|"AES-GCM + HMAC"| N1 -->|"AES-GCM + HMAC"| N2
    N2 -.->|"feedback loop"| S
Loading

Security Features

1. Encrypted Communication

  • AES-256-GCM encryption for all activations
  • Per-session key exchange
  • HMAC-SHA256 for message authentication

2. Integrity Verification

  • Cryptographic commitment to model weights
  • Merkle tree commitments for activations
  • Challenge-response proofs

3. Probabilistic Verification

  • 10% of computations verified by default
  • Adjustable verification rate
  • Minimal performance overhead

Performance

On GPT-2 (124M parameters) with 3 nodes:

  • Generation Speed: ~2-5 tokens/sec (CPU)
  • Memory per Node: ~500MB-1GB
  • Verification Overhead: ~5-10%
  • Communication: ~100-500KB per activation

Configuration

Edit the configuration in examples/demo.py:

MODEL_NAME = "gpt2"  # Model to use
NUM_NODES = 3        # Number of nodes
DEVICE = 'cpu'       # 'cpu' or 'cuda'

Project Structure

mdi-llm-secure/
├── src/
│   ├── node.py              # Node implementations
│   ├── security.py          # Security layer
│   ├── partitioner.py       # Model partitioning
│   ├── communication.py     # Secure messaging
│   └── verification.py      # Commitment-based proofs
├── examples/
│   ├── run_demo_simple.py   # Simple single-process demo
│   └── demo.py              # Full distributed demo
├── requirements.txt
└── README.md

Extending

Add More Nodes

Increase NUM_NODES and add corresponding network configuration.

Use Different Models

Change MODEL_NAME to any HuggingFace model:

  • gpt2 (124M)
  • gpt2-medium (355M)
  • gpt2-large (774M)

Adjust Security

In src/verification.py:

# Change verification rate
verification_rate = 0.1  # Verify 10% of operations

Troubleshooting

Port already in use:

# Change ports in demo.py
nodes_config = [
    {'port': 6000, ...},
    {'port': 6001, ...},
    ...
]

Out of memory:

Use smaller model or fewer transformer blocks per node

Connection timeout:

# Increase sleep time between node starts
time.sleep(2)  # Give nodes more time to initialize

Example Output

Simple Demo

Simple Secure MDI-LLM Demo
==================================================

[1] Loading model...
[2] Setting up security...
    Weight commitment: 8f3a4b9c2d1e5f6a...
[3] Generating text with verification...
    Prompt: The future of artificial intelligence
    Token 0: Verification ✓
    Token 5: Verification ✓
    Token 10: Verification ✓
    Token 15: Verification ✓
[4] Generated text:
    The future of artificial intelligence is likely to be...

✓ Demo complete!

Full Distributed Demo

============================================================
Secure Model-Distributed LLM Inference Demo
============================================================

Configuration:
  Model: gpt2
  Nodes: 3
  Device: cpu

[1/5] Loading tokenizer...
[2/5] Partitioning model...
  Created 3 chunks:
    Node 0 (starter): 41,158,656 parameters
    Node 1 (secondary): 42,467,328 parameters
    Node 2 (secondary): 40,894,464 parameters

[3/5] Starting secondary nodes...
[4/5] Creating starter node...
[5/5] Generating text with secure distributed inference...
============================================================

[node_0] Starting generation: 3 samples, 30 tokens each
[node_0] Progress: 30/90 (2.45 tokens/sec)
[node_0] Progress: 60/90 (2.51 tokens/sec)
[node_0] Generation complete: 36.2s, 2.48 tokens/sec

============================================================
Generated Texts:
============================================================

Sample 1:
Prompt: Once upon a time
Generated: there was a young woman who lived in...

Sample 2:
Prompt: The future of AI is
Generated: going to be shaped by advances in...

Sample 3:
Prompt: In a galaxy far away
Generated: , a group of rebels fought against...

Key Components

Security Layer (src/security.py)

  • SecureChannel: AES-GCM encryption and HMAC authentication
  • SecureSocket: Encrypted socket wrapper
  • TLSSocketWrapper: Optional TLS layer

Verification Layer (src/verification.py)

  • CommitmentScheme: Cryptographic commitments and Merkle trees
  • VerificationManager: Probabilistic verification tracking

Communication Layer (src/communication.py)

  • Message: Structured message format with proofs
  • NetworkInterface: Thread-safe network communication
  • SecureMessageQueue: Thread-safe message queuing

Model Partitioner (src/partitioner.py)

  • ModelPartitioner: Splits models across nodes
  • ModelChunk: Wrapper for model segments with caching

Node Implementations (src/node.py)

  • StarterNode: Coordinator node with embeddings and output layers
  • SecondaryNode: Worker node processing intermediate activations

Future Improvements

  • Full zkSNARK integration
  • TLS certificate authentication
  • Dynamic node discovery
  • Adaptive partitioning
  • Hardware acceleration (CUDA kernels)
  • Production-ready key management
  • Support for more model architectures (LLaMA, Mistral, etc.)

License

MIT License - See LICENSE file

Citation

Based on concepts from:

Model-Distributed Inference for Large Language Models at the Edge
Macario et al., 2025

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

This PoC demonstrates that secure model-distributed inference is feasible with:

  • ✅ Practical encryption overhead (~5-10%)
  • ✅ Scalable verification mechanisms
  • ✅ Real distributed execution across nodes
  • ✅ Production-ready security primitives

The system is suitable for trusted environments and can be extended with additional security features for untrusted deployments.

About

Proof-of-concept secure model-distributed LLM inference: layer partitioning across nodes, recurrent pipeline parallelism, AES-GCM encrypted comms, commitment-based verification & HMAC auth.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors