Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ONNXModifier for optimising the ONNX model before converting for RVC4 execution #55

Merged
merged 15 commits into from
Dec 13, 2024

Conversation

ptoupas
Copy link
Contributor

@ptoupas ptoupas commented Dec 10, 2024

The ONNXModifier performs a series of onnx modifications that were found to be beneficial in terms of performance on RVC4, without altering the final outputs of the model. Below is the list of optimisations/modifications introduced:

  • Substituting division layers for multiplication layers (when possible).
  • Substituting subtraction layers for addition layers (when possible).
  • Fusing add/mul layers that come right after a conv layer, into a BatchNormalization layer, which is then fused into the conv (when possible).
  • Fusing add/mul layers that precede a conv layer into the conv layer by incorporating the addition and multiplication into the conv's weights and biases when possible.
  • Removing the Split and Concat layers at the start of the model by reordering the covolutional weights in first conv layer when such transformation is possible.

@ptoupas ptoupas requested a review from a team as a code owner December 10, 2024 07:41
@ptoupas ptoupas requested review from kozlov721, klemen1999, tersekmatija, conorsim and aljazkonec1 and removed request for a team December 10, 2024 07:41
Copy link
Collaborator

@kozlov721 kozlov721 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM in general, just left a few comments.

modelconverter/utils/onnx_tools.py Outdated Show resolved Hide resolved
modelconverter/packages/rvc4/exporter.py Outdated Show resolved Hide resolved
modelconverter/utils/onnx_tools.py Outdated Show resolved Hide resolved
import onnx
import onnx_graphsurgeon as gs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use graphsurgeon to also simplify the code in onnx_attach_normalization_to_inputs? Or add it to the ONNXModifier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I would like the onnx_attach_normalization_to_inputs to be another function of ONNXModifier, I think its better to leave this one outside of the class. The reason is that its the fallback solution in case the optimisations fail or do not produce the correct results compared to the "base" model which is produced by this exact function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the onnx_attach_normalization_to_inputs alone can be changed to use graphsurgeon instead, yeah.

requirements-dev.txt Outdated Show resolved Hide resolved
Copy link

@aljazkonec1 aljazkonec1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, some comments mostly regarding where and what operations to check.

Copy link
Collaborator

@klemen1999 klemen1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

modelconverter/packages/rvc4/exporter.py Outdated Show resolved Hide resolved
modelconverter/utils/onnx_tools.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@klemen1999 klemen1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

modelconverter/utils/onnx_tools.py Outdated Show resolved Hide resolved
@ptoupas ptoupas merged commit deac688 into main Dec 13, 2024
6 checks passed
@ptoupas ptoupas deleted the feat/rvc4-related-onnx-optimisations branch December 13, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants