onnxruntime: DirectML error: The parameter is incorrect with KBNet S
Describe the issue
When trying to run KBNet-S (see here) using ONNXRuntime with DirectML, an error occurs during the creation of the session that reads:
2024-07-31 17:26:51.9481346 [E:onnxruntime:, inference_session.cc:2045 onnxruntime::InferenceSession::Initialize::<lambda_deb1d9a98dc3fb814563870e4f4b9f20>::operator ()] Exception during initialization: D:\a\_work\1\s\onnxruntime\core\providers\dml\DmlExecutionProvider\src\AbiCustomRegistry.cpp(519)\onnxruntime_pybind11_state.pyd!00007FF9E88E1B92: (caller: 00007FF9E88485ED) Exception(3) tid(25d60) 80070057 The parameter is incorrect.
This issue does not appear when trying to run using the CPU execution provider.
To reproduce
Create a new virtual environment and run:
pip install torch onnx onnxruntime-directml
Then copy the following files from the KBNet repo:
Open kbnet_s_arch.py and modify the imports as follows:
# Replace this:
from basicsr.models.archs.kb_utils import KBAFunction
from basicsr.models.archs.kb_utils import LayerNorm2d, SimpleGate
# With this:
from kb_utils import KBAFunction, LayerNorm2d, SimpleGate
Next, download sidd.pth into the same directory from here
Finally, create a new file called test.py with the following code:
from kbnet import KBNet_s
import torch
import onnx
import onnxruntime
net = KBNet_s(lightweight=True, ffn_scale=1.5).cpu()
state = torch.load("sidd.pth", weights_only=True)
net.load_state_dict(state["model"])
net.eval()
x = torch.randn((1, 3, 128, 128))
with torch.no_grad():
out = net(x)
torch.onnx.export(net, x, "sidd.onnx", export_params=True, do_constant_folding=True,
input_names=["input"],
output_names=["output"])
onnx_model = onnx.load("sidd.onnx")
onnx.checker.check_model(onnx_model) # Note that this passes, so the exported ONNX file is correct.
ort_session = onnxruntime.InferenceSession("sidd.onnx", providers=["DmlExecutionProvider"]) # <- This line fails!
Run with python3 test.py and see that it fails when trying to create the session. If instead the providers are set to providers=["CpuExecutionProvider"] or providers=["CpuExecutionProvider, DmlExecutionProvider"], the session is created correctly.
Urgency
Medium urgency. This is blocking a research task I’m currently working on and the deadline is coming up fast. I can work around the issue for now by using the CPU, but I need the GPU for performance reasons.
Platform
Windows
OS Version
Windows 11
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.18.1
ONNX Runtime API
Python
Architecture
X64
Execution Provider
DirectML
Execution Provider Library Version
DirectML 1.14.1.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (17 by maintainers)
@fdwr That’s great! Thanks for letting me know. Looking forward to when the fix is available.
Note: This seems to have been fixed after upgrading to DirectML.dll 1.15.1. I have verified that the failures I reported above now all work.
See also #21875 This same issue happens with the following models on a Windows on ARM machine. https://aihub.qualcomm.com/mobile/models/ffnet_54s https://aihub.qualcomm.com/models/esrgan https://aihub.qualcomm.com/models/whisper_tiny_en https://aihub.qualcomm.com/models/mediapipe_hand (MediaPipeHandLandmarkDetector)
Reduced to minimal repro, a single operator .onnx file: minimal-repro.zip
Opened bug for DirectML.dll. I’ll see what the response is, but in the meantime, we should probably attempt to flatten the leading dimensions when >4D here https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/providers/dml/DmlExecutionProvider/src/Operators/DmlOperatorGemm.cpp#L36. Cheers TotK fan.
Thanks - downloading now.Oooh, so there’s a 5D matmul in this model then?
Would you know if there’s an upload on HuggingFace or elsewhere of the direct .onnx model?
I’ve not actually encountered a 5D matmul before in ONNX models, and
DML_GEMM_OPERATOR_DESCcurrently only accepts 4D, requiring either DirectML.dll updates or some flattening to 4D of leading dimensions (if not broadcasted) before calling DirectML.Here’s the output with the debug layer enabled: