Session
Session in DX-ModelZoo is a runtime interface for performing model inference. It supports two backends: ONNX Runtime and DxRuntime (NPU).
Module: dx_modelzoo.session
SessionBase
Abstract base class that all sessions inherit from.
class SessionBase(ABC):
def __init__(self, path: str, device_count: int = 1, runtime_config: Optional[RuntimeConfig] = None)
@abstractmethod
def run(self, inputs: np.ndarray) -> list[np.ndarray]
"""Synchronous inference"""
def run_async(self, inputs: np.ndarray, **kwargs) -> int
"""Asynchronous inference request → returns job_id"""
def wait(self, job_id: int) -> list[np.ndarray]
"""Wait for asynchronous inference result"""
def close(self) -> None
"""Release session resources"""
def __enter__(self): ...
def __exit__(self, ...): ...
Attributes
| Attribute | Type | Description |
|---|---|---|
path | str | Path to the model file |
device_count | int | Number of devices to use |
runtime_config | RuntimeConfig | None | Runtime options (device, batch size, async mode) |
Context Manager
with OnnxRuntimeSession("model.onnx") as session:
output = session.run(input_tensor)
# close() is called automatically
OnnxRuntimeSession
Inference session based on ONNX Runtime.
Module: dx_modelzoo.session.onnx_session
class OnnxRuntimeSession(SessionBase):
def __init__(self, path: str, runtime_config: Optional[RuntimeConfig] = None)
def run(self, inputs: np.ndarray) -> list[np.ndarray]
def run_async(self, inputs: np.ndarray, **kwargs) -> int
def wait(self, job_id: int) -> list[np.ndarray]
Features
- Automatically detects CUDA availability for GPU inference
run_asyncis based onThreadPoolExecutor- Configurable
batch_size(YAMLprofiles.onnx.runtime.batch_size)
Usage Example
from dx_modelzoo.session.onnx_session import OnnxRuntimeSession
session = OnnxRuntimeSession("resnet18.onnx")
# Synchronous inference
import numpy as np
input_tensor = np.random.randn(1, 3, 224, 224).astype(np.float32)
outputs = session.run(input_tensor)
# outputs: list[np.ndarray]
session.close()
DxRuntimeSession
Inference session based on the DeepX NPU runtime.
Module: dx_modelzoo.session.dx_session
class DxRuntimeSession(SessionBase):
def __init__(self, path: str, runtime_config: Optional[RuntimeConfig] = None)
def run(self, inputs: np.ndarray) -> list[np.ndarray]
def run_async(self, inputs: np.ndarray, **kwargs) -> int
def wait(self, job_id: int) -> list[np.ndarray]
Attributes
| Attribute | Type | Description |
|---|---|---|
dtype | list[dtype] | Model input data types (extracted from InferenceEngine) |
Features
- Uses the
dx_enginelibrary - Native support for asynchronous inference
- Device selection:
runtime_config.deviceor theDXNN_DEVICESenvironment variable - Fixed
batch_size=1
Usage Example
from dx_modelzoo.session.dx_session import DxRuntimeSession
from dx_modelzoo.session.runtime_config import DxnnRuntimeConfig
# Use device 0
session = DxRuntimeSession("model.dxnn", runtime_config=DxnnRuntimeConfig(device=0))
# Asynchronous inference
job_id = session.run_async(input_tensor)
outputs = session.wait(job_id)
session.close()
# Specify via runtime_config
session = DxRuntimeSession("model.dxnn", runtime_config=DxnnRuntimeConfig(device=0))
session = DxRuntimeSession("model.dxnn", runtime_config=DxnnRuntimeConfig(device=[0, 1])) # Multi-device
# Specify via environment variable
# export DXNN_DEVICES=0,1
session = DxRuntimeSession("model.dxnn")
Session Factory
A factory function that automatically creates the appropriate session based on profile information.
Module: dx_modelzoo.session.factory
def create_session(
model_or_profile: str,
*,
builder: Optional[object] = None,
runtime_config: Optional[RuntimeConfig] = None,
) -> SessionBase
| Parameter | Type | Description |
|---|---|---|
model_or_profile | str | Model file path or profile name |
builder | object | None | ModelBuilder instance (for profile resolution) |
runtime_config | RuntimeConfig | None | Runtime options such as device, batch size, and async mode |
How It Works
Automatic Download
If the DXMZ_MODEL_URL environment variable is set, the model file is automatically downloaded when it is not available locally:
export DXMZ_MODEL_URL=https://models.example.com/modelzoo
# Downloads automatically if file is not found
session = create_session("onnx", builder=builder)
# → Downloads https://models.example.com/modelzoo/onnx/resnet50_224x224.onnx
Automatic download only works when DXMZ_MODEL_URL is set and the model file is not available locally. In offline environments, model files must be prepared in advance.
Sync vs Async Comparison
The following compares synchronous and asynchronous inference:
- Sync
- Async
session = OnnxRuntimeSession("model.onnx")
output = session.run(input_tensor) # Blocks until result is returned
- Simple and intuitive
- Runs only one inference at a time
session = DxRuntimeSession("model.dxnn")
# Inference requests (non-blocking)
job1 = session.run_async(input1)
job2 = session.run_async(input2)
# Wait for results
output1 = session.wait(job1)
output2 = session.wait(job2)
- High throughput through pipeline optimization
- Overlaps data loading and inference on the NPU
Runtime Configuration
Runtime options are defined in
dx_modelzoo.session.runtime_config and are built from the YAML
profiles.<name>.runtime block before a session is created.
RuntimeConfig
Base dataclass shared by all backends.
@dataclass
class RuntimeConfig:
device: Any = None
batch_size: int = 1
async_mode: Optional[bool] = None
start_method: str = "auto"
| Field | Description |
|---|---|
device | Device selection (cpu/gpu for ONNX, device IDs for DXNN) |
batch_size | Evaluation batch size carried from YAML runtime config |
async_mode | Explicit async override from YAML runtime.async |
start_method | Multiprocessing start method for the DataLoader: auto, forkserver, or spawn |
RuntimeConfig.use_async resolves async_mode to the backend default when the
YAML omits runtime.async.
OnnxRuntimeConfig
ONNX Runtime backend config.
@dataclass
class OnnxRuntimeConfig(RuntimeConfig):
ASYNC_DEFAULT = False
- Default async mode:
False - Used by
OnnxRuntimeSession
DxnnRuntimeConfig
DXNN backend config.
@dataclass
class DxnnRuntimeConfig(RuntimeConfig):
ASYNC_DEFAULT = True
buffer_count: Optional[int] = None
use_ort: bool = True
| Field | Description |
|---|---|
buffer_count | Engine I/O buffer count from YAML runtime.buffer_count |
use_ort | Run unsupported ops on ONNX Runtime via YAML runtime.use_ort |
- Default async mode:
True - Used by
DxRuntimeSession
YAML Mapping
profiles:
onnx:
target: onnx
runtime:
device: gpu
batch_size: 4
async: false
q-lite:
target: dxnn
runtime:
device: 0
async: true
buffer_count: 6
use_ort: true
This maps to:
target: onnx→OnnxRuntimeConfigtarget: dxnn→DxnnRuntimeConfigruntime.async→async_moderuntime.buffer_count/runtime.use_ort→ DXNN-only fields
Environment Variables
DXMZ_MODEL_URL and DXNN_DEVICES are relevant to session creation — see CLI Reference for the full list of environment variables.
Integration with ModelBuilder
Typically, sessions are not created directly but through ModelBuilder:
from dx_modelzoo.loader.model_builder import ModelBuilder
builder = ModelBuilder("resnet50_224x224.yaml")
# Automatically creates the appropriate session for the profile
session = builder.build_session("onnx") # → OnnxRuntimeSession
session = builder.build_session("q-lite") # → DxRuntimeSession
# Specify model file directly
session = builder.build_session("onnx", model_path="/path/to/model.onnx")