Implementation:Onnx Onnx Load External Data For Model
| Knowledge Sources | |
|---|---|
| Domains | External_Data, Large_Model_Support |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for loading externally stored tensor data into an ONNX model provided by the ONNX external_data_helper module.
Description
The load_external_data_for_model function iterates over all tensors in the model that use external data storage, reads the data from the external files, populates the tensor's raw_data field, resets data_location to DEFAULT, and clears the external_data reference entries. This function modifies the model in-place.
Usage
Import this function when you have loaded a model with load_external_data=False and need to load the external data from a different directory than the model file. This is common in deployment scenarios where model structure and weights are stored separately.
Code Reference
Source Location
- Repository: onnx
- File: onnx/external_data_helper.py
- Lines: 68-81
Signature
def load_external_data_for_model(
model: ModelProto,
base_dir: str,
) -> None:
"""Loads external tensors into model.
Args:
model: ModelProto to load external data into (modified in-place).
base_dir: Directory containing the external data files.
"""
Import
from onnx.external_data_helper import load_external_data_for_model
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto | Yes | Model with external data references (modified in-place) |
| base_dir | str | Yes | Directory containing the external data files |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | Model is modified in-place with tensor data loaded |
Usage Examples
Load External Data from Custom Directory
import onnx
from onnx.external_data_helper import load_external_data_for_model
# Load model structure without data
model = onnx.load_model("models/model.onnx", load_external_data=False)
# Load data from a different location
load_external_data_for_model(model, "/data/weights/")
# Now all tensors have raw_data populated
for init in model.graph.initializer:
print(f"{init.name}: {len(init.raw_data)} bytes")