|
24 | 24 | import torch.nn as nn
|
25 | 25 | from _test_utils.torch_model.deploy_models import BaseDeployModel, get_deploy_models
|
26 | 26 | from _test_utils.torch_model.vision_models import get_tiny_resnet_and_input
|
27 |
| -from onnx.helper import make_graph, make_model, make_node, make_tensor_value_info |
| 27 | +from onnx.helper import ( |
| 28 | + make_graph, |
| 29 | + make_model, |
| 30 | + make_node, |
| 31 | + make_opsetid, |
| 32 | + make_tensor, |
| 33 | + make_tensor_value_info, |
| 34 | +) |
28 | 35 |
|
29 | 36 | from modelopt.onnx.utils import (
|
30 | 37 | get_batch_size_from_bytes,
|
31 | 38 | get_input_names_from_bytes,
|
32 | 39 | get_output_names_from_bytes,
|
33 | 40 | randomize_weights_onnx_bytes,
|
| 41 | + remove_node_training_mode, |
34 | 42 | remove_weights_data,
|
35 | 43 | validate_batch_size,
|
36 | 44 | )
|
@@ -255,3 +263,80 @@ def test_reproducible_random_weights():
|
255 | 263 | onnx_bytes_1 = randomize_weights_onnx_bytes(onnx_bytes_wo_weights)
|
256 | 264 | onnx_bytes_2 = randomize_weights_onnx_bytes(onnx_bytes_wo_weights)
|
257 | 265 | assert onnx_bytes_1 == onnx_bytes_2
|
| 266 | + |
| 267 | + |
| 268 | +def _make_bn_initializer(name: str, shape, value=1.0): |
| 269 | + """Helper to create an initializer tensor for BatchNorm.""" |
| 270 | + data = np.full(shape, value, dtype=np.float32) |
| 271 | + return make_tensor(name, onnx.TensorProto.FLOAT, shape, data.flatten()) |
| 272 | + |
| 273 | + |
| 274 | +def _make_batchnorm_model(bn_node, extra_value_infos=None): |
| 275 | + """Helper to create an ONNX model with a BatchNormalization node.""" |
| 276 | + initializers = [ |
| 277 | + _make_bn_initializer("scale", [3], 1.0), |
| 278 | + _make_bn_initializer("bias", [3], 0.0), |
| 279 | + _make_bn_initializer("mean", [3], 0.0), |
| 280 | + _make_bn_initializer("var", [3], 1.0), |
| 281 | + ] |
| 282 | + |
| 283 | + graph_def = make_graph( |
| 284 | + [bn_node], |
| 285 | + "test_graph", |
| 286 | + [make_tensor_value_info("input", onnx.TensorProto.FLOAT, [1, 3, 224, 224])], |
| 287 | + [make_tensor_value_info("output", onnx.TensorProto.FLOAT, [1, 3, 224, 224])], |
| 288 | + initializer=initializers, |
| 289 | + value_info=extra_value_infos or [], |
| 290 | + ) |
| 291 | + |
| 292 | + return make_model(graph_def, opset_imports=[make_opsetid("", 14)]) |
| 293 | + |
| 294 | + |
| 295 | +def test_remove_node_training_mode_attribute(): |
| 296 | + """Test removal of training_mode attribute from BatchNormalization nodes.""" |
| 297 | + bn_node = make_node( |
| 298 | + "BatchNormalization", |
| 299 | + inputs=["input", "scale", "bias", "mean", "var"], |
| 300 | + outputs=["output"], |
| 301 | + name="bn1", |
| 302 | + training_mode=1, # This attribute should be removed |
| 303 | + ) |
| 304 | + |
| 305 | + model = _make_batchnorm_model(bn_node) |
| 306 | + result_model = remove_node_training_mode(model, "BatchNormalization") |
| 307 | + |
| 308 | + bn_node_result = result_model.graph.node[0] |
| 309 | + assert bn_node_result.op_type == "BatchNormalization" |
| 310 | + |
| 311 | + # Check that training_mode attribute is not present |
| 312 | + attr_names = [attr.name for attr in bn_node_result.attribute] |
| 313 | + assert "training_mode" not in attr_names |
| 314 | + |
| 315 | + |
| 316 | +def test_remove_node_extra_training_outputs(): |
| 317 | + """Test removal of extra training outputs from BatchNormalization nodes.""" |
| 318 | + bn_node = make_node( |
| 319 | + "BatchNormalization", |
| 320 | + inputs=["input", "scale", "bias", "mean", "var"], |
| 321 | + outputs=["output", "saved_mean", "saved_inv_std"], # Extra training outputs |
| 322 | + name="bn1", |
| 323 | + training_mode=1, |
| 324 | + ) |
| 325 | + |
| 326 | + value_infos = [ |
| 327 | + make_tensor_value_info("saved_mean", onnx.TensorProto.FLOAT, [3]), |
| 328 | + make_tensor_value_info("saved_inv_std", onnx.TensorProto.FLOAT, [3]), |
| 329 | + ] |
| 330 | + |
| 331 | + model = _make_batchnorm_model(bn_node, extra_value_infos=value_infos) |
| 332 | + result_model = remove_node_training_mode(model, "BatchNormalization") |
| 333 | + |
| 334 | + # Verify only first output remains |
| 335 | + bn_node_result = result_model.graph.node[0] |
| 336 | + assert len(bn_node_result.output) == 1 |
| 337 | + assert bn_node_result.output[0] == "output" |
| 338 | + |
| 339 | + # Verify value_info entries for removed outputs are cleaned up |
| 340 | + value_info_names = [vi.name for vi in result_model.graph.value_info] |
| 341 | + assert "saved_mean" not in value_info_names |
| 342 | + assert "saved_inv_std" not in value_info_names |
0 commit comments