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

Commit 1151abf

Browse files
authored
fixing ONNX and PyTorch model/data tests (#55)
* ONNX model and data test fixes * pytorch model test fixes
1 parent 1661057 commit 1151abf

File tree

23 files changed

+2147
-3050
lines changed

23 files changed

+2147
-3050
lines changed

src/sparseml/onnx/optim/sensitivity_pruning.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def pruning_perf_sens_one_shot_iter(
389389
model: Union[str, ModelProto],
390390
data: DataLoader,
391391
batch_size: int,
392-
num_cores: int = -1,
392+
num_cores: int = None,
393393
iterations_per_check: int = 10,
394394
warmup_iterations_per_check: int = 5,
395395
sparsity_levels: List[float] = default_pruning_sparsities_perf(),
@@ -408,7 +408,8 @@ def pruning_perf_sens_one_shot_iter(
408408
to calculate the sparse sensitivity analysis for
409409
:param data: the data to run through the model
410410
:param batch_size: the size of the batch to create the model in neural magic for
411-
:param num_cores: number of physical cores to run on
411+
:param num_cores: number of physical cores to run on. Default is the maximum number
412+
of cores available
412413
:param iterations_per_check: number of iterations to run for perf details
413414
:param warmup_iterations_per_check: number of iterations to run before perf details
414415
:param sparsity_levels: the sparsity levels to calculate the loss for for each param
@@ -484,7 +485,7 @@ def pruning_perf_sens_one_shot(
484485
model: Union[str, ModelProto],
485486
data: DataLoader,
486487
batch_size: int,
487-
num_cores: int = -1,
488+
num_cores: int = None,
488489
iterations_per_check: int = 10,
489490
warmup_iterations_per_check: int = 5,
490491
sparsity_levels: List[float] = default_pruning_sparsities_perf(),
@@ -500,7 +501,8 @@ def pruning_perf_sens_one_shot(
500501
to calculate the sparse sensitivity analysis for
501502
:param data: the data to run through the model
502503
:param batch_size: the size of the batch to create the model in neural magic for
503-
:param num_cores: number of physical cores to run on
504+
:param num_cores: number of physical cores to run on. Default is the maximum
505+
available
504506
:param iterations_per_check: number of iterations to run for perf details
505507
:param warmup_iterations_per_check: number of iterations to run before perf details
506508
:param sparsity_levels: the sparsity levels to calculate the loss for for each param

src/sparseml/onnx/utils/data.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ class DataLoader(object):
4848
Iterator returns a tuple containing (data, label).
4949
label is only returned if label data was passed in.
5050
51-
:param data: a file glob pointing to numpy files or loaded numpy data
52-
:param labels: a file glob pointing to numpy files or loaded numpy data
51+
:param data: a file glob pointing to numpy files, path to a tar ball of numpy
52+
files, or loaded numpy data
53+
:param labels: a file glob pointing to numpy files path to a tar ball of numpy
54+
files, or loaded numpy data
5355
:param batch_size: the size of batches to create for the iterator
5456
:param iter_steps: the number of steps (batches) to create.
5557
Set to -1 for infinite, 0 for running through the loaded data once,

src/sparseml/onnx/utils/model.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,16 @@ class DeepSparseModelRunner(_DeepSparseBaseModelRunner):
617617
618618
:param model: the path to the ONNX model file or the loaded onnx.ModelProto
619619
:param batch_size: the size of the batch to create the model for
620-
:param num_cores: the number of physical cores to run the model on
620+
:param num_cores: the number of physical cores to run the model on. Defaults
621+
to run on all available cores
621622
:param loss: the loss function, if any, to run for evaluation of the model
622623
"""
623624

624625
def __init__(
625626
self,
626627
model: Union[str, ModelProto],
627628
batch_size: int,
628-
num_cores: int = -1,
629+
num_cores: int = None,
629630
loss: Union[
630631
Callable[[Dict[str, numpy.ndarray], Dict[str, numpy.ndarray]], Any], None
631632
] = None,
@@ -783,14 +784,15 @@ class DeepSparseAnalyzeModelRunner(_DeepSparseBaseModelRunner):
783784
784785
:param model: the path to the ONNX model file or the loaded onnx.ModelProto
785786
:param batch_size: the size of the batch to create the model for
786-
:param num_cores: the number of physical cores to run the model on
787+
:param num_cores: the number of physical cores to run the model on. Defaults
788+
to run on all available cores
787789
"""
788790

789791
def __init__(
790792
self,
791793
model: Union[str, ModelProto],
792794
batch_size: int,
793-
num_cores: int = -1,
795+
num_cores: int = None,
794796
):
795797
super().__init__(model, batch_size, num_cores, loss=None)
796798

src/sparseml/pytorch/models/classification/mnist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def forward(self, inp: Tensor):
132132
domain="cv",
133133
sub_domain="classification",
134134
architecture="mnistnet",
135-
sub_architecture="none",
135+
sub_architecture=None,
136136
default_dataset="mnist",
137137
default_desc="base",
138138
)

src/sparseml/utils/helpers.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import errno
2121
import fnmatch
22-
import glob
2322
import logging
2423
import os
2524
import re
@@ -30,6 +29,8 @@
3029

3130
import numpy
3231

32+
from sparsezoo.utils import load_numpy_list
33+
3334

3435
__all__ = [
3536
"ALL_TOKEN",
@@ -487,20 +488,22 @@ def load_labeled_data(
487488
Assumes sorted ordering for on disk. Will match between when a file glob is passed
488489
for either data and/or labels.
489490
490-
:param data: the file glob or list of arrays to use for data
491-
:param labels: the file glob or list of arrays to use for labels, if any
491+
:param data: the file glob, file path to numpy data tar ball, or list of arrays to
492+
use for data
493+
:param labels: the file glob, file path to numpy data tar ball, or list of arrays
494+
to use for labels, if any
492495
:param raise_on_error: True to raise on any error that occurs;
493496
False to log a warning, ignore, and continue
494497
:return: a list containing tuples of the data, labels. If labels was passed in
495498
as None, will now contain a None for the second index in each tuple
496499
"""
497500
if isinstance(data, str):
498-
data = sorted(glob.glob(data))
501+
data = load_numpy_list(data)
499502

500503
if labels is None:
501504
labels = [None for _ in range(len(data))]
502505
elif isinstance(labels, str):
503-
labels = sorted(glob.glob(labels))
506+
labels = load_numpy_list(labels)
504507

505508
if len(data) != len(labels) and labels:
506509
# always raise this error, lengths must match

tests/sparseml/onnx/helpers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def analyzer_models(request):
753753
{
754754
"domain": "cv",
755755
"sub_domain": "classification",
756-
"architecture": "resnet-v1",
756+
"architecture": "resnet_v1",
757757
"sub_architecture": "50",
758758
"framework": "pytorch",
759759
"repo": "sparseml",
@@ -769,7 +769,7 @@ def analyzer_models(request):
769769
{
770770
"domain": "cv",
771771
"sub_domain": "classification",
772-
"architecture": "mobilenet-v1",
772+
"architecture": "mobilenet_v1",
773773
"sub_architecture": "1.0",
774774
"framework": "pytorch",
775775
"repo": "sparseml",
@@ -786,14 +786,14 @@ def analyzer_models(request):
786786
def onnx_repo_models(request) -> OnnxRepoModelFixture:
787787
model_args, model_name = request.param
788788
model = Zoo.load_model(**model_args)
789-
model_path = model.download_onnx_file(overwrite=False)
790-
data_paths = model.download_data_files(overwrite=False)
789+
model_path = model.onnx_file.downloaded_path()
790+
data_paths = [data_file.downloaded_path() for data_file in model.data.values()]
791791

792792
input_paths = None
793793
output_paths = None
794794
for path in data_paths:
795-
if "_sample-inputs" in path:
796-
input_paths = path.split(".tar")[0]
797-
elif "_sample-outputs" in path:
798-
output_paths = path.split(".tar")[0]
795+
if "sample-inputs" in path:
796+
input_paths = path
797+
elif "sample-outputs" in path:
798+
output_paths = path
799799
return OnnxRepoModelFixture(model_path, model_name, input_paths, output_paths)

tests/sparseml/onnx/optim/quantization/test_quantize_model_post_training.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_quantize_model_post_training_mnist():
9393
sub_domain="classification",
9494
architecture="mnistnet",
9595
framework="pytorch",
96-
)[0].download_onnx_file()
96+
)[0].onnx_file.downloaded_path()
9797
quant_model_path = tempfile.NamedTemporaryFile(suffix=".onnx", delete=False).name
9898

9999
# Prepare sample validation dataset
@@ -132,7 +132,7 @@ def test_quantize_model_post_training_resnet50_imagenette():
132132
resnet50_imagenette_path = Zoo.load_model(
133133
domain="cv",
134134
sub_domain="classification",
135-
architecture="resnet-v1",
135+
architecture="resnet_v1",
136136
sub_architecture="50",
137137
framework="pytorch",
138138
repo="sparseml",
@@ -141,7 +141,7 @@ def test_quantize_model_post_training_resnet50_imagenette():
141141
optim_name="base",
142142
optim_category="none",
143143
optim_target=None,
144-
).download_onnx_file(overwrite=True)
144+
).onnx_file.downloaded_path()
145145
quant_model_path = tempfile.NamedTemporaryFile(suffix=".onnx", delete=False).name
146146

147147
# Prepare sample validation dataset

tests/sparseml/onnx/optim/test_analyzer_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
{
3838
"domain": "cv",
3939
"sub_domain": "classification",
40-
"architecture": "resnet-v1",
40+
"architecture": "resnet_v1",
4141
"sub_architecture": "50",
4242
"framework": "pytorch",
4343
"repo": "sparseml",
@@ -55,7 +55,7 @@ def analyzer_models_repo(request):
5555
model_args, output_path = request.param
5656
output_path = os.path.join(RELATIVE_PATH, "test_analyzer_model_data", output_path)
5757
model = Zoo.load_model(**model_args)
58-
model_path = model.download_onnx_file(overwrite=False)
58+
model_path = model.onnx_file.downloaded_path()
5959

6060
if GENERATE_TEST_FILES:
6161
analyzer = ModelAnalyzer(model_path)

tests/sparseml/onnx/optim/test_analyzer_model_data/resnet50pytorch.json

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
"bias_name": null,
176176
"bias_shape": null,
177177
"attributes": {
178+
"ceil_mode": 0,
178179
"kernel_shape": [
179180
3,
180181
3
@@ -8014,35 +8015,12 @@
80148015
},
80158016
{
80168017
"id": "494",
8017-
"op_type": "Constant",
8018-
"input_names": [],
8019-
"output_names": [
8020-
"494"
8021-
],
8022-
"input_shapes": null,
8023-
"output_shapes": null,
8024-
"params": 0,
8025-
"prunable": false,
8026-
"prunable_params": -1,
8027-
"prunable_params_zeroed": 0,
8028-
"prunable_equation_sensitivity": null,
8029-
"flops": null,
8030-
"weight_name": null,
8031-
"weight_shape": null,
8032-
"bias_name": null,
8033-
"bias_shape": null,
8034-
"attributes": {
8035-
"value": null
8036-
}
8037-
},
8038-
{
8039-
"id": "495",
80408018
"op_type": "Shape",
80418019
"input_names": [
80428020
"493"
80438021
],
80448022
"output_names": [
8045-
"495"
8023+
"494"
80468024
],
80478025
"input_shapes": [
80488026
[
@@ -8070,41 +8048,11 @@
80708048
"attributes": {}
80718049
},
80728050
{
8073-
"id": "496",
8074-
"op_type": "Gather",
8075-
"input_names": [
8076-
"495",
8077-
"494"
8078-
],
8079-
"output_names": [
8080-
"496"
8081-
],
8082-
"input_shapes": [
8083-
[
8084-
4
8085-
]
8086-
],
8087-
"output_shapes": null,
8088-
"params": 0,
8089-
"prunable": false,
8090-
"prunable_params": -1,
8091-
"prunable_params_zeroed": 0,
8092-
"prunable_equation_sensitivity": null,
8093-
"flops": null,
8094-
"weight_name": null,
8095-
"weight_shape": null,
8096-
"bias_name": null,
8097-
"bias_shape": null,
8098-
"attributes": {
8099-
"axis": 0
8100-
}
8101-
},
8102-
{
8103-
"id": "497",
8051+
"id": "495",
81048052
"op_type": "Constant",
81058053
"input_names": [],
81068054
"output_names": [
8107-
"497"
8055+
"495"
81088056
],
81098057
"input_shapes": null,
81108058
"output_shapes": null,
@@ -8123,20 +8071,21 @@
81238071
}
81248072
},
81258073
{
8126-
"id": "498",
8127-
"op_type": "Unsqueeze",
8074+
"id": "496",
8075+
"op_type": "Gather",
81288076
"input_names": [
8129-
"496"
8077+
"494",
8078+
"495"
81308079
],
81318080
"output_names": [
8132-
"498"
8081+
"496"
81338082
],
8134-
"input_shapes": null,
8135-
"output_shapes": [
8083+
"input_shapes": [
81368084
[
8137-
1
8085+
4
81388086
]
81398087
],
8088+
"output_shapes": null,
81408089
"params": 0,
81418090
"prunable": false,
81428091
"prunable_params": -1,
@@ -8148,19 +8097,17 @@
81488097
"bias_name": null,
81498098
"bias_shape": null,
81508099
"attributes": {
8151-
"axes": [
8152-
0
8153-
]
8100+
"axis": 0
81548101
}
81558102
},
81568103
{
8157-
"id": "499",
8104+
"id": "498",
81588105
"op_type": "Unsqueeze",
81598106
"input_names": [
8160-
"497"
8107+
"496"
81618108
],
81628109
"output_names": [
8163-
"499"
8110+
"498"
81648111
],
81658112
"input_shapes": null,
81668113
"output_shapes": [
@@ -8188,16 +8135,12 @@
81888135
"id": "500",
81898136
"op_type": "Concat",
81908137
"input_names": [
8191-
"498",
8192-
"499"
8138+
"498"
81938139
],
81948140
"output_names": [
81958141
"500"
81968142
],
81978143
"input_shapes": [
8198-
[
8199-
1
8200-
],
82018144
[
82028145
1
82038146
]

0 commit comments

Comments
 (0)