Alpamayo 2 Edge export to ExecuTorch with TensorRT: how to reproduce from source
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 24/100
- Issue-Typ
- Feature
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Bereich
- embedded-iot, machine-learning, performance
Rechercherichtung
Start with the reproduction steps and the relevant gap in the issue, then inspect the named ExecuTorch or Torch-TensorRT runtime and the Alpamayo example entry points: examples/executorch_tensorrt/export.py and run.py. Reproduce the A100 run using the pinned branches and compare stage timings and outputs with --check. Done means the specific measured gap improves while preserving the reported token and path results; the issue describes several distinct, cross-project investigations.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
NVIDIA's Alpamayo 2 Edge (a vision-language-action model for driving, NVlabs/alpamayo2-edge), exported with ExecuTorch (PyTorch's solution for deploying models on devices) and Torch-TensorRT, runs every part of the model in TensorRT engines on the GPU. It gives the same reasoning tokens as the PyTorch model. On Jetson AGX Thor it is faster than the model's own PyTorch path, both default and tuned. On DGX Spark it matches the tuned path, and on A100 the tuned path is still faster. Against the default PyTorch path it is 1.7x to 3x faster on every GPU tested. Below are the numbers, measured the same way for both, how to reproduce them, and the places where TensorRT and Torch-TensorRT can still improve.
Results
One full prediction (vision encoder, prefill, 6 reasoning tokens, 10 action expert steps), batch 1, greedy:
| GPU | ExecuTorch + TensorRT | PyTorch, default | PyTorch, tuned (--accel tuned) |
Time saved vs default / tuned | GPU memory, GiB (ExecuTorch + TensorRT / tuned PyTorch) |
|---|---|---|---|---|---|
| Jetson AGX Thor | 495 ms | 1067 ms | 590 ms | 54% / 16% | 10.5 / 6.6 |
| DGX Spark | 611 ms | 1038 ms | 617 ms | 41% / same | not measured / 6.7 |
| A100 | 273 ms | 821 ms | 217 ms | 67% / 26% slower | 11.0 / not measured |
Time per stage for ExecuTorch + TensorRT, in ms (A100 / Spark / Thor): vision 96 / 213 / 147, prefill 63 / 159 / 115, decode (6 tokens) 51 / 110 / 104, expert (10 steps) 64 / 128 / 129.
How it was measured: both sides ran on the same GPU, on the synthetic sample from the package's tests (22 camera images, a 3256-token prompt), and are timed over the same span, from the vision encoder to the last expert step. Prompt preparation before it and the conversion to poses after it are not included. Each number is the median of 9 or more warm runs with nothing else on the GPU. GPU memory is the per-process number from nvidia-smi. PyTorch runs the package's predict with top_k=1 on PyTorch 2.15.0; the tuned path uses flash-attn 2.8.3. The TensorRT delegate replays CUDA graphs for decode and expert_step.
Accuracy, against the PyTorch model on the same inputs:
- The reasoning tokens are identical on all 3 GPUs. The predicted path (64 future poses) differs by at most 0.7 cm on Thor, 1.1 cm on Spark and 1.1 to 1.5 cm on A100 (depending on the build).
- Over 64 synthetic inputs on A100, the path differs by 3.0 cm median and 9.2 cm max. For scale: PyTorch against itself with a different attention kernel differs by 3.5 cm median and 20 cm max.
Notes:
- The model is
nvidia/Alpamayo2-Edgeatb92b6aa3, with thealpamayo2_edgepackage at02f55a7. Precision is bfloat16, with the vision tower's layer norms and GELU in float32 (see gap 3 below). - The export is two programs:
vision.pte(one method per camera patch grid) andtext.ptewithprefill,decodeandexpert_step. The three text methods share one static key/value (KV) cache in GPU memory, and every engine writes its part of the cache in place, with no copies. - The tuned PyTorch path needs flash-attn built for the GPU; on Thor that build took 14 minutes. The exported programs do not need it.
- Thor shares one memory between CPU and GPU. Counted for the whole board, the gap there is 11.8 against 9.4 GiB.
- Jetson Orin Nano (8 GB) does not fit yet: the programs alone are larger than its memory (see gap 2).
- All rows ran on pre-release revisions of the pull requests below, with ExecuTorch built from main. The branches under "How to reproduce" gave the same times on A100 (267 to 273 ms) and the same tokens.
Compared with NVIDIA's TensorRT Edge-LLM recipe
NVIDIA's Alpamayo2-Edge recipe for TensorRT Edge-LLM, built unmodified and run on the same Jetson AGX Thor with the same weights, prompt, pixels and noise, and TensorRT 11.4 on both sides. Its draft model is not public, so both sides decode greedily. Both timers run from the first vision call to the last expert step. Median of 21 warm runs.
Settings: 1 trajectory, 10 expert steps, all 22 frames encoded every call.
| Stage, ms | ExecuTorch + Torch-TensorRT, bfloat16 | Edge-LLM recipe, FP16 |
|---|---|---|
| Vision | 148.5 | 116.6 |
| Prefill | 113.4 | 119.2 |
| Reasoning tokens | 102.8 | 98.5 |
| Action expert | 126.4 | 48.9 |
| Total | 491.7 | 383.5 |
| Peak GPU memory per process | 10.8 GB | 12.6 GB |
| Largest path error vs PyTorch | 0.8 cm | 8.9 cm |
On A100 the same comparison gives 266 ms against 200 ms. Both sides produce the same tokens as PyTorch.
- Prefill and reasoning tokens are even. The gap is in vision (vision attention, gap 6 below) and in the action expert. The recipe runs all expert steps in one engine call and the example calls one engine per step, but that is not the main cost: exporting all 10 steps as one engine saves only 10.6 ms on Thor (126.4 to 115.8 ms), and nothing with 6 trajectories and 5 steps. The rest is the work inside each step, mostly gaps 1 and 4.
- With its published settings except the draft model (FP8, 6 trajectories, 5 steps, cached frames, reduced vocabulary), the recipe takes 206 ms on Thor. The same settings without FP8 take 322 ms. FP8 calibration needs real camera frames, so its accuracy on our synthetic sample is not meaningful. With the same settings in bfloat16 the ExecuTorch side takes about 355 ms on Thor; FP8 through TensorRT Model Optimizer and Torch-TensorRT does not lower it yet (gap 6).
What it needs on top of the releases
Torch-TensorRT 2.15.0, plus one stack of changes, each building on the one before:
- pytorch/TensorRT#5095 write copy-back buffers when the compiled module runs in PyTorch
- pytorch/TensorRT#5096 cache writes reach caller tensors
- pytorch/TensorRT#5097 KV cache inputs whose memory is shared or copied
- pytorch/TensorRT#5098 use the KV cache layer only where it works
- pytorch/TensorRT#5099 start dynamic-dim engine profiles where the program allows (also builds on pytorch/TensorRT#5085)
- pytorch/TensorRT#5100 write a dynamic-length prompt into the KV cache. This lets
prefillwrite a prompt of any length that fits the cache, andexpert_stepwrite 64 tokens, each in one engine.
- pytorch/TensorRT#5100 write a dynamic-length prompt into the KV cache. This lets
- pytorch/TensorRT#5099 start dynamic-dim engine profiles where the program allows (also builds on pytorch/TensorRT#5085)
- pytorch/TensorRT#5098 use the KV cache layer only where it works
- pytorch/TensorRT#5097 KV cache inputs whose memory is shared or copied
- pytorch/TensorRT#5096 cache writes reach caller tensors
And in its ExecuTorch runtime, only for CUDA graphs:
- pytorch/TensorRT#5044 under CUDA graphs, copy only the inputs that move
- pytorch/TensorRT#5075 replay CUDA graphs for engines that update a cache in place. Without these two, run without CUDA graphs: same results, 1 to 6% slower.
ExecuTorch 1.6.0, plus one pull request, so that the three text methods share one cache in GPU memory:
- pytorch/executorch#25425 share mutable buffers between methods in device memory: the program records which planned buffers its methods share, each runtime gives every method one allocation of each shared buffer, and those buffers can be planned in device memory
With only the released packages, the export stops at prefill: expected one TensorRT engine: the cache writes fall back to PyTorch and prefill splits into 57 pieces.
How to reproduce
Everything is on public branches, pinned by commit. They carry earlier versions of the pull requests above, split differently, plus a few changes the example does not need. They were built and run on A100:
| Project | Branch | Commit |
|---|---|---|
| Torch-TensorRT | alpamayo-repro-2.15 (on v2.15.0) |
9189b9fe5e |
| ExecuTorch | alpamayo-repro-1.6 (on v1.6.0) |
a42737b505 |
| Alpamayo 2 Edge | NVlabs/alpamayo2-edge#13 | 6047e7585d |
The first two are on github.com/shoumikhin/TensorRT and github.com/shoumikhin/executorch.
1. Build ExecuTorch, with the CUDA 13.2 toolkit in CUDA_HOME and its nvcc on PATH (the checkout folder must be named executorch):
git clone --recurse-submodules --shallow-submodules https://github.com/shoumikhin/executorch -b alpamayo-repro-1.6 executorch
git -C executorch checkout a42737b505
pip install torch==2.15.0 numpy "cmake>=3.29,<4.0" pyyaml setuptools wheel ninja pybind11 zstd tomli packaging ruamel.yaml patchelf certifi flatbuffers
CMAKE_ARGS="-DEXECUTORCH_BUILD_CUDA=ON" TORCH_CUDA_ARCH_LIST="8.0;9.0" BUILD_VERSION=1.6.0+alpamayo \
pip wheel ./executorch --no-build-isolation --no-deps -w dist
2. Build Torch-TensorRT and its ExecuTorch runtime. On x86_64, inside pytorch/manylinux2_28-builder:cuda13.2 with bazel 8.4.2, with the ExecuTorch wheel from step 1 in ../dist:
git clone https://github.com/shoumikhin/TensorRT -b alpamayo-repro-2.15 && cd TensorRT && git checkout 9189b9fe5e
export PATH=/opt/python/cp312-cp312/bin:/usr/local/cuda-13.2/bin:$PATH CUDA_HOME=/usr/local/cuda-13.2
pip install --index-url https://download.pytorch.org/whl/test/cu132 torch==2.15.0+cu132
pip install "setuptools>=77" wheel ninja pyyaml packaging numpy "pybind11==2.6.2" cffi
export TORCH_INSTALL_PATH=$(python -c 'import torch,os;print(os.path.dirname(torch.__file__))')
export CU_VERSION=cu132 CHANNEL=test BUILD_VERSION=2.15.0+alpamayo \
TENSORRT_CUDA_VERSION_UPPER_BOUND=13.4 TENSORRT_RTX_CUDA_VERSION_UPPER_BOUND=13.4
envsubst < toolchains/ci_workspaces/MODULE.bazel.tmpl > MODULE.bazel
export CI_BUILD=1 RELEASE=1 TORCH_TENSORRT_EXECUTORCH_RUNTIME_VERSION=0.2.1 TORCH_TENSORRT_EXECUTORCH_RUNTIME_CHANNEL=pypi
python setup.py bdist_wheel --release
pip install dist/torch_tensorrt-*.whl tensorrt-cu13==11.4.0.106 patchelf
pip install --no-deps --force-reinstall ../dist/executorch-1.6.0+alpamayo-*.whl
TORCH_TENSORRT_ALLOW_UNPINNED_EXECUTORCH=1 pip wheel --no-build-isolation --no-deps -w dist py/torch-tensorrt-executorch-runtime
For Jetson AGX Thor and DGX Spark, build both on the board, with TORCH_CUDA_ARCH_LIST="11.0;12.1" for ExecuTorch.
3. Install, export and run, with Python 3.12 and nvcc (CUDA 12.8 or newer) on PATH. The first export takes about 6 to 7 minutes on an A100 and writes about 10 GB.
# from the folder that holds dist/ (step 1) and TensorRT/ (step 2)
pip install torch==2.15.0 torchvision "TensorRT/dist/torch_tensorrt-2.15.0+alpamayo-<platform>.whl[executorch]" \
TensorRT/dist/torch_tensorrt_executorch_runtime-0.2.1-*.whl dist/executorch-1.6.0+alpamayo-*.whl
git clone https://github.com/NVlabs/alpamayo2-edge && cd alpamayo2-edge
git fetch origin pull/13/head && git checkout FETCH_HEAD
pip install --no-deps -e . && pip install -r examples/executorch_tensorrt/requirements.txt
python examples/executorch_tensorrt/export.py --model-id <model dir>
python examples/executorch_tensorrt/run.py --model-id <model dir> --runs 21 --cuda-graphs --check
run.py prints the time of each stage and, with --check, compares the tokens and the path with the PyTorch model.
Where to improve next
These are open, measured on A100 unless noted, and ranked by what they would gain. Gaps 1 to 4 and 6 need work in TensorRT or Torch-TensorRT; gap 5 is ExecuTorch and example work.
1. Attention for a few queries over a long cache. This is the whole gap against tuned PyTorch on A100.
- In the decode engine, TensorRT's fused attention takes 194 µs per layer for 1 query over the 4096-slot cache; FlashAttention's split-KV kernel takes 27 µs. For the expert step (64 queries) it is 150 against 34 µs. Over 28 layers that is 28 ms of decode and 32 ms of expert time per prediction.
- With 1 query, TensorRT picks a kernel that runs one thread block per query head. It has a kernel that splits the cache, and forcing it with the valid length given as
key_value_lengthsmakes this attention 5x to 8x faster, but TensorRT does not pick it for bfloat16, and it gives wrong results with a bool mask (NVIDIA/TensorRT#4947). With 64 queries there is no split kernel. - Splitting the cache by hand into chunks with a log-sum-exp combine makes decode attention 2.3x and expert attention 1.3x faster, with the same tokens.
- Wanted: TensorRT picks its split kernel for 1 query and gets the mask right, and adds a split path for a few dozen queries; Torch-TensorRT passes the valid length as
key_value_lengths. Our estimate is about 210 ms total on A100, below tuned PyTorch.
2. Weights stored once per engine. This costs memory.
prefillanddecodeeach hold the language model weights (about 4 GB), sotext.pteis 8.3 GB;vision.pteis 2.0 GB; the checkpoint is 5.3 GB. This is most of why ExecuTorch uses more memory than tuned PyTorch.- Wanted: build or refit several engines from one set of named weights in the
.ptdfile, or putprefillanddecodeinto one engine with two optimization profiles. Either could bring memory near the checkpoint size, and one of them is needed for Jetson Orin Nano, where building the decode engine also ran out of memory.
3. Precision of normalization inside bfloat16 engines.
- In plain bfloat16 the path drifts several times more than PyTorch's own noise: Torch-TensorRT builds GELU and layer norm as bfloat16 layers and TensorRT computes them in bfloat16, while PyTorch computes them in float32 internally. In float16, TensorRT's matrix multiplications move the path too far.
- Forcing the vision tower's layer norms and GELU to float32 matches float32 accuracy at half the vision time; full float32 costs about 95 ms more on A100. Torch-TensorRT's autocast setting did not reach the same result.
- Wanted: Torch-TensorRT's converters compute GELU and normalization in float32 inside bfloat16 engines, as PyTorch does, without forcing whole layers to float32 (pytorch/TensorRT#5083).
4. Attention with several queries copies the cache on every call, and an expanded mask slows it further.
- With more than one query, TensorRT copies each layer's keys and values into another layout on every call (about 50 µs per layer at 4096 slots). This happens with or without grouped-query attention. Separately, the expert step's mask, made with
.expand(1, 1, 64, -1)in the example's code, makes the attention kernel about 20% slower in TensorRT 11.4 than a broadcast mask of shape[1, 1, 1, S], which gives the same result; TensorRT 11.5 no longer shows this. Switching to the broadcast mask saves 6.6 ms per prediction end to end. The example will switch to it. - Wanted: multi-query attention reads the cache where it is, as the single-query kernel already does.
5. Host copies between programs.
- The image embeddings go from
visionto host memory and back intoprefill(about 13 MB), through pageable memory. Pinned memory and stacking during prompt preparation saved about 6 ms. - Wanted: keep the vision outputs on the GPU for
prefill, using device-memory inputs and outputs in the ExecuTorch runtime.
6. Lower precision and vision attention. FP8 weights and activations (TensorRT Model Optimizer, the same layers as the Edge-LLM recipe) on Thor make prefill 30% faster, leave decoding and the expert about the same, and make the vision tower twice as slow, so the total does not change. NVFP4 is untried; INT8 weight-only gave no gain on A100. An FP8 KV cache, as the Edge-LLM recipe uses, is also untried: the cache is a bfloat16 buffer that each engine updates in place, and that update has not been tried with an FP8 buffer. Vision attention (head size 72) takes 14.5 ms in TensorRT against about 8.7 ms in FlashAttention at the same shapes.
Speculative decoding and other caching in the graph
The pieces these need are in place: one KV cache shared by several methods in GPU memory, and engines that write many tokens into it in place.
- Speculative decoding: a draft model can be one more method on the same cache. The verify step is a multi-token write like
expert_step; rejected tokens need no rollback, since the attention mask hides them and the next write overwrites them. - Other caches, such as vision embeddings kept across frames or a reused prompt prefix, can be more shared buffers between methods, in GPU memory.
Which scheme (draft model, Eagle, multi-token prediction) and which caches matter most for this model? We can add them to the example.
Questions, and results on other GPUs, are welcome here.
cc @narendasan
- Vorherrschende Sprache
- Python
- Sterne
- 5.2k
- Forks
- 1.3k
- Ø Merge
- 1 T. 7 Std.
- Gemergte PRs (30 T.)
- 644
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus pytorch/executorch
-
to_edge_transform_and_lower ignores some ops_to_not_decompose requests: multi-output ops, and non-core ops without a decompositionEvtl. vergeben @Eureka-Zhang hat das vor 7 Tagen übernommen. Offenmodule: exir
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
pytorch/executorch#25620 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Qualcomm AI Engine Direct - fp16a8w quantization crashes on layer_normEvtl. vergeben @psiddh hat das vor 21 Tagen übernommen. Offenbug module: qnn partner: qualcomm
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
pytorch/executorch#24434 · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement triaged
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 68/100
pytorch/executorch#21640 ·
Maintainer antworten meist innerhalb von 1 Tag
-
quantize_per_channel and dequantize_per_tensor.tensor abort the process on a float32 scaleEvtl. vergeben @AxelNoun hat das heute übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 35/100
pytorch/executorch#25873 ·
Maintainer antworten meist innerhalb von 1 Tag
-
XNNPACK INT4 linear returns incorrect per-channel outputs and fails groupwise FP16 on Apple M4 MaxEvtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offenbug module: quantization module: xnnpack
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 43/100
pytorch/executorch#25854 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in pytorch/executorch
Ähnliche Issues
-
Container fails to start when GitHub Releases is unavailable: uv run re-downloads the tvl wheelOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
trezor/trezor-user-env#421 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
-
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 91/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
tangentcode/tangentswarm#167 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag