Optimize main.py for inference efficiency and GPU throughput (torch.compile, memory tuning, warp alignment) #253
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces targeted performance improvements to main.py to support high-throughput and enterprise-scale inference workloads. The changes are focused, measurable, and maintain backward compatibility with the current CLI interface.
Key Improvements:
Enabled torch.compile() for optimized graph execution paths on supported backends (e.g., TorchInductor)
Fused tensor operations and removed non-essential memory copies
Enabled pin_memory=True and non_blocking=True in data movement paths for efficient host-to-device transfers
Aligned token padding to 32-token warp boundaries to reduce divergence and improve occupancy
Added kernel launch timing and memory tracking support (optional CLI flags can be added for runtime profiling)
Impact:
These changes aim to reduce kernel launch overhead, minimize unnecessary device transfers, and align computation with optimal CUDA occupancy patterns. Benchmarks (to be submitted separately) show:
Improved throughput at batch sizes 1–4
Reduced memory consumption during interactive sessions
Validation:
Verified outputs against baseline for consistency
Tested in single-GPU interactive and torchrun distributed settings
Model behavior preserved across transformer and mamba types