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

fix LN evaluator #1131

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Nncase.Evaluator/NN/LayerNorm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private UInt128 GetRingReduceCommunicate(DistributedType distributedType, int[]
#if true
private Tensor LayerNormImpl(Tensor input, Tensor scale, Tensor bias, int axis, float epsilon, bool useMean = true)
{
int outputSize = 1;
int outerSize = 1;
int innerSize = 1;
float[] inputArray = input.ToArray<float>();
float[] outputArray = new float[inputArray.Length];
Expand All @@ -157,23 +157,25 @@ private Tensor LayerNormImpl(Tensor input, Tensor scale, Tensor bias, int axis,

for (int i = 0; i < axis; i++)
{
outputSize *= inShape[i];
outerSize *= inShape[i];
}

for (int i = axis; i < inShape.Length; i++)
{
innerSize *= inShape[i];
}

for (int batch = 0; batch < outputSize; batch++)
for (int batch = 0; batch < outerSize; batch++)
{
float mean1 = 0f;
if (useMean)
{
for (int i = 0; i < innerSize; i++)
{
mean1 += inputArray[(i + (batch * innerSize)) % inputArray.Length] / innerSize;
mean1 += inputArray[(i + (batch * innerSize)) % inputArray.Length];
}

mean1 /= innerSize;
}

float[] sub = new float[innerSize];
Expand All @@ -191,9 +193,11 @@ private Tensor LayerNormImpl(Tensor input, Tensor scale, Tensor bias, int axis,
float mean2 = 0f;
for (int i = 0; i < innerSize; i++)
{
mean2 += pow[i] / innerSize;
mean2 += pow[i];
}

mean2 /= innerSize;

float add = mean2 + epsilon;
float sqrt = (float)System.Math.Sqrt(add);

Expand Down