Skip to main content
SDK Version: 2.5.0

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​

AttributeTypeDescription
pathstrPath to the model file
device_countintNumber of devices to use
runtime_configRuntimeConfig | NoneRuntime 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_async is based on ThreadPoolExecutor
  • Configurable batch_size (YAML profiles.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​

AttributeTypeDescription
dtypelist[dtype]Model input data types (extracted from InferenceEngine)

Features​

  • Uses the dx_engine library
  • Native support for asynchronous inference
  • Device selection: runtime_config.device or the DXNN_DEVICES environment 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()
Device Selection
# 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
ParameterTypeDescription
model_or_profilestrModel file path or profile name
builderobject | NoneModelBuilder instance (for profile resolution)
runtime_configRuntimeConfig | NoneRuntime 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
Network Required

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:

session = OnnxRuntimeSession("model.onnx")
output = session.run(input_tensor) # Blocks until result is returned
  • Simple and intuitive
  • Runs only one inference at a time

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"
FieldDescription
deviceDevice selection (cpu/gpu for ONNX, device IDs for DXNN)
batch_sizeEvaluation batch size carried from YAML runtime config
async_modeExplicit async override from YAML runtime.async
start_methodMultiprocessing 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
FieldDescription
buffer_countEngine I/O buffer count from YAML runtime.buffer_count
use_ortRun 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 → OnnxRuntimeConfig
  • target: dxnn → DxnnRuntimeConfig
  • runtime.async → async_mode
  • runtime.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")