[Mlir-commits] [mlir] [MLIR][Presburger] Add Gram-Schmidt (PR #70843)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Nov 2 07:07:59 PDT 2023


https://github.com/Abhinav271828 updated https://github.com/llvm/llvm-project/pull/70843

>From 060e3c8f7f76e083723e4c303ec396447e6a1df3 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 31 Oct 2023 17:55:43 +0000
Subject: [PATCH 1/6] Add gram-schmidt and tests

---
 .../include/mlir/Analysis/Presburger/Matrix.h |  5 ++++
 mlir/include/mlir/Analysis/Presburger/Utils.h |  5 ++++
 mlir/lib/Analysis/Presburger/Matrix.cpp       | 23 +++++++++++++++++++
 mlir/lib/Analysis/Presburger/Utils.cpp        |  8 +++++++
 .../Analysis/Presburger/MatrixTest.cpp        | 15 ++++++++++++
 5 files changed, 56 insertions(+)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index 4d9f13832e0692a..b591b0b4fdad167 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -265,6 +265,11 @@ class FracMatrix : public Matrix<Fraction> {
   // does not exist, which happens iff det = 0.
   // Assert-fails if the matrix is not square.
   Fraction determinant(FracMatrix *inverse = nullptr) const;
+
+  // Computes the Gram-Schmidt orthogonalisation
+  // of the matrix (cubic time).
+  FracMatrix gramSchmidt() const;
+
 };
 
 } // namespace presburger
diff --git a/mlir/include/mlir/Analysis/Presburger/Utils.h b/mlir/include/mlir/Analysis/Presburger/Utils.h
index a451ae8bf55723e..639683cea0d9f2e 100644
--- a/mlir/include/mlir/Analysis/Presburger/Utils.h
+++ b/mlir/include/mlir/Analysis/Presburger/Utils.h
@@ -276,6 +276,11 @@ SmallVector<MPInt, 8> getNegatedCoeffs(ArrayRef<MPInt> coeffs);
 /// a_1 x_1 + ... + a_n x_ + c < 0, i.e., -a_1 x_1 - ... - a_n x_ - c - 1 >= 0,
 /// since all the variables are constrained to be integers.
 SmallVector<MPInt, 8> getComplementIneq(ArrayRef<MPInt> ineq);
+
+/// Compute the dot product of two vectors.
+/// Assumes that the vectors have the same sizes.
+Fraction dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b);
+
 } // namespace presburger
 } // namespace mlir
 
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index ae97e456d9820cf..7669e461f079472 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -548,4 +548,27 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
     determinant *= m.at(i, i);
 
   return determinant;
+}
+
+FracMatrix FracMatrix::gramSchmidt() const {
+
+    // Create a copy of the argument to store
+    // the orthogonalised version.
+    FracMatrix orth(*this);
+    Fraction projectionScale;
+
+    // For each vector (row) in the matrix,
+    // subtract its unit projection along
+    // each of the previous vectors.
+    // This ensures that it has no component
+    // in the direction of any of the
+    // previous vectors.
+    for (unsigned i = 1; i < getNumRows(); i++) {
+        for (unsigned j = 0; j < i; j++) {
+            projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+                              dotProduct(orth.getRow(j), orth.getRow(j));
+            orth.addToRow(j, i, -projectionScale);
+        }
+    }
+    return orth;
 }
\ No newline at end of file
diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 9aef2f5de109357..30f01f545819101 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -520,3 +520,11 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
   std::transform(range.begin(), range.end(), result.begin(), int64FromMPInt);
   return result;
 }
+
+Fraction presburger::dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b) {
+  assert(a.size() == b.size() && "Dot product of two unequal vectors!");
+  Fraction sum = 0;
+  for (unsigned i = 0; i < a.size(); i++)
+    sum += a[i] * b[i];
+  return sum;
+}
\ No newline at end of file
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index d05b05e004c5c5f..b7cc8e07a54e1ea 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -310,3 +310,18 @@ TEST(MatrixTest, intInverse) {
 
   EXPECT_EQ(det, 0);
 }
+
+TEST(MatrixTest, gramSchmidt) {
+  FracMatrix mat = makeFracMatrix(3, 5, {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
+                                         {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1), Fraction(13, 1), Fraction(20, 1)},
+                                         {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1), Fraction(16, 1), Fraction(24, 1)}});
+
+  FracMatrix gramSchmidt = makeFracMatrix(3, 5,
+         {{Fraction(3, 1),     Fraction(4, 1),     Fraction(5, 1),    Fraction(12, 1),     Fraction(19, 1)},
+          {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111), Fraction(13, 185),   Fraction(-262, 555)},
+          {Fraction(53, 463),  Fraction(27, 463),  Fraction(1, 463),  Fraction(-181, 463), Fraction(100, 463)}});
+
+  FracMatrix gs = mat.gramSchmidt();
+
+  EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
+}
\ No newline at end of file

>From b42b51c0c1fc8599fbec422f262672cad1bc41d9 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 10:39:47 +0000
Subject: [PATCH 2/6] Minor fixes

---
 mlir/include/mlir/Analysis/Presburger/Utils.h | 2 +-
 mlir/lib/Analysis/Presburger/Matrix.cpp       | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Utils.h b/mlir/include/mlir/Analysis/Presburger/Utils.h
index 639683cea0d9f2e..3fb978e4faf6dac 100644
--- a/mlir/include/mlir/Analysis/Presburger/Utils.h
+++ b/mlir/include/mlir/Analysis/Presburger/Utils.h
@@ -278,7 +278,7 @@ SmallVector<MPInt, 8> getNegatedCoeffs(ArrayRef<MPInt> coeffs);
 SmallVector<MPInt, 8> getComplementIneq(ArrayRef<MPInt> ineq);
 
 /// Compute the dot product of two vectors.
-/// Assumes that the vectors have the same sizes.
+/// The vectors must have the same sizes.
 Fraction dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b);
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 7669e461f079472..896a45cef7361a9 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,11 +551,9 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
 }
 
 FracMatrix FracMatrix::gramSchmidt() const {
-
     // Create a copy of the argument to store
     // the orthogonalised version.
     FracMatrix orth(*this);
-    Fraction projectionScale;
 
     // For each vector (row) in the matrix,
     // subtract its unit projection along
@@ -565,8 +563,8 @@ FracMatrix FracMatrix::gramSchmidt() const {
     // previous vectors.
     for (unsigned i = 1; i < getNumRows(); i++) {
         for (unsigned j = 0; j < i; j++) {
-            projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
-                              dotProduct(orth.getRow(j), orth.getRow(j));
+            Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+                                       dotProduct(orth.getRow(j), orth.getRow(j));
             orth.addToRow(j, i, -projectionScale);
         }
     }

>From bd5be3b1712eb5408ea1b2f07b4b6ae526155292 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 10:50:28 +0000
Subject: [PATCH 3/6] Test for zero dot product

---
 mlir/unittests/Analysis/Presburger/MatrixTest.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index b7cc8e07a54e1ea..7e94c717a985bc8 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -324,4 +324,7 @@ TEST(MatrixTest, gramSchmidt) {
   FracMatrix gs = mat.gramSchmidt();
 
   EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
+  for (unsigned i = 0; i < 3u; i++)
+    for (unsigned j = i+1; j < 3u; j++)
+      EXPECT_EQ(dotProduct(gramSchmidt.getRow(i), gramSchmidt.getRow(j)), 0);
 }
\ No newline at end of file

>From 94beea95b63abf17a96d6beca7afb43c4f89760d Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 13:44:36 +0000
Subject: [PATCH 4/6] Add assert for linear independence

---
 mlir/include/mlir/Analysis/Presburger/Matrix.h | 3 ++-
 mlir/lib/Analysis/Presburger/Matrix.cpp        | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index b591b0b4fdad167..d0df429e225945d 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -267,7 +267,8 @@ class FracMatrix : public Matrix<Fraction> {
   Fraction determinant(FracMatrix *inverse = nullptr) const;
 
   // Computes the Gram-Schmidt orthogonalisation
-  // of the matrix (cubic time).
+  // of the rows of matrix (cubic time).
+  // The rows of the matrix must be linearly independent.
   FracMatrix gramSchmidt() const;
 
 };
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 896a45cef7361a9..2bfc07fa8402ec8 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,6 +551,9 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
 }
 
 FracMatrix FracMatrix::gramSchmidt() const {
+    bool linIndep = (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
+    assert(linIndep && "the vectors must be linearly independent!");
+
     // Create a copy of the argument to store
     // the orthogonalised version.
     FracMatrix orth(*this);

>From 6769effdb2a8d27530a1a537f6a8598527256ceb Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 13:46:26 +0000
Subject: [PATCH 5/6] Formatting

---
 .../include/mlir/Analysis/Presburger/Matrix.h |  1 -
 mlir/lib/Analysis/Presburger/Matrix.cpp       | 41 ++++++++++---------
 .../Analysis/Presburger/MatrixTest.cpp        | 27 ++++++++----
 3 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Matrix.h b/mlir/include/mlir/Analysis/Presburger/Matrix.h
index d0df429e225945d..89fad85c0c3374a 100644
--- a/mlir/include/mlir/Analysis/Presburger/Matrix.h
+++ b/mlir/include/mlir/Analysis/Presburger/Matrix.h
@@ -270,7 +270,6 @@ class FracMatrix : public Matrix<Fraction> {
   // of the rows of matrix (cubic time).
   // The rows of the matrix must be linearly independent.
   FracMatrix gramSchmidt() const;
-
 };
 
 } // namespace presburger
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 2bfc07fa8402ec8..c2adea94216b14b 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -551,25 +551,26 @@ Fraction FracMatrix::determinant(FracMatrix *inverse) const {
 }
 
 FracMatrix FracMatrix::gramSchmidt() const {
-    bool linIndep = (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
-    assert(linIndep && "the vectors must be linearly independent!");
-
-    // Create a copy of the argument to store
-    // the orthogonalised version.
-    FracMatrix orth(*this);
-
-    // For each vector (row) in the matrix,
-    // subtract its unit projection along
-    // each of the previous vectors.
-    // This ensures that it has no component
-    // in the direction of any of the
-    // previous vectors.
-    for (unsigned i = 1; i < getNumRows(); i++) {
-        for (unsigned j = 0; j < i; j++) {
-            Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
-                                       dotProduct(orth.getRow(j), orth.getRow(j));
-            orth.addToRow(j, i, -projectionScale);
-        }
+  bool linIndep =
+      (nRows < nColumns) || (nRows == nColumns && determinant(nullptr) != 0);
+  assert(linIndep && "the vectors must be linearly independent!");
+
+  // Create a copy of the argument to store
+  // the orthogonalised version.
+  FracMatrix orth(*this);
+
+  // For each vector (row) in the matrix,
+  // subtract its unit projection along
+  // each of the previous vectors.
+  // This ensures that it has no component
+  // in the direction of any of the
+  // previous vectors.
+  for (unsigned i = 1; i < getNumRows(); i++) {
+    for (unsigned j = 0; j < i; j++) {
+      Fraction projectionScale = dotProduct(orth.getRow(i), orth.getRow(j)) /
+                                 dotProduct(orth.getRow(j), orth.getRow(j));
+      orth.addToRow(j, i, -projectionScale);
     }
-    return orth;
+  }
+  return orth;
 }
\ No newline at end of file
diff --git a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
index 7e94c717a985bc8..1c4f91884b346ec 100644
--- a/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MatrixTest.cpp
@@ -312,19 +312,28 @@ TEST(MatrixTest, intInverse) {
 }
 
 TEST(MatrixTest, gramSchmidt) {
-  FracMatrix mat = makeFracMatrix(3, 5, {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1), Fraction(19, 1)},
-                                         {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1), Fraction(13, 1), Fraction(20, 1)},
-                                         {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1), Fraction(16, 1), Fraction(24, 1)}});
-
-  FracMatrix gramSchmidt = makeFracMatrix(3, 5,
-         {{Fraction(3, 1),     Fraction(4, 1),     Fraction(5, 1),    Fraction(12, 1),     Fraction(19, 1)},
-          {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111), Fraction(13, 185),   Fraction(-262, 555)},
-          {Fraction(53, 463),  Fraction(27, 463),  Fraction(1, 463),  Fraction(-181, 463), Fraction(100, 463)}});
+  FracMatrix mat =
+      makeFracMatrix(3, 5,
+                     {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1),
+                       Fraction(12, 1), Fraction(19, 1)},
+                      {Fraction(4, 1), Fraction(5, 1), Fraction(6, 1),
+                       Fraction(13, 1), Fraction(20, 1)},
+                      {Fraction(7, 1), Fraction(8, 1), Fraction(9, 1),
+                       Fraction(16, 1), Fraction(24, 1)}});
+
+  FracMatrix gramSchmidt = makeFracMatrix(
+      3, 5,
+      {{Fraction(3, 1), Fraction(4, 1), Fraction(5, 1), Fraction(12, 1),
+        Fraction(19, 1)},
+       {Fraction(142, 185), Fraction(383, 555), Fraction(68, 111),
+        Fraction(13, 185), Fraction(-262, 555)},
+       {Fraction(53, 463), Fraction(27, 463), Fraction(1, 463),
+        Fraction(-181, 463), Fraction(100, 463)}});
 
   FracMatrix gs = mat.gramSchmidt();
 
   EXPECT_EQ_FRAC_MATRIX(gs, gramSchmidt);
   for (unsigned i = 0; i < 3u; i++)
-    for (unsigned j = i+1; j < 3u; j++)
+    for (unsigned j = i + 1; j < 3u; j++)
       EXPECT_EQ(dotProduct(gramSchmidt.getRow(i), gramSchmidt.getRow(j)), 0);
 }
\ No newline at end of file

>From e352ed9772e0665f114f47098d29d66053aca37f Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Thu, 2 Nov 2023 14:07:29 +0000
Subject: [PATCH 6/6] Formatting

---
 mlir/lib/Analysis/Presburger/Utils.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Analysis/Presburger/Utils.cpp b/mlir/lib/Analysis/Presburger/Utils.cpp
index 30f01f545819101..a11bd9db005855d 100644
--- a/mlir/lib/Analysis/Presburger/Utils.cpp
+++ b/mlir/lib/Analysis/Presburger/Utils.cpp
@@ -521,7 +521,8 @@ SmallVector<int64_t, 8> presburger::getInt64Vec(ArrayRef<MPInt> range) {
   return result;
 }
 
-Fraction presburger::dotProduct(MutableArrayRef<Fraction> a, MutableArrayRef<Fraction> b) {
+Fraction presburger::dotProduct(MutableArrayRef<Fraction> a,
+                                MutableArrayRef<Fraction> b) {
   assert(a.size() == b.size() && "Dot product of two unequal vectors!");
   Fraction sum = 0;
   for (unsigned i = 0; i < a.size(); i++)



More information about the Mlir-commits mailing list