Skip to content

Commit

Permalink
Address CodeQL notices
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlizarraga committed Nov 21, 2023
1 parent 49b477a commit 8bb660f
Showing 1 changed file with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import numpy as np
import onnx
from onnx import TensorProto, helper, numpy_helper

from onnxruntime import quantization
from onnxruntime.quantization.quant_utils import compute_scale_zp, get_qmin_qmax_for_qType
Expand All @@ -23,9 +22,9 @@ def setUp(self):

self.weight = np.array([[[-1.0, -2.0], [1.0, 2.0]], [[-0.5, -1.5], [0.5, 1.5]]], dtype=np.float32)
self.bias = np.array([0.0, 1.0], dtype=np.float32)
self.default_act_qtype = TensorProto.UINT8
self.default_wgt_qtype = TensorProto.UINT8
self.default_bias_qtype = TensorProto.INT32
self.default_act_qtype = onnx.TensorProto.UINT8
self.default_wgt_qtype = onnx.TensorProto.UINT8
self.default_bias_qtype = onnx.TensorProto.INT32

self.default_zp_scales = {
"INP": (0, np.float32(0.0235294122248888)),
Expand All @@ -44,16 +43,18 @@ def perform_qdq_quantization(self, output_model_name, tensor_quant_overrides=Non
# |
# (output)

inp = helper.make_tensor_value_info("INP", TensorProto.FLOAT, self.activations[0].shape)
inp = onnx.helper.make_tensor_value_info("INP", onnx.TensorProto.FLOAT, self.activations[0].shape)
sigmoid_node = onnx.helper.make_node("Sigmoid", ["INP"], ["SIG_OUT"])

out = helper.make_tensor_value_info("OUT", TensorProto.FLOAT, [None, None, None])
wgt_init = numpy_helper.from_array(self.weight, "WGT")
bias_init = numpy_helper.from_array(self.bias, "BIAS")
out = onnx.helper.make_tensor_value_info("OUT", onnx.TensorProto.FLOAT, [None, None, None])
wgt_init = onnx.numpy_helper.from_array(self.weight, "WGT")
bias_init = onnx.numpy_helper.from_array(self.bias, "BIAS")
conv_node = onnx.helper.make_node("Conv", ["SIG_OUT", "WGT", "BIAS"], ["OUT"])

graph = helper.make_graph([sigmoid_node, conv_node], "test", [inp], [out], initializer=[wgt_init, bias_init])
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 11)])
graph = onnx.helper.make_graph(
[sigmoid_node, conv_node], "test", [inp], [out], initializer=[wgt_init, bias_init]
)
model = onnx.helper.make_model(graph, opset_imports=[onnx.helper.make_opsetid("", 11)])
onnx.save(model, "model.onnx")

# Quantize model
Expand Down Expand Up @@ -219,7 +220,7 @@ def test_override_validation_nonexisting_tensor(self):
tensor_quant_overrides={"NON_EXISTING": {"rmin": 0.0, "rmax": 0.5}},
)

self.assertTrue("is not present in the model" in str(context.exception))
self.assertIn("is not present in the model", str(context.exception))

def test_override_validation_scale_missing_zp(self):
"""
Expand All @@ -231,7 +232,7 @@ def test_override_validation_scale_missing_zp(self):
tensor_quant_overrides={"SIG_OUT": {"scale": 0.0}},
)

self.assertTrue("Must provide both 'scale' and 'zero_point'" in str(context.exception))
self.assertIn("Must provide both 'scale' and 'zero_point'", str(context.exception))

def test_override_validation_bad_combination(self):
"""
Expand All @@ -243,31 +244,31 @@ def test_override_validation_bad_combination(self):
tensor_quant_overrides={"SIG_OUT": {"scale": 0.0, "zero_point": 0, "rmax": 10.0}},
)

self.assertTrue("option 'rmax' is invalid with 'scale' and 'zero_point'" in str(context.exception))
self.assertIn("option 'rmax' is invalid with 'scale' and 'zero_point'", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"SIG_OUT": {"scale": 0.0, "zero_point": 0, "rmin": 10.0}},
)

self.assertTrue("option 'rmin' is invalid with 'scale' and 'zero_point'" in str(context.exception))
self.assertIn("option 'rmin' is invalid with 'scale' and 'zero_point'", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"SIG_OUT": {"scale": 0.0, "zero_point": 0, "symmetric": True}},
)

self.assertTrue("option 'symmetric' is invalid with 'scale' and 'zero_point'" in str(context.exception))
self.assertIn("option 'symmetric' is invalid with 'scale' and 'zero_point'", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"SIG_OUT": {"scale": 0.0, "zero_point": 0, "reduce_range": True}},
)

self.assertTrue("option 'reduce_range' is invalid with 'scale' and 'zero_point'" in str(context.exception))
self.assertIn("option 'reduce_range' is invalid with 'scale' and 'zero_point'", str(context.exception))

def test_override_invalid_for_initializer(self):
"""
Expand All @@ -279,31 +280,31 @@ def test_override_invalid_for_initializer(self):
tensor_quant_overrides={"WGT": {"scale": 0.0}},
)

self.assertTrue("option 'scale' is invalid for initializers" in str(context.exception))
self.assertIn("option 'scale' is invalid for initializers", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"WGT": {"zero_point": 0}},
)

self.assertTrue("option 'zero_point' is invalid for initializers" in str(context.exception))
self.assertIn("option 'zero_point' is invalid for initializers", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"WGT": {"rmin": 0.0}},
)

self.assertTrue("option 'rmin' is invalid for initializers" in str(context.exception))
self.assertIn("option 'rmin' is invalid for initializers", str(context.exception))

with self.assertRaises(ValueError) as context:
self.perform_qdq_quantization(
"model_validation.onnx",
tensor_quant_overrides={"WGT": {"rmax": 0.0}},
)

self.assertTrue("option 'rmax' is invalid for initializers" in str(context.exception))
self.assertIn("option 'rmax' is invalid for initializers", str(context.exception))


if __name__ == "__main__":
Expand Down

0 comments on commit 8bb660f

Please sign in to comment.