Как использовать долото dsptools с поплавками - PullRequest
0 голосов
/ 23 сентября 2018

Мне нужно преобразовать Float32 в долото FixedPoint, выполнить некоторые вычисления и преобразовать обратно FixedPoint в Float32.

Например, мне нужно следующее:

val a = 3.1F
val b = 2.2F
val res = a * b // REPL returns res: Float 6.82

Теперь ясделать это:

import chisel3.experimental.FixedPoint

val fp_tpe = FixedPoint(6.W, 2.BP)
val a_fix = a.Something (fp_tpe) // convert a to FixPoint
val b_fix = b.Something (fp_tpe) // convert b to FixPoint
val res_fix = a_fix * b_fix
val res0 = res_fix.Something (fp_tpe) // convert back to Float

В результате я бы ожидал, что дельта будет в диапазоне, например,

val eps = 1e-4
assert ( abs(res - res0) < eps, "The error is too big")

Кто может предоставить рабочий пример для класса Chisel3 FixedPoint дляпсевдокод выше?

Ответы [ 3 ]

0 голосов
/ 25 сентября 2018

Для других пользователей я предоставляю улучшенное решение от @Chick, переписанное в более абстрактном Scala с переменными допусками DSP.

package my_pkg

import chisel3._
import chisel3.core.{FixedPoint => FP}

import dsptools.{DspTester, DspTesterOptions, DspTesterOptionsManager}

class FPGenericIO (inType:FP, outType:FP) extends Bundle {
  val a = Input(inType)
  val b = Input(inType)
  val c = Output(outType)
}

class FPMul (inType:FP, outType:FP) extends Module {
  val io  = IO(new FPGenericIO(inType, outType))
  io.c := io.a * io.b
}

class FPMulTester(c: FPMul) extends DspTester(c) {

  val uut = c.io

  // This will PASS, there is sufficient precision to model the inputs
  poke(uut.a, 3.25)
  poke(uut.b, 2.5)

  step(1)
  expect(uut.c, 3.25*2.5)

  // This will FAIL, if you won't increase tolerance, which is eps = 0.0 by default
  poke(uut.a, 3.1)
  poke(uut.b, 2.2)

  step(1)
  expect(uut.c, 3.1*2.2)
}


object FPUMain extends App {

  val fpInType  = FP(8.W, 4.BP)
  val fpOutType = FP(12.W, 6.BP)

// Update default DspTester options and increase tolerance
  val opts = new DspTesterOptionsManager {

    dspTesterOptions = DspTesterOptions(
      fixTolLSBs = 2,
      genVerilogTb = false,
      isVerbose = true
    )
  }

  dsptools.Driver.execute (() => new FPMul(fpInType, fpOutType), opts) {
    c => new FPMulTester(c)
  }
}
0 голосов
/ 25 сентября 2018

Вот моя окончательная реализация множителя DSP, которая должна поддерживать числа FixedPoint и DspComplex.@ChickMarkley, как мне обновить этот класс для реализации сложного умножения?

package my_pkg

import chisel3._

import dsptools.numbers.{Ring,DspComplex}
import dsptools.numbers.implicits._
import dsptools.{DspContext}

import chisel3.core.{FixedPoint => FP}
import dsptools.{DspTester, DspTesterOptions, DspTesterOptionsManager}


class FPGenericIO[A <: Data:Ring, B <: Data:Ring] (inType:A, outType:B) extends Bundle {
  val a = Input(inType.cloneType)
  val b = Input(inType.cloneType)
  val c = Output(outType.cloneType)

  override def cloneType = (new FPGenericIO(inType, outType)).asInstanceOf[this.type]

}

class FPMul[A <: Data:Ring, B <: Data:Ring] (inType:A, outType:B) extends Module {

  val io  = IO(new FPGenericIO(inType, outType))

  DspContext.withNumMulPipes(3) {
    io.c := io.a * io.b
  }
}

class FPMulTester[A <: Data:Ring, B <: Data:Ring](c: FPMul[A,B]) extends DspTester(c) {

  val uut = c.io

  //
  // This will PASS, there is sufficient precision to model the inputs
  //
  poke(uut.a, 3.25)
  poke(uut.b, 2.5)

  step(1)
  expect(uut.c, 3.25*2.5)

  //
  // This will FAIL, there is not sufficient precision to model the inputs
  // But this is only caught on output, this is likely the right approach
  // because you can't really pass in wrong precision data in hardware.
  //
  poke(uut.a, 3.1)
  poke(uut.b, 2.2)

  step(1)
  expect(uut.c, 3.1*2.2)
}


object FPUMain extends App {

  val fpInType  = FP(8.W, 4.BP)
  val fpOutType = FP(12.W, 6.BP)
  //val comp = DspComplex[Double] // How to declare a complex DSP type ?

  val opts = new DspTesterOptionsManager {

    dspTesterOptions = DspTesterOptions(
      fixTolLSBs = 0,
      genVerilogTb = false,
      isVerbose = true
    )
  }

  dsptools.Driver.execute (() => new FPMul(fpInType, fpOutType), opts) {
  //dsptools.Driver.execute (() => new FPMul(comp, comp), opts) { // <-- this won't compile
    c => new FPMulTester(c)
  }
}
0 голосов
/ 24 сентября 2018

Взгляните на следующий код:

import chisel3._
import chisel3.core.FixedPoint
import dsptools._


class FPMultiplier extends Module {
  val io = IO(new Bundle {
    val a = Input(FixedPoint(6.W, binaryPoint = 2.BP))
    val b = Input(FixedPoint(6.W, binaryPoint = 2.BP))
    val c = Output(FixedPoint(12.W, binaryPoint = 4.BP))
  })

  io.c := io.a * io.b
}

class FPMultiplierTester(c: FPMultiplier) extends DspTester(c) {
  //
  // This will PASS, there is sufficient precision to model the inputs
  //
  poke(c.io.a, 3.25)
  poke(c.io.b, 2.5)

  step(1)
  expect(c.io.c, 8.125)

  //
  // This will FAIL, there is not sufficient precision to model the inputs
  // But this is only caught on output, this is likely the right approach
  // because you can't really pass in wrong precision data in hardware.
  //
  poke(c.io.a, 3.1)
  poke(c.io.b, 2.2)

  step(1)
  expect(c.io.c, 6.82)
}


object FPMultiplierMain {
  def main(args: Array[String]): Unit = {
    iotesters.Driver.execute(Array("-fiv"), () => new FPMultiplier) { c =>
      new FPMultiplierTester(c)
    }
  }
}

Я бы также посоветовал посмотреть на ParameterizedAdder в dsptools , что дает вам представление о том, какписать аппаратные модули, которые вы передаете разными типами.Обычно вы начинаете с DspReals, подтверждаете модель, затем начинаете экспериментировать / вычислять с размерами FixedPoint, которые возвращают результаты с желаемой точностью.

...