PERF: Remove GPU-CPU Synchronization in FIRE

Merged
CU
1mo ago
+168-95

Summary

There is a lot of GPU-CPU synchronization in the fire optimizer. This PR attempts to remove them all so everything stays on GPU.

Across 5 runs each against main and this branch, Mace small optimized 100 structures with these timings:

RevisionMedian timeTime SDMedian structures/minThroughput SD
main12.104 s0.165 s495.716.79
PR (276d04d)11.883 s0.062 s504.932.66

1.019× throughput: 1.86% faster, or 1.83% less runtime

For faster models, this change will be even faster.

Here are the specific optimizations we implemented:

  • We remove .item() in batched_vdot

  • We remove an if statement which caused a sync from GPU to CPU and replaced this with no-branching GPU operations

  • We no longer call state.n_systems which causes a GPU to CPU sync when we look at the size of the resulting tensor from the .unique() call.

  • Replaced x[mask] with torch.where calls. Why? It's because:

    • x[mask] *= f works by trying to ONLY do the minimal amount of multiplication needed. Unfortunately, this means that pytorch needs to dynamically resolve the shape of the indices of x resolved by mask. dynamically resolving the shape of these indices builds a temp array which sends information to the CPU. This is best explained with an example by GPT:

      For example, if mask is [True, False, True, False], PyTorch calls nonzero(mask) to produce the index tensor [0, 2] on the GPU.
      
      The CPU knows the mask has 4 entries, but doesn’t know that 2 are true. PyTorch’s CUDA implementation counts the matches, copies that count back to the CPU, and waits for the copy to finish. It can then set the index tensor’s shape to (2,) and gather x[0] and x[2].
      
    • torch.where(mask, x*f, x) on the other hand, just pre-multipled all indices of x by f before selecting by the mask. It doesn't need to wait to dynamically figure out the size of the mask. The extra multiplication is cheaper than the wait.

    Here is a script to benchmark the two approaches

    import time
    import torch
    
    mask = torch.tensor([True, False] * 4, device="cuda")
    factor = torch.tensor(0.99, device="cuda")
    
    
    def indexing(x):
        x[mask] *= factor
        return x
    
    
    def where(x):
        return torch.where(mask, x * factor, x)
    
    
    def measure(update):
        x = torch.ones(8, device="cuda")
    
        # Warm up before starting the timer.
        for _ in range(100):
            x = update(x)
        torch.cuda.synchronize()
    
        start = time.perf_counter()
        for _ in range(1000):
            x = update(x)
        torch.cuda.synchronize()  # Wait for all timed GPU work to finish.
    
        return (time.perf_counter() - start) / 1000 * 1e6
    
    
    print(f"indexing: {measure(indexing):.1f} microseconds/update")
    print(f"where:    {measure(where):.1f} microseconds/update")
    
    # output:
    # indexing: 66.7 microseconds/update
    # where:    12.2 microseconds/update