@@ -2300,7 +2300,50 @@ Applies to session load, initialization, etc. Default is 0.)pbdoc")
2300
2300
ORT_UNUSED_PARAMETER (ort_values);
2301
2301
ORT_THROW (" External initializers are not supported in this build." );
2302
2302
#endif
2303
- });
2303
+ })
2304
+ .def (" add_external_initializers_from_files_in_memory" , [](PySessionOptions* options, std::vector<std::string> names, std::vector<py::buffer> buffers, std::vector<size_t > lengths) -> void {
2305
+ #if !defined(ORT_MINIMAL_BUILD) && !defined(DISABLE_EXTERNAL_INITIALIZERS)
2306
+ const auto num = names.size ();
2307
+ ORT_ENFORCE (num == buffers.size () && num == lengths.size (),
2308
+ " add_external_initializers_from_files_in_memory: expecting 'names', 'buffers' and 'lengths' to have equal length" );
2309
+
2310
+ InlinedVector<PathString> file_names;
2311
+ InlinedVector<std::pair<char *, const size_t >> files_buffers;
2312
+ file_names.reserve (num);
2313
+ files_buffers.reserve (num);
2314
+
2315
+ for (size_t i = 0 ; i < num; ++i) {
2316
+ // Convert name and buffer using pybind-provided conversions
2317
+ file_names.emplace_back (ToPathString (names[i]));
2318
+
2319
+ // buffers[i] is a py::buffer; request() retrieves pointer without copying
2320
+ py::buffer_info info = buffers[i].request ();
2321
+ char * data_ptr = static_cast <char *>(info.ptr );
2322
+
2323
+ files_buffers.emplace_back (std::make_pair (data_ptr, lengths[i]));
2324
+ }
2325
+
2326
+ ORT_THROW_IF_ERROR (options->value .AddExternalInitializersFromFilesInMemory (file_names, files_buffers));
2327
+ #else
2328
+ ORT_UNUSED_PARAMETER (options);
2329
+ ORT_UNUSED_PARAMETER (names);
2330
+ ORT_UNUSED_PARAMETER (buffers);
2331
+ ORT_UNUSED_PARAMETER (lengths);
2332
+ ORT_THROW (" External initializers are not supported in this build." );
2333
+ #endif
2334
+ },
2335
+ R"pbdoc(
2336
+ Provide external initializer file contents from memory.
2337
+
2338
+ Args:
2339
+ names: sequence[str] of external file names (as referenced by the model's external_data locations).
2340
+ buffers: sequence[bytes-like] objects exposing the buffer protocol (e.g., bytes, bytearray, memoryview, numpy uint8 array) containing the corresponding file contents.
2341
+ lengths: sequence[int] sizes in bytes for each buffer.
2342
+
2343
+ Notes:
2344
+ - Keep the provided buffers alive until after session creation completes. ONNX Runtime copies needed data during session creation.
2345
+ - The bytestream must match the external file layout expected by the model (raw tensor bytes at the specified offsets).
2346
+ )pbdoc" );
2304
2347
2305
2348
py::class_<RunOptions>(m, " RunOptions" , R"pbdoc( Configuration information for a single Run.)pbdoc" )
2306
2349
.def (py::init ())
0 commit comments