IOBinding
TheIOBinding class provides an API to bind model inputs and outputs to specific device memory (CPU, CUDA, DirectML, etc.), enabling zero-copy inference and improved performance for GPU workloads.
Constructor
IOBinding is created through an InferenceSession:Methods
bind_cpu_input()
Bind an input to a numpy array on CPU.str
required
Name of the input.
np.ndarray
required
Input values as a numpy array on CPU.
bind_input()
Bind an input to pre-allocated device memory.str
required
Name of the input.
str
required
Device type: “cpu”, “cuda”, “cann”, “dml”, etc.
int
required
Device ID (e.g., 0 for first GPU).
np.dtype | int
required
Element data type (numpy type like np.float32 or ONNX TensorProto type).
tuple[int]
required
Shape of the input tensor.
int
required
Memory pointer to the input data buffer.
bind_ortvalue_input()
Bind an input to an OrtValue object.str
required
Name of the input.
OrtValue
required
OrtValue instance containing input data.
bind_output()
Bind an output to device memory.str
required
Name of the output.
str
Device type: “cpu”, “cuda”, etc. Default is “cpu”.
int
Device ID. Default is 0.
np.dtype | int
Element data type. Required if buffer_ptr is provided.
tuple[int]
Output shape. Required if buffer_ptr is provided.
int
Pre-allocated memory pointer. If None, ORT allocates memory.
bind_ortvalue_output()
Bind an output to an OrtValue object.get_outputs()
Get output OrtValues after running inference.list[OrtValue]
List of OrtValue objects containing output data on their respective devices.
copy_outputs_to_cpu()
Copy output contents to CPU as numpy arrays.list[np.ndarray]
List of output tensors as numpy arrays on CPU.
synchronize_inputs()
Synchronize device inputs before inference.synchronize_outputs()
Synchronize device outputs after inference.clear_binding_inputs()
Clear all bound inputs.clear_binding_outputs()
Clear all bound outputs.Example Usage
Basic CUDA Inference
Reusing IOBinding for Multiple Runs
Pre-allocated Output Buffers
Multi-Input Model
CPU Binding
Performance Best Practices
Related APIs
- InferenceSession - Create and run sessions
- OrtValue - Device-agnostic tensor wrapper
- Execution Providers - Hardware acceleration