Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Distribute-Train-Scala

License Scala PyTorch CUDA JavaCPP

English | 简体中文

Table of Contents

Introduction

distribute-train-scala is a comprehensive Scala 3 framework for native GPU-accelerated distributed deep learning on the JVM. It provides production-ready implementations of state-of-the-art distributed training techniques, leveraging Bytedeco's JavaCPP bindings to achieve zero-copy interoperability with native PyTorch, CUDA, and the complete GPU computing ecosystem.

Built for researchers, ML engineers, and system engineers who want to prototype and deploy distributed training workloads directly in Scala/JVM environments without sacrificing performance or native capabilities.

Key Features

🚀 High-Performance Native Integration

  • Direct libtorch Access: Full PyTorch C++ API exposure via JavaCPP with zero-overhead bindings
  • CUDA 13.1 + GPU Support: Multi-GPU training with native NCCL/Gloo backends, automatic device management
  • Native Library Ecosystem: OpenBLAS, OpenCV, FFmpeg integration out-of-the-box
  • Zero-Copy Data Transfer: Direct tensor sharing between JVM and native layers

📡 Production-Grade Distributed Training

  • DDP (Distributed Data Parallel): Multi-GPU data parallelism with synchronized gradient reduction via NCCL
  • FSDP (Fully Sharded Data Parallel): Model parameter sharding across distributed ranks, memory-efficient training for large models
  • ProcessGroup Abstractions: Pluggable NCCL / Gloo communication backends with unified APIs
  • Distributed Sampling: DistributedSampler with proper epoch shuffling and rank-balanced partitioning
  • Fault Tolerance: Checkpoint/recovery mechanisms for long-running distributed jobs

🎯 Mixed Precision & Performance Optimization

  • AutoCast (AMP): Automatic precision casting (FP16, BF16, FP32) to reduce memory footprint and accelerate computation
  • GradScaler: Loss scaling for mixed-precision training stability
  • Tensor Fusion: Efficient gradient aggregation to reduce communication overhead

🔢 Advanced Quantization Framework

  • FP8 Quantization: E4M3FN (inference) and E5M2 (training) formats for extreme memory efficiency
  • Dynamic & Static Quantization: Runtime-adaptive and calibration-based quantization strategies
  • QuantizedLinear Layers: Drop-in replacements for standard linear layers with per-channel quantization
  • Calibration Tools: Histogram-based quantization parameter estimation

📊 Comprehensive Benchmarking Suite

  • DDP/FSDP Efficiency Benchmarks: Throughput, scaling efficiency, communication overhead analysis
  • Backend Performance Tests: NCCL vs. Gloo comparative benchmarks across topologies
  • Quantization Impact Analysis: End-to-end performance measurements with FP8
  • Concurrency & Stability Tests: Multi-rank concurrent training, recovery under failures
  • Customizable Metrics: AUC, loss curves, throughput per sample, communication time breakdowns

🎪 Rich Demo & Example Suite

  • Transformer Models: Full PyTorch Transformer implementations with attention mechanisms
  • FlashAttention Integration: High-performance attention kernel demonstrations
  • Vision Workloads: Image processing pipelines using OpenCV and FFmpeg
  • CuPy-to-Java Interop: Examples of NumPy-like array operations in Java via native libraries
  • JIT/AOT Compilation: TorchScript export and inference patterns
  • CUDA Kernel Access: Low-level CUDA API usage examples

🏗️ Production-Ready Architecture

  • Modular Trainer Design: Extensible trainer base classes for custom training loops
  • Type-Safe Scala 3 APIs: Leverage Scala's type system for compile-time safety
  • Unified Configuration: DDPConfig / FSDPConfig for train setup reproducibility
  • Resource Management: Automatic cleanup via AutoCloseable pattern
  • Comprehensive Logging: Detailed rank-aware logging for distributed debugging

Core Components

Component Purpose Key Classes
Trainers Orchestrate distributed training loops DDPTrainer, FSDPTrainer, base trainer utilities
Distributed Communication & coordination ProcessGroupWrapper, DistributedStore, DistributedConfig
Sampling Data distribution across ranks DistributedSampler with shuffle & batching
AMP Mixed-precision training AutoCast (FP16/BF16), GradScaler for loss scaling
Quantization Low-precision inference & training FP8Quantizer, QuantizedLinear, calibration utilities
Benchmarks Performance profiling & regression testing 13+ standalone benchmark programs
Demos Reference implementations Transformer, FlashAttention, vision, CuPy interop examples

Installation

Prerequisites

  • OS: Linux x86_64 (Bytedeco native libs target this platform)
  • Java: JDK 17+ (OpenJDK or Azul recommended)
  • Build Tool: sbt 1.9+ (Scala Build Tool)
  • GPU (Optional): NVIDIA GPU + matching driver for CUDA 13.1
    • Drivers should support compute capability 3.5+ (Tesla K40 or newer)
    • For RTX/A100 series: latest drivers recommended

Install from Source

# Clone repository
git clone https://github.com/your-org/distribute-train-scala.git
cd distribute-train-scala

# Compile and test
sbt compile
sbt test  # optional

# Package as JAR (CPU-compatible)
sbt package

# Or: create fat JAR (requires sbt-assembly plugin)
sbt assembly

Build Configuration (build.sbt)

ThisBuild / scalaVersion := "3.8.3"

libraryDependencies ++= Seq(
  // PyTorch + JavaCPP core
  "org.bytedeco" % "pytorch" % "2.10.0-1.5.13",
  
  // CUDA runtime (v13.1)
  "org.bytedeco" % "cuda" % "13.1-9.19-1.5.13",
  
  // Optional: GPU-specific libraries
  "org.bytedeco" % "cuda-redist-cublas" % "13.1-9.19-1.5.13",
  "org.bytedeco" % "cuda-redist-cudnn" % "13.1-9.19-1.5.13",
  
  // Linear algebra & vision
  "org.bytedeco" % "openblas" % "0.3.31-1.5.13",
  "org.bytedeco" % "opencv" % "4.13.0-1.5.13",
  
  // Serialization
  "com.google.code.gson" % "gson" % "2.14.0"
)

Quick Start

Example 1: Single-GPU Training with DDP Setup

import torch.distributed._
import org.bytedeco.pytorch._
import org.bytedeco.pytorch.global.torch as pt

// Initialize distributed training (rank=0, world_size=2)
val config = DistributedConfig(
  rank = 0,
  worldSize = 2,
  masterAddr = "localhost",
  masterPort = 29500,
  backend = BackendType.NCCL  // or GLOO
)

val store = DistributedStore.fileStore(
  filePath = "/tmp/torch-store",
  rank = config.rank,
  worldSize = config.worldSize
)

val processGroup = ProcessGroupWrapper.create(store, config)

// Create model and trainer
val model: Module = new SimpleNet(inputSize = 784, hiddenSize = 128, numClasses = 10)
val trainer = new DDPTrainer(
  module = model,
  processGroup = processGroup,
  rank = config.rank,
  worldSize = config.worldSize
)

println(s"[Rank ${config.rank}] DDP trainer initialized")

Example 2: Mixed-Precision Training with AMP

import torch.amp._
import org.bytedeco.pytorch.global.torch as pt

model.train(true)

for (epoch <- 0 until numEpochs) {
  for ((input, target) <- dataLoader) {
    // Enable automatic mixed precision (FP16)
    val autocast = AutoCast.cuda(AutoCast.Precision.FP16)
    
    try {
      val output = model.forward(input)
      val loss = pt.cross_entropy(output, target)
      
      // Loss scaling to prevent underflow
      val scaledLoss = loss.mul(new Scalar(gradScale))
      
      optimizer.zero_grad()
      scaledLoss.backward()
      optimizer.step()
      
    } finally {
      autocast.close()
    }
    
    println(f"Loss: ${loss.item.floatValue}%8.4f")
  }
}

Example 3: FP8 Quantization for Inference

import torch.quantization._
import org.bytedeco.pytorch._

// Quantize model weights to FP8 (E4M3FN format)
val quantizer = new FP8Quantizer(FP8Quantizer.FP8Type.E4M3FN)

model.eval()
for (param <- model.parameters) {
  val quantized = quantizer.quantize(param)
  param.copy_(quantized)
}

// Inference with FP8 (~4x memory reduction)
val output = model.forward(input)

Example 4: Running Distributed Benchmarks

# DDP efficiency benchmark (2 GPUs)
sbt "runMain torch.benchmark.DDPEfficiencyBenchmark --rank 0 --world-size 2 --master-addr localhost"

# FSDP integration test
sbt "runMain torch.benchmark.FSDPIntegrationBenchmark --shard-dim 4"

# Quantization impact analysis
sbt "runMain torch.benchmark.QuantizationBenchmark"

# Main benchmark suite (multiple models)
sbt "runMain torch.benchmark.MainBenchmark"

Project Structure

distribute-train-scala/
├── README.md                                 # English documentation
├── README_zh.md                              # Chinese documentation
├── LICENSE                                   # MIT License
├── build.sbt                                 # sbt project definition
│
├── src/main/scala/torch/
│   ├── SimpleNet.scala                       # Baseline feed-forward network
│   ├── BenchmarkNet.scala                    # Benchmark model
│   ├── DistributedSampler.scala              # Rank-aware data sampling
│   │
│   ├── distributed/                          # Core distributed training
│   │   ├── ProcessGroupWrapper.scala         # NCCL/Gloo abstraction
│   │   ├── DDPTrainer.scala                  # Data-parallel trainer
│   │   ├── FSDPTrainer.scala                 # Fully-sharded parallel trainer
│   │   ├── DistributedConfig.scala           # Training configuration
│   │   ├── DistributedStore.scala            # Rank coordination store
│   │   ├── ModuleForward.scala               # Model forward wrapper
│   │   ├── BackendType.scala                 # NCCL / Gloo selector
│   │   ├── ShardingStrategy.scala            # FSDP sharding modes
│   │   └── StoreType.scala                   # File/TCP store selection
│   │
│   ├── amp/                                  # Automatic Mixed Precision
│   │   ├── AutoCast.scala                    # Precision context manager
│   │   └── GradScaler.scala                  # Loss scaling utility
│   │
│   ├── quantization/                         # Quantization framework
│   │   ├── Quantizer.scala                   # Base quantizer trait
│   │   ├── FP8Quantizer.scala                # FP8 (E4M3FN / E5M2)
│   │   └── QuantizedLinear.scala             # Quantized linear layer
│   │
│   ├── benchmark/                            # Comprehensive benchmarks
│   │   ├── MainBenchmark.scala               # Benchmark suite runner
│   │   ├── DDPEfficiencyBenchmark.scala      # DDP throughput & scaling
│   │   ├── FSDPIntegrationBenchmark.scala    # FSDP parameter sharding
│   │   ├── ProcessGroupNCCLBenchmark.scala   # NCCL backend performance
│   │   ├── ProcessGroupGlooBenchmark.scala   # Gloo backend performance
│   │   ├── QuantizationBenchmark.scala       # FP8 impact analysis
│   │   ├── ConcurrencyBenchmark.scala        # Multi-rank stability
│   │   ├── RecoveryBenchmark.scala           # Fault tolerance testing
│   │   ├── FP8Benchmark.scala                # Detailed FP8 profiling
│   │   ├── ComprehensiveStabilityBenchmark.scala  # Long-running tests
│   │   └── BenchmarkNet.scala                # Models for benchmarking
│   │
│   ├── demo/                                 # Reference implementations
│   │   ├── TransformerCUDAExampleV2.scala    # Full Transformer + CUDA
│   │   ├── FlashAttentionJavaExampleV2.scala # FlashAttention kernel demo
│   │   ├── PureCuApiTestV2.scala             # Low-level CUDA APIs
│   │   ├── CupyToJavaCorrectV4.scala         # NumPy-like operations
│   │   ├── RealFSDPV3.scala                  # FSDP end-to-end example
│   │   └── PagedAttentionVLLMV2.scala        # Paged attention (vLLM style)
│   │
│   └── jit/ & others                         # Reserved for future extensions
│
├── src/main/resources/torch/                 # Java reference implementations
│   ├── DDPTraining.java                      # Java DDP reference
│   ├── FSDPTraining.java                     # Java FSDP reference
│   ├── BenchmarkNet.java                     # Java benchmark models
│   ├── distributed/                          # Java distributed utilities
│   ├── quantization/                         # Java quantization examples
│   └── demo/                                 # Additional Java demos
│
├── src/test/scala/                           # Unit & integration tests
│
└── project/
    └── build.properties                      # sbt version

Supported Trainers

Trainer Training Method Communication Use Case
DDPTrainer Data Parallel NCCL / Gloo Multi-GPU single-node or multi-node, each rank replicates full model
FSDPTrainer Fully Sharded NCCL / Gloo Large models > GPU memory; parameter sharding + gradient accumulation

Both trainers support:

  • Automatic synchronization modes (reduce-scatter, all-gather)
  • Sharding strategies (FULL_SHARD, SHARD_GRAD_OP, NO_SHARD)
  • Mixed-precision training integration
  • Checkpoint/resume workflows

Supported Quantization Methods

Method Format Precision Use Case File Size Reduction
FP8 Dynamic E4M3FN / E5M2 8-bit Real-time inference, memory-constrained ~4x reduced
FP8 Static E4M3FN 8-bit Post-training quantization ~4x reduced
INT8 Signed 8-bit 8-bit Embedded/edge deployment ~4x reduced

Benchmarks & Demos

Running Benchmarks

# 1. Compile
sbt compile

# 2. DDP scaling benchmark (vary GPU count)
sbt "runMain torch.benchmark.DDPEfficiencyBenchmark"

# 3. FSDP sharding strategies
sbt "runMain torch.benchmark.FSDPIntegrationBenchmark"

# 4. Communication backends (NCCL vs Gloo)
sbt "runMain torch.benchmark.ProcessGroupNCCLBenchmark"
sbt "runMain torch.benchmark.ProcessGroupGlooBenchmark"

# 5. Quantization overhead
sbt "runMain torch.benchmark.QuantizationBenchmark"

# 6. Full suite
sbt "runMain torch.benchmark.MainBenchmark"

Sample Benchmark Output

============================================================
            Distribute-Train-Scala Benchmarks
============================================================

--- DDP Scaling Test (2 GPUs) ---
  Epoch 1: Throughput = 1024 samples/sec, Loss = 0.5432
  Epoch 2: Throughput = 1035 samples/sec, Loss = 0.4821
  Scaling Efficiency: 98.2%

--- FSDP Sharding (FULL_SHARD) ---
  Per-GPU Memory: 2.3GB (vs 4.1GB DDP)
  Computation Overhead: 1.2%
  Training Time: 125.4s / epoch

--- Quantization (FP8) ---
  Model Size: 240MB (DDP) -> 60MB (FP8)
  Inference Latency: 12ms (FP32) -> 8ms (FP8)
  Accuracy Loss: 0.2%

============================================================

Advanced Usage

Custom Communication Backend Selection

val config = DistributedConfig(
  rank = 0,
  worldSize = 4,
  backend = BackendType.NCCL  // High-performance GPU collective ops
  // Or: BackendType.GLOO      // CPU-friendly, works with mixed setups
)

FSDP Sharding Strategy

val trainer = new FSDPTrainer(
  module = model,
  processGroup = pg,
  shardingStrategy = ShardingStrategy.FULL_SHARD,    // Shard params + gradients
  reshardAfterForward = true,                        // Re-gather before forward
  useFullPrecision = false                           // Mixed precision enabled
)

Checkpoint & Resume

// Save checkpoint
val checkpoint = Map(
  "model" -> model.state_dict(),
  "optimizer" -> optimizer.state_dict(),
  "epoch" -> epoch
)
// Save to disk...

// Resume training
val loaded = // load from disk
model.load_state_dict(loaded("model"))
optimizer.load_state_dict(loaded("optimizer"))
var epoch = loaded("epoch").asInstanceOf[Int]

Contributing

We welcome contributions! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Add tests or benchmarks for new functionality
  4. Ensure all benchmarks pass
  5. Commit with clear messages (git commit -m 'Add feature')
  6. Push to branch (git push origin feature/amazing-feature)
  7. Open a Pull Request with description

Areas for Contribution

  • Additional trainer implementations (gradient accumulation, mixed precision schedulers)
  • New quantization methods (INT8, NF4, etc.)
  • Additional vision models and datasets
  • Documentation and tutorials
  • Performance optimizations

License

Licensed under MIT License - see LICENSE file for details


Last Updated: 2026-06-10

For issues, benchmarks, or feature requests, please open a GitHub issue or start a discussion.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages