-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into release/2.0
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 Nncase.IR; | ||
using Nncase.IR.Math; | ||
using Nncase.IR.NN; | ||
using Nncase.IR.Tensors; | ||
using Nncase.Passes; | ||
using Nncase.PatternMatch; | ||
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.F.NN; | ||
using static Nncase.PatternMatch.F.Tensors; | ||
using static Nncase.PatternMatch.Utility; | ||
|
||
namespace Nncase.Passes.Rules.Neutral; | ||
|
||
/// <summary> | ||
/// squeeze to reshape. | ||
/// </summary> | ||
[RuleGenerator] | ||
public sealed partial class SpaceToBatchToPad : IRewriteRule | ||
{ | ||
/// <inheritdoc/> | ||
public IPattern Pattern { get; } = IsSpaceToBatch( | ||
IsWildcard("input") with { TypePattern = HasFixedShape() }, | ||
IsTensorConst("blockShape"), | ||
IsTensorConst("paddings")); | ||
|
||
private Expr? GetReplace(Expr input, Tensor<int> blockShape, Tensor<int> paddings) | ||
{ | ||
var blockShapeArray = blockShape.ToArray(); | ||
var paddingsArray = paddings.ToArray(); | ||
if (input.CheckedShape.Rank == 4 && blockShapeArray.Length == 2 && blockShapeArray[0] == 1 && blockShape[1] == 1) | ||
{ | ||
var newPaddingsArray = new int[8]; | ||
for (var i = 0; i < paddingsArray.Length; i++) | ||
{ | ||
newPaddingsArray[i + 2] = paddingsArray[i]; | ||
} | ||
|
||
var newPaddings = Tensor.From(newPaddingsArray, new[] { 4, 2 }); | ||
|
||
return Pad(input, newPaddings, PadMode.Constant, 0f); | ||
} | ||
|
||
return null; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Nncase.Tests/Rules/Neutral/UnitTestSpaceToBatchTransform.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,54 @@ | ||
// 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.IO; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Nncase.Passes; | ||
using Nncase.Passes.Rules.Neutral; | ||
using Xunit; | ||
using Math = Nncase.IR.F.Math; | ||
using NN = Nncase.IR.F.NN; | ||
using Random = Nncase.IR.F.Random; | ||
|
||
namespace Nncase.Tests.Rules.NeutralTest; | ||
|
||
public class UnitTestSpaceToBatchToPad : TransformTestBase | ||
{ | ||
public static IEnumerable<object[]> TestSpaceToBatchToPadPositiveData => | ||
new[] | ||
{ | ||
new object[] { new[] { 1, 128, 128, 3 }, new[] { 1, 1 }, new[,] { { 1, 1 }, { 1, 1 } } }, | ||
new object[] { new[] { 3, 64, 64, 16 }, new[] { 1, 1 }, new[,] { { 2, 2 }, { 0, 3 } } }, | ||
new object[] { new[] { 3, 32, 32, 16 }, new[] { 1, 1 }, new[,] { { 3, 8 }, { 7, 4 } } }, | ||
}; | ||
|
||
public static IEnumerable<object[]> TestSpaceToBatchToPadNegativeData => | ||
new[] | ||
{ | ||
new object[] { new[] { 1, 128, 128, new IR.Dimension(1) }, new[] { 2, 2 }, new[,] { { 0, 0 }, { 0, 0 } } }, | ||
new object[] { new[] { 1, 128, 128, IR.Dimension.Unknown }, new[] { 1, 1 }, new[,] { { 1, 1 }, { 1, 1 } } }, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(TestSpaceToBatchToPadPositiveData))] | ||
public void TestSpaceToBatchToPadPositive(int[] shape, int[] blockShape, int[,] paddings) | ||
{ | ||
var a = Random.Normal(DataTypes.Float32, 0, 1, 0, shape); | ||
var rootPre = NN.SpaceToBatch(a, blockShape, paddings); | ||
TestMatched<SpaceToBatchToPad>(rootPre); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestSpaceToBatchToPadNegativeData))] | ||
public void TestFlattenToReshapeNegative(IR.Dimension[] shape, int[] blockShape, int[,] paddings) | ||
{ | ||
var a = new IR.Var(new IR.TensorType(DataTypes.Float32, shape)); | ||
var rootPre = NN.SpaceToBatch(a, blockShape, paddings); | ||
TestNotMatch<SpaceToBatchToPad>(rootPre); | ||
} | ||
} |