|
| 1 | +#include <algorithm> |
| 2 | +#include <cstdint> |
| 3 | +#include <numeric> |
| 4 | +#include <optional> |
| 5 | +#include <tuple> |
| 6 | +#include <utility> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include "gtest/gtest.h" // from @googletest |
| 10 | +#include "lib/Kernel/AbstractValue.h" |
| 11 | +#include "lib/Kernel/ArithmeticDag.h" |
| 12 | +#include "lib/Kernel/EvalVisitor.h" |
| 13 | +#include "lib/Kernel/KernelImplementation.h" |
| 14 | +#include "lib/Utils/Layout/Evaluate.h" |
| 15 | +#include "lib/Utils/Layout/Utils.h" |
| 16 | +#include "mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h" // from @llvm-project |
| 17 | +#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project |
| 18 | +#include "mlir/include/mlir/IR/MLIRContext.h" // from @llvm-project |
| 19 | +#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project |
| 20 | + |
| 21 | +// copybara hack: avoid reordering include |
| 22 | +#include "fuzztest/fuzztest.h" // from @fuzztest |
| 23 | + |
| 24 | +namespace mlir { |
| 25 | +namespace heir { |
| 26 | +namespace kernel { |
| 27 | +namespace { |
| 28 | + |
| 29 | +std::vector<std::vector<int>> runDiagonalMatmul(bool isCtPt, |
| 30 | + const std::vector<int>& vecCt, |
| 31 | + const std::vector<int>& vecPt, |
| 32 | + int64_t m, int64_t n, int64_t p, |
| 33 | + bool unroll = true) { |
| 34 | + MLIRContext context; |
| 35 | + int64_t minSlots = (isCtPt ? m * n : n * p) + m * p; |
| 36 | + int64_t numSlots = 1; |
| 37 | + while (numSlots <= minSlots) numSlots *= 2; |
| 38 | + |
| 39 | + int64_t rowsCt = isCtPt ? m : n; |
| 40 | + int64_t colsCt = isCtPt ? n : p; |
| 41 | + |
| 42 | + auto layoutCt = getBicyclicLayoutRelation( |
| 43 | + RankedTensorType::get({rowsCt, colsCt}, mlir::IndexType::get(&context)), |
| 44 | + numSlots); |
| 45 | + |
| 46 | + auto packedCt = |
| 47 | + evaluateLayout<int>(layoutCt, [&](const std::vector<int64_t>& pt) { |
| 48 | + return vecCt[pt[0] * colsCt + pt[1]]; |
| 49 | + }); |
| 50 | + |
| 51 | + int64_t rowsPt = isCtPt ? n : m; |
| 52 | + int64_t colsPt = isCtPt ? p : n; |
| 53 | + int64_t contractionDim = isCtPt ? 0 : 1; |
| 54 | + int64_t stride = isCtPt ? m : p; |
| 55 | + int64_t steps = n; |
| 56 | + int64_t period = isCtPt ? m : p; |
| 57 | + |
| 58 | + RankedTensorType weightType = |
| 59 | + RankedTensorType::get({rowsPt, colsPt}, mlir::IndexType::get(&context)); |
| 60 | + auto layoutPt = |
| 61 | + getBicyclicDiagonalRelation(weightType, contractionDim, stride, numSlots); |
| 62 | + auto packedPt = |
| 63 | + evaluateLayout<int>(layoutPt, [&](const std::vector<int64_t>& pt) { |
| 64 | + return vecPt[pt[0] * colsPt + pt[1]]; |
| 65 | + }); |
| 66 | + |
| 67 | + LiteralValue secretVal = packedCt[0]; |
| 68 | + LiteralValue plainVal = packedPt; |
| 69 | + |
| 70 | + auto dag = implementRotateAndReduce( |
| 71 | + secretVal, std::optional<LiteralValue>(plainVal), period, steps, |
| 72 | + DagType::intTensor(32, {numSlots}), {}, "arith.addi", unroll); |
| 73 | + |
| 74 | + LiteralValue result = evalKernel(dag)[0]; |
| 75 | + auto resultVec = std::get<std::vector<int>>(result.get()); |
| 76 | + |
| 77 | + auto resultLayout = getBicyclicLayoutRelation( |
| 78 | + RankedTensorType::get({m, p}, mlir::IndexType::get(&context)), numSlots); |
| 79 | + // Restrict the unpacking to the first output period. |
| 80 | + addBounds(resultLayout, |
| 81 | + resultLayout.getVarKindOffset(presburger::VarKind::Range) + 1, 0, |
| 82 | + m * p - 1); |
| 83 | + return unpackLayoutToMatrix<int>(resultLayout, {resultVec}, {m, p}); |
| 84 | +} |
| 85 | + |
| 86 | +void diagonalMatmulMatchesNaive( |
| 87 | + const std::tuple<bool, int64_t, int64_t, int64_t, std::vector<int>, |
| 88 | + std::vector<int>>& args, |
| 89 | + bool unroll) { |
| 90 | + const auto& [isCtPt, m, n, p, vecCt, vecPt] = args; |
| 91 | + |
| 92 | + if (std::gcd(m, n) != 1 || std::gcd(n, p) != 1 || std::gcd(m, p) != 1) return; |
| 93 | + |
| 94 | + std::vector<std::vector<int>> expected(m, std::vector<int>(p, 0)); |
| 95 | + if (isCtPt) { |
| 96 | + for (int64_t i = 0; i < m; ++i) { |
| 97 | + for (int64_t j = 0; j < p; ++j) { |
| 98 | + for (int64_t k = 0; k < n; ++k) { |
| 99 | + expected[i][j] += vecCt[i * n + k] * vecPt[k * p + j]; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } else { |
| 104 | + for (int64_t i = 0; i < m; ++i) { |
| 105 | + for (int64_t j = 0; j < p; ++j) { |
| 106 | + for (int64_t k = 0; k < n; ++k) { |
| 107 | + expected[i][j] += vecPt[i * n + k] * vecCt[k * p + j]; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + std::vector<std::vector<int>> actual = |
| 114 | + runDiagonalMatmul(isCtPt, vecCt, vecPt, m, n, p, unroll); |
| 115 | + |
| 116 | + EXPECT_EQ(expected, actual); |
| 117 | +} |
| 118 | + |
| 119 | +auto shapeAndMatrices() { |
| 120 | + return fuzztest::FlatMap( |
| 121 | + [](bool isCtPt, int64_t m, int64_t n, int64_t p) { |
| 122 | + int64_t sizeCt = isCtPt ? m * n : n * p; |
| 123 | + int64_t sizePt = isCtPt ? n * p : m * n; |
| 124 | + return fuzztest::TupleOf( |
| 125 | + fuzztest::Just(isCtPt), fuzztest::Just(m), fuzztest::Just(n), |
| 126 | + fuzztest::Just(p), |
| 127 | + /*vecCt=*/ |
| 128 | + fuzztest::VectorOf(fuzztest::InRange(-100, 100)).WithSize(sizeCt), |
| 129 | + /*vecPt=*/ |
| 130 | + fuzztest::VectorOf(fuzztest::InRange(-100, 100)).WithSize(sizePt)); |
| 131 | + }, |
| 132 | + /*isCtPt=*/fuzztest::Arbitrary<bool>(), |
| 133 | + /*m=*/fuzztest::InRange<int64_t>(1, 16), |
| 134 | + /*n=*/fuzztest::InRange<int64_t>(1, 16), |
| 135 | + /*p=*/fuzztest::InRange<int64_t>(1, 16)); |
| 136 | +} |
| 137 | + |
| 138 | +TEST(BicyclicDiagonalMatmulFuzzTest, CtPtRegression) { |
| 139 | + diagonalMatmulMatchesNaive( |
| 140 | + {true, |
| 141 | + 3, |
| 142 | + 5, |
| 143 | + 2, |
| 144 | + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
| 145 | + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, |
| 146 | + /*unroll=*/true); |
| 147 | +} |
| 148 | + |
| 149 | +TEST(BicyclicDiagonalMatmulFuzzTest, PtCtRegression) { |
| 150 | + diagonalMatmulMatchesNaive( |
| 151 | + {false, |
| 152 | + 3, |
| 153 | + 5, |
| 154 | + 2, |
| 155 | + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 156 | + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}, |
| 157 | + /*unroll=*/true); |
| 158 | +} |
| 159 | + |
| 160 | +TEST(BicyclicDiagonalMatmulFuzzTest, UnitRowDimRegression) { |
| 161 | + diagonalMatmulMatchesNaive( |
| 162 | + {true, 1, 5, 2, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, |
| 163 | + /*unroll=*/true); |
| 164 | +} |
| 165 | + |
| 166 | +TEST(BicyclicDiagonalMatmulFuzzTest, UnitContractionDimRegression) { |
| 167 | + diagonalMatmulMatchesNaive({true, 3, 1, 2, {1, 2, 3}, {4, 5}}, |
| 168 | + /*unroll=*/true); |
| 169 | +} |
| 170 | + |
| 171 | +FUZZ_TEST(BicyclicDiagonalMatmulFuzzTest, diagonalMatmulMatchesNaive) |
| 172 | + .WithDomains(shapeAndMatrices(), fuzztest::Arbitrary<bool>()); |
| 173 | + |
| 174 | +} // namespace |
| 175 | +} // namespace kernel |
| 176 | +} // namespace heir |
| 177 | +} // namespace mlir |
0 commit comments