Skip to content

A weight read by several TensorRT engines is stored once per engine #5036

Open

Description

@shoumikhin

What is wrong

When one weight is used in several parts of a graph, and those parts end up in different TensorRT engines, every engine embeds its own copy of the weight. The output is correct, but the saved program and the GPU memory needed to load it grow by one copy per engine.

Two common ways to get there:

  1. An op between the uses runs outside TensorRT (unsupported, or listed in torch_executed_ops). A denoising loop unrolled over 10 steps that reuses the same layers is a typical case.
  2. max_partition_size splits a model that runs the same layers twice (for example classifier-free guidance). Each part gets its own engine, so each engine stores its own copy of the shared layers.

What this blocks

On Jetson Thor, lingbot_vla_v2 (pytorch/executorch#24353) puts each unrolled denoising step in its own engine, so its program stores the shared layers once per step, on disk and in memory at load.
Any model with shared weights split into N engines needs N times the disk and memory for them.
A fix stores each weight once, which matters most on Jetsons, where memory is small.

Repro

import torch, torch.nn as nn, torch.nn.functional as F, torch_tensorrt

STEPS, D = 10, 2048
SDPA = "torch.ops.aten.scaled_dot_product_attention.default"

class Loop(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin = nn.Linear(D, D, bias=False)
    def forward(self, x):
        for _ in range(STEPS):
            x = F.scaled_dot_product_attention(*(3 * [self.lin(x)]))
        return x

def engine_bytes(gm):
    total = 0
    for name, m in gm.named_children():
        if name.startswith("_run_on_acc"):
            blob = getattr(m, "serialized_engine", None)
            total += len(blob if blob is not None else m.engine.__getstate__()[0][3])
    return total

model = Loop().eval().cuda().bfloat16()
x = torch.randn(1, 4, 64, D, device="cuda", dtype=torch.bfloat16)
ep = torch.export.export(model, (x,))
w = model.lin.weight.numel() * 2
for label, ops in (("whole", set()), ("sdpa kept out", {SDPA})):
    gm = torch_tensorrt.dynamo.compile(ep, inputs=[x], min_block_size=1, torch_executed_ops=ops)
    print(label, f"{engine_bytes(gm) / 1e6:.1f} MB of engines for a {w / 1e6:.1f} MB weight")

Actual

Output (A100):

whole 8.5 MB of engines for a 8.4 MB weight
sdpa kept out 84.1 MB of engines for a 8.4 MB weight

Expected

Each weight stored once. For example: weight-stripped or refittable engines, filled at load time from one shared weight store (the ExecuTorch .ptd file is already there for this). At least, max_partition_size should count a shared weight once, and the docs should say that a weight read by several engines is copied into each.

Versions

torch 2.15.0 (CUDA 13.2), torch_tensorrt 2.15.0, TensorRT 11.4.0.106, A100 80 GB, driver 580.126.09.

What was tried

The copies are inside the serialized engines. Related: #3940 (refit on resource-partitioned graphs), #4855 (max_partition_size).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions