test_autotvm_feature.py 4.35 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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
"""Test feature extraction"""

import numpy as np

import tvm
from tvm.autotvm import feature

def test_iter_feature_gemm():
    N = 128

    k = tvm.reduce_axis((0, N), 'k')
    A = tvm.placeholder((N, N), name='A')
    B = tvm.placeholder((N, N), name='B')
    C = tvm.compute(
        A.shape,
        lambda y, x: tvm.sum(A[y, k] * B[k, x], axis=k),
        name='C')

    s = tvm.create_schedule(C.op)

    feas = feature.get_itervar_feature(s, [A, B, C], take_log=False)

    expected = [
        {
            '_attr_': [128, 1, 128, 2097152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
            'A_0': [128, -1, 16384, 128, 0, 0], 'B_0': [0, -1, 16384, 128, 0, 0],
            'C_0': [128, -1, 16384, 128, 0, 0], 'C_1': [128, -1, 16384, 128, 0, 0],
        },
        {
            '_attr_': [128, 2, 16384, 16384, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
            'A_0': [0, -1, 128, 128, 0, 0], 'B_0': [1, -1, 16384, 1, 0, 0],
            'C_0': [1, -1, 128, 128, 0, 0], 'C_1': [1, -1, 128, 128, 0, 0],
        },
        {
            '_attr_': [128, 3, 2097152, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
            'A_0': [1, -1, 128, 1, 0, 0], 'B_0': [128, -1, 128, 1, 0, 0],
            'C_1': [0, -1, 1, 128, 0, 0], 'C_2':  [0, -1, 1, 128, 0, 0],
        }
    ]

    for ans, row in zip(expected, feas):
        for pair in row:
            if pair[0] not in ans:
                continue
            assert ans[pair[0]] == pair[1:], "%s: %s vs %s" % (pair[0], ans[pair[0]], pair[1:])


64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
def test_curve_feature_gemm():
    N = 128

    k = tvm.reduce_axis((0, N), 'k')
    A = tvm.placeholder((N, N), name='A')
    B = tvm.placeholder((N, N), name='B')
    C = tvm.compute(
        A.shape,
        lambda y, x: tvm.sum(A[y, k] * B[k, x], axis=k),
        name='C')

    s = tvm.create_schedule(C.op)

    feas = feature.get_buffer_curve_sample_flatten(s, [A, B, C], sample_n=30)
    # sample_n * #buffers * #curves * 2 numbers per curve
    assert len(feas) == 30 * 3 * 4 * 2

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
def test_feature_shape():
    """test the dimensions of flatten feature are the same"""

    N = 1024
    n_sample = 100

    def get_gemm_feature(target):
        k = tvm.reduce_axis((0, N), 'k')
        A = tvm.placeholder((N, N), name='A')
        B = tvm.placeholder((N, N), name='B')
        C = tvm.compute(A.shape, lambda y, x: tvm.sum(A[y, k] * B[k, x], axis=k),
                        name='C')

        s = tvm.create_schedule(C.op)

        y, x = s[C].op.axis
        axes = list(s[C].tile(y, x, 8, 8)) + [k]
        perm = np.random.permutation(5)
        axes = [axes[x] for x in perm]
        s[C].reorder(*axes)

        if "gpu" in target.keys:
            pick = []
            # filter out reduction axis
            for i in range(len(perm)):
                if perm[i] != 4:
                    pick.append(axes[i])
            s[C].bind(pick[0], tvm.thread_axis("blockIdx.x"))
            s[C].bind(pick[1], tvm.thread_axis("vthread"))
            s[C].bind(pick[2], tvm.thread_axis("threadIdx.y"))

        with target:
            feas = feature.get_itervar_feature(s, [A, B, C])
            feas = feature.flatten_itervar_feature(feas)
        return feas

    targets = [
        tvm.target.cuda(),
        tvm.target.mali(),
120
        tvm.target.arm_cpu(),
121 122 123 124 125 126 127 128 129 130 131
    ]

    for target in targets:
        dim = len(get_gemm_feature(target))
        for i in range(n_sample):
            assert dim == len(get_gemm_feature(target)), "dimensions of feature do not match" \
                                                   " for different configurations"


if __name__ == "__main__":
    test_iter_feature_gemm()
132
    test_curve_feature_gemm()
133
    test_feature_shape()
134