public override void Process(TransformContext context) { var concat = (ExclusiveConcatenation)context.MatchedLayers[0]; var output = concat.Output; var exConcat = new QuantizedExclusiveConcatenation(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); } }
public override void Process(TransformContext context) { var space = (SpaceToBatchNd)context.MatchedLayers[0]; var dwConv2d = (DepthwiseConv2d)context.MatchedLayers[1]; var conv2d = (Conv2d)context.MatchedLayers[2]; var input = space.Input.Connection.From; var output = conv2d.Output; space.Input.ClearConnection(); var newDwConv2d = new DepthwiseConv2d(input.Dimensions, dwConv2d.Weights, dwConv2d.Bias, Padding.Same, 1, 1, dwConv2d.FusedActivationFunction); var quantize = new Quantize(newDwConv2d.Output.Dimensions); var upload = new K210Upload(quantize.Output.Dimensions); var newConv2d = new K210Conv2d(upload.Output.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); var dequantize = new Dequantize(newConv2d.Output.Dimensions); newDwConv2d.Input.SetConnection(input); quantize.Input.SetConnection(newDwConv2d.Output); upload.Input.SetConnection(quantize.Output); newConv2d.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public override void Process(TransformContext context) { K210Conv2d newLayer; OutputConnector output; OutputConnector input; var conv = context.MatchedLayers[0]; if (conv is Conv2d conv2d) { newLayer = new K210Conv2d(conv2d.Input.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); input = conv2d.Input.Connection.From; output = conv2d.Output; } else { throw new InvalidOperationException(); } var quantize = new Quantize(input.Dimensions); var upload = new K210Upload(input.Dimensions); var dequantize = new Dequantize(newLayer.Output.Dimensions); quantize.Input.SetConnection(input); upload.Input.SetConnection(quantize.Output); newLayer.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newLayer.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public DequantizeLayerArgument Convert(Dequantize layer, ConvertContext context) { return(new DequantizeLayerArgument { Count = (uint)(layer.Input.Dimensions.GetSize()), QuantParam = context.Quantization.Distributions[layer.Input.Connection.From].Global.GetQuantizationParam(8) }); }
public void Infer(Dequantize layer, DequantizeLayerArgument argument, InferenceContext context) { var inputAlloc = context.MainMemoryMap[layer.Input.Connection.From]; var outputAlloc = context.MainMemoryMap[layer.Output]; argument.Flags = K210LayerFlags.MainMemoryOutput; argument.MainMemoryInputAddress = inputAlloc.GetAddress(); argument.MainMemoryOutputAddress = outputAlloc.GetAddress(); }
public override void Process(TransformContext context) { var resize = (ResizeNearestNeighbor)context.MatchedLayers[0]; var input = resize.Input.Connection.From.Owner.InputConnectors[0].Connection.From; var output = resize.Output; var quantResize = new QuantizedResizeNearestNeighbor(resize.Input.Dimensions, resize.Output.Dimensions[3], resize.Output.Dimensions[2], resize.AlignCorners); var dequant = new Dequantize(quantResize.Output.Dimensions); quantResize.Input.SetConnection(input); dequant.Input.SetConnection(quantResize.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequant.Output); } }
public override void Process(TransformContext context) { var maxPool = (MaxPool2d)context.MatchedLayers[0]; var input = maxPool.Input.Connection.From.Owner.InputConnectors[0].Connection.From; var output = maxPool.Output; var quantMaxPool = new QuantizedMaxPool2d(maxPool.Input.Dimensions, maxPool.Padding, maxPool.FilterWidth, maxPool.FilterHeight, maxPool.StrideWidth, maxPool.StrideHeight, maxPool.FusedActivationFunction); var dequant = new Dequantize(quantMaxPool.Output.Dimensions); quantMaxPool.Input.SetConnection(input); dequant.Input.SetConnection(quantMaxPool.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequant.Output); } }
public override void Process(TransformContext context) { var space = (SpaceToBatchNd)context.MatchedLayers[0]; var input = space.Input.Connection.From; space.Input.ClearConnection(); K210Conv2d newLayer; OutputConnector output; var conv = context.MatchedLayers[1]; if (conv is Conv2d conv2d) { newLayer = new K210Conv2d(input.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, conv2d.StrideWidth == 2 ? K210PoolType.LeftTop : K210PoolType.None, conv2d.FusedActivationFunction, null); output = conv2d.Output; } else if (conv is DepthwiseConv2d dwConv2d) { newLayer = new K210Conv2d(input.Dimensions, K210Conv2dType.DepthwiseConv2d, dwConv2d.Weights, dwConv2d.Bias, K210PoolType.None, dwConv2d.FusedActivationFunction, null); output = dwConv2d.Output; } else { throw new InvalidOperationException(); } var quantize = new Quantize(input.Dimensions); var upload = new K210Upload(input.Dimensions); var dequantize = new Dequantize(newLayer.Output.Dimensions); quantize.Input.SetConnection(input); upload.Input.SetConnection(quantize.Output); newLayer.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newLayer.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public override void Process(TransformContext context) { var add = (Add)context.MatchedLayers[0]; var inputA = add.InputA.Connection.From.Owner.InputConnectors[0].Connection.From; var inputB = add.InputB.Connection.From.Owner.InputConnectors[0].Connection.From; var output = add.Output; var quantAdd = new QuantizedAdd(add.InputA.Dimensions, add.InputB.Dimensions); var dequant = new Dequantize(quantAdd.Output.Dimensions); quantAdd.InputA.SetConnection(inputA); quantAdd.InputB.SetConnection(inputB); dequant.Input.SetConnection(quantAdd.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequant.Output); } }
public override void Process(TransformContext context) { var conv2d = (K210Conv2d)context.MatchedLayers[0]; var act = context.MatchedLayers[2]; var input = conv2d.Input.Connection.From; var output = act.OutputConnectors[0]; conv2d.Input.ClearConnection(); var newConv2d = new K210Conv2d(conv2d.Input.Dimensions, conv2d.Conv2dType, conv2d.Weights, conv2d.Bias, conv2d.PoolType, conv2d.FusedActivationFunction, act); newConv2d.Input.SetConnection(input); var newDequantize = new Dequantize(newConv2d.Output.Dimensions); newDequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(newDequantize.Output); } }
public override void Process(TransformContext context) { var space = context.MatchedLayers[0]; var dwConv2d = (DepthwiseConv2d)context.MatchedLayers[1]; var conv2d = (Conv2d)context.MatchedLayers.Last(); var input = space.InputConnectors[0].Connection.From; var output = conv2d.Output; space.InputConnectors[0].ClearConnection(); var newDwConv2d = new DepthwiseConv2d(input.Dimensions, dwConv2d.Weights, dwConv2d.Bias, Padding.Same, 1, 1, dwConv2d.FusedActivationFunction); var quantize = new Quantize(newDwConv2d.Output.Dimensions); var upload = new K210Upload(quantize.Output.Dimensions); var newConv2d = new K210Conv2d(upload.Output.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); var dequantize = new Dequantize(newConv2d.Output.Dimensions); newDwConv2d.Input.SetConnection(input); quantize.Input.SetConnection(newDwConv2d.Output); upload.Input.SetConnection(quantize.Output); newConv2d.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); if (context.MatchedLayers.Count == 3) { foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } } else { var newOutput = dequantize.Output; foreach (var middleLayer in context.MatchedLayers.Skip(2).Take(context.MatchedLayers.Count - 3)) { Layer newLayer; switch (middleLayer) { case Quantize _: newLayer = new Quantize(newOutput.Dimensions); break; case Dequantize _: newLayer = new Dequantize(newOutput.Dimensions); break; case LeakyRelu l: newLayer = new LeakyRelu(newOutput.Dimensions, l.Slope); break; default: throw new NotImplementedException(); } newLayer.InputConnectors[0].SetConnection(newOutput); newOutput = newLayer.OutputConnectors[0]; } foreach (var oldOut in oldOuts) { oldOut.SetConnection(newOutput); } } }
public override void Process(TransformContext context) { var conv2d = (K210Conv2d)context.MatchedLayers[0]; var input = conv2d.Input.Connection.From; OutputConnector output; conv2d.Input.ClearConnection(); K210PoolType poolType; if (context.MatchedLayers[2] is MaxPool2d maxPool) { if (maxPool.FilterWidth == 2 && maxPool.StrideWidth == 2) { poolType = K210PoolType.MaxPool2x2; } else if (maxPool.FilterWidth == 2 && maxPool.StrideWidth == 1) { poolType = K210PoolType.MaxPool2x2Stride1; } else if (maxPool.FilterWidth == 4 && maxPool.StrideWidth == 4) { poolType = K210PoolType.MaxPool4x4; } else { throw new NotSupportedException("Unsupported max pool."); } output = maxPool.Output; } else { var avgPool = (AveragePool2d)context.MatchedLayers[2]; if (avgPool.FilterWidth == 2 && avgPool.StrideWidth == 2) { poolType = K210PoolType.AveragePool2x2; } else if (avgPool.FilterWidth == 2 && avgPool.StrideWidth == 1) { poolType = K210PoolType.AveragePool2x2Stride1; } else if (avgPool.FilterWidth == 4 && avgPool.StrideWidth == 4) { poolType = K210PoolType.AveragePool4x4; } else { throw new NotSupportedException("Unsupported average pool."); } output = avgPool.Output; } var newConv2d = new K210Conv2d(conv2d.Input.Dimensions, conv2d.Conv2dType, conv2d.Weights, conv2d.Bias, poolType, conv2d.FusedActivationFunction, conv2d.NonTrivialActivation); newConv2d.Input.SetConnection(input); var newDequantize = new Dequantize(newConv2d.Output.Dimensions); newDequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(newDequantize.Output); } }