test_pass_split_pipeline.py 1.75 KB
Newer Older
Tianqi Chen committed
1 2
import tvm

3 4 5 6 7 8
def lower(s, args):
    binds = {}
    arg_list = []

    for x in args:
        assert isinstance(x, tvm.tensor.Tensor)
9
        buf = tvm.decl_buffer(x.shape, dtype=x.dtype, name=x.op.name)
10 11 12 13 14 15 16 17 18 19
        binds[x] = buf
        arg_list.append(buf)
    s.normalize()
    bounds = tvm.schedule.InferBound(s)
    stmt = tvm.schedule.ScheduleOps(s, bounds)
    stmt = tvm.ir_pass.StorageFlatten(stmt, binds)
    stmt = tvm.ir_pass.CanonicalSimplify(stmt)
    stmt = tvm.ir_pass.Simplify(stmt)
    return stmt

Tianqi Chen committed
20 21 22 23 24 25 26 27 28 29 30
def test_basic_pipeline():
    n = tvm.convert(128)
    A = tvm.placeholder((n,), name='A')
    stages = []
    num_stage = 3

    B = A
    for k in range(num_stage):
        stages.append(B)
        B = tvm.compute((n,), lambda i: B[i] + k, name="A%s" % k)

31
    s = tvm.create_schedule(B.op)
32 33
    xo, xi = s[B].split(B.op.axis[0], nparts=1)
    s[B].bind(xo, tvm.thread_axis("pipeline"))
34
    xo, xi = s[B].split(xi, factor=4)
Tianqi Chen committed
35 36 37
    for S in stages:
        s[S].compute_at(s[B], xo)

38 39 40 41
    stmt = lower(s, [A, B])
    stmt = tvm.ir_pass.SplitPipeline(stmt, False)
    print(stmt)
    stmt = tvm.ir_pass.NarrowChannelAccess(stmt)
Tianqi Chen committed
42 43 44
    print(stmt)
    assert(tvm.ir_pass.VerifySSA(stmt))

45
def test_conv1d():
46
    n = tvm.var('n')
47 48 49 50 51
    A = tvm.compute((n+2), lambda i: 1,  name='A')
    def computeB(ii):
        i = ii + 1
        return A[i-1] + A[i] + A[i+1]
    B = tvm.compute(n, computeB, name='B')
52
    s = tvm.create_schedule(B.op)
53 54
    px, xi = s[B].split(B.op.axis[0], nparts=1)
    s[B].bind(px, tvm.thread_axis("pipeline"))
55 56 57 58 59 60 61 62
    s[A].compute_at(s[B], px)
    stmt = lower(s, [B])
    stmt = tvm.ir_pass.SplitPipeline(stmt, False)
    print(stmt)
    stmt = tvm.ir_pass.NarrowChannelAccess(stmt)
    print(stmt)


Tianqi Chen committed
63 64
if __name__ == "__main__":
    test_basic_pipeline()
65
    test_conv1d()