GPU利用率98%仍提速6倍:大模型训练优化方法

Even when GPU average utilization reaches 98%, large model fine-tuning can still be accelerated by significant margins. A recent performance optimization effort achieved a 6x speedup for a large model, cutting fixed-dataset fine-tuning time from 7 hours 44 minutes to just 1 hour 17 minutes. Below is a breakdown of the troubleshooting process, operator changes, and data packing techniques used.
Two core approaches unlock this performance: first, identifying real bottlenecks beyond surface-level GPU utilization metrics, and second, targeted optimizations to kernel operations and batch processing.
Model Setup
The work started with a standard training pipeline using common libraries and a widely used fine-tuning dataset to train a large model. The initial setup included:
- FSDP2, a state-of-the-art model sharding technique essential for distributing large models across GPUs (a single large model cannot fit on one GPU with standard distributed data parallelism)
- Activation checkpointing, which trades small amounts of extra computation for reduced memory usage, allowing larger batch sizes per GPU
- Data loading configured to form 2D batches with a maximum of 8192 tokens per batch, padding all samples in a batch to the longest sequence length
# Create the Dataloader
data_loader = make_dataloader("HuggingFaceH4/ultrachat_200k")
model = AutoModelForImageTextToText.from_pretrained(
"Qwen/Qwen3.8-27B", torch_dtype=torch.bfloat16)
# Enabled gradient checkpointing to reduce memory requirements
model.gradient_checkpointing_enable(
gradient_checkpointing_kwargs={"use_reentrant": False})
# Apply FSDP2
mp = MixedPrecisionPolicy(param_dtype=torch.bfloat16, reduce_dtype=torch.float32)
layers = model.model.language_model.layers
for layer in layers:
fully_shard(layer, mp_policy=mp)
fully_shard(model, mp_policy=mp)
model.cuda()
opt = torch.optim.AdamW(
model.parameters(), lr=1e-5, betas=(0.9, 0.95), weight_decay=0.0)
model.train()
for batch in data_loader:
out = model(input_ids=batch["inputs"], labels=batch["labels"])
out.loss.backward()
opt.step()
opt.zero_grad(set_to_none=True)
Baseline Performance
The initial training baseline took 7 hours 44 minutes to complete on the full dataset, with an average GPU utilization of 98%. However, this high utilization was misleading: standard GPU utilization metrics only measure time the GPU is active, not how efficiently it is used. Profiling revealed two key bottlenecks:
- CPU-side operations took longer than GPU execution time, leading to the GPU waiting idle. CPU operations totaled over 1.7 million steps, with many small, frequent operations that could not keep up with the GPU.
- Excessive small kernel operations (over 269,000 GPU operations per step) caused inefficient scheduling and high data transfer overhead between GPU memory and on-chip storage.
Profiling code used to identify these issues:
with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
schedule=torch.profiler.schedule(
wait=1,
warmup=0,
active=1,
repeat=1
)) as prof:
<training code here>
print(prof.key_averages().table(sort_by="self_cuda_time_total", row_limit=10))
Optimizing with Fusion Kernels
To address small kernel overhead, the team used pre-built optimized fusion kernels instead of default implementations. These kernels merge multiple small GPU operations into a single unit, reducing costly data movement and improving scheduling efficiency. For the model's non-standard layers, two specialized kernels were identified: flash-linear-attention and causal-conv1d.
These kernels are available via:
- Direct installation from package managers like pip
- Pre-built versions from model hubs like Hugging Face's Kernel Hub
Installing these two kernels resulted in a 3.7x speedup, cutting training time to 2 hours 5 minutes. The number of operations dropped dramatically, to roughly 1/15th of the original count, with new optimized kernels replacing most small, inefficient operations.
Optimizing Batch Processing
While kernel optimizations improved speed, further gains came from adjusting batch processing. The default approach uses rectangular padded tensors, where all sequences in a batch are padded to the maximum length in that batch, wasting significant space. The team switched to a 1D packed sequence format, where sequences are concatenated and boundary offsets are tracked, ensuring each token only attends to its own sequence.
This change required adjusting position IDs and attention masks in the model to maintain correct training behavior. It led to two key improvements:
- The number of sequences per batch increased, reducing total batches by approximately 36% (depending on data distribution), directly cutting training time.
- Dynamic input shapes had previously caused extra compilation time for the attention kernel; the packed format fixed this, removing additional overhead.
The result was a further speedup, bringing total training time down to 1 hour 17 minutes.
Conclusion
The full 6x speedup came from three core steps:
- Measuring actual performance bottlenecks, not just surface metrics like GPU utilization
- Using mature, optimized fusion kernels to reduce small operation overhead
- Adjusting batch processing to eliminate padding waste
These optimizations can be scaled using a platform that supports these techniques by default, with built-in tools for kernel management, performance monitoring, and data loading.

