onnxruntime
06a3ccfa - python(api): add SessionOptions.add_external_initializers_from_files_… (#26012)

Commit
132 days ago
python(api): add SessionOptions.add_external_initializers_from_files_… (#26012) …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 <jb2528@cornell.edu> Co-authored-by: Jonah Bernard <jb2528@cornell.edu>
Author
Parents
Loading