You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
…in_memory (closes#25873)
### Description
Adds a Python binding to load external initializer files from in‑memory
buffers.
Mirrors existing C/C++ API and Node binding to enable full in‑memory
model loading.
Adds explicit type validation to avoid pybind dumping large raw bytes on
argument errors.
### Motivation and Context
Problem: Models that use external_data for initializers can’t be fully
loaded from bytes in Python because the weights are expected on disk.
Goal: Allow providing the external file contents directly from Python
memory (e.g., bytes, memoryview, numpy), eliminating filesystem
dependency and supporting serverless/remote asset scenarios.
Issue: Fixes#25873.
### Changes
#### New Python API on SessionOptions:
Name: add_external_initializers_from_files_in_memory
Signature: names: list[str], buffers: list[bytes-like], lengths:
list[int]
File: onnxruntime/python/onnxruntime_pybind_state.cc
#### Input validation to prevent noisy errors:
Validates top-level types are lists and that list lengths match.
Validates each name is str, each buffer supports the buffer protocol,
and each length is an int.
Raises clear RuntimeError messages instead of pybind11’s verbose dumps
for mismatched types.
#### Test added:
onnxruntime/test/python/onnxruntime_test_python.py
test_session_options_add_external_initializers_from_files_in_memory:
supplies “Pads_not_on_disk.bin” content from a numpy array’s bytes and
loads model_with_external_initializer_come_from_user.onnx without
touching the filesystem.
### Usage
Provide the external file name(s) as referenced by the model’s
external_data “location”, plus their bytes and lengths:
so.add_external_initializers_from_files_in_memory(["weights.bin"],
[weights_bytes], [len(weights_bytes)])
sess = onnxruntime.InferenceSession(model_bytes, sess_options=so)
---------
Signed-off-by: Jonah Bernard <[email protected]>
Co-authored-by: Jonah Bernard <[email protected]>
0 commit comments