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

support layernorm channel first(C#) #1204

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/Nncase.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void TargetIndependentPass(IPassManager passManager)
p.Add<Passes.Rules.Neutral.FoldLayerNormPattern3>();
p.Add<Passes.Rules.Neutral.FoldLayerNormPattern4>();
p.Add<Passes.Rules.Neutral.FoldLayerNormPattern5>();
p.Add<Passes.Rules.Neutral.ConvertLayerNormChannelFirstToLast>();
p.Add<Passes.Rules.Neutral.FoldGeluWithScale>();
p.Add<Passes.Rules.Neutral.FoldGeneralGelu>();
p.Add<Passes.Rules.Neutral.FoldSwishPattern1>();
Expand Down
65 changes: 65 additions & 0 deletions src/Nncase.Passes/Rules/Neutral/FoldLayerNorm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Copyright (c) Canaan Inc. All rights reserved.
// Licensed under the Apache license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using Nncase.IR;
using Nncase.IR.F;
using Nncase.IR.Math;
using Nncase.IR.NN;
using Nncase.IR.Tensors;
using Nncase.PatternMatch;
using static Nncase.IR.F.NN;
using static Nncase.PatternMatch.F.Math;
using static Nncase.PatternMatch.F.NN;
using static Nncase.PatternMatch.F.Tensors;
using static Nncase.PatternMatch.Utility;

Expand Down Expand Up @@ -333,3 +338,63 @@ public sealed partial class FoldLayerNormPattern5 : RewriteRule<CallPattern>
return null;
}
}

[RuleGenerator]
public sealed partial class ConvertLayerNormChannelFirstToLast : RewriteRule<CallPattern>
{
public override CallPattern Pattern { get; } =
IsLayerNorm(
"ln",
"_",
_ => true,
IsWildcard("x"),
IsWildcard("scale"),
IsWildcard("bias"));

private static List<int> GetPermWithAxis(long axis, int shapeSize)
{
xhuohai marked this conversation as resolved.
Show resolved Hide resolved
var perm = new List<int>();
for (int i = 0; i < shapeSize; i++)
{
if (i != axis)
{
perm.Add(i);
}
}

perm.Add((int)axis);
return perm;
}

private Expr? GetReplace(LayerNorm ln, Expr x, Expr scale, Expr bias)
{
int axis = ln.Axis;
float eps = ln.Epsilon;
bool useMean = ln.UseMean;
if (axis == x.CheckedShape.Count - 1)
{
return null;
}

var inPerm = GetPermWithAxis(axis, x.CheckedShape.Count);
var outPerm = new List<int>();
for (int i = 0; i < inPerm.Count; i++)
{
outPerm.Add(inPerm[inPerm[i]]);
}

var newScale = scale;
var newBias = bias;

if (scale.CheckedShape.Count != 1 && bias.CheckedShape.Count != 1)
{
int axisGap = x.CheckedShape.Count - scale.CheckedShape.Count;
var constPerm = GetPermWithAxis(axisGap, scale.CheckedShape.Count);

newScale = Tensors.Transpose(scale, constPerm.ToArray());
newBias = Tensors.Transpose(bias, constPerm.ToArray());
}

return Tensors.Transpose(LayerNorm(x.CheckedShape.Count - 1, eps, Tensors.Transpose(x, inPerm.ToArray()), newScale, newBias, useMean), outPerm.ToArray());
}
}
Loading