[Mlir-commits] [mlir] Sub-channel quantized type implementation (PR #120172)

Kevin Gleason llvmlistbot at llvm.org
Tue Feb 11 14:55:22 PST 2025


================
@@ -405,6 +572,74 @@ static void printUniformQuantizedPerAxisType(UniformQuantizedPerAxisType type,
   out << "}>";
 }
 
+/// Prints quantization parameters as a nested list of `scale`[:`zero_point`]
+/// elements.  The nesting corresponds to the `shape` dimensions.
+///
+/// Elements are delimited by commas, and the inner dimensions are enclosed in
+/// braces.  `zero_point` is only printed if it is non-zero.  For example:
+///
+///   printDenseQuantizationParameters(scales=[1.0, 2.0, 3.0, 4.0],
+///                                   zeroPoints=[0, 0, 1, 9],
+///                                   shape=[2, 2])
+///
+///   would print:
+///
+///     {{1.0, 2.0}, {3.0:1, 4.0:9}}
+void printDenseQuantizationParameters(ArrayRef<APFloat> scales,
+                                      ArrayRef<APInt> zeroPoints,
+                                      ArrayRef<int64_t> shape,
+                                      DialectAsmPrinter &out) {
+  int64_t rank = shape.size();
+  SmallVector<unsigned, 4> counter(rank, 0);
+  unsigned openBrackets = 0;
+
+  auto bumpCounter = [&]() {
+    ++counter[rank - 1];
+    for (unsigned i = rank - 1; i > 0; --i) {
+      if (counter[i] >= shape[i]) {
+        counter[i] = 0;
+        ++counter[i - 1];
+        --openBrackets;
+        out << '}';
+      }
+    }
+  };
+
+  for (unsigned idx = 0, e = scales.size(); idx != e; ++idx) {
----------------
GleasonK wrote:

`idx < e` ?

https://github.com/llvm/llvm-project/pull/120172


More information about the Mlir-commits mailing list