-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Relax][Frontend][ONNX] Add GroupNormalization support #19907
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
Open
napronald
wants to merge
1
commit into
apache:main
Choose a base branch
from
napronald:relax-onnx-group-normalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3893,6 +3893,117 @@ def _impl_v23(cls, bb, inputs, attr, params): | |||||||||||||||||||||||||||||||||||||||||
| return output | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class GroupNormalization(OnnxOpConverter): | ||||||||||||||||||||||||||||||||||||||||||
| """Converts an onnx GroupNormalization node into an equivalent Relax expression""" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||
| def _impl_v18(cls, bb, inputs, attr, params): | ||||||||||||||||||||||||||||||||||||||||||
| data = inputs[0] | ||||||||||||||||||||||||||||||||||||||||||
| scale = inputs[1] | ||||||||||||||||||||||||||||||||||||||||||
| bias = inputs[2] | ||||||||||||||||||||||||||||||||||||||||||
| num_groups = attr["num_groups"] | ||||||||||||||||||||||||||||||||||||||||||
| epsilon = attr.get("epsilon", 1e-05) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ndim = _get_known_tensor_rank(data) | ||||||||||||||||||||||||||||||||||||||||||
| if ndim is None: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("GroupNormalization requires a statically known input rank.") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ty = data.ty | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(ty, relax.TensorType) or len(ty.shape) < 2: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| "GroupNormalization-18 requires a statically typed input with rank >= 2." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if num_groups <= 0: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| f"GroupNormalization requires num_groups to be positive, got {num_groups}." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| channel_dim = ty.shape[1] | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(channel_dim, tirx.IntImm): | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| "GroupNormalization-18 requires a statically known channel count " | ||||||||||||||||||||||||||||||||||||||||||
| "to expand per-group scale/bias to per-channel." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| channels = int(channel_dim) | ||||||||||||||||||||||||||||||||||||||||||
| if channels % num_groups != 0: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| f"GroupNormalization requires num_groups to divide channel count, " | ||||||||||||||||||||||||||||||||||||||||||
| f"but got C={channels} and num_groups={num_groups}." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| channels_per_group = channels // num_groups | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| scale = relax.op.reshape(scale, [num_groups, 1]) | ||||||||||||||||||||||||||||||||||||||||||
| scale = relax.op.broadcast_to(scale, [num_groups, channels_per_group]) | ||||||||||||||||||||||||||||||||||||||||||
| scale = relax.op.reshape(scale, [channels]) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| bias = relax.op.reshape(bias, [num_groups, 1]) | ||||||||||||||||||||||||||||||||||||||||||
| bias = relax.op.broadcast_to(bias, [num_groups, channels_per_group]) | ||||||||||||||||||||||||||||||||||||||||||
| bias = relax.op.reshape(bias, [channels]) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3938
to
+3944
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| axes = list(range(2, ndim)) | ||||||||||||||||||||||||||||||||||||||||||
| return relax.op.nn.group_norm( | ||||||||||||||||||||||||||||||||||||||||||
| data, scale, bias, num_groups, channel_axis=1, axes=axes, epsilon=epsilon | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||
| def _impl_v21(cls, bb, inputs, attr, params): | ||||||||||||||||||||||||||||||||||||||||||
| data = inputs[0] | ||||||||||||||||||||||||||||||||||||||||||
| scale = inputs[1] | ||||||||||||||||||||||||||||||||||||||||||
| bias = inputs[2] | ||||||||||||||||||||||||||||||||||||||||||
| num_groups = attr["num_groups"] | ||||||||||||||||||||||||||||||||||||||||||
| epsilon = attr.get("epsilon", 1e-05) | ||||||||||||||||||||||||||||||||||||||||||
| stash_type = attr.get("stash_type", 1) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if stash_type not in [0, 1]: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| f"GroupNormalization currently only supports stash_type 0 and 1, " | ||||||||||||||||||||||||||||||||||||||||||
| f"but got stash_type={stash_type}." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ndim = _get_known_tensor_rank(data) | ||||||||||||||||||||||||||||||||||||||||||
| if ndim is None: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("GroupNormalization requires a statically known input rank.") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ty = data.ty | ||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(ty, relax.TensorType) or len(ty.shape) < 2: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| "GroupNormalization requires a statically typed input with rank >= 2." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if num_groups <= 0: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| f"GroupNormalization requires num_groups to be positive, got {num_groups}." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| channel_dim = ty.shape[1] | ||||||||||||||||||||||||||||||||||||||||||
| if isinstance(channel_dim, tirx.IntImm): | ||||||||||||||||||||||||||||||||||||||||||
| channels = int(channel_dim) | ||||||||||||||||||||||||||||||||||||||||||
| if channels % num_groups != 0: | ||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||
| f"GroupNormalization requires num_groups to divide channel count, " | ||||||||||||||||||||||||||||||||||||||||||
| f"but got C={channels} and num_groups={num_groups}." | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| axes = list(range(2, ndim)) | ||||||||||||||||||||||||||||||||||||||||||
| input_dtype = ty.dtype | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if stash_type == 1 and input_dtype != "float32": | ||||||||||||||||||||||||||||||||||||||||||
| data = relax.op.astype(data, "float32") | ||||||||||||||||||||||||||||||||||||||||||
| scale = relax.op.astype(scale, "float32") | ||||||||||||||||||||||||||||||||||||||||||
| bias = relax.op.astype(bias, "float32") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| output = relax.op.nn.group_norm( | ||||||||||||||||||||||||||||||||||||||||||
| data, scale, bias, num_groups, channel_axis=1, axes=axes, epsilon=epsilon | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if stash_type == 1 and input_dtype != "float32": | ||||||||||||||||||||||||||||||||||||||||||
| output = relax.op.astype(output, input_dtype) | ||||||||||||||||||||||||||||||||||||||||||
| return output | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class ReduceMax(OnnxOpConverter): | ||||||||||||||||||||||||||||||||||||||||||
| """Converts an onnx ReduceMax node into an equivalent Relax expression.""" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -5273,6 +5384,7 @@ def _get_convert_map(): | |||||||||||||||||||||||||||||||||||||||||
| "BatchNormalization": BatchNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "LayerNormalization": LayerNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "RMSNormalization": RMSNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "GroupNormalization": GroupNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "SkipLayerNormalization": SkipLayerNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "EmbedLayerNormalization": EmbedLayerNormalization, | ||||||||||||||||||||||||||||||||||||||||||
| "InstanceNormalization": InstanceNormalization, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
get_constantto resolvescaleandbiasto constants if they are initializers. This allows us to perform the per-group to per-channel expansion at import time using NumPy, avoiding redundantreshapeandbroadcast_tooperators in the Relax graph.