def gen_dag1(): #(a*b) + 4 - b x = Input(name="x") y = Input(name="y") z = Input(name="z") i = Mul(x, y, name="i") j = Sub(y, z, name="j") k = Mul(i, j, name="k") dag = Dag(outputs=[k], inputs=[x, y, z]) return dag
def gen_ex1(): #(a * b) + (b * c) a = Input(name="a") b = Input(name="b") c = Input(name="c") d = Mul(a, b, name="d") e = Mul(b, c, name="e") z = Add(e, d, name="z") dag = Dag(outputs=[z], inputs=[a, b, c]) return dag
def gen_ex2(): # sqrt((a * a) + (b * b) + (c * c)) a = Input(name="a") b = Input(name="b") c = Input(name="c") out = LookupTable(np.sqrt, Add( Add(Mul(a, a), Mul(b, b)), Mul(c, c)), name="res", precision=8.) dag = Dag(outputs=[out], inputs=[a, b, c]) return dag
def generate_poly_approx(a, b, c, d): x = Input(name="x") a = Constant(a, name="a") b = Constant(b, name="b") c = Constant(c, name="c") d = Constant(d, name="d") output = Mul( Add(Mul(Add(Mul(Add(Mul(a, x), b), x), c), x), d), x, name="res") approx_dag = Dag(outputs=[output], inputs=[x]) return approx_dag
def matrix_multiply(): a00 = Input(name="a00") a01 = Input(name="a01") a10 = Input(name="a10") a11 = Input(name="a11") b00 = Input(name="b00") b01 = Input(name="b01") b10 = Input(name="b10") b11 = Input(name="b11") p0 = Mul(Add(a00, a11), Add(b00, b11), name="p0") p1 = Mul(Add(a10, a11), b00, name="p1") p2 = Mul(a00, Sub(b01, b11), name="p2") p3 = Mul(a11, Sub(b10, b00), name="p3") p4 = Mul(b11, Add(a00, a01), name="p4") p5 = Mul(Add(b00, b01), Sub(a10, a00), name="p5") p6 = Mul(Add(b10, b11), Sub(a01, a11), name="p6") y00 = Add(Sub(Add(p0, p3), p4), p6, name="y00") y01 = Add(p2, p4, name="y01") y10 = Add(p1, p3, name="y10") y11 = Add(Sub(Add(p0, p2), p1), p5, name="y11") matrix_dag = Dag(outputs=[y00, y01, y10, y11], inputs=[ a00, a01, a10, a11, b00, b01, b10, b11]) return matrix_dag
def generate_basic(a): x = Input(name="x") a = Constant(a, name="a") output = Mul(a, x, name="res") approx_dag = Dag(outputs=[output], inputs=[x]) return approx_dag
def RGB_to_YCbCr(): r = Input(name="r") g = Input(name="g") b = Input(name="b") col_1 = Add(Add(Mul(Constant(.299, name="C1"), r), Mul(Constant(.587, name="C2"), g)), Mul(Constant(.114, name="C3"), b), name="col_1") col_2 = Add(Add(Mul(Constant(-.16875, name="C4"), r), Mul(Constant(-.33126, name="C5"), g)), Mul(Constant(.5, name="C6"), b), name="col_2") col_3 = Add(Add(Mul(Constant(.5, name="C7"), r), Mul(Constant(-.41869, name="C8"), g)), Mul(Constant(-.08131, name="C9"), b), name="col_3") casestudy_dag = Dag(outputs=[col_1, col_2, col_3], inputs=[r, g, b]) return casestudy_dag
def gen_fig3(): #(a*b) + 4 - b a = Input(name="a") b = Input(name="b") c = Constant(4, name="c") d = Mul(a, b, name="d") e = Add(d, c, name="e") z = Sub(e, b, name="z") fig3_dag = Dag(outputs=[z], inputs=[a, b]) return fig3_dag
def generate_square_wave(): x = Input(name="x") a = LookupTable(np.sin, x) b = Add(Mul(Constant(1/3.), LookupTable(np.sin, Mul(Constant(3.), x))), a) c = Add(Mul(Constant(1/5.), LookupTable(np.sin, Mul(Constant(5.), x))), b) d = Add(Mul(Constant(1/7.), LookupTable(np.sin, Mul(Constant(7.), x))), c) e = Add(Mul(Constant(1/9.), LookupTable(np.sin, Mul(Constant(9.), x))), d, name="res") dag = Dag(outputs=[e], inputs=[x]) return dag
def generate_basic_lut(): x = Input(name="x") amplitude = Input(name="a") shift = Input(name="b") output = Add(Mul(amplitude, LookupTable( np.sin, x)), shift, name="res") # output = Add(Mul(amplitude, x), shift, name="res") sin_dag = Dag(outputs=[output], inputs=[x, amplitude, shift]) return sin_dag
def gen_ex3(): a = Input(name="a") b = Input(name="b") c = Input(name="c") d = Input(name="d") out = Add(Mul(a, Mul(b, Mul(c, d))), Mul(a, b), name="res") dag = Dag(outputs=[out], inputs=[a, b, c, d]) return dag # def test_fig3(): # dag = gen_fig3() # bf = BitFlow(dag, {"z": 8.}, {'a': (-3., 2.), # 'b': (4., 8.)}, lr=1e-2, range_lr=1e-2, train_range=True, training_size=50000, testing_size=10000, distribution=2, incorporate_ulp_loss=False, batch_size=16, test_optimizer=True) # bf.train(epochs=10, decay=0.8) # rounded_dag = bf.rounded_dag # original_dag = bf.original_dag # verilog_gen = BitFlow2Verilog( # "fig3", bf.P, bf.R, bf.filtered_vars, original_dag, {"z": 8.}) # verilog_gen.evaluate() # # check saving object works # BitFlow.save("./models/fig3", bf) # new_bf = BitFlow.load("./models/fig3") # new_bf.train(epochs=5) # assert new_bf.range_lr == bf.range_lr return