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

Commit 071cbf1

Browse files
rahul-tulihorheynmdsikkadbogunowiczbfineran
authored
[BugFix] Add analyze to init (#421) (#434)
* Add analyze to init * Move onnxruntime to deps * Print model analysis (#423) * [model.download] fix function returning nothing (#420) * [BugFix] Path not expanded (#418) * print model-analysis * [Fix] Allow for processing Path in the sparsezoo analysis (#417) * add print statement at the end of cli run --------- * Omit scalar weight (#424) * ommit scalar weights: * remove unwanted files * comment * Update src/sparsezoo/utils/onnx/analysis.py --------- --------- Co-authored-by: George <george@neuralmagic.com> Co-authored-by: Dipika Sikka <dipikasikka1@gmail.com> Co-authored-by: dbogunowicz <97082108+dbogunowicz@users.noreply.github.com> Co-authored-by: Benjamin Fineran <bfineran@users.noreply.github.com>
1 parent 59e3151 commit 071cbf1

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"pandas>1.3",
5252
"py-machineid>=0.3.0",
5353
"geocoder>=1.38.0",
54+
"onnxruntime>=1.0.0",
5455
]
5556
_notebook_deps = ["ipywidgets>=7.0.0", "jupyter>=1.0.0"]
5657

@@ -62,7 +63,6 @@
6263
"isort>=5.7.0",
6364
"pytest>=6.0.0",
6465
"wheel>=0.36.2",
65-
"onnxruntime>=1.0.0",
6666
"matplotlib>=3.0.0",
6767
]
6868

src/sparsezoo/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
from . import deployment_package as deployment_package_module
2626
from .deployment_package import *
2727
from .analytics import sparsezoo_analytics as _analytics
28+
from .analyze_v2 import analyze
2829

2930
_analytics.send_event("python__init")

src/sparsezoo/analyze_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def main(
7777
with open(save, "w") as file:
7878
file.write(analysis.to_yaml())
7979

80+
print(analysis)
81+
8082

8183
if __name__ == "__main__":
8284
main()

src/sparsezoo/analyze_v2/model_analysis.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515

16-
from typing import Dict
16+
from typing import Dict, Optional
1717

1818
import onnx
1919
import yaml
@@ -73,11 +73,70 @@ def to_dict(self):
7373
nodes=nodes,
7474
).dict()
7575

76+
def calculate_sparsity_percentage(self, category: Dict):
77+
counts_sparse = category["counts_sparse"]
78+
counts = category["counts"]
79+
return (counts_sparse / counts) * 100 if counts != 0 else 0
80+
81+
def calculate_quantized_percentage(self, tensor: Dict):
82+
bits_quant = tensor["bits_quant"]
83+
bits = tensor["bits"]
84+
return (bits_quant / bits) * 100 if bits != 0 else 0
85+
86+
def __repr__(self):
87+
data = self.to_dict()
88+
summaries = data["summaries"]
89+
90+
param_total = summaries["params"]["sparsity"]["single"]["counts"]
91+
param_sparsity = self.calculate_sparsity_percentage(
92+
summaries["params"]["sparsity"]["single"]
93+
)
94+
param_size = summaries["params"]["quantization"]["tensor"]["bits"]
95+
param_quantized = self.calculate_quantized_percentage(
96+
summaries["params"]["quantization"]["tensor"]
97+
)
98+
99+
ops_total = summaries["ops"]["sparsity"]["single"]["counts"]
100+
ops_sparsity = self.calculate_sparsity_percentage(
101+
summaries["ops"]["sparsity"]["single"]
102+
)
103+
ops_size = summaries["ops"]["quantization"]["tensor"]["bits"]
104+
ops_quantized = self.calculate_quantized_percentage(
105+
summaries["ops"]["quantization"]["tensor"]
106+
)
107+
108+
mem_access_total = summaries["mem_access"]["sparsity"]["single"]["counts"]
109+
mem_access_sparsity = self.calculate_sparsity_percentage(
110+
summaries["mem_access"]["sparsity"]["single"]
111+
)
112+
mem_access_size = summaries["mem_access"]["quantization"]["tensor"]["bits"]
113+
mem_access_quantized = self.calculate_quantized_percentage(
114+
summaries["mem_access"]["quantization"]["tensor"]
115+
)
116+
117+
return (
118+
"Params:\n"
119+
f"\ttotal\t\t: {param_total}\n"
120+
f"\tsparsity%\t: {param_sparsity}\n"
121+
f"\tsize [bits]\t: {param_size}\n"
122+
f"\tquantized %\t: {param_quantized}\n"
123+
"Ops:\n"
124+
f"\ttotal\t\t: {ops_total}\n"
125+
f"\tsparsity%\t: {ops_sparsity}\n"
126+
f"\tsize [bits]\t: {ops_size}\n"
127+
f"\tquantized %\t: {ops_quantized}\n"
128+
"Memory Access:\n"
129+
f"\ttotal\t\t: {mem_access_total}\n"
130+
f"\tsparsity%\t: {mem_access_sparsity}\n"
131+
f"\tsize [bits]\t: {mem_access_size}\n"
132+
f"\tquantized %\t: {mem_access_quantized}\n"
133+
)
134+
76135
def to_yaml(self):
77136
return yaml.dump(self.to_dict())
78137

79138

80-
def analyze(path: str) -> "ModelAnalysis":
139+
def analyze(path: str, download_path: Optional[str] = None) -> "ModelAnalysis":
81140
"""
82141
Entry point to run the model analysis.
83142
@@ -89,7 +148,7 @@ def analyze(path: str) -> "ModelAnalysis":
89148
if path.endswith(".onnx"):
90149
onnx_model = load_model(path)
91150
elif is_stub(path):
92-
model = Model(path)
151+
model = Model(path, download_path)
93152
onnx_model_path = model.onnx_model.path
94153
onnx_model = onnx.load(onnx_model_path)
95154
else:

src/sparsezoo/utils/onnx/analysis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,15 @@ def get_node_weight(
385385
:param node: node to which parameter belongs to
386386
:return: a numpy array of param value, None if not found
387387
"""
388-
389388
initializer_name = get_node_weight_name(model_graph, node)
390389
weight = get_initializer_value(model_graph, node, initializer_name)
390+
391391
if initializer_name is not None and weight is None and node.op_type != "Gather":
392392
raise Exception(f"Parameter for {node.name} not found")
393393

394+
# some weights are not accessible, and returns the zero_points, which are scalars
395+
if weight is not None and weight.ndim in [0, 1]:
396+
return None
394397
return weight
395398

396399

0 commit comments

Comments
 (0)