|
| 1 | +# Set pytest parameters |
| 2 | +import pytest |
| 3 | + |
| 4 | +# Numpy for handling simulation of tensor operations |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# Helper for creating ONNX nodes |
| 8 | +from onnx import NodeProto, TensorProto # noqa |
| 9 | +from onnx import helper as oh # noqa |
| 10 | + |
| 11 | +# QONNX wrapper of ONNX model graphs |
| 12 | +from qonnx.core.modelwrapper import ModelWrapper # noqa |
| 13 | + |
| 14 | +# Execute QONNX model graphs |
| 15 | +from qonnx.core.onnx_exec import execute_onnx # noqa |
| 16 | + |
| 17 | +# QONNX quantizer function modeling the behavior of the Quant operator |
| 18 | +from qonnx.custom_op.general.quant import quant as quant_fn # noqa |
| 19 | + |
| 20 | +# QONNX graph transformations for inferring datatypes and shapes required by |
| 21 | +# test setup |
| 22 | +from qonnx.transformation.infer_datatypes import InferDataTypes # noqa |
| 23 | +from qonnx.transformation.infer_shapes import InferShapes # noqa |
| 24 | + |
| 25 | +# Graph transformation to be tested: Transposes the initializers to Quantizer if |
| 26 | +# ALL inputs are initializers |
| 27 | +from qonnx.transformation.quant_constant_folding import FoldTransposeIntoQuantInit # noqa |
| 28 | + |
| 29 | +# QONNX utility for creating models from ONNX graphs |
| 30 | +from qonnx.util.basic import qonnx_make_model # noqa |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("quant_init", [True, False]) |
| 34 | +@pytest.mark.parametrize("signed", [0, 1]) |
| 35 | +@pytest.mark.parametrize("narrow", [0, 1]) |
| 36 | +@pytest.mark.parametrize("rounding_mode", ["ROUND"]) |
| 37 | +@pytest.mark.parametrize("shape", [(16, 8, 12)]) |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "perm", |
| 40 | + [ |
| 41 | + # All axis permutations |
| 42 | + (0, 1, 2), |
| 43 | + (0, 2, 1), |
| 44 | + (1, 0, 2), |
| 45 | + (1, 2, 0), |
| 46 | + (2, 0, 1), |
| 47 | + (2, 1, 0), |
| 48 | + ], |
| 49 | +) |
| 50 | +@pytest.mark.parametrize("scale", [0.01]) |
| 51 | +@pytest.mark.parametrize("zeropoint", [0]) |
| 52 | +@pytest.mark.parametrize("bitwidth", [8]) |
| 53 | +# Tests the FoldTransposeIntoQuantInit transformation |
| 54 | +def test_fold_transpose_into_quant_init(quant_init, signed, narrow, rounding_mode, shape, perm, scale, zeropoint, bitwidth): |
| 55 | + # Prepare the quantizer node attributes and input/output lists |
| 56 | + node_attrs = { |
| 57 | + # Type of the operation |
| 58 | + "op_type": "Quant", |
| 59 | + # This operator type is defined within QONNX |
| 60 | + "domain": "qonnx.custom_op.general", |
| 61 | + # List the inputs to the operator in order |
| 62 | + # Note: The proper input followed by initializers configuring the |
| 63 | + # quantizer |
| 64 | + "inputs": ["input", "scale", "zeropoint", "bitwidth"], |
| 65 | + # List the outputs of the operator in order |
| 66 | + # Note: Intermediate feeds to the next operator input |
| 67 | + "outputs": ["intermediate"], |
| 68 | + # Whether the quantization interval should be signed or not |
| 69 | + # (e.g. at 8b unsigned=[0, 255] vs signed=[-128, 127]) |
| 70 | + "signed": signed, |
| 71 | + # When signed=1, whether to use narrow range or not |
| 72 | + # (e.g. at 8b regular=[-128, 127] vs narrow=[-127, 127]) |
| 73 | + "narrow": narrow, |
| 74 | + # The rounding mode, which is used for the quant function |
| 75 | + "rounding_mode": rounding_mode, |
| 76 | + } |
| 77 | + # Create a dummy quantizer node |
| 78 | + quant = oh.make_node(**node_attrs, name="Quant") |
| 79 | + # Attach a Transpose operation to the quantizer |
| 80 | + transpose = oh.make_node("Transpose", ["intermediate"], ["output"], name="Transpose", perm=perm) |
| 81 | + # Derive the transposed shape |
| 82 | + transposed_shape = np.transpose(np.zeros(shape), perm).shape |
| 83 | + # Create tensor information for the input, intermediate and output tensor |
| 84 | + x = oh.make_tensor_value_info("input", TensorProto.FLOAT, shape) # noqa |
| 85 | + y = oh.make_tensor_value_info("output", TensorProto.FLOAT, transposed_shape) |
| 86 | + # Create the initializer tensors for quantizer parameters |
| 87 | + s = oh.make_tensor_value_info("scale", TensorProto.FLOAT, (1,)) |
| 88 | + z = oh.make_tensor_value_info("zeropoint", TensorProto.FLOAT, (1,)) |
| 89 | + b = oh.make_tensor_value_info("bitwidth", TensorProto.FLOAT, (1,)) |
| 90 | + # Create the graph connecting the nodes and tensors |
| 91 | + graph = oh.make_graph( |
| 92 | + [quant, transpose], |
| 93 | + "quant-transpose", |
| 94 | + [x, s, z, b], |
| 95 | + [y], |
| 96 | + ) |
| 97 | + # Wrap the graph in an QONNX model wrapper |
| 98 | + model = ModelWrapper(qonnx_make_model(graph, producer_name="qonnx-tests")) |
| 99 | + # Add the actual initializers to the initializer tensors |
| 100 | + model.set_initializer("scale", np.array(scale)) |
| 101 | + model.set_initializer("zeropoint", np.array(zeropoint)) |
| 102 | + model.set_initializer("bitwidth", np.array(bitwidth)) |
| 103 | + # Prepare the model graph by inferring all missing shape and datatype |
| 104 | + # information |
| 105 | + model = model.transform(InferShapes()) |
| 106 | + model = model.transform(InferDataTypes()) |
| 107 | + |
| 108 | + # Get a random dummy input for testing |
| 109 | + x = np.random.rand(*shape) # noqa |
| 110 | + # Fill the execution context with dummy input data |
| 111 | + context = {"input": x} |
| 112 | + |
| 113 | + # Some test cases even turn the input into an initializer |
| 114 | + if quant_init: |
| 115 | + # Turn the model input into an initializer |
| 116 | + model.set_initializer("input", x) |
| 117 | + # Clear the execution context removing the input as it is now baked into |
| 118 | + # the model graph |
| 119 | + context = {} |
| 120 | + |
| 121 | + # Run the transformation to be tested |
| 122 | + model = model.transform(FoldTransposeIntoQuantInit()) |
| 123 | + # Verify that shape and datatype inference still works |
| 124 | + # Note: This has been an issue, please see |
| 125 | + # https://github.com/fastmachinelearning/qonnx/issues/77 |
| 126 | + model = model.transform(InferShapes()) |
| 127 | + model = model.transform(InferDataTypes()) |
| 128 | + |
| 129 | + # For the case of quant-initializers there must not be a Transpose left |
| 130 | + # after transforming and contrariwise, the Transpose must remain in place if |
| 131 | + # there is non-initializer input. |
| 132 | + assert quant_init != ("Transpose" in [n.op_type for n in model.graph.node]) |
| 133 | + |
| 134 | + # Execute the ONNX model |
| 135 | + o_produced = execute_onnx(model, context)["output"] |
| 136 | + # Use numpy and QONNX quantizer to generate expectation |
| 137 | + o_expected = np.transpose( |
| 138 | + quant_fn(x, np.array(scale), np.array(zeropoint), np.array(bitwidth), signed, narrow, rounding_mode), perm |
| 139 | + ) |
| 140 | + |
| 141 | + # The output must match the "manual" execution using numpy |
| 142 | + assert np.allclose(o_produced, o_expected) |
0 commit comments