test_pass_split_pipeline.py 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
Tianqi Chen committed
17 18
import tvm

19 20 21 22 23 24
def lower(s, args):
    binds = {}
    arg_list = []

    for x in args:
        assert isinstance(x, tvm.tensor.Tensor)
25
        buf = tvm.decl_buffer(x.shape, dtype=x.dtype, name=x.op.name)
26 27 28 29 30
        binds[x] = buf
        arg_list.append(buf)
    s.normalize()
    bounds = tvm.schedule.InferBound(s)
    stmt = tvm.schedule.ScheduleOps(s, bounds)
31
    stmt = tvm.ir_pass.StorageFlatten(stmt, binds, 64)
32 33 34 35
    stmt = tvm.ir_pass.CanonicalSimplify(stmt)
    stmt = tvm.ir_pass.Simplify(stmt)
    return stmt

Tianqi Chen committed
36 37 38 39 40 41 42 43 44 45 46
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)

47
    s = tvm.create_schedule(B.op)
48 49
    xo, xi = s[B].split(B.op.axis[0], nparts=1)
    s[B].bind(xo, tvm.thread_axis("pipeline"))
50
    xo, xi = s[B].split(xi, factor=4)
Tianqi Chen committed
51 52 53
    for S in stages:
        s[S].compute_at(s[B], xo)

54 55 56 57
    stmt = lower(s, [A, B])
    stmt = tvm.ir_pass.SplitPipeline(stmt, False)
    print(stmt)
    stmt = tvm.ir_pass.NarrowChannelAccess(stmt)
Tianqi Chen committed
58 59 60
    print(stmt)
    assert(tvm.ir_pass.VerifySSA(stmt))

61
def test_conv1d():
62
    n = tvm.var('n')
63 64 65 66 67
    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')
68
    s = tvm.create_schedule(B.op)
69 70
    px, xi = s[B].split(B.op.axis[0], nparts=1)
    s[B].bind(px, tvm.thread_axis("pipeline"))
71 72 73 74 75 76 77 78
    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
79 80
if __name__ == "__main__":
    test_basic_pipeline()
81
    test_conv1d()