Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/resources/test_data/cnn_thresholds1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-57 34 155 88 80 176 -14 -615 -175 -207 -30 115 74 38 -13
12 63 87 -36 -15
2 changes: 2 additions & 0 deletions src/main/resources/test_data/cnn_thresholds2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[31 31 32 31 37 36 33 30 32 31 34 30 33 36 37 35 33 31 31 43 38 34 34 30 34
29 32 30 35 31 34 31 32 35 33 32 33 32 33 29 31 36 35 37 37 40 34 32 33 31]
84 changes: 84 additions & 0 deletions src/main/resources/test_data/convolution1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
55 30 7 10 28 119 31 37 55 15 48 121 77 115 56 26 54 16 41 71 110 47 34 76 82 79 88 90 82 103 90 64 96 15 40 7 23 22 116 63 23 19 68 58 29 26 92 5 54 77 112 74 50 100 36 108 111 118 71 70 20 44 52 56 126 79 30 63 39 96 89 100 27 2 110
-739
-607
-349
-327
133
367
-793
-389
-589
-363
-349
245
-275
-443
545
-151
-103
-189
-863
-277
41 121 28 16 27 126 95 88 119 109 10 125 72 54 7 86 18 125 79 85 15 35 107 65 19 48 122 119 40 29 24 81 121 115 90 24 59 6 37 34 111 119 53 10 74 110 22 81 56 125 52 56 125 2 21 41 99 125 43 21 2 75 57 53 94 17 51 78 107 25 115 44 49 20 48
-214
-146
-144
-408
-568
458
-566
-776
-654
-606
-354
134
-4
-56
336
-454
-256
606
-474
-1020
100 14 79 27 97 82 26 84 3 57 125 54 73 55 69 20 30 101 19 79 84 126 14 56 123 74 61 106 109 43 98 120 24 34 15 112 10 78 11 39 112 45 39 97 72 20 74 91 65 61 86 70 33 65 103 103 90 14 110 108 125 101 12 89 94 33 53 103 4 33 65 38 33 40 9
-73
-451
-659
-221
-409
-147
-39
-647
-141
-761
-53
-143
-61
293
-317
243
-111
-343
-349
23
31 54 40 28 41 22 13 88 41 67 43 109 128 95 99 118 78 122 22 112 102 9 31 108 101 32 87 72 46 56 106 49 114 78 112 92 73 112 61 77 115 125 41 50 91 112 16 58 57 2 4 57 96 37 88 27 24 111 11 86 86 4 50 11 91 9 117 69 67 55 4 29 69 30 91
-547
67
-59
-451
-457
55
31
-1081
-643
-349
-623
481
199
263
-457
-85
-87
317
197
-661
20 changes: 20 additions & 0 deletions src/main/resources/weights/convolution1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1 1 1 1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 -1 -1 1 1 1 1 -1
-1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 1 1 1 1 -1 1 1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 1 1 1 1 -1
1 -1 -1 -1 -1 -1 -1 1 1 1 -1 -1 1 1 1 -1 1 1 1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 1 1 1 -1 1 1 1 1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 1 1 1 -1 1 1 1 1 -1 1 1 1 -1
1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 1 -1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 1 1 1 1 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 -1 -1 -1 1 1 1 1
122 changes: 122 additions & 0 deletions src/main/scala/Convolution.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package rosetta

import Chisel._


class Convolution(data_width: Int, input_size: Int, filter_size: Int, channels: Int, filters: Int) extends RosettaAccelerator {
// Load weights from file
val weights = scala.io.Source.fromInputStream(this.getClass.getResourceAsStream("/weights/convolution1.txt")).getLines
.map(line => line.split(" ").map(i => if (i.toInt == 1) 1 else 0))
.map(filter => UInt(BigInt(filter.mkString, 2), width=5*5*3)).toArray

val numMemPorts = 0
val input_width = data_width * input_size * channels // 8 bits * 6 * 3 - one row of 6*6*3
val output_width = log2Up(filter_size * filter_size * channels * math.pow(2, data_width).toInt) // 5*5*3 * max value of each number
val pixel_width = data_width * channels
val window_width = filter_size * filter_size * channels * data_width // 5 * 5 * 3 * 8

val io = new RosettaAcceleratorIF(numMemPorts){
val input = Flipped(Decoupled(Vec.fill(input_size){Bits(INPUT, width=input_width)}))
val output = Decoupled(Vec.fill(4 * filters){Bits(OUTPUT, width=output_width)})
}
io.input.ready := Bool(true)
io.output.valid := Bool(false)

val x_start = input_width - 1
val x_end = input_width - pixel_width * filter_size
val dot_products = Array.fill(4 * filters){Module(new DotProduct(5*5*3, data_width))}
for (w <- 0 until 4) {
for (f <- 0 until filters) {
val x = w % 2
val y = w / 2
val offset = x * pixel_width
val window = (0 until filter_size).map(i => io.input.bits(i + y)(x_start - offset, x_end - offset)).foldLeft(Bits(width=window_width)){(a, b) => Cat(a,b)}
dot_products(w * filters + f).io.vec_1 := (0 until 5*5*3).map(i => window(window_width - 1 - i * 8, window_width - 8 - i*8))

dot_products(w * filters + f).io.vec_2 := Bits(0)
io.output.bits(w * filters + f) := dot_products(w * filters + f).io.output_data
}
}

// Steps is equal to 20 divided by filters per iteration
val steps = 20 / filters
val step = Reg(init=UInt(0, width=log2Up(steps)))
switch (step) {
for (step_i <- 0 until steps) {
is (UInt(step_i)) {
for (w <- 0 until 4) {
for (f <- 0 until filters) {
dot_products(w * filters + f).io.vec_2 := weights(step_i * filters + f)
}
}
}
}
}

val ready :: calc :: Nil = Enum(UInt(), 2)
val state = Reg(init = ready)

switch(state) {
is (ready) {
io.input.ready := Bool(true)
io.output.valid := Bool(false)
when (io.input.valid) {
io.input.ready := Bool(false)
state := calc
step := UInt(0)
}
}
is (calc) {
io.input.ready := Bool(false)
io.output.valid := Bool(true)
when (io.output.ready) {
step := step + UInt(1)
when (step === UInt(steps - 1)) {
io.input.ready := Bool(true)
state := ready
}
}
}
}


}

class ConvolutionTests(c: Convolution) extends Tester(c) {
def toBinary(i: Int, digits: Int = 8) =
String.format("%" + digits + "s", i.toBinaryString).replace(' ', '0')
val filters = 1
val windows = 4
val filter_dim = 5
val input = (0 to 32*32*3).map(_ => toBinary((math.random * (30-20) + 20).toInt)).toArray.grouped(32*3).map(g => g.mkString).toArray
val input_window = (0 until 6).map(i => BigInt(input(i).slice(0, 6*3*8).mkString, 2)).toArray

val weights = scala.io.Source.fromInputStream(this.getClass.getResourceAsStream("/weights/convolution1.txt")).getLines
.map(line => line.split(" ").map(_.toInt)).toArray

for (y_i <- 0 until 14) {
val input_window = (0 until 6).map(i => input(i+y_i*2).slice(0, 6*3*8)).toArray
poke(c.io.input.valid, 1)
poke(c.io.input.bits, (0 until 6).map(i => BigInt(input(i+y_i*2).slice(0, 6*3*8).mkString, 2)).toArray)
step(1)
poke(c.io.input.valid, 0)
for (f <- 0 until 20) {
peek(c.step)
for (w <- 0 until windows) {
val x = w % 2
val y = w / 2
val offset = x * 3*8
val window = (0 until 5).map(i => input_window(i + y).slice(0 + offset, 5*3*8 + offset).grouped(8).map(v => BigInt(v.mkString, 2))).reduce(_++_).toArray
val filter = weights(f)
val result = (window zip filter).map{case (i1: BigInt, i2: Int) => i1*i2}.reduceLeft(_ + _)
println(result)
expect(c.io.output.bits(w), result)
}
step(1)
poke(c.io.output.ready, 1)
step(1)
poke(c.io.output.ready, 0)
}
peek(c.state)
}
}
8 changes: 4 additions & 4 deletions src/main/scala/ImageQueue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import fpgatidbits.ocm._
class ImageQueue(dataWidth: Int, queueDepth: Int, vec_fill_size: Int) extends RosettaAccelerator {
val numMemPorts = 0
val io = new RosettaAcceleratorIF(numMemPorts) {
val input_data = Vec.fill(vec_fill_size){UInt(INPUT, width=dataWidth)}
val input_data = UInt(INPUT, width=dataWidth)
val input_pulse = Bool(INPUT)

val output = Decoupled(Vec.fill(vec_fill_size){UInt(OUTPUT, dataWidth)})
val output = Decoupled(UInt(OUTPUT, dataWidth))
val full = Bool(OUTPUT)
val empty = Bool(OUTPUT)

}

val pulse_reg = Reg(next=io.input_pulse)
val queue = Module(new FPGAQueue(Vec.fill(vec_fill_size){UInt(width=dataWidth)}, queueDepth))
val queue = Module(new FPGAQueue(UInt(width=dataWidth), queueDepth))

queue.io.enq.valid := !io.input_pulse && pulse_reg
queue.io.enq.bits := io.input_data
Expand All @@ -31,7 +31,7 @@ class ImageQueue(dataWidth: Int, queueDepth: Int, vec_fill_size: Int) extends Ro
class ImageQueueTests(c: ImageQueue) extends Tester(c) {
val input_data = Array(1,2,3,4,5,6,7,8).map(BigInt(_))
// Poke input_data & pulse to 1 and step(1)
poke(c.io.input_data, input_data)
poke(c.io.input_data, input_data(0))
poke(c.io.input_pulse, 1)

expect(c.io.empty, 1)
Expand Down
8 changes: 7 additions & 1 deletion src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ object Settings {
// output products. all cmdline arguments are passed straight to Chisel.
object ChiselMain {
def main(args: Array[String]): Unit = {
chiselMain(args, () => Module(new RosettaWrapper(Settings.myInstFxn)))
//chiselMain(args, () => Module(new RosettaWrapper(Settings.myInstFxn)))
//chiselMainTest(args, () => Module(new Max(4, 1))){c => new MaxTests(c)}
//chiselMainTest(args, () => Module(new Sum(9,8))){c => new SumTests(c)}
//chiselMainTest(args, () => Module(new Mux2())){c => new Mux2Tests(c)}
//chiselMainTest(args, () => Module(new Comparator(8))){c => new ComparatorTest(c)}
//chiselMainTest(args, () => Module(new ComparatorWrapper(12, 256))){c => new ComparatorWrapperTest(c)}
//chiselMainTest(args, () => Module(new ROM())){c => new ROMTests(c)}
//chiselMainTest(args, () => Module(new AutoSimple("/test_data/fc1.txt", "/test_data/fc2.txt"))){c => new AutoSimpleTest(c)}
chiselMainTest(args, () => Module(new AutoConvolution())){c => new AutoConvolutionTest(c)}
//chiselMainTest(args, () => Module(new DotProduct(5, 8))){c => new DotProductTests(c)}
//chiselMainTest(args, () => Module(new FullyConnected("/test_data/fc1.txt", 64, 1024, 32, 64, 8))){c => new FullyConnectedTests(c)}
//chiselMainTest(args, () => Module(new ImageQueue(8, 128, 32))){c => new ImageQueueTests(c)}
//chiselMainTest(args, () => Module(new Convolution(8, 6, 5, 3, 1))){c => new ConvolutionTests(c)}
//chiselMainTest(args, () => Module(new DotProduct(75, 8))){c => new DotProductTests(c)}
//chiselMainTest(args, () => Module(new ROM())){c => new ROMTests(c)}
//chiselMainTest(args, () => Module(new Convolution(8, 4, 2))){c => new ConvolutionTests(c)}
//chiselMainTest(args, () => Module(new WindowSelector())){c => new WindowSelectorTests(c)}
}
}

Expand Down
Loading