|
| 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 | + |
| 18 | +import pytest |
| 19 | +import numpy as np |
| 20 | +from tvm import te, topi |
| 21 | + |
| 22 | +import tvm.testing |
| 23 | +from tvm.topi import testing |
| 24 | +from tvm.contrib.hexagon.build import HexagonLauncher |
| 25 | + |
| 26 | +import tvm.topi.hexagon.slice_ops as sl |
| 27 | +from .infrastructure import allocate_hexagon_array |
| 28 | + |
| 29 | + |
| 30 | +def transform_numpy(arr_np, layout): |
| 31 | + |
| 32 | + if layout in ["nc-512c-2d"]: |
| 33 | + N, C = arr_np.shape |
| 34 | + return arr_np.reshape([N, C // 512, 512]) |
| 35 | + raise RuntimeError(f"Unexpected layout '{layout}'") |
| 36 | + |
| 37 | + |
| 38 | +@tvm.testing.fixture |
| 39 | +def input_np(input_shape, dtype): |
| 40 | + return (np.random.uniform(size=input_shape)).astype(dtype) |
| 41 | + |
| 42 | + |
| 43 | +@tvm.testing.fixture |
| 44 | +def transformed_expected_output_np(expected_output_np, output_layout): |
| 45 | + return transform_numpy(expected_output_np, output_layout) |
| 46 | + |
| 47 | + |
| 48 | +@tvm.testing.fixture |
| 49 | +def transformed_input_np(input_np, input_layout): |
| 50 | + return transform_numpy(input_np, input_layout) |
| 51 | + |
| 52 | + |
| 53 | +class Basesoftmax2d: |
| 54 | + |
| 55 | + input_shape, input_layout, output_layout, axis_sep = tvm.testing.parameters( |
| 56 | + ((1, 1024), "nc-512c-2d", "nc-512c-2d", [2]) |
| 57 | + ) |
| 58 | + dtype = tvm.testing.parameter("float32") |
| 59 | + working_scope = tvm.testing.parameter("global.vtcm") |
| 60 | + |
| 61 | + |
| 62 | +class TestSoftmax2d(Basesoftmax2d): |
| 63 | + @tvm.testing.fixture |
| 64 | + def expected_output_np(self, input_np): |
| 65 | + if len(input_np.shape) == 2: |
| 66 | + ref_np_2d = tvm.topi.testing.softmax_python(input_np) |
| 67 | + return ref_np_2d |
| 68 | + raise RuntimeError(f"Unexpected input shape '{input_np.shape}'") |
| 69 | + |
| 70 | + @tvm.testing.requires_hexagon |
| 71 | + def test_softmax_f32( |
| 72 | + self, |
| 73 | + dtype, |
| 74 | + input_layout, |
| 75 | + output_layout, |
| 76 | + input_shape, |
| 77 | + input_np, |
| 78 | + transformed_input_np, |
| 79 | + transformed_expected_output_np, |
| 80 | + expected_output_np, |
| 81 | + working_scope, |
| 82 | + axis_sep, |
| 83 | + hexagon_session, |
| 84 | + ): |
| 85 | + |
| 86 | + target_hexagon = tvm.target.hexagon( |
| 87 | + "v69", |
| 88 | + llvm_options="--disable-loop-unrolling-pass", |
| 89 | + ) |
| 90 | + A = te.placeholder(input_shape, name="A", dtype=dtype) |
| 91 | + |
| 92 | + O = sl.softmax_compute(A) |
| 93 | + |
| 94 | + if input_layout == "nc-512c-2d": |
| 95 | + tir_s = sl.softmax_stir_schedule(O, A, output_layout, input_layout) |
| 96 | + sch = tir_s.mod |
| 97 | + else: |
| 98 | + raise RuntimeError(f"Unexpected input layout '{input_layout}'") |
| 99 | + |
| 100 | + with tvm.transform.PassContext( |
| 101 | + opt_level=3, |
| 102 | + config={ |
| 103 | + "tir.LoopPartition": {"partition_const_loop": True}, |
| 104 | + }, |
| 105 | + ): |
| 106 | + |
| 107 | + func = tvm.build( |
| 108 | + sch, |
| 109 | + [A, O], |
| 110 | + tvm.target.Target(target_hexagon, host=target_hexagon), |
| 111 | + name="softmax_slice", |
| 112 | + ) |
| 113 | + |
| 114 | + input_arr = allocate_hexagon_array( |
| 115 | + hexagon_session.device, |
| 116 | + data=transformed_input_np, |
| 117 | + axis_separators=axis_sep, |
| 118 | + mem_scope=working_scope, |
| 119 | + ) |
| 120 | + |
| 121 | + output_arr = allocate_hexagon_array( |
| 122 | + hexagon_session.device, |
| 123 | + tensor_shape=transformed_expected_output_np.shape, |
| 124 | + dtype=transformed_expected_output_np.dtype, |
| 125 | + axis_separators=axis_sep, |
| 126 | + mem_scope=working_scope, |
| 127 | + ) |
| 128 | + |
| 129 | + mod = hexagon_session.load_module(func) |
| 130 | + mod(input_arr, output_arr) |
| 131 | + |
| 132 | + n, c = input_np.shape |
| 133 | + output_np = output_arr.numpy().reshape(1, c // 512, 512) |
| 134 | + |
| 135 | + np.testing.assert_allclose(output_np, transformed_expected_output_np, rtol=1e-4, atol=1e-4) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + |
| 140 | + sys.exit(pytest.main(sys.argv)) |
0 commit comments