Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modelopt/onnx/autocast/precisionconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class InitializerConsumerTracker:
OP_TYPES_NOT_SUPPORTED_IN_LOW_PRECISION = ["Upsample", "NonMaxSuppression", "Celu"]

# Mapping of op types to indices of inputs that should not be converted to low precision.
SKIP_LOW_PRECISION_MAPPING_FP16 = {"Resize": {2}}
SKIP_LOW_PRECISION_MAPPING_FP16 = {"Resize": {1, 2}} # {X, roi (opt), scales (opt)}
SKIP_LOW_PRECISION_MAPPING_BF16 = {"Resize": {1, 2}}


Expand Down
83 changes: 83 additions & 0 deletions tests/_test_utils/onnx/lib_test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,3 +924,86 @@ def build_conv_isinf_model(opset_version=13):
onnx.checker.check_model(model_inferred)

return model_inferred


def build_conv_resize_model():
# Define your model inputs and outputs
input_names = ["input_0"]
output_names = ["output_0"]
input_shapes = [(1, 288, 32, 32)]
output_shapes = [(1, 16, 64, 64)]

inputs = [
helper.make_tensor_value_info(input_name, onnx.TensorProto.FLOAT, input_shape)
for input_name, input_shape in zip(input_names, input_shapes)
]
outputs = [
helper.make_tensor_value_info(output_name, onnx.TensorProto.FLOAT, output_shape)
for output_name, output_shape in zip(output_names, output_shapes)
]

# Create the ONNX graph with the nodes
nodes = [
helper.make_node(
op_type="Conv",
inputs=["input_0", "weights_1"],
outputs=["conv1_conv/Conv2D:0"],
name="conv1_conv/Conv2D",
dilations=[1, 1],
group=1,
kernel_shape=[1, 1],
pads=[0, 0, 0, 0],
strides=[1, 1],
),
helper.make_node(
op_type="Resize",
inputs=[
"conv1_conv/Conv2D:0",
"resize_roi_scales",
"resize_roi_scales",
"resize_sizes",
],
outputs=["output_0"],
name="resize1_resize/Resize",
coordinate_transformation_mode="asymmetric",
cubic_coeff_a=-0.75,
mode="nearest",
nearest_mode="floor",
),
]

# Create the ONNX initializers
initializers = [
helper.make_tensor(
name="weights_1",
data_type=onnx.TensorProto.FLOAT,
dims=(16, 288, 1, 1),
vals=np.random.uniform(low=0.5, high=1.0, size=16 * 288 * 1 * 1),
),
helper.make_tensor(
name="resize_roi_scales",
data_type=onnx.TensorProto.FLOAT,
dims=(0,),
vals=[],
),
helper.make_tensor(
name="resize_sizes",
data_type=onnx.TensorProto.INT64,
dims=(4,),
vals=[1, 16, 64, 64],
),
]

# Create the ONNX graph with the nodes and initializers
graph = helper.make_graph(nodes, "conv_resize", inputs, outputs, initializer=initializers)

# Create the ONNX model
model = helper.make_model(graph)
model.opset_import[0].version = 13
model.ir_version = 10

# Check the ONNX model
model_inferred = onnx.shape_inference.infer_shapes(model)
onnx.checker.check_model(model_inferred)

return model_inferred
22 changes: 21 additions & 1 deletion tests/unit/onnx/autocast/test_autocast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import onnx
import onnx_graphsurgeon as gs
import pytest
from _test_utils.onnx.lib_test_models import build_conv_isinf_model
from _test_utils.onnx.lib_test_models import build_conv_isinf_model, build_conv_resize_model

import modelopt.onnx.autocast.utils as utils
import modelopt.onnx.utils as onnx_utils
Expand Down Expand Up @@ -190,6 +190,26 @@ def test_conv_isinf_conversion(tmp_path, opset_version):
assert assert_input_precision(isinf_nodes, dtype=supported_dtype)


def test_conv_resize_conversion(tmp_path):
onnx_model = build_conv_resize_model()
onnx_path = os.path.join(tmp_path, "conv_resize_model.onnx")
onnx.save(onnx_model, onnx_path)

# Convert the model
converted_model = convert_to_mixed_precision(onnx_path=onnx_path)

# Output model should be produced in the same tmp_path
output_onnx_path = onnx_path.replace(".onnx", ".fp16.onnx")
onnx.save(converted_model, output_onnx_path)

# Load the output model and check QDQ node placements
graph = gs.import_onnx(converted_model)

# Check that Conv is converted
conv_nodes = [n for n in graph.nodes if "Conv" in n.op]
assert assert_input_precision(conv_nodes)


@pytest.mark.parametrize("target_opset", [13, 17, 19, 21])
def test_opset_parameter(temp_model_path, target_opset):
"""Test that the opset parameter correctly sets the output model's opset version."""
Expand Down