test_lang_target.py 2.12 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
import tvm

@tvm.target.generic_func
def mygeneric(data):
    # default generic function
    return data + 1

@mygeneric.register(["cuda", "gpu"])
def cuda_func(data):
    return data + 2

@mygeneric.register("rocm")
def rocm_func(data):
    return data + 3

@mygeneric.register("cpu")
def rocm_func(data):
    return data + 10


def test_target_dispatch():
    with tvm.target.cuda():
        assert mygeneric(1) == 3

    with tvm.target.rocm():
        assert mygeneric(1) == 4

    with tvm.target.create("cuda"):
        assert mygeneric(1) == 3

47
    with tvm.target.arm_cpu():
48 49 50 51 52
        assert mygeneric(1) == 11

    with tvm.target.create("metal"):
        assert mygeneric(1) == 3

53
    assert tvm.target.current_target() is None
54

55

56
def test_target_string_parse():
57
    target = tvm.target.create("cuda -model=unknown -libs=cublas,cudnn")
58 59

    assert target.target_name == "cuda"
60
    assert target.options == ['-model=unknown', '-libs=cublas,cudnn']
61 62
    assert target.keys == ['cuda', 'gpu']
    assert target.libs == ['cublas', 'cudnn']
63
    assert str(target) == str(tvm.target.cuda(options="-libs=cublas,cudnn"))
64

65
    assert tvm.target.intel_graphics().device_name == "intel_graphics"
66 67
    assert tvm.target.mali().device_name == "mali"
    assert tvm.target.arm_cpu().device_name == "arm_cpu"
68

69 70
if __name__ == "__main__":
    test_target_dispatch()
71
    test_target_string_parse()