|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Test code for instance_norm.""" |
| 18 | +import numpy as np |
| 19 | +import pytest |
| 20 | +import tvm |
| 21 | +from tvm import te |
| 22 | +from tvm import topi |
| 23 | +from tvm.topi.utils import get_const_tuple |
| 24 | +import tvm.topi.testing |
| 25 | + |
| 26 | +import tvm.testing |
| 27 | + |
| 28 | + |
| 29 | +_instance_norm_schedule = { |
| 30 | + "generic": topi.generic.schedule_injective, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +# only test on llvm because schedule is missing |
| 35 | +@tvm.testing.parametrize_targets("llvm") |
| 36 | +@pytest.mark.parametrize("shape,axis", [([4, 16], (1,)), ([4, 16, 16], (1, 2))]) |
| 37 | +def test_instance_norm( |
| 38 | + target, dev, shape, axis, episilon=1e-5, dtype="float32", rtol=1e-5, atol=1e-5 |
| 39 | +): |
| 40 | + data = te.placeholder(shape, dtype=dtype, name="data") |
| 41 | + scale_shape = [shape[dim] for dim in axis] |
| 42 | + gamma = te.placeholder(scale_shape, dtype=dtype, name="gamma") |
| 43 | + beta = te.placeholder(scale_shape, dtype=dtype, name="beta") |
| 44 | + B = topi.nn.instance_norm(data, gamma, beta, axis, episilon) |
| 45 | + |
| 46 | + data_np = np.random.uniform(size=shape).astype(dtype) |
| 47 | + gamma_np = np.random.uniform(size=scale_shape).astype(dtype) |
| 48 | + beta_np = np.random.uniform(size=scale_shape).astype(dtype) |
| 49 | + b_np = tvm.topi.testing.instance_norm_python(data_np, gamma_np, beta_np, axis, episilon) |
| 50 | + |
| 51 | + with tvm.target.Target(target): |
| 52 | + s_func = tvm.topi.testing.dispatch(target, _instance_norm_schedule) |
| 53 | + s = s_func([B]) |
| 54 | + data_tvm = tvm.nd.array(data_np, dev) |
| 55 | + gamma_tvm = tvm.nd.array(gamma_np, dev) |
| 56 | + beta_tvm = tvm.nd.array(beta_np, dev) |
| 57 | + b_tvm = tvm.nd.array(np.zeros(get_const_tuple(B.shape), dtype=dtype), dev) |
| 58 | + f = tvm.build(s, [data, gamma, beta, B], target) |
| 59 | + f(data_tvm, gamma_tvm, beta_tvm, b_tvm) |
| 60 | + tvm.testing.assert_allclose(b_tvm.numpy(), b_np, rtol=rtol, atol=atol) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + tvm.testing.main() |
0 commit comments