Make EdgeNeXt onnx-exportable. #1385
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When I convert EdgeNeXt to ONNX, the converted model throws an error at inference time. This occurs when I use the 3rd or 4th features of EdgeNeXt.
I tested the following in linux and mac environment with various python & opset version, and they all returned the same error messages. So this problem seems not environment-dependent, but specific to pytorch->onnx conversion.
Error message:
After some probe I found that this error comes from
F.normalizeinCrossCovarianceAttn, especially within its clip operation by eps, and it was resolved when I wrote a naive normalize function without using torch.nn.functional.I also modified the line
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 4, 1)according to the above warning.I have verified that
np.testing.assert_allclosewithrtol=1e-03, atol=1e-05according to this),The last correspondence was checked using the following (dirty) script. By the way my testing environment is python=3.10.5, torch=1.12.0, onnxruntime=1.12.0.
I hope this will help others who want to deploy this model in other formats.