Skip to content

Commit 454f20d

Browse files
committed
first draft, need to refine
1 parent 70a069b commit 454f20d

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

docs/docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"group": "🧠 Core Concepts",
6767
"pages": [
6868
"codeflash-concepts/how-codeflash-works",
69-
"codeflash-concepts/benchmarking"
69+
"codeflash-concepts/benchmarking",
70+
"support-for-jit/index"
7071
]
7172
},
7273
{

docs/support-for-jit/index.mdx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: "Support for Just-in-Time Compilation"
3+
description: "Learn how Codeflash optimizes code using JIT compilation with Numba, PyTorch, TensorFlow, and JAX"
4+
icon: "bolt"
5+
sidebarTitle: "JIT Compilation"
6+
keywords: ["JIT", "just-in-time", "numba", "pytorch", "tensorflow", "jax", "GPU", "CUDA", "compilation", "performance"]
7+
---
8+
9+
# Support for Just-in-Time Compilation
10+
11+
Codeflash supports optimizing code using Just-in-Time (JIT) compilation. This allows Codeflash to suggest optimizations that leverage JIT compilers from popular frameworks including **Numba**, **PyTorch**, **TensorFlow**, and **JAX**.
12+
13+
## Supported JIT Frameworks
14+
15+
Each framework uses different compilation strategies to accelerate Python code:
16+
17+
### Numba
18+
19+
Numba compiles Python functions to optimized machine code using the LLVM compiler infrastructure. Codeflash can suggest Numba optimizations that use:
20+
21+
- **`@jit` / `@njit`** - General-purpose JIT compilation with `nopython` mode for removing Python interpreter overhead
22+
- **`parallel=True`** - Enables automatic SIMD parallelization
23+
- **`fastmath=True`** - Uses aggressive floating-point optimizations via LLVM's fastmath flag
24+
- **`@vectorize` / `@guvectorize`** - Creates NumPy universal functions (ufuncs)
25+
- **`@cuda.jit`** - Compiles functions to run on NVIDIA GPUs
26+
27+
### PyTorch
28+
29+
PyTorch provides multiple compilation approaches:
30+
31+
- **`torch.compile()`** - The recommended compilation API that uses TorchDynamo to trace operations and create optimized CUDA graphs
32+
- **`torch.jit.script`** - Compiles functions using TorchScript
33+
- **`torch.jit.trace`** - Traces tensor operations to create optimized execution graphs
34+
35+
### TensorFlow
36+
37+
TensorFlow uses the XLA (Accelerated Linear Algebra) backend for JIT compilation:
38+
39+
- **`@tf.function`** - Compiles Python functions into optimized TensorFlow graphs using XLA
40+
41+
### JAX
42+
43+
JAX captures side-effect-free operations and optimizes them:
44+
45+
- **`@jax.jit`** - JIT compiles functions using XLA, with automatic operation fusion for improved performance
46+
47+
## How Codeflash Optimizes with JIT
48+
49+
When Codeflash identifies a function that could benefit from JIT compilation, it:
50+
51+
1. **Rewrites the code** in a JIT-compatible format, which may involve breaking down complex functions into separate JIT-compiled components
52+
2. **Generates appropriate tests** that are compatible with JIT-compiled code, carefully handling data types since JIT compilers have stricter type requirements
53+
3. **Adds GPU synchronization calls** for accurate profiling when code runs on GPU, since GPU operations are inherently non-blocking
54+
55+
## Accurate Benchmarking with GPU Code
56+
57+
Since GPU operations execute asynchronously, Codeflash automatically inserts synchronization barriers before measuring performance. This ensures timing measurements reflect actual computation time rather than just the time to queue operations:
58+
59+
- **PyTorch**: Uses `torch.cuda.synchronize()` or `torch.mps.synchronize()` depending on the device
60+
- **JAX**: Uses `jax.block_until_ready()` to wait for computation to complete
61+
- **TensorFlow**: Uses `tf.test.experimental.sync_devices()` for device synchronization
62+
63+
## When JIT Compilation Helps
64+
65+
JIT compilation is most effective for:
66+
67+
- Numerical computations with loops that can't be easily vectorized
68+
- Custom algorithms not covered by existing optimized libraries
69+
- Functions that are called repeatedly with consistent input types
70+
- Code that benefits from hardware-specific optimizations (SIMD, GPU acceleration)
71+
72+
## When JIT Compilation May Not Help
73+
74+
JIT compilation may not provide speedups when:
75+
76+
- The code already uses highly optimized libraries (e.g., NumPy with MKL, cuBLAS, cuDNN)
77+
- Functions have variable input types or shapes that prevent effective compilation
78+
- The compilation overhead exceeds the runtime savings for short-running functions
79+
- The code relies heavily on Python objects or dynamic features that JIT compilers can't optimize
80+
81+
## Configuration
82+
83+
JIT compilation support is **enabled automatically** in Codeflash. You don't need to modify any configuration to enable JIT-based optimizations. Codeflash will automatically detect when JIT compilation could improve performance and suggest appropriate optimizations.
84+
85+
When running tests with coverage measurement, Codeflash temporarily disables JIT compilation to ensure accurate coverage data, then re-enables it for performance benchmarking.

0 commit comments

Comments
 (0)