Skip to content

Commit

Permalink
Merge branch 'master' into kernel_bug_fixv2
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenhua authored Aug 24, 2023
2 parents ff70c6b + 43bb878 commit d0ef898
Show file tree
Hide file tree
Showing 23 changed files with 846 additions and 582 deletions.
4 changes: 3 additions & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pytest-xdist
pyyaml
pythonnet==3.0.1
clr_loader==0.2.4
toml==0.10.2
toml==0.10.2
pandas
tabulate
96 changes: 15 additions & 81 deletions src/Nncase.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Nncase.Passes.Rules.ShapeExpr;
using Nncase.Passes.Transforms;
using Nncase.Quantization;
using static Nncase.Passes.Rules.ShapeBucket.ShapeBucketRegister;
using FoldConstCall = Nncase.Passes.Rules.Neutral.FoldConstCall;

namespace Nncase.Compiler;
Expand Down Expand Up @@ -92,9 +93,9 @@ public void AddPreAndPostProcess(IPassManager passManager)
public void TargetIndependentPass(IPassManager passManager)
{
passManager.AddWithName<DataflowPass>("ReshapeMatMul").Configure(p =>
{
p.Add<Passes.Rules.Neutral.ReshapeMatMul>();
});
{
p.Add<Passes.Rules.Neutral.ReshapeMatMul>();
});

passManager.AddWithName<DataflowPass>("SqueezeShape").Configure(p =>
{
Expand All @@ -118,7 +119,6 @@ public void TargetIndependentPass(IPassManager passManager)
p.Add<Passes.Rules.Neutral.FocusFull>();
p.Add<Passes.Rules.Neutral.ReshapeMatMul>();
});

passManager.AddWithName<EGraphRulesPass>("NeutralOptimizeTranspose").Configure(p =>
{
p.Add<Passes.Rules.Neutral.FoldConstCall>();
Expand Down Expand Up @@ -179,82 +179,25 @@ public void TargetIndependentPass(IPassManager passManager)

public void RegisterShapeBucket(IPassManager p)
{
var singleVar = _compileSession.CompileOptions.ShapeBucketOptions.VarMap.Values.SelectMany(x => x).OfType<Var>().ToHashSet().Count <= 1;

void MergeOp(IPassManager iPassManager)
{
if (!singleVar)
{
return;
}

iPassManager.AddWithName<DataflowPass>("MergeNextCall").Configure(c =>
{
c.Add<MergeNextCallToFusion>();
c.Add<MergeNextMarkerToFusion>();
});
iPassManager.AddWithName<DataflowPass>("MergePrevCall").Configure(c =>
{
c.Add<MergePrevCallToFusion>();
c.Add<MergePrevMarkerToFusion>();
});
}

if (!_compileSession.CompileOptions.ShapeBucketOptions.Enable)
var options = _compileSession.CompileOptions.ShapeBucketOptions;
var singleVar = options.VarMap.Values.SelectMany(x => x).OfType<Var>().ToHashSet().Count <= 1;
if (!options.Enable)
{
return;
}

CheckShapeBucketOptions(options);
ToFusion(p);

MergeOp(p);
LostToFusion(p, singleVar);
MergeOp(p);
ClearMarker(p);

p.AddWithName<DataflowPass>("LostToFusion").Configure(c =>
{
c.Add<TransposeToFusion>();
c.Add<UnaryToFusion>();
c.Add<ActToFusion>();
if (singleVar)
{
c.Add<BinaryToFusion>();
}
});

// MergeOp(p);
p.AddWithName<DataflowPass>("ClearSomeMarker").Configure(p =>
{
p.Add<ClearFusionOuterMarker>();
p.Add<RemoveMarker>();
});

if (singleVar)
{
// do twice
p.AddWithName<MergeBucketFusion>("MergeFusion");
MergeOp(p);
p.AddWithName<MergeBucketFusion>("MergeFusion");
MergeOp(p);
}

p.AddWithName<DataflowPass>("FusionBucket").Configure(c =>
{
c.Add<FusionBucket>();
});
// MergeFusion(p, singleVar);
Bucket(p);

p.AddWithName<DataflowPass>("Simplify").Configure(c =>
{
c.Add<FoldStackGetItem>();
c.Add<FoldConstCall>();
c.Add<FoldShapeOf>();
c.Add<FoldTwoReshapes>();
c.Add<FoldTwoCasts>();
c.Add<FoldTwoSlices>();
c.Add<FoldNopBinary>();
c.Add<FoldNopCast>();
c.Add<FoldNopReshape>();
c.Add<FoldNopSlice>();
c.Add<FoldIf>();
});
// Rebuild(p);
Simplify(p);
}

public void ClearFixShape(IPassManager p)
Expand Down Expand Up @@ -305,15 +248,6 @@ public void Gencode(Stream output)
linkedModel.Serialize(output);
}

private static void ToFusion(IPassManager p, bool onlyDynamic = false) =>
p.AddWithName<DataflowPass>("ToFusion").Configure(c =>
{
c.Add<MatmulToFusion>(onlyDynamic);
c.Add<Conv2DToFusion>(onlyDynamic);
c.Add<TFConv2DTransposeToFusion>(onlyDynamic);
c.Add<Conv2DTransposeToFusion>(onlyDynamic);
});

private void RegisterTargetIndependQuantPass(IPassManager passManager)
{
var quantMode = _compileSession.CompileOptions.QuantizeOptions.ModelQuantMode;
Expand Down
5 changes: 5 additions & 0 deletions src/Nncase.Core/Utilities/ShapeExprUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static Expr Replace(Expr shapeExpr, Expr index, Expr value)

public static Expr Insert(Expr shapeExpr, Expr index, Expr value)
{
if (shapeExpr.CheckedShape.IsScalar)
{
return SliceAndMerge(StackScalar(shapeExpr), index, value, 0);
}

return SliceAndMerge(shapeExpr, index, value, 0);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Nncase.Evaluator/EvaluatorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the Apache license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using NetFabric.Hyperlinq;
using Nncase.IR;
using OrtKISharp;
using static Nncase.IR.F.Tensors;

Expand All @@ -23,4 +25,11 @@ public static long[] ToOnnxPadFormat(OrtKISharp.Tensor pads)
// note the pads will be int or long, need cast to long
return OrtKI.Transpose(pads.Cast(OrtDataType.Int64), new long[] { 1, 0 }).ToArray<long>();
}

public static Dictionary<Expr, IValue> GetMemo(Expr input, Dictionary<Var, IValue> varValues)
{
var visitor = new EvaluateVisitor(varValues, new());
visitor.Visit(input);
return visitor.ExprMemo;
}
}
1 change: 0 additions & 1 deletion src/Nncase.Evaluator/NN/Pad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public Expr Visit(IShapeEvaluateContext context, Pad target)
// outShape = inShape + paddings
var padsSumShape = StackScalar(Cast(ShapeOf(paddings)[0], DataTypes.Int32));
var outShape = inShape + Cast(Reshape(paddings, padsSumShape), DataTypes.Int32);
DumpScope.Current.DumpIR(outShape, "paddings");
return outShape;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Nncase.Passes/Rules/Neutral/AddRangeOfAndMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,13 @@ public static bool CheckOp(Op op)
{
if (!pairs.ContainsKey(callParams[i]))
{
// 动态shape的情况下会先统计range再分段,matmul转conv2d则是需要知道shape才能做
// 动态shape情况下执行的顺序是range -> 分段 -> matmul转conv2d
// 这里必须要对matmul的rhs进行判断,如果matmul是动态的那么不会走量化,如果是静态的那么一定会转到conv2d
// 因此认为matmul的rhs为const的情况下一定能转成conv2d
bool isWeights = ((call.Target is Conv2D || call.Target is Conv2DTranspose) && (i == 1))
|| (call.Target is LSTM && i > 0);
|| (call.Target is LSTM && i > 0)
|| (call.Target is MatMul && i == 1 && callParams[1] is TensorConst);

if (!configExist && !useAutoMixQuant)
{
Expand Down
Loading

0 comments on commit d0ef898

Please sign in to comment.