src/test/scala/chipsldpc/RelayBiasSpec.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 org.scalatest.freespec.AnyFreeSpec
  6
  7private final class RelayBiasHarness(q: Quantization, format: RelayFormat) extends Module {
  8  val io = IO(new Bundle {
  9    val value = Input(SInt(q.accumulatorBits.W))
 10    val prior = Input(SInt(q.accumulatorBits.W))
 11    val previous = Input(SInt(q.accumulatorBits.W))
 12    val beta = Input(UInt(format.betaBits.W))
 13    val product = Output(SInt((q.accumulatorBits + format.betaBits + 1).W))
 14    val bias = Output(SInt(q.accumulatorBits.W))
 15  })
 16
 17  io.product := RelayBias.reducedMultiply(io.value, io.beta, format.fractionalBits)
 18  io.bias := RelayBias(io.prior, io.previous, io.beta, q, format)
 19}
 20
 21final class RelayBiasSpec extends AnyFreeSpec with ChiselSim {
 22  private val q = Quantization(magnitudeBits = 4, accumulatorBits = 5)
 23  private val format = RelayDefaults.paperFormat
 24  private val wideFormat = RelayFormat(betaBits = 5, fractionalBits = 3)
 25
 26  private def reducedMultiply(value: Int, beta: Int): Int = {
 27    val magnitude = math.abs(value)
 28    val unsigned = (0 until q.accumulatorBits)
 29      .filter(bit => ((magnitude >> bit) & 1) != 0)
 30      .map(bit => (beta << bit) >> format.fractionalBits)
 31      .sum
 32    if (value < 0) -unsigned else unsigned
 33  }
 34
 35  private def clip(value: Int): Int =
 36    value.max(-(1 << (q.accumulatorBits - 1))).min((1 << (q.accumulatorBits - 1)) - 1)
 37
 38  private def relayBias(prior: Int, previous: Int, beta: Int): Int =
 39    clip(reducedMultiply(prior, beta) + previous - reducedMultiply(previous, beta))
 40
 41  "reduced multiplication follows Table 2 and restores the sign after truncation" in {
 42    val vectors = Seq(
 43      (15, 7, 11),
 44      (-15, 7, -11),
 45      (1, 7, 0),
 46      (-1, 7, 0),
 47      (-2, 7, -1),
 48      (-4, 7, -3),
 49      (15, 10, 18),
 50      (-16, 10, -20),
 51    )
 52    simulate(new RelayBiasHarness(q, format)) { dut =>
 53      dut.io.prior.poke(0.S)
 54      dut.io.previous.poke(0.S)
 55      vectors.foreach { case (value, beta, expected) =>
 56        dut.io.value.poke(value.S)
 57        dut.io.beta.poke(beta.U)
 58        dut.io.product.expect(expected.S)
 59      }
 60    }
 61  }
 62
 63  "paper-profile bias matches an independent exhaustive model" in {
 64    simulate(new RelayBiasHarness(q, format)) { dut =>
 65      for {
 66        prior <- 0 to 15
 67        previous <- -16 to 15
 68        beta <- 3 to 10
 69      } {
 70        dut.io.value.poke(previous.S)
 71        dut.io.prior.poke(prior.S)
 72        dut.io.previous.poke(previous.S)
 73        dut.io.beta.poke(beta.U)
 74        dut.io.product.expect(reducedMultiply(previous, beta).S)
 75        dut.io.bias.expect(relayBias(prior, previous, beta).S)
 76      }
 77    }
 78  }
 79
 80  "identity, initialization, oddness, and both saturation limits are explicit" in {
 81    simulate(new RelayBiasHarness(q, wideFormat)) { dut =>
 82      for (value <- -16 to 15) {
 83        dut.io.value.poke(value.S)
 84        dut.io.beta.poke(8.U)
 85        dut.io.product.expect(value.S)
 86      }
 87      for {
 88        value <- 0 to 15
 89        beta <- 0 to 31
 90      } {
 91        dut.io.value.poke((-value).S)
 92        dut.io.beta.poke(beta.U)
 93        dut.io.product.expect((-reducedMultiply(value, beta)).S)
 94        dut.io.prior.poke(value.S)
 95        dut.io.previous.poke(value.S)
 96        dut.io.bias.expect(value.S)
 97      }
 98
 99      Seq(
100        (15, -16, 10, 15),
101        (0, 7, 28, -16),
102        (0, -16, 16, 15),
103        (0, -16, 0, -16),
104      ).foreach { case (prior, previous, beta, expected) =>
105        dut.io.prior.poke(prior.S)
106        dut.io.previous.poke(previous.S)
107        dut.io.beta.poke(beta.U)
108        dut.io.bias.expect(expected.S)
109      }
110    }
111  }
112}