Tensorrt fp16 compilation

From wenmin-wu/ds-skills · View interactive version

Tensorrt fp16 compilation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Compiles a PyTorch model to a TensorRT FP16 engine via torch_tensorrt for 2-5x inference speedup, saved as reusable TorchScript.

How to use this skill

Skill file

TensorRT FP16 Compilation

Overview

Kaggle inference kernels have strict time limits (2–9 hours for large datasets). TensorRT compiles a PyTorch model into an optimized GPU engine with FP16 precision, typically achieving 2–5x speedup over vanilla PyTorch. The compiled model is saved as TorchScript and loaded for inference without recompilation. Works with any model that can run a traced forward pass — CNNs, vision transformers, etc.

Quick Start

import torch
import torch_tensorrt

# Load trained model
model = MyModel()
model.load_state_dict(torch.load('best_model.pth'))
model.eval().cuda().half()

# Merge BatchNorm for better optimization (if applicable)
if hasattr(model, 'merge_bn'):
    model.merge_bn()

# Compile to TensorRT FP16
batch_size = 8
trt_model = torch_tensorrt.compile(
    model,
    inputs=[
        torch_tensorrt.Input(
            [batch_size, 1, 1024, 512],
            dtype=torch.half
        )
    ],
    enabled_precisions={torch.half},
    workspace_size=1 << 32,  # 4GB workspace
    require_full_compilation=True,
)

# Save as TorchScript for reuse
torch.jit.save(trt_model, 'model.trt_fp16.ts')

# Load and run (no recompilation needed)
trt_model = torch.jit.load('model.trt_fp16.ts')
with torch.no_grad():
    output = trt_model(batch.cuda().half())

Workflow

  1. Train model in standard PyTorch, save weights
  2. Load model, set to eval mode, cast to half precision
  3. Compile with torch_tensorrt.compile() specifying input shape and FP16
  4. Save compiled model as TorchScript (.ts)
  5. At inference, load TorchScript and run — no recompilation

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.