src/test/scala/chipsldpc/EndToEnd.scala

Source from the repository checkout used to build these docs. Line numbers link to individual source lines.

  1package chipsldpc
  2
  3import chisel3._
  4import chisel3.util.Cat
  5import chipsldpc.graph.{TannerGraph, TannerNodeGraphs}
  6import java.nio.file.{Files, Path, Paths, StandardCopyOption}
  7
  8/** One validated, fixed-quantization decoder experiment. */
  9private[chipsldpc] final case class DecoderProblem(
 10    schema: Int,
 11    n: Int,
 12    rowOnes: Vector[Vector[Int]],
 13    prior: Vector[Int],
 14    syndrome: Vector[Boolean],
 15    iterations: Int,
 16) {
 17  require(schema == 1, "unsupported problem schema")
 18  val graph: TannerGraph = TannerGraph.fromRows(n, rowOnes)
 19  val nodes: TannerNodeGraphs = TannerNodeGraphs.from(graph)
 20  require(nodes.checks.forall(_.degree >= 2), "check degrees must be at least two")
 21  require(prior.size == n, "prior length must equal n")
 22  require(prior.forall(p => p >= 0 && p < (1 << RelayDefaults.q.magnitudeBits)), "prior is out of range")
 23  require(syndrome.size == graph.checkCount, "syndrome length must equal the row count")
 24  require(iterations > 0, "iterations must be positive")
 25
 26  def control(iteration: Int): Int =
 27    iteration.min((1 << RelayDefaults.scale.controlBits) - 1)
 28}
 29
 30private[chipsldpc] object DecoderProblem {
 31  private val Fields = Set("schema", "n", "row_ones", "prior", "syndrome", "iterations")
 32
 33  private def integer(value: ujson.Value): Int = {
 34    val number = value.num
 35    require(number.isValidInt && number == number.toInt, s"expected an integer, got $number")
 36    number.toInt
 37  }
 38
 39  def parse(text: String): DecoderProblem = {
 40    val json = ujson.read(text)
 41    require(json.obj.keySet == Fields, s"expected fields: ${Fields.toSeq.sorted.mkString(", ")}")
 42    def vector(name: String): Vector[Int] = json(name).arr.map(integer).toVector
 43    val syndrome = vector("syndrome")
 44    require(syndrome.forall(bit => bit == 0 || bit == 1), "syndrome entries must be binary")
 45    DecoderProblem(
 46      integer(json("schema")),
 47      integer(json("n")),
 48      json("row_ones").arr.map(_.arr.map(integer).toVector).toVector,
 49      vector("prior"),
 50      syndrome.map(_ != 0),
 51      integer(json("iterations")),
 52    )
 53  }
 54
 55  def read(path: Path): DecoderProblem = parse(Files.readString(path))
 56}
 57
 58/** Packed, graph-independent Verilator ABI around the production static core. */
 59private[chipsldpc] final class StaticTannerArtifact(nodes: TannerNodeGraphs) extends Module {
 60  override def desiredName: String = "StaticTannerArtifact"
 61  private val graph = nodes.graph
 62  private val q = RelayDefaults.q
 63
 64  val loadValid = IO(Input(Bool()))
 65  val loadReady = IO(Output(Bool()))
 66  val syndrome = IO(Input(UInt(graph.checkCount.W)))
 67  val prior = IO(Input(UInt((graph.variableCount * q.magnitudeBits).W)))
 68  val stepValid = IO(Input(Bool()))
 69  val stepReady = IO(Output(Bool()))
 70  val stepControl = IO(Input(UInt(RelayDefaults.scale.controlBits.W)))
 71  val resultValid = IO(Output(Bool()))
 72  val marginal = IO(Output(UInt((graph.variableCount * q.accumulatorBits).W)))
 73  val correction = IO(Output(UInt(graph.variableCount.W)))
 74  val residual = IO(Output(UInt(graph.checkCount.W)))
 75  val converged = IO(Output(Bool()))
 76
 77  private val core = Module(new StaticTannerDatapath(nodes))
 78  core.io.load.valid := loadValid
 79  loadReady := core.io.load.ready
 80  core.io.load.bits.syndrome.zipWithIndex.foreach { case (bit, i) => bit := syndrome(i) }
 81  core.io.load.bits.prior.zipWithIndex.foreach { case (value, i) =>
 82    value := prior((i + 1) * q.magnitudeBits - 1, i * q.magnitudeBits)
 83  }
 84  core.io.step.valid := stepValid
 85  core.io.step.bits := stepControl
 86  stepReady := core.io.step.ready
 87
 88  resultValid := core.io.result.valid
 89  marginal := Cat(core.io.result.bits.marginal.reverse.map(_.asUInt))
 90  correction := Cat(core.io.result.bits.decision.reverse)
 91  residual := Cat(core.io.result.bits.residual.reverse)
 92  converged := core.io.result.bits.converged
 93}
 94
 95/** Emit one specialized artifact and its independent per-iteration golden records. */
 96object EndToEnd {
 97  private def bit(value: Boolean): Int = if (value) 1 else 0
 98
 99  private[chipsldpc] def golden(problem: DecoderProblem): String = {
100    val q = RelayDefaults.q
101    val lines = Vector.newBuilder[String]
102    lines += Seq(
103      problem.graph.checkCount, problem.n, q.magnitudeBits, q.accumulatorBits,
104      problem.iterations, RelayDefaults.priorScale,
105    ).mkString(" ")
106    lines += (problem.syndrome.map(bit) ++ problem.prior).mkString(" ")
107
108    var state = Reference.initialize(problem.nodes, problem.prior, q)
109    for (iteration <- 1 to problem.iterations) {
110      val control = problem.control(iteration)
111      state = Reference.iterate(
112        problem.nodes, state, problem.syndrome, problem.prior,
113        control, q, RelayDefaults.scale,
114      )
115      val residual = Reference.residual(problem.nodes, state, problem.syndrome)
116      lines += (
117        Seq(control) ++ state.marginal ++ state.decision.map(bit) ++
118          residual.map(bit) ++ Seq(bit(!residual.contains(true)))
119      ).mkString(" ")
120    }
121    lines.result().mkString("", "\n", "\n")
122  }
123
124  def main(args: Array[String]): Unit = {
125    require(args.length == 2, "usage: EndToEnd <problem.json> <output-directory>")
126    val input = Paths.get(args(0))
127    val output = Paths.get(args(1))
128    val problem = DecoderProblem.read(input)
129    Files.createDirectories(output)
130    Files.copy(input, output.resolve("problem.json"), StandardCopyOption.REPLACE_EXISTING)
131    Files.writeString(output.resolve("golden.txt"), golden(problem))
132    Generate.emit("StaticTannerArtifact", () => new StaticTannerArtifact(problem.nodes), output)
133    println(output.toAbsolutePath)
134  }
135}