Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit b0d0e9f

Browse files
authored
external data fix (#466) (#468)
1 parent edf177e commit b0d0e9f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/sparsezoo/utils/onnx/external_data.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def save_onnx(
7676
model_path: str,
7777
max_external_file_size: int = 16e9,
7878
external_data_file: Optional[str] = None,
79+
do_split_external_data: bool = True,
7980
) -> bool:
8081
"""
8182
Save model to the given path.
@@ -95,6 +96,8 @@ def save_onnx(
9596
specified in the variable EXTERNAL_ONNX_DATA_NAME
9697
:param max_external_file_size: The maximum file size in bytes of a single split
9798
external data out file. Defaults to 16000000000 (16e9 = 16GB)
99+
:param do_split_external_data: True to split external data file into chunks of max
100+
size max_external_file_size, false otherwise
98101
:return True if the model was saved with external data, False otherwise.
99102
"""
100103
if external_data_file is not None:
@@ -112,7 +115,8 @@ def save_onnx(
112115
all_tensors_to_one_file=True,
113116
location=external_data_file,
114117
)
115-
split_external_data(model_path, max_file_size=max_external_file_size)
118+
if do_split_external_data:
119+
split_external_data(model_path, max_file_size=max_external_file_size)
116120
return True
117121

118122
if model.ByteSize() > DUMP_EXTERNAL_DATA_THRESHOLD:
@@ -132,7 +136,8 @@ def save_onnx(
132136
all_tensors_to_one_file=True,
133137
location=external_data_file,
134138
)
135-
split_external_data(model_path, max_file_size=max_external_file_size)
139+
if do_split_external_data:
140+
split_external_data(model_path, max_file_size=max_external_file_size)
136141
return True
137142

138143
onnx.save(model, model_path)
@@ -247,6 +252,9 @@ def split_external_data(
247252
f"{external_data_file_path} not found. {model_path} must have external "
248253
"data written to a single file in the same directory"
249254
)
255+
if os.path.getsize(external_data_file_path) <= max_file_size:
256+
# return immediately if file is small enough to not split
257+
return
250258

251259
# UPDATE: external data info of graph tensors so they point to the new split out
252260
# files with updated offsets

0 commit comments

Comments
 (0)