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

GNNE-1714:add norm op name #1127

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Nncase.Passes/Rules/Neutral/AddPreProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,28 @@ protected override Task<IRModule> RunCoreAsync(IRModule module, RunPassContext o
// Normalization
if (mean.Length != 0)
{
newInput = mean.Length switch
switch (mean.Length)
{
3 when inputShape.Length == 4 => (newInput - Tensor.From(mean, new[] { 1, mean.Length, 1, 1 })) /
Tensor.From(std, new[] { 1, std.Length, 1, 1 }),
_ => (newInput - Tensor.From(new float[] { mean[0] }, new[] { 1 })) /
Tensor.From(new float[] { std[0] }, new[] { 1 }),
};
case 3 when inputShape.Length == 4:
var meanCall = (Expr)Tensor.From(mean, new[] { 1, mean.Length, 1, 1 });
meanCall.Metadata.OutputNames = new[] { "Mean" };
var stdCall = (Expr)Tensor.From(std, new[] { 1, std.Length, 1, 1 });
stdCall.Metadata.OutputNames = new[] { "Std" };
var subMean = (newInput - meanCall).With(metadata: new IRMetadata() { OutputNames = new[] { input.Metadata.OutputNames?[0] + "_SubMean" } });
var divStd = (subMean / stdCall).With(metadata: new IRMetadata() { OutputNames = new[] { input.Metadata.OutputNames?[0] + "_DivStd" } });
newInput = divStd;
break;

default:
meanCall = (Expr)Tensor.From(new float[] { mean[0] }, new[] { 1 });
meanCall.Metadata.OutputNames = new[] { "Mean" };
FusionBolt marked this conversation as resolved.
Show resolved Hide resolved
stdCall = (Expr)Tensor.From(new float[] { std[0] }, new[] { 1 });
stdCall.Metadata.OutputNames = new[] { "Std" };
subMean = (newInput - meanCall).With(metadata: new IRMetadata() { OutputNames = new[] { input.Metadata.OutputNames?[0] + "_SubMean" } });
divStd = (subMean / stdCall).With(metadata: new IRMetadata() { OutputNames = new[] { input.Metadata.OutputNames?[0] + "_DivStd" } });
newInput = divStd;
break;
}

// newInput = Binary(BinaryOp.Div, Binary(BinaryOp.Sub, newInput, Tensor.From(mean, new []{1,3,1,1})), Const.FromTensor(std) );
}
Expand Down
Loading