test_tir_pass_lower_intrin.py 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 # 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.
import tvm
18
from tvm import te
19 20 21 22
import numpy as np

def lower_intrin(stmt):
    """wrapper to call transformation in stmt"""
23 24
    lower_expr = isinstance(stmt, tvm.tir.PrimExpr)
    stmt = tvm.tir.Evaluate(stmt) if lower_expr else stmt
25 26
    stmt = tvm.tir.ir_pass.CanonicalSimplify(stmt)
    stmt  = tvm.tir.ir_pass._LowerIntrinStmt(stmt, "llvm")
27 28 29 30 31
    return stmt.value if lower_expr else stmt.body


def check_value(expr, vx, vy, data, fref):
    n = len(data)
32 33
    A = te.placeholder((n,), name="A", dtype=expr.dtype)
    B = te.placeholder((n,), name="B", dtype=expr.dtype)
34 35 36

    def make_binds(i):
        x = expr
37 38
        x = tvm.tir.Let(vx, A[i], x)
        x = tvm.tir.Let(vy, B[i], x)
39 40
        return x

41 42
    C = te.compute((n,), make_binds)
    s = te.create_schedule([C.op])
43

44
    if not tvm.runtime.enabled("llvm"):
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        return

    f = tvm.build(s, [A, B, C], "llvm")
    a = tvm.nd.array(np.array([x for x, y in data], dtype=expr.dtype))
    b = tvm.nd.array(np.array([y for x, y in data], dtype=expr.dtype))
    c = tvm.nd.array(np.zeros(len(data), dtype=expr.dtype))
    f(a, b, c)
    cref = np.array([fref(x, y) for x, y in data])
    np.testing.assert_equal(c.asnumpy(), cref)



def get_ref_data():
    """Get reference data for every pairs"""
    import itertools
    x = range(-10, 10)
    y = list(range(-10, 10))
    y.remove(0)
    return list(itertools.product(x, y))


def test_lower_floordiv():
    data = get_ref_data()
    for dtype in ["int32", "int64", "int16"]:
69 70 71
        x = te.var("x", dtype=dtype)
        y = te.var("y", dtype=dtype)
        zero = tvm.tir.const(0, dtype)
72
        # no constraints
73
        res = lower_intrin(tvm.te.floordiv(x, y))
74 75
        check_value(res, x, y, data, lambda a, b: a // b)
        # rhs >= 0
76
        res = lower_intrin(tvm.tir.Select(y >= 0, tvm.te.floordiv(x, y), zero))
77 78
        check_value(res, x, y, data, lambda a, b: a // b if b > 0 else 0)
        # involves max
79
        res = lower_intrin(tvm.tir.Select(y >= 0, tvm.te.max(tvm.te.floordiv(x, y), zero), zero))
80 81
        check_value(res, x, y, data, lambda a, b: max(a // b, 0) if b > 0 else 0)
        # lhs >= 0
82
        res = lower_intrin(tvm.tir.Select(tvm.tir.all(y >= 0, x >= 0), tvm.te.floordiv(x, y), zero))
83 84
        check_value(res, x, y, data, lambda a, b: a // b if b > 0 and a >= 0 else 0)
        # const power of two
85
        res = lower_intrin(tvm.te.floordiv(x, tvm.tir.const(8, dtype=dtype)))
86 87 88 89 90 91
        check_value(res, x, y, [(a, b) for a, b in data if b == 8], lambda a, b: a // b)


def test_lower_floormod():
    data = get_ref_data()
    for dtype in ["int32", "int64", "int16"]:
92 93 94
        x = te.var("x", dtype=dtype)
        y = te.var("y", dtype=dtype)
        zero = tvm.tir.const(0, dtype)
95
        # no constraints
96
        res = lower_intrin(tvm.te.floormod(x, y))
97 98
        check_value(res, x, y, data, lambda a, b: a % b)
        # rhs >= 0
99
        res = lower_intrin(tvm.tir.Select(y >= 0, tvm.te.floormod(x, y), zero))
100 101
        check_value(res, x, y, data, lambda a, b: a % b if b > 0 else 0)
        # lhs >= 0
102
        res = lower_intrin(tvm.tir.Select(tvm.tir.all(y >= 0, x >= 0), tvm.te.floormod(x, y), zero))
103 104
        check_value(res, x, y, data, lambda a, b: a % b if b > 0 and a >= 0 else 0)
        # const power of two
105
        res = lower_intrin(tvm.te.floormod(x, tvm.tir.const(8, dtype=dtype)))
106 107 108 109 110 111 112
        check_value(res, x, y, [(a, b) for a, b in data if b == 8], lambda a, b: a % b)



if __name__ == "__main__":
    test_lower_floordiv()
    test_lower_floormod()