# How Surogate Works

Source: https://docs.surogate.ai/engine/about/how-it-works/

Surogate is built to train Large Language Models (LLMs) as fast and efficiently as possible on modern NVIDIA hardware. Many frameworks stack high-level abstractions that add overhead. Surogate instead pairs Python's flexibility with CUDA's raw performance through a DSL-driven compiler pipeline: you describe models in Python, and a native engine runs them.

## Core Architecture

### 1. Native C++/CUDA Execution Engine
At the center of Surogate is an engine written in C++ and CUDA. It gives you:
- Near-zero overhead: Python-to-CUDA dispatch latency is kept to a minimum.
- Custom kernel fusions: specialized kernels combine several operations (such as activation, normalization and projection) into a single pass, which greatly reduces memory-bandwidth bottlenecks.
- Memory management: fine-grained control over how GPU memory is allocated and reused.

### 2. DSL + Graph Compiler
You define model architectures with a decorator-based Python DSL. The DSL produces explicit forward graphs and serializes them to a JSON IR, which the runtime executes. This gives you:

- **Custom architectures** without changing C++/CUDA code
- **Static shape metadata** for planning and verification
- **Save/recompute annotations** for memory-efficient training
- **Stable tensor naming** for deterministic checkpointing

See the [DSL Language Specification](/engine/about/dsl/) for details.

### 3. Graph-Based Autodiff
Surogate derives the backward graph from the forward graph ahead of time. Because the backward pass is generated at compile time instead of at runtime, Surogate can:

- Optimize whole-graph execution (fusion, dead code elimination)
- Plan memory across forward and backward passes
- Avoid Python overhead during training

See [Automatic Differentiation (Autodiff)](/engine/about/automatic-differentiation/) for details.

### 4. Multi-threaded Scheduler
A custom multi-threaded backend runs data loading, gradient synchronization and kernel dispatch in parallel. This keeps the GPU busy instead of waiting for the next batch of data or for CPU-bound work.

### 5. Mixed-Precision & Quantization
Surogate supports a wide range of mixed-precision training:
- BF16 and FP8: native support for these data types on Hopper (H100/H200) and Blackwell (B200) GPUs.
- NVFP4: 4-bit floating-point training on Blackwell GPUs.
- Stochastic rounding: used in low-precision training to keep numerics stable without giving up speed.

The [Precision & Recipes guide](/engine/guides/precision-and-recipes/) explains how to choose between them.

## Scalability

### Multi-GPU Training
Surogate uses an efficient Distributed Data Parallel (DDP) strategy. Multi-threading lets it overlap gradient communication with computation, so throughput scales almost linearly as you add GPUs. See [Multi-GPU Training](/engine/guides/multi-gpu/).

### Multi-Node with Ray
For larger runs, Surogate uses **Ray** to manage multi-node clusters, so the same setup carries over from a single workstation to a large GPU cluster. See [Multi-Node Training](/engine/guides/multi-node/).

## Deployment & Production
Surogate aims for reliable, repeatable runs. Deterministic configurations and explicit training "recipes" mean you can expect predictable results from run to run.

Next: [Installation](/engine/getting-started/installation/) gets Surogate running, and [Training Modes](/engine/getting-started/training-modes/) helps you pick a workflow.
