-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
src/NnCase.Converter.K210/Converters/Layers/QuantizedConcatenation.cs
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,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NnCase.Converter.K210.Converters.Stages.Convert; | ||
using NnCase.Converter.K210.Converters.Stages.Inference; | ||
using NnCase.Converter.Model.Layers; | ||
|
||
namespace NnCase.Converter.K210.Converters.Layers | ||
{ | ||
[LayerConverter(typeof(QuantizedConcatenation), K210LayerType.QuantizedConcatenation)] | ||
public class QuantizedConcatenationConverter | ||
{ | ||
public ConcatenationLayerArgument Convert(QuantizedConcatenation layer, ConvertContext context) | ||
{ | ||
return new ConcatenationLayerArgument | ||
{ | ||
InputCount = (uint)layer.Inputs.Count | ||
}; | ||
} | ||
|
||
public void Infer(QuantizedConcatenation layer, ConcatenationLayerArgument argument, InferenceContext context) | ||
{ | ||
var outputAlloc = context.MainMemoryMap[layer.Output]; | ||
|
||
argument.Flags = K210LayerFlags.MainMemoryOutput; | ||
argument.MainMemoryOutputAddress = outputAlloc.GetAddress(); | ||
argument.InputsMainMemory = (from i in layer.Inputs | ||
let a = context.MainMemoryMap[i.Connection.From] | ||
select new MemoryRange | ||
{ | ||
Start = a.GetAddress(), | ||
Size = a.Size | ||
}).ToList(); | ||
} | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/NnCase.Converter/Transforms/QuantizedConcatenationTransform.cs
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,60 @@ | ||
using NnCase.Converter.Model; | ||
using NnCase.Converter.Model.Layers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace NnCase.Converter.Transforms | ||
{ | ||
public class QuantizedConcatenationTransform : Transform | ||
{ | ||
protected override bool OnTryMatch(Layer layer, TransformContext context) | ||
{ | ||
try | ||
{ | ||
if (layer is Concatenation concat) | ||
{ | ||
if (concat.Inputs.Select(x => x.Connection.From.Owner).All(x => x is Dequantize)) | ||
{ | ||
context.Inputs.AddRange(concat.Inputs); | ||
context.Outputs.Add(concat.Output); | ||
|
||
context.MatchedLayers.Add(layer); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public override void Process(TransformContext context) | ||
{ | ||
var concat = (Concatenation)context.MatchedLayers[0]; | ||
var output = concat.Output; | ||
|
||
var exConcat = new QuantizedConcatenation(concat.Inputs.Select(x => new ReadOnlyMemory<int>(x.Dimensions.ToArray()))); | ||
for (int i = 0; i < exConcat.Inputs.Count; i++) | ||
{ | ||
var input = concat.Inputs[i].Connection.From; | ||
var quantize = new Quantize(input.Dimensions); | ||
var requantize = new Requantize(quantize.Output.Dimensions); | ||
quantize.Input.SetConnection(input); | ||
requantize.Input.SetConnection(quantize.Output); | ||
exConcat.Inputs[i].SetConnection(requantize.Output); | ||
} | ||
|
||
var dequantize = new Dequantize(exConcat.Output.Dimensions); | ||
dequantize.Input.SetConnection(exConcat.Output); | ||
|
||
var oldOuts = output.Connections.Select(o => o.To).ToList(); | ||
foreach (var oldOut in oldOuts) | ||
oldOut.SetConnection(dequantize.Output); | ||
} | ||
} | ||
} |