1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Futhark.IR.Prop.ReshapeTests
( tests,
)
where
import Data.List qualified as L
import Futhark.IR.Prop.Constants
import Futhark.IR.Prop.Reshape
import Futhark.IR.Syntax
import Futhark.IR.SyntaxTests ()
import Test.Tasty
import Test.Tasty.HUnit
intShape :: [Int] -> Shape
intShape = Shape . map (intConst Int32 . toInteger)
reshapeOuterTests :: [TestTree]
reshapeOuterTests =
[ testCase (unwords ["reshapeOuter", show sc, show n, show shape, "==", show sc_res]) $
reshapeOuter (intShape sc) n (intShape shape) @?= intShape sc_res
| (sc, n, shape, sc_res) <-
[ ([1], 1, [4, 3], [1, 3]),
([1], 2, [4, 3], [1]),
([2, 2], 1, [4, 3], [2, 2, 3]),
([2, 2], 2, [4, 3], [2, 2])
]
]
reshapeInnerTests :: [TestTree]
reshapeInnerTests =
[ testCase (unwords ["reshapeInner", show sc, show n, show shape, "==", show sc_res]) $
reshapeInner (intShape sc) n (intShape shape) @?= intShape sc_res
| (sc, n, shape, sc_res) <-
[ ([1], 1, [4, 3], [4, 1]),
([1], 0, [4, 3], [1]),
([2, 2], 1, [4, 3], [4, 2, 2]),
([2, 2], 0, [4, 3], [2, 2])
]
]
dimFlatten :: Int -> Int -> d -> DimSplice d
dimFlatten i k w = DimSplice i k (Shape [w])
dimUnflatten :: Int -> [d] -> DimSplice d
dimUnflatten i ws = DimSplice i 1 (Shape ws)
dimCoerce :: Int -> d -> DimSplice d
dimCoerce i w = DimSplice i 1 (Shape [w])
dimSplice :: Int -> Int -> [d] -> DimSplice d
dimSplice i n s = DimSplice i n $ Shape s
flipReshapeRearrangeTests :: [TestTree]
flipReshapeRearrangeTests =
[ testCase
( unwords
[ "flipReshapeRearrange",
show v0_shape,
show v1_shape,
show perm
]
)
$ flipReshapeRearrange v0_shape v1_shape perm @?= res
| (v0_shape :: [String], v1_shape, perm, res) <-
[ ( ["A", "B", "C"],
["A", "BC"],
[1, 0],
Just [1, 2, 0]
),
( ["A", "B", "C", "D"],
["A", "BCD"],
[1, 0],
Just [1, 2, 3, 0]
),
( ["A"],
["B", "C"],
[1, 0],
Nothing
),
( ["A", "B", "C"],
["AB", "C"],
[1, 0],
Just [2, 0, 1]
),
( ["A", "B", "C", "D"],
["ABC", "D"],
[1, 0],
Just [3, 0, 1, 2]
)
]
]
flipRearrangeReshapeTests :: [TestTree]
flipRearrangeReshapeTests =
[ testCase
( unwords
[ "flipRearrangeReshape",
show perm,
prettyStringOneLine newshape
]
)
$ flipRearrangeReshape perm newshape @?= res
| (perm, newshape :: NewShape String, res) <-
[ ( [1, 0],
NewShape
[dimUnflatten 1 ["B", "C"]]
(Shape ["A", "B", "C"]),
Just
( NewShape
[dimUnflatten 0 ["B", "C"]]
(Shape ["B", "C", "A"]),
[2, 0, 1]
)
),
( [1, 0],
NewShape
[dimFlatten 0 2 "AB"]
(Shape ["AB"]),
Nothing
)
]
]
simplifyTests :: TestTree
simplifyTests =
testGroup
"simplifyNewShape"
[ testCase "Inverse flatten and unflatten - simple case" $
lhs
["A", "B"]
[dimFlatten 0 2 "AB", dimUnflatten 0 ["A", "B"]]
@?= Just [],
testCase "Non-inverse flatten and unflatten - simple case" $
lhs
["A", "B"]
[dimFlatten 0 2 "AB", dimUnflatten 0 ["C", "D"]]
@?= Just [dimSplice 0 2 ["C", "D"]],
testCase "Inverse flatten and unflatten - separated by coercion" $
lhs
["A", "B"]
[ dimFlatten 0 2 "AB",
dimCoerce 0 "CD",
dimUnflatten 0 ["C", "D"]
]
@?= Just [dimSplice 0 2 ["C", "D"]],
testCase "Two unflattens - simple case" $
lhs
["ABC"]
[dimUnflatten 0 ["A", "BC"], dimUnflatten 1 ["B", "C"]]
@?= Just [dimUnflatten 0 ["A", "B", "C"]],
testCase "Two unflattens with unchanged prefix" $
lhs
["A", "B", "C", "D", "E"]
[ DimSplice 3 2 $ Shape ["DE"],
DimSplice 2 2 $ Shape ["CDE"]
]
@?= Just [dimFlatten 2 3 "CDE"],
testCase "Identity coerce" $
lhs
["A", "B", "C"]
[dimCoerce 1 "B", dimCoerce 2 "C"]
@?= Just [],
testCase "Identity coerce (multiple dimensions)" $
lhs
["A", "B", "C"]
[DimSplice 1 2 (Shape ["B", "C"])]
@?= Just [],
testCase "Identity coerce (with non-identity stuff afterwards)" $
lhs
["B", "CD"]
[dimCoerce 0 "B", dimUnflatten 1 ["C", "D"]]
@?= Just [dimUnflatten 1 ["C", "D"]],
testCase "Get rid of a coerce before an unflatten" $
lhs
["CD"]
[dimCoerce 0 "AB", dimUnflatten 0 ["A", "B"]]
@?= Just [dimUnflatten 0 ["A", "B"]],
testCase "Get rid of a coerce after a flatten" $
lhs
["A", "B", "C"]
[dimFlatten 0 2 "ABC", dimCoerce 0 "K"]
@?= Just [dimFlatten 0 2 "K"],
testCase "Flatten and unflatten (invariant suffix)" $
lhs
["A", "B", "C"]
[dimFlatten 0 3 "ABC", dimUnflatten 0 ["D", "E", "C"]]
@?= Just [dimSplice 0 2 ["D", "E"]],
testCase "Flatten and unflatten (invariant prefix)" $
lhs
["A", "B", "C"]
[dimFlatten 0 3 "ABC", dimUnflatten 0 ["A", "D", "E"]]
@?= Just [dimSplice 1 2 ["D", "E"]],
testCase "Invariant part of splice" $
lhs
["A", "B", "C", "D"]
[DimSplice 1 3 $ Shape ["BC", "D"]]
@?= Just [DimSplice 1 2 $ Shape ["BC"]],
testCase "Necessary coercion" $
lhs
["A", "B"]
[dimCoerce 0 "C", dimCoerce 1 "D"]
@?= Nothing,
testCase "Another necessary coercion" $
lhs
["A", "B", "C"]
[dimCoerce 0 "A'", dimCoerce 1 "A'", dimCoerce 2 "A'"]
@?= Nothing,
testCase "Long with redundancies" $
lhs
["A", "B", "C", "D"]
[ DimSplice 1 3 $ Shape ["BC", "D"],
dimCoerce 1 "BC",
dimCoerce 2 "D",
dimFlatten 1 2 "BCD",
dimFlatten 0 2 "ABCD"
]
@?= Just [dimFlatten 0 4 "ABCD"]
]
where
lhs orig_shape ss =
let res_shape :: ShapeBase String =
L.foldl' applySplice (Shape orig_shape) ss
in dimSplices
<$> simplifyNewShape (Shape orig_shape) (NewShape ss res_shape)
tests :: TestTree
tests =
testGroup "ReshapeTests" . mconcat $
[ reshapeOuterTests,
reshapeInnerTests,
flipReshapeRearrangeTests,
flipRearrangeReshapeTests,
[simplifyTests]
]
|