test_autotvm_common.py 3.27 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
"""Common utilities for testing autotvm"""
import time

20 21
import numpy as np

22
import tvm
23
from tvm import te
24 25
from tvm import autotvm
from tvm.autotvm import MeasureInput, MeasureResult
26 27 28 29 30 31 32 33 34 35 36 37 38
from tvm.autotvm.measure.measure import Runner


class DummyRunner(Runner):
    def __init__(self):
        super(DummyRunner, self).__init__(1, 1)

    def run(self, measure_inputs, build_results):
        return [MeasureResult((np.random.random(),), 0, 0.2, time.time())
                for _ in range(len(measure_inputs))]

    def get_build_kwargs(self):
        return {}
39

40
@autotvm.template("testing/matmul")
41
def matmul(N, L, M, dtype):
42 43
    A = te.placeholder((N, L), name='A', dtype=dtype)
    B = te.placeholder((L, M), name='B', dtype=dtype)
44

45 46 47
    k = te.reduce_axis((0, L), name='k')
    C = te.compute((N, M), lambda i, j: te.sum(A[i, k] * B[k, j], axis=k), name='C')
    s = te.create_schedule(C.op)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    # schedule
    y, x = s[C].op.axis
    k = s[C].op.reduce_axis[0]

    ##### define space begin #####
    cfg = autotvm.get_config()
    cfg.define_split("tile_y", y, num_outputs=2)
    cfg.define_split("tile_x", x, num_outputs=2)
    ##### define space end #####

    # schedule according to config
    yo, yi = cfg["tile_y"].apply(s, C, y)
    xo, xi = cfg["tile_x"].apply(s, C, x)

    s[C].reorder(yo, xo, k, yi, xi)

    return s, [A, B, C]

67
@autotvm.template("testing/bad_matmul")
68
def bad_matmul(N, L, M, dtype):
69
    if 'bad_device' in tvm.target.Target.current().keys:
70 71
        A = te.placeholder((N, L), name='A', dtype=dtype)
        B = te.placeholder((L, M), name='B', dtype=dtype)
72

73 74 75
        k = te.reduce_axis((0, L-1), name='k')
        C = te.compute((N, M), lambda i, j: te.sum(A[i, k] * B[k, j], axis=k), name='C')
        s = te.create_schedule(C.op)
76 77 78 79 80 81 82 83 84 85

        # schedule
        y, x = s[C].op.axis
        cfg = autotvm.get_config()
        cfg.define_split("tile_y", y, num_outputs=2)
        cfg.define_split("tile_x", x, num_outputs=2)
        return s, [A, B, C]

    return matmul(N, L, M, dtype)

86 87 88
def get_sample_task(n=128):
    """return a sample task for testing"""
    target = tvm.target.create("llvm")
89
    task = autotvm.task.create("testing/matmul", args=(n, n, n, 'float32'), target=target)
90 91 92 93 94 95 96 97 98 99 100
    return task, target

def get_sample_records(n):
    """get sample records for testing"""
    tsk, target = get_sample_task()

    inps, ress = [], []
    for i in range(n):
        inps.append(MeasureInput(target, tsk, tsk.config_space.get(i)))
        ress.append(MeasureResult((i+1,), 0, i, time.time()))
    return list(zip(inps, ress))