Commit 06a3ccf
authored
python(api): add SessionOptions.add_external_initializers_from_files_… (microsoft#26012)
…in_memory (closes microsoft#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 microsoft#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 <jb2528@cornell.edu>
Co-authored-by: Jonah Bernard <jb2528@cornell.edu>1 parent d251f3a commit 06a3ccf
File tree
2 files changed
+63
-1
lines changed- onnxruntime
- python
- test/python
2 files changed
+63
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2300 | 2300 | | |
2301 | 2301 | | |
2302 | 2302 | | |
2303 | | - | |
| 2303 | + | |
| 2304 | + | |
| 2305 | + | |
| 2306 | + | |
| 2307 | + | |
| 2308 | + | |
| 2309 | + | |
| 2310 | + | |
| 2311 | + | |
| 2312 | + | |
| 2313 | + | |
| 2314 | + | |
| 2315 | + | |
| 2316 | + | |
| 2317 | + | |
| 2318 | + | |
| 2319 | + | |
| 2320 | + | |
| 2321 | + | |
| 2322 | + | |
| 2323 | + | |
| 2324 | + | |
| 2325 | + | |
| 2326 | + | |
| 2327 | + | |
| 2328 | + | |
| 2329 | + | |
| 2330 | + | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
| 2343 | + | |
| 2344 | + | |
| 2345 | + | |
| 2346 | + | |
2304 | 2347 | | |
2305 | 2348 | | |
2306 | 2349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1305 | 1305 | | |
1306 | 1306 | | |
1307 | 1307 | | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
1308 | 1327 | | |
1309 | 1328 | | |
1310 | 1329 | | |
| |||
0 commit comments