# Performance

Source: https://docs.surogate.ai/engine/guides/performance/

This page is a work in progress. It collects practical tuning tips and describes the main optimizations in Surogate, so you know which ones run on their own and which you switch on.

---

## High-Level Optimizations

These optimizations make training use less memory and run faster.

## Kernel Fusions

Kernel fusion is always on; there is no config key for it. Surogate fuses every run of successive operations that contains neither a global reduction nor a matrix multiplication, so intermediate results don't make a round trip through device memory. Non-linearities and the joint RMS-norm + residual kernel also return the abs-max of their output, so FP8 quantization needs no extra read. FP8 GEMMs on consumer GPUs accept only the TN layout, so a fused transpose+quantize kernel prepares their inputs, and the cross-entropy forward and backward run in one kernel without materializing the per-token loss tensor. [Multi-GPU training](/engine/guides/multi-gpu/#fused-operations) describes each fusion.

## LoRA Fusions

LoRA fusion is automatic too. Where a LoRA adapter targets a slice of a fused projection (q of qkv, or gate or up of gate_up) in BF16 at rank 8, 16, 32 or 64, Surogate accumulates the adapter's output into that slice in place, with the LoRA scaling folded in. That reads and writes the slice once, where a separate GEMM followed by an add would move it four times, which is most of a LoRA step's extra memory traffic on a bandwidth-bound GPU such as the DGX Spark. Ranks without a dedicated kernel, and cases the kernel rejects, fall back to the regular GEMM path. The adapter itself is set with the [LoRA settings](/engine/reference/config/#lora-settings).

## Fused RoPE

Rotary Position Embedding (RoPE) rotates query and key vectors by position. The standard implementation precomputes frequency tensors (`freq_cis`) and keeps them in GPU memory. The fused implementation computes these frequencies on the fly inside the kernel.

### Enable via config

```yaml
use_fused_rope: true
```

### How it works

Standard RoPE:
1. Precompute a `freq_cis` tensor of shape `(T, 2*head_dim)` with the cos/sin values for all positions.
2. Store it in GPU global memory.
3. During forward/backward, load these values from global memory for each head.

Fused RoPE:
1. Allocate no precomputed tensor.
2. Each CUDA block computes the cos/sin values on the fly with `sincosf()`.
3. The results are cached in shared memory and reused across all heads in the block.

### When to use

Fused RoPE helps when:
- you train with long sequences, where the `freq_cis` allocation gets large
- memory is tight and every MB counts
- the model has many attention layers, so the savings add up

## Multi-threaded multi-GPU

On one node, Surogate drives all GPUs from threads in a single process, so GPUs can copy memory to each other directly without IPC handles. See [Multi-threading vs multiprocessing](/engine/guides/multi-gpu/#multi-threading-vs-multiprocessing).

## Chunked Attention

`attn_bwd_chunks` splits the attention backward pass into N chunks to save workspace memory. For sequences too long to fit at all, `sequence_chunks` runs the whole layer stack in chunks and carries attention KV between them (see [Chunked-Sequence Training](/engine/reference/config/#chunked-sequence-training-long-sequences)).

## Chunked LM

`lmhead_chunks` splits the LM-head computation into N chunks, which makes the logit tensor N times smaller. See [Memory Optimization Settings](/engine/reference/config/#memory-optimization-settings).

---

## See also

- [Multi-GPU](/engine/guides/multi-gpu/): fused kernels and communication overlap across GPUs
- [Precision & recipes](/engine/guides/precision-and-recipes/): FP8 and FP4 training for higher throughput
- [Back to docs index](/engine/)
