cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
326 stars 92 forks source link

[DOC] Tutorial for Sobel example #384

Closed yn224 closed 2 years ago

yn224 commented 3 years ago

In this PR, I attempt to add additional code sample to the HeteroCL documentation: Sobel Edge Detection.

It also features the new report interface addressed in PR #376.

hecmay commented 3 years ago

@yn224 Here is fixed version for Sobel with FIFO channels. I changed the code a bit and added a few lines to add the .to primitives to create FIFO channels. You may just update that sobel_stream.py with the following code and commit.

hcl.init(init_dtype=hcl.Float())
img = Image.open(urlopen('http://i.stack.imgur.com/8zINU.gif'))
width, height = img.size

A = hcl.placeholder((height,width), "A", dtype=hcl.Float())
Gx = hcl.placeholder((3,3), "Gx",dtype=hcl.Float())
Gy = hcl.placeholder((3,3), "Gy",dtype=hcl.Float())

def sobel(A, Gx, Gy):
    r = hcl.reduce_axis(0,3)
    c = hcl.reduce_axis(0,3)

    B1 = hcl.compute((height-2,width-2), 
            lambda x,y: hcl.sum(A[x+r,y+c]*Gx[r,c], axis=[r,c], name="sum1"),
            name="B1", dtype=hcl.Float())

    t = hcl.reduce_axis(0,3)
    g = hcl.reduce_axis(0,3)

    B2 = hcl.compute((height-2,width-2), 
            lambda x,y: hcl.sum(A[x+t,y+g]*Gy[t,g], axis=[t,g], name="sum2"),
            name="B2", dtype=hcl.Float())

    def avg(in1, in2):
        ll = hcl.scalar(in1, "in1")
        lr = hcl.scalar(in2, "in2")
        return hcl.sqrt(ll.v * ll.v + lr.v * lr.v)/4328*255

    return hcl.compute((height-2,width-2), 
               lambda x, y : avg(B1[x,y], B2[x,y]),
               name="output", dtype=hcl.Float())

target = hcl.Platform.aws_f1
target.config(compiler="vitis", backend="vhls")

s = hcl.create_schedule([A, Gx, Gy], sobel)

# Move inputs to FPGA and output back to CPU
s.to(A, target.xcel, burst_len=128).to([sobel.B1, sobel.B2])
s.to([Gx, Gy], target.xcel)
s.to(sobel.output, target.host)

# Create FIFO channels between sub-kernels
s.to(sobel.B1, sobel.output)
s.to(sobel.B2, sobel.output)
hecmay commented 3 years ago

@seanlatias do you want to have a quick look? If there is not any problems, I will merge it into master.

hecmay commented 3 years ago

ok.

hecmay commented 3 years ago

@yn224 it seems that some files are missing.

Another kind reminder — you may use the interactive shell provided in CircleCI to debug, if it is hard to figure out why something went wrong from the log.

seanlatias commented 3 years ago

@yn224 It seems the problem is that the input image is not there.

hecmay commented 2 years ago

Sean and I have reviewed this PR before. I will just merge it.