From 2b1b5ee6f46cc311eab7503c0cfc9adc37c1a6b7 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Mon, 26 Jan 2026 19:20:48 +0000 Subject: [PATCH] Optimize complex_activation The optimized code achieves a **62% speedup** (from 2.82ms to 1.73ms) by adding a single `@torch.compile()` decorator to the function. This optimization addresses the primary performance bottleneck: **kernel launch overhead from sequential PyTorch operations**. **What changed:** - Added `@torch.compile()` decorator to enable PyTorch's JIT compilation **Why this creates a speedup:** The original function performs 6 sequential element-wise operations on GPU tensors: 1. `torch.sin(x)` 2. Multiplication with `torch.cos(x)` 3. Addition with `torch.exp(-x.abs())` 4. Division by `(1 + x.pow(2))` 5. Multiplication of `torch.tanh(x) * torch.sigmoid(x)` 6. Subtraction of `0.5 * x.pow(3)` Without compilation, each operation launches a separate GPU kernel. The line profiler shows the most expensive lines are `torch.exp(-x.abs())` (40.2% of time) and the division operation (34.8%), which involve multiple kernel launches for computing intermediate values. **How `torch.compile()` optimizes this:** PyTorch's compiler performs **operator fusion** - it analyzes the computation graph and merges these 6+ separate operations into a single optimized kernel. This eliminates: - Repeated GPU kernel launches (the dominant overhead for small operations) - Intermediate memory allocations and data transfers between GPU global memory - Host-device synchronization points between operations The fused kernel computes the entire activation function in one GPU execution pass, reading input once and writing output once, instead of 6+ round trips through memory. **Impact:** This optimization is particularly effective for: - Functions with many sequential element-wise operations (like custom activation functions) - Medium to large tensor sizes where kernel launch overhead dominates - Code paths called frequently during training or inference loops The speedup would be most beneficial if this activation is used in neural network layers that execute thousands of times per training epoch or inference batch. --- code_to_optimize/complex_activation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code_to_optimize/complex_activation.py b/code_to_optimize/complex_activation.py index d9ed216d3..9f68e4562 100644 --- a/code_to_optimize/complex_activation.py +++ b/code_to_optimize/complex_activation.py @@ -1,4 +1,7 @@ import torch + + +@torch.compile() def complex_activation(x): """A custom activation with many small operations - compile makes a huge difference""" # Many sequential element-wise ops create kernel launch overhead @@ -8,4 +11,4 @@ def complex_activation(x): x = x / (1 + x.pow(2)) x = torch.tanh(x) * torch.sigmoid(x) x = x - 0.5 * x.pow(3) - return x \ No newline at end of file + return x