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 Studio #1136

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
58 changes: 28 additions & 30 deletions src/Nncase.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,47 +244,34 @@ public void ClearFixShape(IPassManager p)
});
}

public async Task CompileWithReportAsync(IProgress<int> progress, CancellationToken token)
{
CancellationTokenSource cts = new();
var internalToken = cts.Token;
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(internalToken, token))
{
try
{
var task = Task.Run(CompileAsync, linkedCts.Token);
Report(progress, 9, linkedCts.Token);
await task.WaitAsync(linkedCts.Token);
}
catch (Exception)
{
return;
}
}
}

public async Task CompileAsync()
public async Task CompileAsync(IProgress<int>? progress = null, CancellationToken? token = null)
{
var target = _compileSession.Target;
await RunPassAsync(p => TargetIndependentPass(p), "TargetIndependentPass");
await RunPassAsync(p => RegisterTargetIndependQuantPass(p), "TargetIndependentQuantPass");
await RunPassAsync(p => TargetIndependentPass(p), "TargetIndependentPass", progress, token);
await RunPassAsync(p => RegisterTargetIndependQuantPass(p), "TargetIndependentQuantPass", progress, token);
if (_compileSession.CompileOptions.ShapeBucketOptions.Enable)
{
await RunPassAsync(p => RegisterShapeBucket(p), "ShapeBucket");
await RunPassAsync(p => TargetIndependentPass(p), "TargetIndependentPass");
await RunPassAsync(p => RegisterShapeBucket(p), "ShapeBucket", progress, token);
await RunPassAsync(p => TargetIndependentPass(p), "TargetIndependentPass", progress, token);
}

await RunPassAsync(
p => target.RegisterTargetDependentPass(p, _compileSession.CompileOptions),
"TargetDependentPass");
await RunPassAsync(p => target.RegisterQuantizePass(p, _compileSession.CompileOptions), "QuantizePass");
"TargetDependentPass",
progress,
token);
await RunPassAsync(p => target.RegisterQuantizePass(p, _compileSession.CompileOptions), "QuantizePass", progress, token);
await RunPassAsync(
p => target.RegisterTargetDependentAfterQuantPass(p, _compileSession.CompileOptions),
"TargetDependentAfterQuantPass");
await RunPassAsync(p => ClearFixShape(p), "ClearFixShape");
"TargetDependentAfterQuantPass",
progress,
token);
await RunPassAsync(p => ClearFixShape(p), "ClearFixShape", progress, token);
await RunPassAsync(
p => target.RegisterTargetDependentBeforeCodeGen(p, _compileSession.CompileOptions),
"TargetDependentBeforeCodeGen");
"TargetDependentBeforeCodeGen",
progress,
token);
if (_dumpper.IsEnabled(DumpFlags.Compile))
{
DumpScope.Current.DumpModule(_module!, "ModuleAfterCompile");
Expand All @@ -301,6 +288,8 @@ private void Report(IProgress<int> progress, int maxPassCount, CancellationToken
{
while (_runPassCount < maxPassCount && !token.IsCancellationRequested)
{
// Without this, the progress bar will get stuck
Thread.Sleep(5);
progress?.Report(_runPassCount);
}
}
Expand Down Expand Up @@ -342,7 +331,7 @@ private void RegisterTargetIndependQuantPass(IPassManager passManager)
}
}

private async Task RunPassAsync(Action<IPassManager> register, string name)
private async Task RunPassAsync(Action<IPassManager> register, string name, IProgress<int>? progress = null, CancellationToken? token = null)
{
var newName = $"{_runPassCount++}_" + name;
var pmgr = _compileSession.CreatePassManager(newName);
Expand All @@ -354,5 +343,14 @@ private async Task RunPassAsync(Action<IPassManager> register, string name)
_dumpper.DumpModule(_module, newName);
_dumpper.DumpDotIR(_module.Entry!, newName);
}

progress?.Report(_runPassCount);
if (token != null)
{
if (token.Value.IsCancellationRequested)
{
token?.ThrowIfCancellationRequested();
}
}
}
}
7 changes: 1 addition & 6 deletions src/Nncase.Core/ICompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ public interface ICompiler
/// Compile module.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task CompileAsync();

/// <summary>
/// Compile module with report pass number.
/// </summary>
Task CompileWithReportAsync(IProgress<int> progress, CancellationToken token);
Task CompileAsync(IProgress<int>? progress = null, CancellationToken? token = null);

/// <summary>
/// Generate code to stream.
Expand Down
3 changes: 3 additions & 0 deletions src/Nncase.Studio/Util/CompileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Runtime.InteropServices.JavaScript;
using Nncase.Diagnostics;
using Nncase.Quantization;
using Nncase.Studio.ViewModels;

namespace Nncase.Studio.Util;
Expand All @@ -26,6 +27,8 @@ public CompileConfig()
CompileOption.Mean = new[] { 0f };
CompileOption.Std = new[] { 0f };
CompileOption.LetterBoxValue = 0f;
UseQuantize = true;
CompileOption.QuantizeOptions.ModelQuantMode = ModelQuantMode.UsePTQ;
}

public CompileOptions CompileOption { get; set; } = new();
Expand Down
8 changes: 7 additions & 1 deletion src/Nncase.Studio/Util/DataUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DynamicData;
using Nncase.IR;
using Nncase.Quantization;
using Nncase.Studio.Views;
Expand Down Expand Up @@ -76,6 +77,11 @@ public static bool TryParseFixVarMap(string input, out Dictionary<string, int> m

try
{
if (!input.Contains(":", StringComparison.Ordinal))
{
return false;
}

map = input.Trim().Split(",").Select(x => x.Trim().Split(":")).ToDictionary(x => x[0], x => int.Parse(x[1]));
return true;
}
Expand All @@ -101,7 +107,7 @@ public static bool TryParseRangeInfo(string input, out Dictionary<string, (int M
.ToDictionary(x => x[0], x =>
{
var pair = x[1].Split(",");
return (int.Parse(pair[0]), int.Parse(pair[1]));
return (int.Parse(pair[0].Trim('(')), int.Parse(pair[1].Trim(')')));
});
return true;
}
Expand Down
23 changes: 13 additions & 10 deletions src/Nncase.Studio/ViewModels/CompileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nncase.IR;
Expand Down Expand Up @@ -44,15 +45,15 @@ public Task CancelCompile()
[RelayCommand]
public async Task Compile()
{
var info = Context.CheckViewModel();
if (info.Length != 0)
_cts = new CancellationTokenSource();
var conf = Context.CompileConfig;
var options = conf.CompileOption;
if (!File.Exists(options.InputFile))
{
Context.OpenDialog($"Error List:\n{string.Join("\n", info)}");
Context.OpenDialog($"InputFile {options.InputFile} not found");
return;
}

var conf = Context.CompileConfig;
var options = conf.CompileOption;
if (!Directory.Exists(options.DumpDir))
{
Directory.CreateDirectory(options.DumpDir);
Expand Down Expand Up @@ -87,17 +88,18 @@ public async Task Compile()
_cts = new();

ProgressBarMax = 9;

var progress = new Progress<int>(percent =>
{
ProgressBarValue = percent;
Dispatcher.UIThread.Post(() =>
{
ProgressBarValue = percent;
});
});

try
{
await Task.Run(async () =>
{
await compiler.CompileWithReportAsync(progress, _cts.Token);
}).ContinueWith(_ => Task.CompletedTask, _cts.Token);
await Task.Run(async () => await compiler.CompileAsync(progress, _cts.Token));
}
catch (Exception)
{
Expand All @@ -123,5 +125,6 @@ public override void UpdateConfig(CompileConfig config)
public override void UpdateViewModelCore(CompileConfig config)
{
KmodelPath = config.KmodelPath;
ProgressBarValue = 0;
}
}