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

Feature/egraph sat extract #963

Merged
merged 21 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Google.OrTools" Version="9.4.1874" />
<PackageVersion Include="AnyTensorFlow.NET" Version="0.70.1" />
<PackageVersion Include="BitFields" Version="0.1.0" />
<PackageVersion Include="coverlet.collector" Version="3.0.2" />
Expand Down
46 changes: 46 additions & 0 deletions modules/Nncase.Modules.StackVM/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
"StyleCop.Analyzers.Unstable": "1.2.0.435"
}
},
"Google.OrTools.runtime.linux-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Z46ndZcZa2Lt5b76xU9kxVYbPLg/LfuMufhUVsu3Qo3L7Bibf7WXd9j7RRldjnuv8RIHWTqb0b+2FwwMxs0c5A=="
},
"Google.OrTools.runtime.linux-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "zGeDb8FuvP9HXjrsU7krVXtSDFpR+DUGNEsH51k94jL9tzf2vWYI8+WUBRHZ/cGe50dpLr+vIjfcNo3gFyOpkQ=="
},
"Google.OrTools.runtime.osx-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Wo0ZfDaH6DhiQw0jZm4HWJm/oPGPpWNwOLUz+EYaoH3MLtocSxItHGQj/Ta3HyhXnYNOv+TliAH8L+8RCXu/2w=="
},
"Google.OrTools.runtime.osx-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "IAfGgKR1og6vU87axK1d37Ak/4jy8B4NMoElovG/KZc/2UY+cJEAQDA709UMegtI4lBhuxTWFNUiHQYmRIB9yQ=="
},
"Google.OrTools.runtime.win-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "fUs5qDnZA6itygolcX6nPuachQkY9CVvQbakIzIiRAWKcaj8umQAbFdGwbkyzp3qp34BKW5mtPVsmMyfQBBjOQ=="
},
"libortki": {
"type": "Transitive",
"resolved": "0.0.2",
Expand Down Expand Up @@ -116,6 +141,7 @@
"type": "Project",
"dependencies": {
"GiGraph.Dot": "[2.0.0, )",
"Google.OrTools": "[9.4.1874, )",
"NetFabric.Hyperlinq": "[3.0.0-beta48, )",
"Nncase.Core": "[1.0.0, )",
"Nncase.Evaluator": "[1.0.0, )",
Expand Down Expand Up @@ -166,6 +192,26 @@
"resolved": "2.0.0",
"contentHash": "ThvS2mQVveSkTMUm04tMbRYzu1XFPV8xBHISrUMp02APjhv9IRbLu3v3upTPCywORx2Ds/c6AqEUL1WU6kPfuQ=="
},
"Google.OrTools": {
"type": "CentralTransitive",
"requested": "[9.4.1874, )",
"resolved": "9.4.1874",
"contentHash": "jqRoI+pYlym+fhoU25u+13oti5h+772bllQ9zDitTVMclDXVTiG6pxzvmYO74wnADBMdpb2SQlgiNQxoNk5dlA==",
"dependencies": {
"Google.OrTools.runtime.linux-arm64": "9.4.1874",
"Google.OrTools.runtime.linux-x64": "9.4.1874",
"Google.OrTools.runtime.osx-arm64": "9.4.1874",
"Google.OrTools.runtime.osx-x64": "9.4.1874",
"Google.OrTools.runtime.win-x64": "9.4.1874",
"Google.Protobuf": "3.19.4"
}
},
"Google.Protobuf": {
"type": "CentralTransitive",
"requested": "[3.19.4, )",
"resolved": "3.19.4",
"contentHash": "fd07/ykL4O4FhqrZIELm5lmiyOHfdPg9+o+hWr6tcfRdS7tHXnImg/2wtogLzlW2eEmr0J7j6ZrZvaWOLiJbxQ=="
},
"Microsoft.Extensions.Hosting.Abstractions": {
"type": "CentralTransitive",
"requested": "[6.0.0, )",
Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def run(self):
sharp_libs = [os.path.join(root, _lib) for root, _, files in
os.walk(os.path.join(bin_dir, 'sharplibs')) for _lib in files if
os.path.isfile(os.path.join(root, _lib)) and
os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"]
(os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"] or
_lib.startswith("lib"))
and not _lib.endswith(".deps.json")]

for lib in sharp_libs:
Expand All @@ -93,7 +94,8 @@ def run(self):
os.walk(bin_dir) for _lib in files if
'sharplibs' not in root and
os.path.isfile(os.path.join(root, _lib)) and
os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"]
(os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"] or
_lib.startswith("lib"))
and not (_lib.startswith("python") or _lib.startswith("_nncase"))]

for lib in libs:
Expand Down Expand Up @@ -249,7 +251,8 @@ def build_cmake(self, ext: Extension):
nncase_libs = [os.path.join(root, _lib) for root, _, files in
os.walk(os.path.join(ext.sourcedir, 'install')) for _lib in files if
os.path.isfile(os.path.join(root, _lib)) and
os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"]]
(os.path.splitext(_lib)[-1] in [".dll", ".so", ".dylib", ".json"] or
_lib.startswith("lib"))]

sharp_libs_dir = os.path.join(bin_dir, 'sharplibs')
os.makedirs(sharp_libs_dir)
Expand Down
40 changes: 40 additions & 0 deletions src/Nncase.Cli/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@
"System.CommandLine": "2.0.0-beta1.21216.1"
}
},
"Google.OrTools.runtime.linux-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Z46ndZcZa2Lt5b76xU9kxVYbPLg/LfuMufhUVsu3Qo3L7Bibf7WXd9j7RRldjnuv8RIHWTqb0b+2FwwMxs0c5A=="
},
"Google.OrTools.runtime.linux-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "zGeDb8FuvP9HXjrsU7krVXtSDFpR+DUGNEsH51k94jL9tzf2vWYI8+WUBRHZ/cGe50dpLr+vIjfcNo3gFyOpkQ=="
},
"Google.OrTools.runtime.osx-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Wo0ZfDaH6DhiQw0jZm4HWJm/oPGPpWNwOLUz+EYaoH3MLtocSxItHGQj/Ta3HyhXnYNOv+TliAH8L+8RCXu/2w=="
},
"Google.OrTools.runtime.osx-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "IAfGgKR1og6vU87axK1d37Ak/4jy8B4NMoElovG/KZc/2UY+cJEAQDA709UMegtI4lBhuxTWFNUiHQYmRIB9yQ=="
},
"Google.OrTools.runtime.win-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "fUs5qDnZA6itygolcX6nPuachQkY9CVvQbakIzIiRAWKcaj8umQAbFdGwbkyzp3qp34BKW5mtPVsmMyfQBBjOQ=="
},
"libortki": {
"type": "Transitive",
"resolved": "0.0.2",
Expand Down Expand Up @@ -684,6 +709,7 @@
"type": "Project",
"dependencies": {
"GiGraph.Dot": "[2.0.0, )",
"Google.OrTools": "[9.4.1874, )",
"NetFabric.Hyperlinq": "[3.0.0-beta48, )",
"Nncase.Core": "[1.0.0, )",
"Nncase.Evaluator": "[1.0.0, )",
Expand Down Expand Up @@ -802,6 +828,20 @@
"resolved": "2.0.0",
"contentHash": "ThvS2mQVveSkTMUm04tMbRYzu1XFPV8xBHISrUMp02APjhv9IRbLu3v3upTPCywORx2Ds/c6AqEUL1WU6kPfuQ=="
},
"Google.OrTools": {
"type": "CentralTransitive",
"requested": "[9.4.1874, )",
"resolved": "9.4.1874",
"contentHash": "jqRoI+pYlym+fhoU25u+13oti5h+772bllQ9zDitTVMclDXVTiG6pxzvmYO74wnADBMdpb2SQlgiNQxoNk5dlA==",
"dependencies": {
"Google.OrTools.runtime.linux-arm64": "9.4.1874",
"Google.OrTools.runtime.linux-x64": "9.4.1874",
"Google.OrTools.runtime.osx-arm64": "9.4.1874",
"Google.OrTools.runtime.osx-x64": "9.4.1874",
"Google.OrTools.runtime.win-x64": "9.4.1874",
"Google.Protobuf": "3.19.4"
}
},
"Google.Protobuf": {
"type": "CentralTransitive",
"requested": "[3.19.4, )",
Expand Down
40 changes: 40 additions & 0 deletions src/Nncase.Compiler/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@
"StyleCop.Analyzers.Unstable": "1.2.0.435"
}
},
"Google.OrTools.runtime.linux-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Z46ndZcZa2Lt5b76xU9kxVYbPLg/LfuMufhUVsu3Qo3L7Bibf7WXd9j7RRldjnuv8RIHWTqb0b+2FwwMxs0c5A=="
},
"Google.OrTools.runtime.linux-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "zGeDb8FuvP9HXjrsU7krVXtSDFpR+DUGNEsH51k94jL9tzf2vWYI8+WUBRHZ/cGe50dpLr+vIjfcNo3gFyOpkQ=="
},
"Google.OrTools.runtime.osx-arm64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "Wo0ZfDaH6DhiQw0jZm4HWJm/oPGPpWNwOLUz+EYaoH3MLtocSxItHGQj/Ta3HyhXnYNOv+TliAH8L+8RCXu/2w=="
},
"Google.OrTools.runtime.osx-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "IAfGgKR1og6vU87axK1d37Ak/4jy8B4NMoElovG/KZc/2UY+cJEAQDA709UMegtI4lBhuxTWFNUiHQYmRIB9yQ=="
},
"Google.OrTools.runtime.win-x64": {
"type": "Transitive",
"resolved": "9.4.1874",
"contentHash": "fUs5qDnZA6itygolcX6nPuachQkY9CVvQbakIzIiRAWKcaj8umQAbFdGwbkyzp3qp34BKW5mtPVsmMyfQBBjOQ=="
},
"libortki": {
"type": "Transitive",
"resolved": "0.0.2",
Expand Down Expand Up @@ -662,6 +687,7 @@
"type": "Project",
"dependencies": {
"GiGraph.Dot": "[2.0.0, )",
"Google.OrTools": "[9.4.1874, )",
"NetFabric.Hyperlinq": "[3.0.0-beta48, )",
"Nncase.Core": "[1.0.0, )",
"Nncase.Evaluator": "[1.0.0, )",
Expand Down Expand Up @@ -750,6 +776,20 @@
"resolved": "2.0.0",
"contentHash": "ThvS2mQVveSkTMUm04tMbRYzu1XFPV8xBHISrUMp02APjhv9IRbLu3v3upTPCywORx2Ds/c6AqEUL1WU6kPfuQ=="
},
"Google.OrTools": {
"type": "CentralTransitive",
"requested": "[9.4.1874, )",
"resolved": "9.4.1874",
"contentHash": "jqRoI+pYlym+fhoU25u+13oti5h+772bllQ9zDitTVMclDXVTiG6pxzvmYO74wnADBMdpb2SQlgiNQxoNk5dlA==",
"dependencies": {
"Google.OrTools.runtime.linux-arm64": "9.4.1874",
"Google.OrTools.runtime.linux-x64": "9.4.1874",
"Google.OrTools.runtime.osx-arm64": "9.4.1874",
"Google.OrTools.runtime.osx-x64": "9.4.1874",
"Google.OrTools.runtime.win-x64": "9.4.1874",
"Google.Protobuf": "3.19.4"
}
},
"Google.Protobuf": {
"type": "CentralTransitive",
"requested": "[3.19.4, )",
Expand Down
10 changes: 10 additions & 0 deletions src/Nncase.Core/CostModel/Cost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ public override int GetHashCode()
{
return Factors.GetHashCode();
}

public override string ToString()
{
if (Equals(Cost.Zero))
{
return "Zero";
}

return $"{{ {string.Join(", ", Factors.Select(kv => $"{kv.Key}: {kv.Value}"))}, Score:{Score} }}";
}
}

/// <summary>
Expand Down
72 changes: 37 additions & 35 deletions src/Nncase.EGraph/CostModel/EGraphCostEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ internal sealed class EGraphCostEvaluator
private readonly Dictionary<EClass, Cost> _eclassCosts = new();
private readonly HashSet<EClass> _allEclasses = new();
private readonly IBaseFuncCostEvaluator? _baseFuncCostEvaluator;
private readonly bool _accumulate;
private bool _changed;

public EGraphCostEvaluator(EClass root, IBaseFuncCostEvaluator? basefunc_cost_evaluator)
public EGraphCostEvaluator(EClass root, IBaseFuncCostEvaluator? basefunc_cost_evaluator, bool accumulate = true)
{
_root = root;
_accumulate = accumulate;
_baseFuncCostEvaluator = basefunc_cost_evaluator;
PopulateAllEclasses(_root);
}
Expand Down Expand Up @@ -133,17 +135,17 @@ private Cost Visit(ENode enode, Op op)

private Cost? Visit(ENode enode, IR.Tuple tuple)
{
return Visit(enode, costs => costs.Sum());
return Visit(enode, costs => _accumulate ? costs.Sum() : Cost.Zero);
}

private Cost? Visit(ENode enode, If @if)
{
return Visit(enode, cost => cost[1] + cost[2]);
return Visit(enode, cost => _accumulate ? cost[1] + cost[2] + cost[3] : Cost.Zero);
}

private Cost? Visit(ENode enode, Marker marker)
{
return Visit(enode, costs => costs[0]);
return Visit(enode, costs => _accumulate ? costs[0] : Cost.Zero);
}

private Cost? Visit(ENode enode, None none)
Expand Down Expand Up @@ -176,7 +178,7 @@ private Cost Visit(ENode enode, Op op)
}
}

return UpdateCost(enode, cost == null ? null : cost + costs.Sum());
return UpdateCost(enode, cost == null ? null : (_accumulate ? cost + costs.Sum() : cost));
});
}

Expand Down Expand Up @@ -275,44 +277,44 @@ private Cost VisitLeaf(ENode enode, Func<Cost> costGetter)
var cost = costGetter(costs);
return UpdateCost(enode, cost);
}
}

internal sealed class EGraphOpCostEvaluateContext : ICostEvaluateContext
{
private readonly IRType? _returnType;
private readonly IRType?[] _argumentTypes;
private readonly EClass[] _arguments;

private sealed class EGraphOpCostEvaluateContext : ICostEvaluateContext
public EGraphOpCostEvaluateContext(IRType? returnType, IRType?[] argumentTypes, EClass[] arguments)
{
private readonly IRType? _returnType;
private readonly IRType?[] _argumentTypes;
private readonly EClass[] _arguments;
_returnType = returnType;
_argumentTypes = argumentTypes;
_arguments = arguments;
}

public EGraphOpCostEvaluateContext(IRType? returnType, IRType?[] argumentTypes, EClass[] arguments)
{
_returnType = returnType;
_argumentTypes = argumentTypes;
_arguments = arguments;
}
public T GetArgument<T>(Op op, ParameterInfo parameter)
where T : BaseFunction
{
System.Diagnostics.Trace.Assert(_arguments[parameter.Index].Nodes.Count == 1);
return (T)_arguments[parameter.Index].Nodes[0].Expr;
}

public T GetArgument<T>(Op op, ParameterInfo parameter)
where T : BaseFunction
public T GetArgumentType<T>(Op op, ParameterInfo parameter)
where T : IRType
{
if (op.GetType() == parameter.OwnerType)
{
System.Diagnostics.Trace.Assert(_arguments[parameter.Index].Nodes.Count == 1);
return (T)_arguments[parameter.Index].Nodes[0].Expr;
return (T?)_argumentTypes[parameter.Index] ?? throw new InvalidOperationException("Run type infer first.");
}

public T GetArgumentType<T>(Op op, ParameterInfo parameter)
where T : IRType
else
{
if (op.GetType() == parameter.OwnerType)
{
return (T?)_argumentTypes[parameter.Index] ?? throw new InvalidOperationException("Run type infer first.");
}
else
{
throw new ArgumentOutOfRangeException($"Operator {op} doesn't have parameter: {parameter.Name}.");
}
throw new ArgumentOutOfRangeException($"Operator {op} doesn't have parameter: {parameter.Name}.");
}
}

public T GetReturnType<T>()
where T : IRType
{
return (T?)_returnType ?? throw new InvalidOperationException("Run type infer first.");
}
public T GetReturnType<T>()
where T : IRType
{
return (T?)_returnType ?? throw new InvalidOperationException("Run type infer first.");
}
}
Loading