A profile-guided PyTorch inference optimizer using FX and Triton
Project description
Custom-DL-Optimizer
Custom-DL-Optimizer is a lightweight, research-oriented PyTorch inference optimizer for NVIDIA GPUs. It applies profiling, PyTorch FX graph rewriting, custom Triton operator replacement, channels-last memory layout conversion, and automatic mixed precision to improve inference latency while preserving the normal PyTorch module interface.
The package is designed for ML systems experiments, compiler-oriented coursework, and prototype deployment studies where the model should remain a PyTorch nn.Module rather than being exported to an external runtime.
Highlights
- PyTorch-native API: optimize an existing
torch.nn.Modulein one call. - FX graph surgery: traces model graphs and rewrites supported operators.
- Triton kernel injection: replaces supported activations with custom GPU kernels when safe.
- Hardware-aware execution: supports channels-last memory layout and FP16 autocast.
- Safe fallbacks: CPU tensors and unsupported layouts fall back to PyTorch operators.
- Research notebook: includes a Colab benchmark workflow for reproducible tables and figures.
Installation
pip install custom-dl-optimizer
For GPU acceleration, use a CUDA-enabled PyTorch build and a Linux/Colab environment with NVIDIA GPU support.
python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
Quick Start
import torch
from torchvision.models import resnet50
from custom_dl_optimizer import AutoOptimizer
device = "cuda" if torch.cuda.is_available() else "cpu"
model = resnet50(weights=None).to(device).eval()
dummy_input = torch.randn(32, 3, 224, 224, device=device)
optimizer = AutoOptimizer(model, device=device)
optimized_model = optimizer.optimize(dummy_input)
with torch.inference_mode():
baseline_output = model(dummy_input)
optimized_output = optimized_model(
dummy_input.to(memory_format=torch.channels_last)
if dummy_input.is_cuda
else dummy_input
)
print(torch.allclose(baseline_output.float(), optimized_output.float(), rtol=8e-2, atol=8e-2))
API
AutoOptimizer(model, device="cuda")
Creates an optimizer wrapper around a PyTorch model.
Parameters:
model: atorch.nn.Module.device: target device. Use"cuda"for GPU optimization or"cpu"for fallback behavior.
optimize(dummy_input)
Runs the optimization pipeline and returns an optimized nn.Module.
Pipeline stages:
- Profile the model with
torch.profiler. - Trace the model into a PyTorch FX graph.
- Replace supported modules, currently
nn.ReLU, with optimized alternatives. - Convert CUDA models to channels-last memory format for convolution-heavy workloads.
- Wrap CUDA inference in FP16 automatic mixed precision.
Architecture
| Component | File | Responsibility |
|---|---|---|
| Orchestrator | custom_dl_optimizer/core/engine.py |
User-facing optimization pipeline |
| Profiler | custom_dl_optimizer/core/profiler.py |
Runtime operator profiling and bottleneck reporting |
| Graph Surgeon | custom_dl_optimizer/core/graph_surgeon.py |
FX tracing and module replacement |
| Triton Kernels | custom_dl_optimizer/core/triton_kernels.py |
Custom GPU kernels and PyTorch fallbacks |
Benchmark Snapshot
The repository includes Custom_DL_Optimizer_Research_Colab.ipynb, a Colab benchmark notebook that generates CSV, LaTeX tables, and publication-style graphs.
Example NVIDIA Tesla T4 results from the research notebook:
| Model | Batch | Eager FP32 (ms) | AMP/NHWC (ms) | Custom-DL (ms) | Speedup vs Eager | Speedup vs AMP |
|---|---|---|---|---|---|---|
| ResNet-50 | 128 | 430.317 | 161.772 | 109.243 | 3.94x | 1.48x |
| MobileNet-V2 | 128 | 124.204 | 62.109 | 35.989 | 3.45x | 1.73x |
| VGG-16 | 128 | 751.234 | 315.222 | 255.445 | 2.94x | 1.23x |
| EfficientNet-B0 | 128 | 171.223 | 80.883 | 45.675 | 3.75x | 1.77x |
| DenseNet-121 | 128 | 414.769 | 227.366 | 132.560 | 3.13x | 1.72x |
These results should be treated as hardware- and software-version-dependent measurements. For papers or reports, rerun the notebook on the target GPU and report the generated CSV values.
Reproducible Benchmarking
- Open
Custom_DL_Optimizer_Research_Colab.ipynbin Google Colab. - Select
Runtime > Change runtime type > GPU. - Restart the runtime.
- Run all cells.
- Use the generated files from:
/content/custom_dl_optimizer_research_outputs
The notebook exports:
custom_dl_optimizer_research_results.csvcustom_dl_optimizer_research_results.jsonpaper_table_results.texfigure_1_latency_comparison.pngfigure_2_speedup.pngfigure_3_pass_coverage.pngfigure_4_output_parity.pnggenerated_abstract_results.txt
Scope and Limitations
Custom-DL-Optimizer is a research prototype, not a replacement for TensorRT, Torch-TensorRT, TVM, XLA, or TorchInductor. It is most useful as a PyTorch-native experimentation layer for studying compiler passes and GPU-aware inference transformations.
Current limitations:
- The package-level optimizer currently focuses on CNN-style graphs and
nn.ReLUreplacement. - Performance depends on GPU architecture, PyTorch version, CUDA version, tensor shapes, and batch size.
- User-defined Triton kernels may interact differently with
torch.compileacross PyTorch/Triton versions. - Output parity should be checked with FP16-appropriate tolerances when AMP is enabled.
Roadmap
- Add explicit TorchInductor and CUDA Graph integration to the package API.
- Add Conv-BatchNorm folding as a first-class graph pass.
- Add transformer-oriented LayerNorm/GELU and MLP block optimizations.
- Add post-training INT8 quantization experiments.
- Add CI tests for graph rewriting and output parity.
- Publish benchmark reports for T4, L4, A10, and A100 GPUs.
Development
git clone https://github.com/Devrajsinh-Jhala/Custom-DL-Optimizer.git
cd Custom-DL-Optimizer
python -m pip install -e ".[dev]"
pytest
Build the package locally:
python -m build
python -m twine check dist/*
Upload to PyPI after checking the package description:
python -m twine upload dist/*
Citation
If you use this project in a paper, report the exact package version, GPU, CUDA version, PyTorch version, batch size, warmup count, benchmark iterations, and output-parity tolerance.
Suggested citation text:
Custom-DL-Optimizer: A profile-guided PyTorch micro-compiler for hardware-aware NVIDIA GPU inference optimization.
License
This project is released under the MIT License. See LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file custom_dl_optimizer-1.1.0.tar.gz.
File metadata
- Download URL: custom_dl_optimizer-1.1.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c63d115d6990f452c03556d4a1982328fefe5c443da8a6c04a3d9de2e1bb7e6a
|
|
| MD5 |
ab636e006bcccdb51e1b1cd86bf360b9
|
|
| BLAKE2b-256 |
7635ae46d86dd840d7f6c5baee8f55f9b7254038dc5bf4dc85b5687e8604a0cb
|
File details
Details for the file custom_dl_optimizer-1.1.0-py3-none-any.whl.
File metadata
- Download URL: custom_dl_optimizer-1.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ae81b17230e4846261781e216e222d7347ced88d29ba78249ab1cb2cb4aa504
|
|
| MD5 |
9c2d646f8a29e8bb0d2d65f83547e884
|
|
| BLAKE2b-256 |
ec5d5d6edc861ded8bc8090109bf98e58a5befb4d052992e5c1d13a3dcd5d0d8
|