Skip to content

Commit

Permalink
Add import ncnn api
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnycase committed Sep 5, 2023
1 parent 7b4ff03 commit e50a807
Show file tree
Hide file tree
Showing 35 changed files with 1,097 additions and 334 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<PackageVersion Include="OrtKISharp" Version="0.0.2" />
<PackageVersion Include="RazorLight" Version="2.3.0" />
<PackageVersion Include="Singulink.Collections.Weak" Version="1.0.2" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.507" />
<PackageVersion Include="System.CommandLine.Hosting" Version="0.3.0-alpha.21216.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Reactive" Version="5.0.0" />
Expand Down
12 changes: 6 additions & 6 deletions modules/Nncase.Modules.StackVM/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"net7.0": {
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"Google.OrTools.runtime.linux-arm64": {
Expand Down Expand Up @@ -103,8 +103,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
6 changes: 1 addition & 5 deletions src/Nncase.Cli/Commands/Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ private async Task RunAsync(CliCompileOptions cliOptions, IHost host)
var target = CompilerServices.GetTarget(cliOptions.Target);
using var compileSession = CompileSession.Create(target, compileOptions);
var compiler = compileSession.Compiler;
IRModule module;
using (var model_stream = File.OpenRead(compileOptions.InputFile))
{
module = await compiler.ImportModuleAsync(model_stream);
}
var module = await compiler.ImportModuleAsync(compileOptions.InputFormat, compileOptions.InputFile);

// 3. create the calib dataset
if (compileOptions.QuantizeOptions.ModelQuantMode == Quantization.ModelQuantMode.UsePTQ)
Expand Down
12 changes: 6 additions & 6 deletions src/Nncase.Cli/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
},
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"System.CommandLine.Hosting": {
Expand Down Expand Up @@ -344,8 +344,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
12 changes: 6 additions & 6 deletions src/Nncase.CodeGen/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
},
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"Microsoft.Extensions.Configuration.Abstractions": {
Expand Down Expand Up @@ -53,8 +53,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
64 changes: 35 additions & 29 deletions src/Nncase.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,22 @@ public Compiler(CompileSession compileSession, IModelBuilder modelBuilder, IDump
/// <inheritdoc/>
public void ImportIRModule(IRModule module) => _module = module;

public async Task<IRModule> ImportModuleAsync(Stream content)
public Task<IRModule> ImportTFLiteModuleAsync(Stream content)
{
var module = ImportModel(content);
if (_dumpper.IsEnabled(DumpFlags.Compile))
{
_dumpper.DumpModule(module, "IRImport");
}

var preprocess_option = _compileSession.CompileOptions;

await RunPassAsync(pmg => BroadcastOutputNamesAfterImportPass(pmg), "BroadcastOutputNamesAfterImport");
await RunPassAsync(pmg => pmg.Add<ShapeInferPass>(), "ShapeInferAfterImport");
await RunPassAsync(pmg => AddPreAndPostProcess(pmg), "AddPreAndPostProcessAfterImport");
var module = Importers.ImportTFLite(content, _compileSession);
return InitializeModuleAsync(module);
}

var inferSucc = CompilerServices.InferenceType(module.Entry!);
if (!inferSucc)
{
throw new InvalidOperationException("InferShape Failed For This Model!");
}
public Task<IRModule> ImportOnnxModuleAsync(Stream content)
{
var module = Importers.ImportOnnx(content, _compileSession);
return InitializeModuleAsync(module);
}

return module;
public Task<IRModule> ImportNcnnModuleAsync(Stream ncnnParam, Stream ncnnBin)
{
var module = Importers.ImportNcnn(ncnnParam, ncnnBin, _compileSession);
return InitializeModuleAsync(module);
}

public void BroadcastOutputNamesAfterImportPass(IPassManager passManager)
Expand Down Expand Up @@ -248,6 +243,28 @@ public void Gencode(Stream output)
linkedModel.Serialize(output);
}

private async Task<IRModule> InitializeModuleAsync(IRModule module)
{
if (_dumpper.IsEnabled(DumpFlags.Compile))
{
_dumpper.DumpModule(module, "IRImport");
}

var preprocess_option = _compileSession.CompileOptions;

await RunPassAsync(pmg => BroadcastOutputNamesAfterImportPass(pmg), "BroadcastOutputNamesAfterImport");
await RunPassAsync(pmg => pmg.Add<ShapeInferPass>(), "ShapeInferAfterImport");
await RunPassAsync(pmg => AddPreAndPostProcess(pmg), "AddPreAndPostProcessAfterImport");

var inferSucc = CompilerServices.InferenceType(module.Entry!);
if (!inferSucc)
{
throw new InvalidOperationException("InferShape Failed For This Model!");
}

return module;
}

private void RegisterTargetIndependQuantPass(IPassManager passManager)
{
var quantMode = _compileSession.CompileOptions.QuantizeOptions.ModelQuantMode;
Expand All @@ -261,17 +278,6 @@ private void RegisterTargetIndependQuantPass(IPassManager passManager)
}
}

private IRModule ImportModel(Stream content)
{
_module = _compileSession.CompileOptions.InputFormat switch
{
"tflite" => Importers.ImportTFLite(content, _compileSession),
"onnx" => Importers.ImportOnnx(content, _compileSession),
var inputFormat => throw new NotImplementedException($"Not Implement {inputFormat} Importer!"),
};
return _module;
}

private async Task RunPassAsync(Action<IPassManager> register, string name)
{
var newName = $"{_runPassCount++}_" + name;
Expand Down
34 changes: 34 additions & 0 deletions src/Nncase.Compiler/CompilerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nncase.IR;

namespace Nncase;

public static class CompilerExtensions
{
public static async Task<IRModule> ImportModuleAsync(this ICompiler compiler, string modelFormat, string fileName)
{
using var fileStream = File.OpenRead(fileName);
switch (modelFormat.ToUpperInvariant())
{
case "TFLITE":
return await compiler.ImportTFLiteModuleAsync(fileStream);
case "ONNX":
return await compiler.ImportOnnxModuleAsync(fileStream);
case "NCNN":
{
using var binStream = File.OpenRead(Path.ChangeExtension(fileName, "bin"));
return await compiler.ImportNcnnModuleAsync(fileStream, binStream);
}

default:
throw new NotSupportedException($"Unsupported model format: {modelFormat}");
}
}
}
31 changes: 27 additions & 4 deletions src/Nncase.Compiler/Interop/CApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public unsafe struct CApiMT
public delegate* unmanaged<IntPtr, IntPtr, IntPtr> CompileSessionCreatePtr;
public delegate* unmanaged<IntPtr, IntPtr> CompileSessionGetCompilerPtr;
public delegate* unmanaged<void> CompilerInitializePtr;
public delegate* unmanaged<IntPtr, IntPtr, IntPtr> CompilerImportModulePtr;
public delegate* unmanaged<IntPtr, IntPtr, IntPtr> CompilerImportTFLiteModulePtr;
public delegate* unmanaged<IntPtr, IntPtr, IntPtr> CompilerImportOnnxModulePtr;
public delegate* unmanaged<IntPtr, IntPtr, IntPtr, IntPtr> CompilerImportNcnnModulePtr;
public delegate* unmanaged<IntPtr, void> CompilerCompilePtr;
public delegate* unmanaged<IntPtr, IntPtr, void> CompilerGencodePtr;
public delegate* unmanaged<Runtime.TypeCode, IntPtr> DataTypeFromTypeCodePtr;
Expand Down Expand Up @@ -131,7 +133,9 @@ public static void Initialize(CApiMT* mt)
mt->CompileSessionCreatePtr = &CompileSessionCreate;
mt->CompileSessionGetCompilerPtr = &CompileSessionGetCompiler;
mt->CompilerInitializePtr = &CompilerInitialize;
mt->CompilerImportModulePtr = &CompilerImportModule;
mt->CompilerImportTFLiteModulePtr = &CompilerImportTFLiteModule;
mt->CompilerImportOnnxModulePtr = &CompilerImportOnnxModule;
mt->CompilerImportNcnnModulePtr = &CompilerImportNcnnModule;
mt->CompilerCompilePtr = &CompilerCompile;
mt->CompilerGencodePtr = &CompilerGencode;
mt->DataTypeFromTypeCodePtr = &DataTypeFromTypeCode;
Expand Down Expand Up @@ -395,11 +399,30 @@ private static void CompilerInitialize()
}

[UnmanagedCallersOnly]
private static IntPtr CompilerImportModule(IntPtr compilerHandle, IntPtr streamHandle)
private static IntPtr CompilerImportTFLiteModule(IntPtr compilerHandle, IntPtr streamHandle)
{
var compiler = Get<Compiler>(compilerHandle);
var stream = Get<CStream>(streamHandle);
var module = compiler.ImportModuleAsync(stream).Result;
var module = compiler.ImportTFLiteModuleAsync(stream).Result;
return GCHandle.ToIntPtr(GCHandle.Alloc(module));
}

[UnmanagedCallersOnly]
private static IntPtr CompilerImportOnnxModule(IntPtr compilerHandle, IntPtr streamHandle)
{
var compiler = Get<Compiler>(compilerHandle);
var stream = Get<CStream>(streamHandle);
var module = compiler.ImportOnnxModuleAsync(stream).Result;
return GCHandle.ToIntPtr(GCHandle.Alloc(module));
}

[UnmanagedCallersOnly]
private static IntPtr CompilerImportNcnnModule(IntPtr compilerHandle, IntPtr ncnnParamHandle, IntPtr ncnnBinHandle)
{
var compiler = Get<Compiler>(compilerHandle);
var ncnnParam = Get<CStream>(ncnnParamHandle);
var ncnnBin = Get<CStream>(ncnnBinHandle);
var module = compiler.ImportNcnnModuleAsync(ncnnParam, ncnnBin).Result;
return GCHandle.ToIntPtr(GCHandle.Alloc(module));
}

Expand Down
12 changes: 6 additions & 6 deletions src/Nncase.Compiler/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
},
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"Google.OrTools.runtime.linux-arm64": {
Expand Down Expand Up @@ -350,8 +350,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
19 changes: 17 additions & 2 deletions src/Nncase.Core/ICompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,26 @@ namespace Nncase;
public interface ICompiler
{
/// <summary>
/// Import DL model as ir module.
/// Import TFLite model as ir module.
/// </summary>
/// <param name="content">Model content.</param>
/// <returns>Imported ir module.</returns>
Task<IRModule> ImportModuleAsync(Stream content);
Task<IRModule> ImportTFLiteModuleAsync(Stream content);

/// <summary>
/// Import Onnx model as ir module.
/// </summary>
/// <param name="content">Model content.</param>
/// <returns>Imported ir module.</returns>
Task<IRModule> ImportOnnxModuleAsync(Stream content);

/// <summary>
/// Import Ncnn model as ir module.
/// </summary>
/// <param name="ncnnParam">Ncnn param stream.</param>
/// <param name="ncnnBin">Ncnn bin stream.</param>
/// <returns>Imported ir module.</returns>
Task<IRModule> ImportNcnnModuleAsync(Stream ncnnParam, Stream ncnnBin);

/// <summary>
/// import ir module into compiler.
Expand Down
12 changes: 6 additions & 6 deletions src/Nncase.Core/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
},
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"System.Reactive": {
Expand Down Expand Up @@ -109,8 +109,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
12 changes: 6 additions & 6 deletions src/Nncase.Diagnostics/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"net7.0": {
"StyleCop.Analyzers": {
"type": "Direct",
"requested": "[1.2.0-beta.435, )",
"resolved": "1.2.0-beta.435",
"contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==",
"requested": "[1.2.0-beta.507, )",
"resolved": "1.2.0-beta.507",
"contentHash": "/FtugDT66cKJJ+GGH7rNpG6UDrT4iIWz45M6lrXXHobDUFDHw+q5VgkbiR+6ffTO564ge7w6fQh/eoQhVdJO8Q==",
"dependencies": {
"StyleCop.Analyzers.Unstable": "1.2.0.435"
"StyleCop.Analyzers.Unstable": "1.2.0.507"
}
},
"Microsoft.Extensions.Configuration.Abstractions": {
Expand Down Expand Up @@ -47,8 +47,8 @@
},
"StyleCop.Analyzers.Unstable": {
"type": "Transitive",
"resolved": "1.2.0.435",
"contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg=="
"resolved": "1.2.0.507",
"contentHash": "gTY3IQdRqDJ4hbhSA3e/R48oE8b/OiKfvwkt1QdNVfrJK2gMHBV8ldaHJ885jxWZfllK66soa/sdcjh9bX49Tw=="
},
"System.Buffers": {
"type": "Transitive",
Expand Down
Loading

0 comments on commit e50a807

Please sign in to comment.