-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/support output layout (#972)
* add postprocess for "NHWC","NCHW" and index: "0,2,3,1" ... * Support outputLayout * Support layout by dims: "0,2,3,1", "0,4,1,3,2", "1,0"... --------- Co-authored-by: yanghaoqi <yanghaoqi_intern@canaan-creative.com> Co-authored-by: curioyang <curioyang@users.noreply.github.com>
- Loading branch information
1 parent
3b6577c
commit 243ca46
Showing
11 changed files
with
496 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Nncase.IR; | ||
using Nncase.IR.Imaging; | ||
using Nncase.IR.Math; | ||
using Nncase.Passes; | ||
using Nncase.PatternMatch; | ||
using OrtKISharp; | ||
using static Nncase.IR.F.Math; | ||
using static Nncase.IR.F.NN; | ||
using static Nncase.IR.F.Tensors; | ||
using static Nncase.IR.TypePatternUtility; | ||
using static Nncase.PatternMatch.F.Math; | ||
using static Nncase.PatternMatch.Utility; | ||
using Pad = Nncase.IR.NN.Pad; | ||
|
||
namespace Nncase.Passes.Rules.Neutral; | ||
|
||
/// <summary> | ||
/// Add preprocess in model. | ||
/// </summary> | ||
[RuleGenerator] | ||
public sealed class AddPostProcess : ModulePass | ||
{ | ||
/// <summary> | ||
/// Postprocess: support outputLayout. | ||
/// </summary> | ||
/// <param name="module"> The graph. </param> | ||
/// <param name="options"> RunPassContext. </param> | ||
/// <returns> Return a new graph with postprocess. </returns> | ||
protected override Task<IRModule> RunCoreAsync(IRModule module, RunPassContext options) | ||
{ | ||
var preProcess = CompileSession.CompileOptions.PreProcess; | ||
var modelLayout = CompileSession.CompileOptions.ModelLayout; | ||
var outputLayout = CompileSession.CompileOptions.OutputLayout; | ||
|
||
var entry = (IR.Function)module.Entry!; | ||
|
||
if (preProcess && modelLayout != outputLayout && outputLayout != string.Empty) | ||
{ | ||
var newOutput = outputLayout switch | ||
{ | ||
"NHWC" when modelLayout == "NCHW" => Transpose(entry.Body, new[] { 0, 2, 3, 1 }), | ||
"NCHW" when modelLayout == "NHWC" => Transpose(entry.Body, new[] { 0, 3, 1, 2 }), | ||
_ => Transpose( | ||
entry.Body, | ||
Array.ConvertAll( | ||
outputLayout.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase).Split(","), | ||
int.Parse)), | ||
}; | ||
var newEntry = entry.With(body: newOutput); | ||
module.Remove(entry); | ||
module.Add(newEntry); | ||
module.Entry = newEntry; | ||
} | ||
|
||
return Task.FromResult(module); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.