v5 refactor: add profiler.py
Browse files- neurogolf_solver/profiler.py +59 -0
neurogolf_solver/profiler.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Static profiling for ONNX models."""
|
| 3 |
+
|
| 4 |
+
import onnx
|
| 5 |
+
from onnx import numpy_helper
|
| 6 |
+
from .constants import BANNED_OPS, GH, GW
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
from neurogolf_utils import score_network as _score_network_official
|
| 10 |
+
HAS_ONNX_TOOL = True
|
| 11 |
+
except ImportError:
|
| 12 |
+
HAS_ONNX_TOOL = False
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def score_network(path):
|
| 16 |
+
"""Score network using official tool or fallback."""
|
| 17 |
+
if HAS_ONNX_TOOL:
|
| 18 |
+
try:
|
| 19 |
+
return _score_network_official(path)
|
| 20 |
+
except:
|
| 21 |
+
pass
|
| 22 |
+
return _static_profile(path)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _static_profile(path):
|
| 26 |
+
"""Static profiling fallback."""
|
| 27 |
+
try:
|
| 28 |
+
model = onnx.load(path)
|
| 29 |
+
except:
|
| 30 |
+
return None, None, None
|
| 31 |
+
tensors = {}
|
| 32 |
+
params = 0
|
| 33 |
+
nbytes = 0
|
| 34 |
+
macs = 0
|
| 35 |
+
for init in model.graph.initializer:
|
| 36 |
+
a = numpy_helper.to_array(init)
|
| 37 |
+
tensors[init.name] = a
|
| 38 |
+
params += a.size
|
| 39 |
+
nbytes += a.nbytes
|
| 40 |
+
for nd in model.graph.node:
|
| 41 |
+
if nd.op_type == 'Constant':
|
| 42 |
+
for attr in nd.attribute:
|
| 43 |
+
if attr.t and attr.t.ByteSize() > 0:
|
| 44 |
+
try:
|
| 45 |
+
a = numpy_helper.to_array(attr.t)
|
| 46 |
+
if nd.output:
|
| 47 |
+
tensors[nd.output[0]] = a
|
| 48 |
+
params += a.size
|
| 49 |
+
nbytes += a.nbytes
|
| 50 |
+
except:
|
| 51 |
+
pass
|
| 52 |
+
if nd.op_type in BANNED_OPS:
|
| 53 |
+
return None, None, None
|
| 54 |
+
if nd.op_type == 'Conv' and len(nd.input) >= 2 and nd.input[1] in tensors:
|
| 55 |
+
w = tensors[nd.input[1]]
|
| 56 |
+
if w.ndim == 4:
|
| 57 |
+
co, ci, kh, kw = w.shape
|
| 58 |
+
macs += co * ci * kh * kw * GH * GW
|
| 59 |
+
return int(macs), int(nbytes), int(params)
|