﻿# ONNX Runtime reused streams: select the owning GPU before allocating the next input

The 1.32 reused-stream path restores device ownership before dependent work; test nondefault GPUs and caller exit with pending asynchronous transfers.

Author: Retrivium team
Updated: 2026-11-15
Canonical URL: https://retrivium.ai/journal/week46-engine-ort-reused-stream-owning-device-restore/
Language: en

ONNX Runtime 1.32.0’s stream-reuse work includes selecting a reused stream’s owning device before input allocation and notification creation. The linked PR tests a nondefault GPU path. Reusing a stream is therefore a device-state transition as well as a pooling action; thread-local current-device state cannot be assumed to follow the borrowed resource automatically.

## A stream handle does not set thread state

A caller can arrive with a different current GPU from the one owning a pooled stream. If input allocation occurs first, storage can be associated with the wrong device. The release work orders device selection before that dependent action. A proposed test begins on another device, acquires a reused stream and verifies allocation and execution on the intended owner. A one-GPU test cannot expose this class of mismatch.

## Notification creation has the same dependency

Completion notifications can also depend on current device context. Correcting only the allocation site would leave a later context-sensitive operation vulnerable. A local suite should cover the whole acquisition-to-completion path, including notification destruction after the caller exits. Preserve actual asynchronous work so the notification has something to order. A synchronous trivial kernel may complete too early to exercise deferred resource ownership.

## Pending host staging must remain owned

The PR includes deferred pinned-memory lifetime during asynchronous copies. Keep a distinct pattern in source staging and verify the destination after completion. Do not release or repurpose storage merely because the initiating caller thread has ended. A proposed check observes that resources are retained while transfer is pending and released afterward, covering both early-free safety and eventual cleanup in the reused-stream scenario.

## Keep the hardware claim specific

The release changes runtime ownership and context handling, not the arithmetic of a model. Upstream reports a focused CUDA validation; it should not be retold as testing every provider or operating system. This article reports the ordering contract and proposed multi-GPU lifecycle case without new measurements. The transferable rule is to restore resource ownership context before dependent allocation or synchronization work, not merely before the final kernel launch.

## Sources

- [ONNX Runtime v1.32.0 release](https://github.com/microsoft/onnxruntime/releases/tag/v1.32.0)
- [ONNX Runtime pull request #33053](https://github.com/microsoft/onnxruntime/pull/33053)

## Related reading

- [NVIDIA_VISIBLE_DEVICES vs CUDA_VISIBLE_DEVICES: why GPU indexes differ](/journal/nvidia-visible-vs-cuda-visible-devices/)
- [CUDA event dependencies: connecting producer and consumer streams](/journal/learn-cross-stream-event-dependency/)
