Resource Management#
Overview#
Efficient control of CPU and GPU memory is essential for successful model compilation, especially when working with large models such as LLMs or diffusion models. Uncontrolled memory growth can cause compilation failures or process termination. This guide describes the symptoms of excessive memory usage and provides methods to reduce both CPU and GPU memory consumption.
Memory Usage Control#
Peak memory during compilation, as multiples of the model’s weight size, measured on a 3 GiB fp16 model. The GPU column includes the model’s own copy. TensorRT’s builder also needs about 2 GB of host working memory that does not grow with the model.
Setting |
Peak CPU memory |
Peak GPU memory |
|---|---|---|
Default |
~1x |
~2x |
|
~2x |
~1x |
Once compilation finishes, the compiled module holds only the TensorRT engine, about 1x the weight size on the GPU. Delete the original PyTorch model if you no longer need it. Models whose lowering creates new weight tensors (for example by folding transposes or casts of weights) need more.
CPU Memory#
Common symptoms of high CPU memory usage:
Program freeze
Process terminated by the operating system
Ways to lower CPU memory usage:
Keep memory trimming enabled
On Linux, Torch-TensorRT returns the memory the TensorRT builder frees to the operating system after each engine build (glibc
malloc_trim). Without it the process keeps on the order of gigabytes of freed builder memory for the rest of its life. Trimming is on by default; to turn it off, set:export TORCHTRT_ENABLE_BUILDER_MALLOC_TRIM=0
Disable CPU offloading
In compilation settings, set:
offload_module_to_cpu = False
The weights then stay on the GPU instead of moving to the CPU. TensorRT still builds from a host copy of them, but that copy is freed as soon as the engine is built.
GPU Memory#
Common symptoms of high GPU memory usage:
CUDA out-of-memory errors
TensorRT compilation errors
Ways to lower GPU memory usage:
Enable offloading to CPU
In compilation settings, set:
offload_module_to_cpu = True
This moves the model’s weights to CPU memory before the engine builds, a block at a time, and TensorRT reads them there in place. Peak GPU memory drops to about the size of the engine (1x), while CPU memory holds the weights for the rest of compilation (about 2x at peak, while the engine is handed to the runtime).
Unified Memory Systems#
On systems where the CPU and GPU share physical memory, such as Jetson or DGX Spark,
CPU and GPU allocations come out of the same pool, so budget for their sum. Counting
the model itself, compilation peaks at about 3.8x the weight size with default
settings and about 3.2x with offload_module_to_cpu=True. Moving the weights to
the CPU frees no memory on these systems, but TensorRT then builds from them directly
instead of from a second copy.
Runtime Weight Streaming#
Weight streaming allows a compiled TRT engine to use less GPU VRAM at inference time by streaming model weights from CPU memory to the GPU on demand. This is useful for very large models (LLMs, diffusion models) that exceed available VRAM.
Enable during compilation:
trt_model = torch_tensorrt.compile(
model,
ir="dynamo",
arg_inputs=inputs,
enable_weight_streaming=True,
)
Adjust the GPU memory budget at runtime:
Use torch_tensorrt.runtime.weight_streaming as a context manager to set how much GPU
memory the engine is allowed to use for weights. Setting a smaller budget forces more
streaming from CPU:
import torch_tensorrt
# Allocate 2 GiB on GPU for weights; the rest streams from CPU
with torch_tensorrt.runtime.weight_streaming(trt_model) as ctx:
ctx.device_budget = 2 * 1024**3 # bytes
output = trt_model(*inputs)
# Budget is reset to the original value on exit
Query available budget information:
with torch_tensorrt.runtime.weight_streaming(trt_model) as ctx:
# Total streamable bytes across all TRT submodules
print(f"Total streamable: {ctx.total_device_budget} bytes")
# Automatically selected optimal budget
auto_budget = ctx.get_automatic_weight_streaming_budget()
print(f"Auto budget: {auto_budget} bytes")
ctx.device_budget = auto_budget
output = trt_model(*inputs)
Note
Weight streaming requires enable_weight_streaming=True at compile time. If the
model was not compiled with this flag, ctx.total_device_budget will be 0 and
setting device_budget will raise a RuntimeError.
Dynamic Resource Allocation#
By default, TRT submodules allocate GPU memory statically at module initialization.
The ResourceAllocationStrategy context manager temporarily switches all TRT
submodules in a compiled graph module to dynamic allocation — resources are allocated
and freed per forward call rather than held for the module lifetime.
This can reduce peak GPU memory when running multiple compiled models concurrently, at the cost of slightly higher per-call latency:
from torch_tensorrt.dynamo.runtime import ResourceAllocationStrategy
trt_model = torch_tensorrt.compile(model, ir="dynamo", arg_inputs=inputs)
with ResourceAllocationStrategy(trt_model, dynamically_allocate_resources=True):
output = trt_model(*inputs)
# Submodules revert to static allocation on exit
Some memory stays allocated between calls. With CUDA graphs on, the C++ runtime keeps each engine’s memory for as long as its captured graph, because a replay writes to the memory the graph was recorded with. The Python runtime keeps each engine’s memory from its last call, with or without CUDA graphs, until a later call replaces it or a settings change drops it.
Use dynamically_allocate_resources=False to force static allocation inside the
context (the opposite direction — useful for profiling or benchmarking).
Compiling from Memory-Mapped Weights#
Note
map_weights, save_weights_for_mapping, checkpoint_source and low_memory are
experimental in 2.15: their API will change in 2.16.
To compile, a model’s weights normally have to be loaded into host memory first, all of them, before
the first engine is built. torch_tensorrt.dynamo.tools.map_weights instead points the model’s weights at
zero-copy views of a safetensors file:
torch.exportreads only shapes, so a weight’s pages become resident only when TensorRT reads them, while it builds the engine that uses the weight.The pages are dropped again once the last engine that reads the weight is built. A mapped weight stays valid; its pages are read back if it is used again.
torch_tensorrt.dynamo.low_memory does all of it in one block: it builds the model on meta, so
building it allocates no weights, maps them, and drops their pages when the block ends, unless
release_weights_after_build=False. Its compile_settings turn the resource partitioner on, so the
model is built as several engines, each small enough to build within cpu_memory_budget (or the
available memory without one), so only about one engine’s weights are resident at a time. They also give a
part of the model that runs in PyTorch its weights on the GPU after compilation
(offload_module_to_cpu). Pass them to compile:
import torch
import torch_tensorrt
# Map the checkpoint's weights, and build engines that fit an 8 GiB host memory budget.
with torch_tensorrt.dynamo.low_memory(
lambda: MyModel().eval(), "model.safetensors", cpu_memory_budget=8 << 30
) as low_mem:
print(f"{low_mem.report.mapped_bytes / 2**30:.2f} GiB mapped")
example = torch.randn(1, 3, 224, 224) # CPU: the mapped weights are CPU tensors
exported = torch.export.export(low_mem.model, (example,))
trt_model = torch_tensorrt.dynamo.compile(
exported, inputs=[example.cuda()], **low_mem.compile_settings
)
cpu_memory_budget is the host memory the compiling process may use, counting what it already uses,
and each engine gets up to a quarter of what’s left. In compile_from_mapped_weights, a model with
192 MiB of weights built this way as 7 engines had at most 32 MiB of its weights file resident while
compiling, against all 192 MiB when built as one engine.
low_memory takes the compile settings that lower memory during compilation:
offload_module_to_cpu, enable_resource_partitioning, cpu_memory_budget and
release_weights_after_build. compile_settings holds the ones given, with the first two on unless
set. They apply only to the calls they’re passed to: another model compiled inside the block keeps its own
settings.
low_memory uses map_weights, which also works alone on a model you build yourself.
What to know:
Compile on the CPU. Mapped weights are CPU tensors, so keep the model and its example inputs on the CPU for
torch.export. TensorRT takes weights as host pointers and reads each one in place: there’s no GPU copy and no host copy of the weights.Checkpoints. Any safetensors file maps if its tensors have the names, dtypes and shapes the model uses; a mismatch raises. A checkpoint stored in other dtypes than the model runs in, for example float32 for a bfloat16 model, is converted once, as below. The file is checked from its header before anything is mapped: a truncated file, bytes after the last tensor, or tensors whose bytes overlap, leave gaps or don’t hold their dtype and shape raise. Damage inside a tensor’s bytes isn’t detected, since that would mean reading the whole file. Tied weights, and distinct tensors that are one view of a storage (
wandParameter(w.detach())), are stored once and mapped to one view, so they stay shared; tensors that share a storage otherwise raise.Converting without loading.
save_weights_for_mapping(model, path, source=...)writes a model built onmetastraight from a checkpoint. It writes one tensor at a time. For each tensor it callssource(name, tensor), which returns the data whole or in chunks along dimension 0, and it casts each chunk to the model’s dtype.checkpoint_source(checkpoint, key=...)is a ready source that reads a safetensors file, or a sharded checkpoint’smodel.safetensors.index.json, in 64 MiB chunks, so even a float32 checkpoint is never loaded. Change the meta model the way the compiled model will be changed first, such as casting it to bfloat16, so the file holds the dtypes and shapes the export maps. For pi0.5, converting the 14.5 GB float32 checkpoint held 0.9 GiB of process memory, against 9.3 GiB for loading it on the CPU and writing the loaded model. A tensor the source doesn’t store (it raisesKeyError) is written from the model, with a warning if it is persistent, so buffers a checkpoint doesn’t store, such as non-persistent rotary frequencies, must be real tensors, not onmeta. accelerate’sinit_empty_weights()builds a model that way, and its persistent buffers, such as batch norm statistics, still come from the checkpoint.save_weights_for_mapping(model, path)without a source writes a model that is already loaded.from torch_tensorrt.dynamo.tools import checkpoint_source, save_weights_for_mapping with torch.device("meta"): model = MyModel().to(torch.bfloat16) save_weights_for_mapping( model, "weights.safetensors", source=checkpoint_source("checkpoint.safetensors") )
Missing names raise for any name in the model’s
state_dict, inmap_weightsand so inlow_memory; non-persistent buffers, such as rotary frequencies, aren’t required, so recompute those or write them withsave_weights_for_mapping. Withstrict=False,map_weightslists missing names in its result’smissingand leaves them as they were. A missing tensor onmetastays there, with no values, and a warning names it: a model that reads it can’t be exported or compiled, andcompilenames it; one that never reads it is fine. On any error the model is left unchanged.The file must stay in place and unchanged while the model or its weights are in use: rewriting it changes or invalidates the weights.
map_weightsmaps it copy-on-write and never writes to it. It reserves no memory for the mapping, so a file larger than RAM and swap maps too, except withvm.overcommit_memory=2. Weights are read when compilation first uses them, socompilechecks that the file of each mapped weight the program reads is still the one mapped (same inode, size and modification time) and raises otherwise; reading a weight past the end of a file truncated since would kill the process withSIGBUS.release_weights_after_build=Truealso frees weights that aren’t mapped, which the source model then can’t use.Releasing pages uses
madvise. Where the process owns the weights file or may write to it,MADV_PAGEOUT(Linux 5.4 and later) drops a weight’s pages and keeps writes made to it; it leaves pages another process maps too. Elsewhere, such as a root-owned file in a container image read by a normal user,MADV_DONTNEEDdrops them and discards such writes, so the weight reverts to the file’s data. The bytes reported released are the pages that left the process’s memory. On Windows the mapping works, but pages stay resident until the file is unmapped, and a warning says so.
With mapped weights, CPU compilation and the resource partitioner, the host’s peak stays near the largest single engine build instead of the whole model. For pi0.5 (6.7 GiB of bfloat16 weights), host RSS before compilation went from 8.7 to 2.9 GiB. See compile_from_mapped_weights for a runnable example.