test_autotvm_dispatch_context.py 1.98 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
"""Test dispatcher.
The dispatcher can choose which template to use according
to the parameters of workload"""

from collections import namedtuple
22
from tvm import autotvm
23 24
from tvm.autotvm.task import dispatcher, DispatchContext

25
SimpleConfig = namedtuple('SimpleConfig', ('template_key', 'is_fallback'))
26 27 28 29

def test_dispatch():
    @dispatcher
    def my_dispatcher(a, b):
30
        return (a, b)
31 32

    @my_dispatcher.register("im2col")
33 34 35 36 37 38
    def _im2col(cfg, a, b):
        return a

    @my_dispatcher.register("spatial_pack")
    def _spatial_pack(cfg, a, b):
        return b
39 40 41

    class SimpleDispatcher(DispatchContext):
        def query(self, target, workload):
42 43 44 45
            a, b = workload
            tkey = "spatial_pack" if a + b > 2 else "im2col"
            cfg = SimpleConfig(tkey, False)
            return cfg
46 47

    with SimpleDispatcher():
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        # this will call im2col
        assert my_dispatcher(1, 0) == 1

        # this will call spatial pack
        assert my_dispatcher(1, 100) == 100

def test_fallback():

    @autotvm.template
    def simple_template(a, b):
        cfg = autotvm.get_config()
        assert cfg.is_fallback

    simple_template(2, 3)

63 64 65

if __name__ == "__main__":
    test_dispatch()
66
    test_fallback()