Skip to content

Commit 2cc6d52

Browse files
committed
[InfCost] per-node norm mac/param counts, always floats for json
1 parent 1dfda07 commit 2cc6d52

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/qonnx/analysis/inference_cost.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def inference_cost_conv(model, node, discount_sparsity):
117117
mac_op_type_str = "op_mac_%s_%s" % (idt_name, wdt_name)
118118
w_mem_type_str = "mem_w_%s" % (wdt_name)
119119
o_mem_type_str = "mem_o_%s" % (odt_name)
120+
# keep in floats to remain compatible with json serialization
121+
n_macs, w_mem, o_mem = float(n_macs), float(w_mem), float(o_mem)
120122
ret = {mac_op_type_str: n_macs, w_mem_type_str: w_mem, o_mem_type_str: o_mem}
121123
return ret
122124

@@ -161,6 +163,8 @@ def inference_cost_matmul(model, node, discount_sparsity):
161163
mac_op_type_str = "op_mac_%s_%s" % (idt_name, wdt_name)
162164
w_mem_type_str = "mem_w_%s" % (wdt_name)
163165
o_mem_type_str = "mem_o_%s" % (odt_name)
166+
# keep in floats to remain compatible with json serialization
167+
n_macs, w_mem, o_mem = float(n_macs), float(w_mem), float(o_mem)
164168
ret = {mac_op_type_str: n_macs, w_mem_type_str: w_mem, o_mem_type_str: o_mem}
165169
return ret
166170

@@ -197,6 +201,8 @@ def inference_cost_upsample(model, node, discount_sparsity):
197201
mac_op_type_str = "op_mac_%s_%s" % (idt_name, idt_name)
198202
o_mem_type_str = "mem_o_%s" % (odt_name)
199203

204+
# keep in floats to remain compatible with json serialization
205+
n_macs, o_mem = float(n_macs), float(o_mem)
200206
ret = {mac_op_type_str: n_macs, o_mem_type_str: o_mem}
201207
return ret
202208

src/qonnx/util/inference_cost.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def inference_cost(
9999
:param preprocess: If set, run preprocessing steps such as shape inference,
100100
datatype inference and constant folding. Strongly recommended.
101101
:param discount_sparsity: If set, will discount op cost of MAC ops with a
102-
constant zero weight, and the mem cost of constant zero weights."""
102+
constant zero weight, and the mem cost of constant zero weights.
103+
:param cost_breakdown: If set, include per-node (by name) and per-node-type
104+
breakdowns as part of the returned inference cost dict."""
103105

104106
combined_results = {}
105107
if isinstance(model_filename_or_wrapper, ModelWrapper):
@@ -130,26 +132,19 @@ def inference_cost(
130132
res["total_macs"] = macs
131133
if "unsupported" in res:
132134
res["unsupported"] = str(res["unsupported"])
133-
if output_json is not None:
134-
with open(output_json, "w") as f:
135-
json.dump(res, f, sort_keys=True, indent=2)
136135
combined_results[i] = res
137-
elif i == "optype_cost":
138-
per_optype_breakdown = {}
136+
else:
137+
per_optype_or_node_breakdown = {}
139138
for optype, op_res in res.items():
140139
bops, macs = compute_bops_and_macs(op_res)
141140
op_res = assign_mem_bits_and_elems(op_res)
142141
op_res["total_bops"] = bops
143142
op_res["total_macs"] = macs
144-
per_optype_breakdown[optype] = op_res
145-
combined_results[i] = per_optype_breakdown
146-
else:
147-
per_node_breakdown = {}
148-
for node_name in res.keys():
149-
node_res = res[node_name]
150-
node_res = assign_mem_bits_and_elems(node_res)
151-
per_node_breakdown[node_name] = node_res
152-
combined_results[i] = per_node_breakdown
143+
per_optype_or_node_breakdown[optype] = op_res
144+
combined_results[i] = per_optype_or_node_breakdown
145+
if output_json is not None:
146+
with open(output_json, "w") as f:
147+
json.dump(combined_results, f, sort_keys=True, indent=2)
153148
return combined_results
154149

155150

tests/analysis/test_inference_cost_breakdown.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def test_inference_cost_breakdown(test_model):
7676
test_details = model_details[test_model]
7777
model = download_model(test_model, do_cleanup=True, return_modelwrapper=True)
7878
inf_cost = infca(model, discount_sparsity=False, cost_breakdown=True)
79-
print(inf_cost.keys())
79+
assert inf_cost["node_cost"]["Conv_0"]["total_macs"] == 118013952
80+
assert inf_cost["node_cost"]["Conv_1"]["total_macs"] == 115605504
81+
assert inf_cost["optype_cost"]["Conv"]["total_macs"] == 1813561344
8082
t_cost = inf_cost["total_cost"] # total cost
8183
op_cost = aggregate_dict_keys(inf_cost["optype_cost"]) # cost per optype
8284
n_cost = aggregate_dict_keys(inf_cost["node_cost"]) # cost per node.

0 commit comments

Comments
 (0)