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