src/test/scala/chipsldpc/BpDecodersSpec.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.simulator.scalatest.ChiselSim
5import chisel3.util.{Decoupled, Valid, log2Ceil}
6import chipsldpc.graph.{TannerGraph, TannerNodeGraphs}
7import org.scalatest.freespec.AnyFreeSpec
8
9private final class TraceRelayCoefficients(trace: Vector[Vector[Int]])
10 extends RelayCoefficientSource(trace.head.size, LfsrRelayCoefficients.BetaBits) {
11 require(trace.nonEmpty && trace.forall(_.size == variableCount))
12 private val indexBits = math.max(1, log2Ceil(trace.size))
13 private val index = RegInit(0.U(indexBits.W))
14 private val table = VecInit(trace.map(row => VecInit(row.map(_.U(betaBits.W)))))
15
16 when(io.frameStart) {
17 index := 0.U
18 }.elsewhen(io.advanceLeg && index =/= (trace.size - 1).U) {
19 index := index + 1.U
20 }
21 if (trace.size == 1) io.beta := table.head else io.beta := table(index)
22}
23
24private final class RelayCoreHarness(
25 nodes: TannerNodeGraphs,
26 q: Quantization,
27 scale: CheckScale,
28 format: RelayFormat,
29) extends Module {
30 private val graph = nodes.graph
31 val io = IO(new Bundle {
32 val load = Flipped(Decoupled(new StaticDecoderInput(graph.checkCount, graph.variableCount, q)))
33 val step = Flipped(Decoupled(UInt(scale.controlBits.W)))
34 val beta = Input(Vec(graph.variableCount, UInt(format.betaBits.W)))
35 val restartMessages = Input(Bool())
36 val bias = Output(Vec(graph.variableCount, SInt(q.accumulatorBits.W)))
37 val commit = Valid(new StaticDecoderResult(graph.checkCount, graph.variableCount, q))
38 val result = Valid(new StaticDecoderResult(graph.checkCount, graph.variableCount, q))
39 })
40 private val core = Module(new StaticTannerCore(nodes, q, scale))
41 core.io.load <> io.load
42 core.io.step <> io.step
43 core.io.restartMessages := io.restartMessages
44 core.io.bias.zipWithIndex.foreach { case (bias, i) =>
45 bias := RelayBias(core.io.prior(i), core.io.result.bits.marginal(i), io.beta(i), q, format)
46 }
47 io.bias := core.io.bias
48 io.commit := core.io.commit
49 io.result := core.io.result
50}
51
52final class BpDecodersSpec extends AnyFreeSpec with ChiselSim {
53 private type Expected = Reference.DecoderOutcome
54
55 private def nodes(variables: Int, rows: Seq[Seq[Int]]): TannerNodeGraphs =
56 TannerNodeGraphs.from(TannerGraph.fromRows(variables, rows))
57
58 private def reset(dut: VanillaBpDecoder): Unit = {
59 dut.io.in.valid.poke(false.B)
60 dut.io.out.ready.poke(false.B)
61 dut.reset.poke(true.B)
62 dut.clock.step()
63 dut.reset.poke(false.B)
64 }
65
66 private def reset(dut: RelayBpDecoder): Unit = {
67 dut.io.in.valid.poke(false.B)
68 dut.io.out.ready.poke(false.B)
69 dut.reset.poke(true.B)
70 dut.clock.step()
71 dut.reset.poke(false.B)
72 }
73
74 private def expectResult(port: BpDecoderResult, expected: Expected): Unit = {
75 port.success.expect(expected.success.B)
76 port.correction.zip(expected.correction).foreach { case (actual, value) => actual.expect(value.B) }
77 port.marginal.zip(expected.state.marginal).foreach { case (actual, value) => actual.expect(value.S) }
78 port.residual.zip(expected.residual).foreach { case (actual, value) => actual.expect(value.B) }
79 port.totalIterations.expect(expected.iterations.U)
80 port.legsExecuted.expect(expected.legs.U)
81 port.solutionsFound.expect(expected.solutions.U)
82 }
83
84 private def input(
85 port: StaticDecoderInput,
86 priors: Vector[Int],
87 syndrome: Vector[Boolean],
88 ): Unit = {
89 port.prior.zip(priors).foreach { case (actual, value) => actual.poke(value.U) }
90 port.syndrome.zip(syndrome).foreach { case (actual, value) => actual.poke(value.B) }
91 }
92
93 private def expectStatic(
94 result: StaticDecoderResult,
95 state: Reference.DecoderState,
96 residual: Vector[Boolean],
97 ): Unit = {
98 result.marginal.zip(state.marginal).foreach { case (actual, value) => actual.expect(value.S) }
99 result.decision.zip(state.decision).foreach { case (actual, value) => actual.expect(value.B) }
100 result.residual.zip(residual).foreach { case (actual, value) => actual.expect(value.B) }
101 result.converged.expect((!residual.contains(true)).B)
102 }
103
104 private def runVanilla(
105 dut: VanillaBpDecoder,
106 priors: Vector[Int],
107 syndrome: Vector[Boolean],
108 expected: Expected,
109 outputStalls: Int = 0,
110 ): Unit = {
111 input(dut.io.in.bits, priors, syndrome)
112 dut.io.in.valid.poke(true.B)
113 dut.io.in.ready.expect(true.B)
114 dut.clock.step()
115 dut.io.in.valid.poke(false.B)
116 var elapsed = 0
117 while (!dut.io.out.valid.peek().litToBoolean && elapsed <= 2 * expected.iterations) {
118 dut.clock.step()
119 elapsed += 1
120 }
121 assert(elapsed == 2 * expected.iterations)
122 expectResult(dut.io.out.bits, expected)
123 for (_ <- 0 until outputStalls) {
124 dut.clock.step()
125 dut.io.out.valid.expect(true.B)
126 expectResult(dut.io.out.bits, expected)
127 }
128 dut.io.out.ready.poke(true.B)
129 dut.clock.step()
130 dut.io.out.ready.poke(false.B)
131 dut.io.in.ready.expect(true.B)
132 }
133
134 private def runRelay(
135 dut: RelayBpDecoder,
136 priors: Vector[Int],
137 syndrome: Vector[Boolean],
138 expected: Expected,
139 outputStalls: Int = 0,
140 ): Unit = {
141 input(dut.io.in.bits, priors, syndrome)
142 dut.io.in.valid.poke(true.B)
143 dut.io.in.ready.expect(true.B)
144 dut.clock.step()
145 dut.io.in.valid.poke(false.B)
146 val trace = Vector.newBuilder[(Int, Int, Int, Boolean)]
147 var elapsed = 0
148 var previousBoundary = 0
149 while (!dut.io.out.valid.peek().litToBoolean && elapsed <= 2 * expected.iterations) {
150 if (dut.io.legTrace.valid.peek().litToBoolean) {
151 val boundary = elapsed + 1
152 trace += ((
153 dut.io.legTrace.bits.index.peek().litValue.toInt,
154 dut.io.legTrace.bits.iterations.peek().litValue.toInt,
155 boundary - previousBoundary,
156 dut.io.legTrace.bits.converged.peek().litToBoolean,
157 ))
158 previousBoundary = boundary
159 }
160 dut.clock.step()
161 elapsed += 1
162 }
163 assert(elapsed == 2 * expected.iterations,
164 s"Relay took $elapsed cycles for ${expected.iterations} iterations")
165 assert(trace.result() == expected.legOutcomes.zipWithIndex.map { case (outcome, index) =>
166 (index, outcome.iterations, 2 * outcome.iterations, outcome.converged)
167 })
168 expectResult(dut.io.out.bits, expected)
169 for (_ <- 0 until outputStalls) {
170 dut.clock.step()
171 dut.io.out.valid.expect(true.B)
172 expectResult(dut.io.out.bits, expected)
173 }
174 dut.io.out.ready.poke(true.B)
175 dut.clock.step()
176 dut.io.out.ready.poke(false.B)
177 dut.io.in.ready.expect(true.B)
178 }
179
180 "vanilla BP remains cycle- and bit-exact across consecutive frames" in {
181 val steane = nodes(7, Seq(Seq(3, 4, 5, 6), Seq(1, 2, 5, 6), Seq(0, 2, 4, 6)))
182 val config = VanillaBpConfig(steane, iterations = 4)
183 val priors = Vector(1, 2, 3, 4, 5, 6, 7)
184 simulate(new VanillaBpDecoder(config)) { dut =>
185 reset(dut)
186 for (word <- 0 until 8) {
187 val syndrome = Vector.tabulate(3)(bit => ((word >> bit) & 1) != 0)
188 runVanilla(
189 dut, priors, syndrome, Reference.runVanilla(config, priors, syndrome),
190 outputStalls = word % 3,
191 )
192 }
193 }
194 }
195
196 "a runtime BP limit reuses a larger elaborated decoder" in {
197 val graph = nodes(3, Seq(Seq(0, 1), Seq(1, 2), Seq(0, 2)))
198 val capacity = VanillaBpConfig(graph, iterations = 4)
199 val limited = capacity.copy(iterations = 1)
200 val priors = Vector(2, 1, 1)
201 val syndrome = Vector.fill(3)(true)
202 val expected = Reference.runVanilla(limited, priors, syndrome)
203 simulate(new VanillaBpDecoder(capacity, runtimeIterationLimit = true)) { dut =>
204 reset(dut)
205 dut.iterationLimit.get.poke(1.U)
206 runVanilla(dut, priors, syndrome, expected)
207 }
208 }
209
210 "a runtime Relay limit stops at a prefix of the elaborated legs" in {
211 val graph = nodes(3, Seq(Seq(0, 1), Seq(1, 2), Seq(0, 2)))
212 val capacity = RelayBpConfig(
213 graph, initialIterations = 1, relayIterations = 1,
214 maximumLegs = 3, solutionTarget = 1,
215 )
216 val limited = capacity.copy(maximumLegs = 1)
217 val beta = Vector(Vector.fill(3)(7), Vector.fill(3)(3), Vector.fill(3)(8))
218 val priors = Vector(2, 1, 1)
219 val syndrome = Vector.fill(3)(true)
220 val expected = Reference.runRelay(limited, priors, syndrome, beta.take(1))
221 assert(expected.legs == 1)
222 simulate(new RelayBpDecoder(
223 capacity, Some(() => new TraceRelayCoefficients(beta)), runtimeLegLimit = true,
224 )) { dut =>
225 reset(dut)
226 dut.legLimit.get.poke(1.U)
227 runRelay(dut, priors, syndrome, expected)
228 }
229 }
230
231 "beta eight makes a one-leg Relay decoder identical to vanilla BP" in {
232 val steane = nodes(7, Seq(Seq(3, 4, 5, 6), Seq(1, 2, 5, 6), Seq(0, 2, 4, 6)))
233 val priors = Vector(1, 2, 3, 4, 5, 6, 7)
234 val vanillaConfig = VanillaBpConfig(
235 steane, iterations = 4, q = RelayDefaults.paperQ,
236 scale = RampScale(RelayDefaults.paperQ.magnitudeBits),
237 )
238 val relayConfig = RelayBpConfig(steane, initialIterations = 4, maximumLegs = 1)
239 val beta = Vector(Vector.fill(7)(8))
240 for (word <- 0 until 8) {
241 val syndrome = Vector.tabulate(3)(bit => ((word >> bit) & 1) != 0)
242 val a = Reference.runVanilla(vanillaConfig, priors, syndrome)
243 val b = Reference.runRelay(relayConfig, priors, syndrome, beta)
244 assert(a == b)
245 }
246 simulate(new RelayBpDecoder(
247 relayConfig, Some(() => new TraceRelayCoefficients(beta)),
248 )) { dut =>
249 reset(dut)
250 for (word <- 0 until 8) {
251 val syndrome = Vector.tabulate(3)(bit => ((word >> bit) & 1) != 0)
252 runRelay(dut, priors, syndrome, Reference.runRelay(relayConfig, priors, syndrome, beta))
253 }
254 }
255 }
256
257 "later legs replace a heavier solution and incur no control cycles" in {
258 val graph = nodes(3, Seq(Seq(0, 1, 2)))
259 val config = RelayBpConfig(
260 graph, initialIterations = 1, relayIterations = 1,
261 maximumLegs = 3, solutionTarget = 2,
262 )
263 val beta = Vector(Vector.fill(3)(7), Vector.fill(3)(3), Vector(3, 8, 8))
264 val priors = Vector.fill(3)(1)
265 val syndrome = Vector(true)
266 val expected = Reference.runRelay(config, priors, syndrome, beta)
267 assert(expected.success && expected.iterations == 3 && expected.legs == 3)
268 assert(expected.solutions == 2 && expected.correction == Vector(true, false, false))
269 simulate(new RelayBpDecoder(
270 config, Some(() => new TraceRelayCoefficients(beta)),
271 )) { dut =>
272 reset(dut)
273 runRelay(dut, priors, syndrome, expected, outputStalls = 4)
274 }
275 }
276
277 "the shared core preserves marginals, resets messages, and restarts alpha at a leg boundary" in {
278 val graph = nodes(3, Seq(Seq(0, 1, 2)))
279 val q = RelayDefaults.paperQ
280 val scale = RampScale(q.magnitudeBits)
281 val format = RelayDefaults.paperFormat
282 val priors = Vector.fill(3)(1)
283 val syndrome = Vector(true)
284 val beta = Vector(Vector.fill(3)(7), Vector.fill(3)(3), Vector(3, 8, 8))
285 var expected = Reference.initialize(graph, priors, q)
286 val initialMessages = expected.v2c
287
288 simulate(new RelayCoreHarness(graph, q, scale, format)) { dut =>
289 dut.io.load.valid.poke(false.B)
290 dut.io.step.valid.poke(false.B)
291 dut.io.step.bits.poke(1.U)
292 dut.io.restartMessages.poke(false.B)
293 dut.io.beta.foreach(_.poke(7.U))
294 dut.reset.poke(true.B)
295 dut.clock.step()
296 dut.reset.poke(false.B)
297
298 input(dut.io.load.bits, priors, syndrome)
299 dut.io.load.valid.poke(true.B)
300 dut.clock.step()
301 dut.io.load.valid.poke(false.B)
302
303 beta.zipWithIndex.foreach { case (legBeta, leg) =>
304 dut.io.beta.zip(legBeta).foreach { case (actual, value) => actual.poke(value.U) }
305 val expectedBias = priors.indices.map { i =>
306 Reference.relayBias(priors(i), expected.marginal(i), legBeta(i), format, q)
307 }
308 dut.io.bias.zip(expectedBias).foreach { case (actual, value) => actual.expect(value.S) }
309
310 dut.io.step.valid.poke(true.B)
311 dut.io.step.ready.expect(true.B)
312 dut.clock.step()
313 dut.io.step.valid.poke(false.B)
314 expected = Reference.iterate(graph, expected, syndrome, expectedBias, 1, q, scale)
315 val residual = Reference.residual(graph, expected, syndrome)
316 dut.io.commit.valid.expect(true.B)
317 expectStatic(dut.io.commit.bits, expected, residual)
318
319 dut.io.restartMessages.poke((leg + 1 < beta.size).B)
320 dut.clock.step()
321 dut.io.restartMessages.poke(false.B)
322 dut.io.result.valid.expect(true.B)
323 expectStatic(dut.io.result.bits, expected, residual)
324 if (leg + 1 < beta.size) expected = expected.copy(v2c = initialMessages)
325 }
326 }
327 }
328
329 "equal-score ties keep the first solution and duplicate solutions count" in {
330 val graph = nodes(2, Seq(Seq(0, 1)))
331 val config = RelayBpConfig(
332 graph, initialIterations = 1, relayIterations = 1,
333 maximumLegs = 3, solutionTarget = 2,
334 )
335 val priors = Vector.fill(2)(1)
336 val syndrome = Vector(true)
337 val cases = Seq(
338 Vector(Vector(7, 7), Vector(3, 8), Vector(8, 3)),
339 Vector(Vector(7, 7), Vector(3, 8), Vector(3, 8)),
340 )
341 cases.foreach { beta =>
342 val expected = Reference.runRelay(config, priors, syndrome, beta)
343 assert(expected.solutions == 2 && expected.correction == Vector(true, false))
344 simulate(new RelayBpDecoder(
345 config, Some(() => new TraceRelayCoefficients(beta)),
346 )) { dut =>
347 reset(dut)
348 runRelay(dut, priors, syndrome, expected)
349 runRelay(dut, priors, syndrome, expected)
350 }
351 }
352 }
353
354 "failure returns one coherent final-leg snapshot" in {
355 val graph = nodes(3, Seq(Seq(0, 1), Seq(1, 2), Seq(0, 2)))
356 val config = RelayBpConfig(
357 graph, initialIterations = 1, relayIterations = 1,
358 maximumLegs = 2, solutionTarget = 1,
359 )
360 val beta = Vector(Vector.fill(3)(7), Vector.fill(3)(3))
361 val priors = Vector(2, 1, 1)
362 val syndrome = Vector.fill(3)(true)
363 val expected = Reference.runRelay(config, priors, syndrome, beta)
364 assert(!expected.success && expected.solutions == 0 && expected.iterations == 2)
365 assert(expected.correction == expected.state.decision)
366 assert(expected.residual == Reference.residual(graph, expected.state, syndrome))
367 simulate(new RelayBpDecoder(
368 config, Some(() => new TraceRelayCoefficients(beta)),
369 )) { dut =>
370 reset(dut)
371 runRelay(dut, priors, syndrome, expected)
372 }
373 }
374
375 "the production LFSRs drive a golden-model Relay run" in {
376 val graph = nodes(3, Seq(Seq(0, 1, 2)))
377 val config = RelayBpConfig(
378 graph, initialIterations = 1, relayIterations = 1,
379 maximumLegs = 4, solutionTarget = 1, seedOffset = 17,
380 )
381 val beta = Reference.lfsrBeta(3, config.maximumLegs, config.seedOffset)
382 val priors = Vector.fill(3)(1)
383 val syndrome = Vector(true)
384 val expected = Reference.runRelay(config, priors, syndrome, beta)
385 simulate(new RelayBpDecoder(config)) { dut =>
386 reset(dut)
387 runRelay(dut, priors, syndrome, expected)
388 runRelay(dut, priors, syndrome, expected)
389 }
390 }
391
392 "the hardware maximumLegs bound includes the initial leg" in {
393 val graph = nodes(2, Seq(Seq(0, 1), Seq(0, 1)))
394 val config = RelayBpConfig(
395 graph, initialIterations = 1, relayIterations = 1,
396 maximumLegs = 600, solutionTarget = 1,
397 )
398 val priors = Vector.fill(2)(1)
399 val syndrome = Vector(true, false)
400 val beta = Reference.lfsrBeta(2, config.maximumLegs, config.seedOffset)
401 val expected = Reference.runRelay(config, priors, syndrome, beta)
402 assert(!expected.success && expected.iterations == 600 && expected.legs == 600)
403 simulate(new RelayBpDecoder(config)) { dut =>
404 reset(dut)
405 runRelay(dut, priors, syndrome, expected)
406 }
407 }
408
409 "matches the pinned trmue oracle fixture at the algorithmic boundary" in {
410 val graph = nodes(3, Seq(Seq(0, 1), Seq(1, 2)))
411 val config = RelayBpConfig(
412 graph, initialIterations = 10, relayIterations = 5,
413 maximumLegs = 2, solutionTarget = 1,
414 )
415 val beta = Vector(Vector.fill(3)(7), Vector(6, 6, 5))
416 val priors = Vector.fill(3)(4)
417 val syndrome = Vector(true, true)
418 val expected = Reference.runRelay(config, priors, syndrome, beta)
419 assert(expected.success && expected.correction == Vector(false, true, false))
420 simulate(new RelayBpDecoder(
421 config, Some(() => new TraceRelayCoefficients(beta)),
422 )) { dut =>
423 reset(dut)
424 runRelay(dut, priors, syndrome, expected)
425 }
426 }
427}