onnxruntime: Mixed Precision ValueError: validation failed for model with all nodes in node_block_list
Describe the issue
Hey guys,
Hope to find you well. I’ve opened tons of issues but I’ve never received replies with solutions, I just want to convert a model to mixed precision. The doc is wrong and the current code doesn’t work for me.
Related issues:
https://github.com/microsoft/onnxruntime/issues/13833 https://github.com/NVIDIA/TensorRT/issues/2542 https://github.com/microsoft/onnxconverter-common/issues/251
It seems l am the only person in the community trying to just run a mixed precision model in ONNX and it looks like there is no documented way to do so.
For instance, in the below snippet, converting the model to float16 only results in an output difference (mean) of 0.02 which is very big
Thanks in advance!
To reproduce
Using torch=1.13.1
import onnxruntime as ort
import torch
from torchvision.models import ConvNeXt_Small_Weights, convnext_small
from onnxconverter_common import float16
from onnxconverter_common.auto_mixed_precision import auto_convert_mixed_precision
import onnx
model_name = "model.onnx"
model = convnext_small(ConvNeXt_Small_Weights.IMAGENET1K_V1).eval().cuda()
x = torch.randn(1, 3, 224, 224, device="cuda")
# Export the model
torch.onnx.export(
model, # model being run
x, # model input (or a tuple for multiple inputs)
model_name, # where to save the model (can be a file or file-like object)
opset_version=16,
export_params=True, # store the trained parameter weights inside the model file
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=["image"], # the model's input names
output_names=["output"], # the model's output names
dynamic_axes={
"image": {0: "batch_size"}, # variable length axes
"output": {0: "batch_size"},
},
)
model = onnx.load("model.onnx")
# model_fp16 = float16.convert_float_to_float16(model)
# onnx.save(model_fp16, "model.fp16.onnx")
model_fp16 = auto_convert_mixed_precision(model, { "image" : x.cpu().numpy() }, None, rtol=0.01, atol=0.001, keep_io_types=True)
# auto_convert_mixed_precision_model_path("./model.onnx", { "image" : x.cpu().numpy() }, "./model_fp16.onnx", provider=["CUDAExecutionProvider"], rtol=0.01, atol=0.001, keep_io_types=True, verbose=False)
onnx.save(model_fp16, "model.fp16.onnx")
# let's check
print("Checking")
x = torch.randn(1, 3, 224, 224, device="cuda", dtype=torch.float16)
ort_session = ort.InferenceSession("model.fp16.onnx", providers=["CUDAExecutionProvider"])
outputs = ort_session.run(None, {"image": x.cpu().numpy()})
print(outputs[0].shape, outputs[0].dtype)
Will fail with stack trace
...
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:53: UserWarning: the float32 number -3.575223672669381e-08 will be truncated to -1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(neg_max, -min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:43: UserWarning: the float32 number 4.082721716258675e-08 will be truncated to 1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(pos_min, min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:53: UserWarning: the float32 number -7.970152182679158e-08 will be truncated to -1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(neg_max, -min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:43: UserWarning: the float32 number 4.87943907501176e-10 will be truncated to 1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(pos_min, min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:53: UserWarning: the float32 number -1.074658939614892e-08 will be truncated to -1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(neg_max, -min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:43: UserWarning: the float32 number 6.295249477261677e-08 will be truncated to 1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(pos_min, min_positive_val))
/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/float16.py:53: UserWarning: the float32 number -7.707149052293971e-08 will be truncated to -1e-07
warnings.warn("the float32 number {} will be truncated to {}".format(neg_max, -min_positive_val))
False
Traceback (most recent call last):
File "/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/export_to_onnx.py", line 32, in <module>
model_fp16 = auto_convert_mixed_precision(model, { "image" : x.cpu().numpy() }, None, rtol=0.01, atol=0.001, keep_io_types=True)
File "/home/zuppif/Documents/medium/how-to-docker+onnx+tensorrt/.venv/lib/python3.9/site-packages/onnxconverter_common/auto_mixed_precision.py", line 81, in auto_convert_mixed_precision
raise ValueError("validation failed for model with all nodes in node_block_list")
ValueError: validation failed for model with all nodes in node_block_list
Converting directly to float16 results in an output difference of 0.02
import onnxruntime as ort
import torch
from torchvision.models import ConvNeXt_Small_Weights, convnext_small
from onnxconverter_common import float16
from onnxconverter_common.auto_mixed_precision import auto_convert_mixed_precision
import onnx
import numpy as np
model_name = "model.onnx"
model = convnext_small(ConvNeXt_Small_Weights.IMAGENET1K_V1).eval().cuda()
x = torch.randn(1, 3, 224, 224, device="cuda")
# let's store the outputs to compare the results
with torch.no_grad():
out_torch = model(x)
# Export the model
torch.onnx.export(
model, # model being run
x, # model input (or a tuple for multiple inputs)
model_name, # where to save the model (can be a file or file-like object)
opset_version=16,
export_params=True, # store the trained parameter weights inside the model file
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=["image"], # the model's input names
output_names=["output"], # the model's output names
dynamic_axes={
"image": {0: "batch_size"}, # variable length axes
"output": {0: "batch_size"},
},
)
model = onnx.load("model.onnx")
model_fp16 = float16.convert_float_to_float16(model)
onnx.save(model_fp16, "model.fp16.onnx")
# let's check
print("Checking")
ort_session = ort.InferenceSession("model.fp16.onnx", providers=["CUDAExecutionProvider"])
outputs = ort_session.run(None, {"image": x.half().cpu().numpy()})
print(outputs[0].shape, outputs[0].dtype)
# checking the difference
print(np.abs(out_torch.cpu().numpy() - outputs[0]).mean())
Urgency
No response
Platform
Linux
OS Version
22.04
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.13.0
ONNX Runtime API
Python
Architecture
X64
Execution Provider
Default CPU, CUDA
Execution Provider Library Version
cuda 11.7
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 1
- Comments: 16 (5 by maintainers)
To my understanding, to this day even if some people shows benchmarks, NOBODY was ever able to run a model in mixed precision in ONNX and it is crazy to have such little support and documentation (but thanks a lot for everything you are doing so far) but at least state somewhere that is not possible or it’s unstable, so we don’t waste hours - thanks a lot
Any update on this? I am also facing the same error, unable to convert fp32 model to mixed_precision