def kernel(A, B, C, O): dtype_xyz = hcl.Struct({ "x": hcl.Int(), "y": hcl.Int(), "z": hcl.Int() }) dtype_out = hcl.Struct({ "v0": hcl.Int(), "v1": hcl.Int(), "v2": hcl.Int(), "v3": hcl.Int(), "v4": hcl.Int(), "v5": hcl.Int() }) D = hcl.compute(A.shape, lambda x: (A[x], B[x], C[x]), dtype=dtype_xyz) E = hcl.compute(A.shape, lambda x: (D[x].x * D[x].x, D[x].y * D[x].y, D[x].z * D[x].z, D[ x].x * D[x].y, D[x].y * D[x].z, D[x].x * D[x].z), dtype=dtype_out) with hcl.Stage(): with hcl.for_(0, 100) as i: for j in range(0, 6): O[i][j] = E[i].__getattr__("v" + str(j))
def kernel(A, B, C): stype = hcl.Struct({"fa": hcl.Int(8), "fb": hcl.Fixed(13, 11), "fc": hcl.Float()}) D = hcl.compute(A.shape, lambda x: (A[x], B[x], C[x]), dtype=stype) E = hcl.compute(A.shape, lambda x: D[x].fa, dtype=hcl.Int(8)) F = hcl.compute(A.shape, lambda x: D[x].fb, dtype=hcl.Fixed(13, 11)) G = hcl.compute(A.shape, lambda x: D[x].fc, dtype=hcl.Float()) return E, F, G
def kernel(A, B, C): stype = hcl.Struct({ "fa": hcl.Int(8), "fb": hcl.Fixed(13, 11), "fc": hcl.Float() }) D = hcl.compute(A.shape, lambda x: (A[x], B[x], C[x]), dtype=stype) E = hcl.compute(A.shape, lambda x: D[x].fa, dtype=hcl.Int(8)) F = hcl.compute(A.shape, lambda x: D[x].fb, dtype=hcl.Fixed(13, 11)) G = hcl.compute(A.shape, lambda x: D[x].fc, dtype=hcl.Float()) # Check the data type assert D[0].fa.dtype == "int8" assert D[0].fb.dtype == "fixed13_11" assert D[0].fc.dtype == "float32" return E, F, G
import heterocl as hcl st = hcl.Struct({"key": hcl.Int(32), "val": hcl.Int(32)}) size = 1024 class_number = 6 compute_units = 4 hcl.init(hcl.UInt(32)) inputs = hcl.placeholder((size, ), dtype=st, name="input") def kernel(inputs): def split(inputs, number): cus = [] size = inputs.shape[0] for i in range(number): base = i * (size / number) name = "batch_" + str(i) ret = hcl.compute((int(size / number), ), lambda x: inputs[base + x], dtype=st, name=name) cus.append(ret) return cus # ret is the input slice { (key, value)...} # res is the intermediate result def count(res, ret, x): res[ret[x].key] += ret[x].val def reducer(ress, output, x): for res in ress:
def test_sobel_vivado_hls(): path = "home.jpg" hcl.init(init_dtype=hcl.Float()) img = Image.open(path) width, height = img.size Gx = hcl.placeholder((3, 3), "Gx") Gy = hcl.placeholder((3, 3), "Gy") ts = hcl.Struct({"fa": hcl.Float(), "fb": hcl.Float(), "fc": hcl.Float()}) A = hcl.placeholder((height, width, 3), dtype=ts) B = hcl.placeholder((height, width), dtype=ts) def sobel(A, Gx, Gy): D = hcl.compute((height, width), lambda x, y: (A[x][y][0], A[x][y][1], A[x][y][2]), dtype=ts) B = hcl.compute((height, width), lambda x, y: D[x][y].fa + D[x][y].fb + D[x][y].fc, "B", dtype=hcl.Float()) r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Fx = hcl.compute( (height - 2, width - 2), lambda x, y: hcl.sum(B[x + r, y + c] * Gx[r, c], axis=[r, c]), "xx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Fy = hcl.compute( (height - 2, width - 2), lambda x, y: hcl.sum(B[x + t, y + g] * Gy[t, g], axis=[t, g]), "yy") return hcl.compute((height - 2, width - 2), lambda x, y: hcl.sqrt(Fx[ x][y] * Fx[x][y] + Fy[x][y] * Fy[x][y]) * 0.05891867, "Fimg") s = hcl.create_schedule([A, Gx, Gy], sobel) LBX = s.reuse_at(sobel.B._op, s[sobel.xx], sobel.xx.axis[0], "LBX") LBY = s.reuse_at(sobel.B._op, s[sobel.yy], sobel.yy.axis[0], "LBY") WBX = s.reuse_at(LBX, s[sobel.xx], sobel.xx.axis[1], "WBX") WBY = s.reuse_at(LBY, s[sobel.yy], sobel.yy.axis[1], "WBY") s.partition(LBX, dim=1) s.partition(LBY, dim=1) s.partition(WBX) s.partition(WBY) s.partition(Gx) s.partition(Gy) s[sobel.B].pipeline(sobel.B.axis[1]) s[sobel.xx].pipeline(sobel.xx.axis[1]) s[sobel.yy].pipeline(sobel.yy.axis[1]) s[sobel.Fimg].pipeline(sobel.Fimg.axis[1]) target = hcl.platform.zc706 s.to([A, Gx, Gy], target.xcel) s.to(sobel.Fimg, target.host) target.config(compile="vivado_hls", mode="csim|csyn") npGx = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) npGy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) hcl_Gx = hcl.asarray(npGx) hcl_Gy = hcl.asarray(npGy) npF = np.zeros((height - 2, width - 2)) hcl_F = hcl.asarray(npF) npA = np.array(img) hcl_A = hcl.asarray(npA) f = hcl.build(s, target) f(hcl_A, hcl_Gx, hcl_Gy, hcl_F) report = f.report()
def test_issue_410(): A = hcl.Struct({'foo': hcl.UInt(16)}) B = hcl.Struct({'foo': 'uint16'}) assert A['foo'] == B['foo']
def f(A): t = hcl.Struct({'foo': 'uint16'}) B = hcl.compute(A.shape, lambda x: (A[x], ), dtype=t) return hcl.compute(A.shape, lambda x: B[x].foo)