Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper functions for large ONNX models support #308

Merged
merged 14 commits into from
May 8, 2023

Conversation

dbogunowicz
Copy link
Contributor

@dbogunowicz dbogunowicz commented Apr 26, 2023

Implementing onnx helper functions that will be shared across our repositories (deepsparse, sparseml, etc.) to reliably save and validate not only the models which fit into a protobuf (as we did up until now) but also the larger ones (where the large data information is stored in the additional accompanying file).

Did not include the helper function for loading onnx models, since this is not needed if we work under the assumption that the onnx file and the auxiliary data file are in the same directory, see: https://github.com/onnx/onnx/blob/main/docs/ExternalData.md

Related PRs:
neuralmagic/deepsparse#995 (deepsparse)
neuralmagic/sparseml#1540 (sparseml)

Manual testing (only the big model pathway):

import shutil
from sparsezoo.utils.onnx import save_onnx, validate_onnx
import os
import onnx
import glob

### Validate the new functions for >2Gb models ###

## Prepare the model and test directory ##
model_path = "/network/damian/big_models/codegen-2B-multi/decoder_model.onnx"
model_onnx = onnx.load(model_path)
test_dir = "test_dir"

## Test validate_onnx ##
# should pass for a big model, input is path
validate_onnx(model = model_path)
print("Test 1 passed")

# should raise warning for a big model, input is model
validate_onnx(model = model_onnx)
print("Test 2 passed")

## Test save_onnx ##
# should produce two files
os.mkdir(test_dir)
save_onnx(model_onnx, os.path.join(test_dir, "test.onnx"))
files = glob.glob(os.path.join(test_dir, "*"))
print(f"Produced files {files}")
shutil.rmtree(test_dir)
print("Test 3 passed")

#should produce two files as well
os.mkdir(test_dir)
save_onnx(model_onnx, os.path.join(test_dir, "test.onnx"), external_data_file="something")
files = glob.glob(os.path.join(test_dir, "*"))
print(f"Produced files {files}")
shutil.rmtree(test_dir)
print("Test 4 passed")

Out:

Test 1 passed
Test 2 passed
Attempting to validate an in-memory ONNX model with size > 2000000000 bytes.`validate_onnx` skipped, as large ONNX models cannot be validated in-memory. To validate this model, save it to disk and call `validate_onnx` on the file path.
Produced files ['test_dir/test.onnx', 'test_dir/963eac78-e441-11ed-8c90-d52fb1102de6']
Test 3 passed
Produced files ['test_dir/test.onnx', 'test_dir/something']
Test 4 passed

Copy link
Contributor

@KSGulin KSGulin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Left a couple comments

src/sparsezoo/utils/onnx.py Outdated Show resolved Hide resolved
src/sparsezoo/utils/onnx.py Show resolved Hide resolved
src/sparsezoo/utils/onnx.py Outdated Show resolved Hide resolved
KSGulin
KSGulin previously approved these changes May 8, 2023
Copy link
Contributor

@KSGulin KSGulin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved 🙂

Copy link
Contributor

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending comments

src/sparsezoo/utils/onnx.py Outdated Show resolved Hide resolved
src/sparsezoo/utils/onnx.py Outdated Show resolved Hide resolved
src/sparsezoo/utils/onnx.py Outdated Show resolved Hide resolved
@dbogunowicz dbogunowicz merged commit 62c123f into main May 8, 2023
@dbogunowicz dbogunowicz deleted the feature/damian/large_models_support branch May 8, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants