test_topi_l2norm.py 2.32 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
"""Test code for L2 normalization"""
18 19 20 21
import numpy as np
import tvm
import topi
from topi.util import get_const_tuple
22
import topi.testing
23

24
def verify_l2_normalize(ishape, eps, axis=None):
25

26 27
    A = tvm.placeholder(ishape, name='A')
    B = topi.nn.l2_normalize(A, eps, axis)
28 29
    dtype = A.dtype

30 31
    a_np = np.random.uniform(size=ishape).astype(dtype)
    b_np = topi.testing.l2_normalize_python(a_np, eps, axis)
32 33

    def check_device(device):
34 35
        ctx = tvm.context(device, 0)
        if not ctx.exist:
36 37 38 39
            print("Skip because %s is not enabled" % device)
            return
        print("Running on target: %s" % device)
        with tvm.target.create(device):
40 41 42 43
            if device == 'llvm':
                s = topi.generic.schedule_l2_normalize([B])
            else:
                s = topi.cuda.schedule_l2_normalize([B])
44 45 46 47
        a = tvm.nd.array(a_np, ctx)
        b = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), ctx)
        f = tvm.build(s, [A, B], device)
        f(a, b)
48
        tvm.testing.assert_allclose(b.asnumpy(), b_np, rtol=1e-5)
49

50
    for device in ['llvm', 'cuda', 'opencl', 'metal', 'rocm', 'vulkan', 'nvptx']:
51 52
        check_device(device)

53 54 55 56 57 58 59
def test_l2_normalize():
    verify_l2_normalize((1, 3, 20, 20), 0.001)
    verify_l2_normalize((1, 3, 20, 20), 0.001, (1,))
    verify_l2_normalize((1, 3, 20, 20), 0.001, (1, 2))
    verify_l2_normalize((1, 3, 20, 20), 0.001, (2, 3))
    verify_l2_normalize((1, 3, 20, 20), 0.001, (0, 3))
    verify_l2_normalize((1, 3, 20, 20), 0.001, (0, 2, 3))
60 61 62


if __name__ == "__main__":
63
    test_l2_normalize()