|
| 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 inspect |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +import tvm.testing |
| 23 | +from tvm import relax |
| 24 | +from tvm.script import ir as I, relax as R, tir as T |
| 25 | + |
| 26 | + |
| 27 | +class Base: |
| 28 | + def test_compare(self): |
| 29 | + transform = relax.transform.ReorderTakeAfterMatmul() |
| 30 | + |
| 31 | + if inspect.isclass(self.Expected) and issubclass(self.Expected, Exception): |
| 32 | + with pytest.raises(self.Expected): |
| 33 | + transform(self.Before) |
| 34 | + else: |
| 35 | + after = transform(self.Before) |
| 36 | + tvm.ir.assert_structural_equal(self.Expected, after) |
| 37 | + |
| 38 | + |
| 39 | +class TestSimple(Base): |
| 40 | + @I.ir_module |
| 41 | + class Before: |
| 42 | + @R.function |
| 43 | + def main( |
| 44 | + x: R.Tensor([1, 16], "float32"), |
| 45 | + weight_table: R.Tensor([16, "weight_table_size"], "float32"), |
| 46 | + routing_table: R.Tensor([32], "int64"), |
| 47 | + ) -> R.Tensor([1, 32], "float32"): |
| 48 | + weight_table_size = T.int64() |
| 49 | + with R.dataflow(): |
| 50 | + weight: R.Tensor([16, 32], "float32") = R.take(weight_table, routing_table, axis=1) |
| 51 | + out: R.Tensor([1, 32], "float32") = R.matmul(x, weight) |
| 52 | + R.output(out) |
| 53 | + return out |
| 54 | + |
| 55 | + @I.ir_module |
| 56 | + class Expected: |
| 57 | + @R.function |
| 58 | + def main( |
| 59 | + x: R.Tensor([1, 16], "float32"), |
| 60 | + weight_table: R.Tensor([16, "weight_table_size"], "float32"), |
| 61 | + routing_table: R.Tensor([32], "int64"), |
| 62 | + ) -> R.Tensor([1, 32], "float32"): |
| 63 | + weight_table_size = T.int64() |
| 64 | + with R.dataflow(): |
| 65 | + out_table: R.Tensor([1, weight_table_size], "float32") = R.matmul(x, weight_table) |
| 66 | + out: R.Tensor([1, 32], "float32") = R.take(out_table, routing_table, axis=1) |
| 67 | + R.output(out) |
| 68 | + return out |
| 69 | + |
| 70 | + |
| 71 | +class TestBatchedActivations(Base): |
| 72 | + @I.ir_module |
| 73 | + class Before: |
| 74 | + @R.function |
| 75 | + def main( |
| 76 | + x: R.Tensor(["batch_size", 1, 16], "float32"), |
| 77 | + weight_table: R.Tensor([16, "weight_table_size"], "float32"), |
| 78 | + routing_table: R.Tensor([32], "int64"), |
| 79 | + ) -> R.Tensor(["batch_size", 1, 32], "float32"): |
| 80 | + batch_size = T.int64() |
| 81 | + weight_table_size = T.int64() |
| 82 | + with R.dataflow(): |
| 83 | + weight: R.Tensor([16, 32], "float32") = R.take(weight_table, routing_table, axis=1) |
| 84 | + out: R.Tensor([batch_size, 1, 32], "float32") = R.matmul(x, weight) |
| 85 | + R.output(out) |
| 86 | + return out |
| 87 | + |
| 88 | + @I.ir_module |
| 89 | + class Expected: |
| 90 | + @R.function |
| 91 | + def main( |
| 92 | + x: R.Tensor(["batch_size", 1, 16], "float32"), |
| 93 | + weight_table: R.Tensor([16, "weight_table_size"], "float32"), |
| 94 | + routing_table: R.Tensor([32], "int64"), |
| 95 | + ) -> R.Tensor(["batch_size", 1, 32], "float32"): |
| 96 | + batch_size = T.int64() |
| 97 | + weight_table_size = T.int64() |
| 98 | + with R.dataflow(): |
| 99 | + out_table: R.Tensor([batch_size, 1, weight_table_size], "float32") = R.matmul( |
| 100 | + x, weight_table |
| 101 | + ) |
| 102 | + out: R.Tensor([batch_size, 1, 32], "float32") = R.take( |
| 103 | + out_table, routing_table, axis=2 |
| 104 | + ) |
| 105 | + R.output(out) |
| 106 | + return out |
| 107 | + |
| 108 | + |
| 109 | +class TestStaticBatchedActivationsAndWeights(Base): |
| 110 | + @I.ir_module |
| 111 | + class Before: |
| 112 | + @R.function |
| 113 | + def main( |
| 114 | + x: R.Tensor([128, 1, 16], "float32"), |
| 115 | + weight_table: R.Tensor(["routing_table_size", 16, 32], "float32"), |
| 116 | + routing_table: R.Tensor([128], "int64"), |
| 117 | + ) -> R.Tensor([128, 1, 32], "float32"): |
| 118 | + batch_size = T.int64() |
| 119 | + routing_table_size = T.int64() |
| 120 | + with R.dataflow(): |
| 121 | + weight = R.take(weight_table, routing_table, axis=0) |
| 122 | + out = R.matmul(x, weight) |
| 123 | + R.output(out) |
| 124 | + return out |
| 125 | + |
| 126 | + @I.ir_module |
| 127 | + class Expected: |
| 128 | + @R.function |
| 129 | + def main( |
| 130 | + x: R.Tensor([128, 1, 16], "float32"), |
| 131 | + weight_table: R.Tensor(["routing_table_size", 16, 32], "float32"), |
| 132 | + routing_table: R.Tensor([128], "int64"), |
| 133 | + ) -> R.Tensor([128, 1, 32], "float32"): |
| 134 | + batch_size = T.int64() |
| 135 | + routing_table_size = T.int64() |
| 136 | + with R.dataflow(): |
| 137 | + reordered_weight = R.permute_dims(weight_table, [1, 0, 2]) |
| 138 | + fused_weight = R.reshape(reordered_weight, [16, routing_table_size * 32]) |
| 139 | + fused_output = R.matmul(x, fused_weight) |
| 140 | + reordered_output = R.reshape(fused_output, [128, 1, routing_table_size, 32]) |
| 141 | + tabular_output = R.take(reordered_output, routing_table, axis=2) |
| 142 | + out = R.einsum([tabular_output], "ijik->ijk") |
| 143 | + R.output(out) |
| 144 | + return out |
| 145 | + |
| 146 | + |
| 147 | +class TestDynamicBatchedActivationsAndWeights(Base): |
| 148 | + @I.ir_module |
| 149 | + class Before: |
| 150 | + @R.function |
| 151 | + def main( |
| 152 | + x: R.Tensor(["batch_size", 1, 16], "float32"), |
| 153 | + weight_table: R.Tensor(["routing_table_size", 16, 32], "float32"), |
| 154 | + routing_table: R.Tensor(["batch_size"], "int64"), |
| 155 | + ) -> R.Tensor(["batch_size", 1, 32], "float32"): |
| 156 | + batch_size = T.int64() |
| 157 | + routing_table_size = T.int64() |
| 158 | + with R.dataflow(): |
| 159 | + weight = R.take(weight_table, routing_table, axis=0) |
| 160 | + out = R.matmul(x, weight) |
| 161 | + R.output(out) |
| 162 | + return out |
| 163 | + |
| 164 | + @I.ir_module |
| 165 | + class Expected: |
| 166 | + @R.function |
| 167 | + def main( |
| 168 | + x: R.Tensor(["batch_size", 1, 16], "float32"), |
| 169 | + weight_table: R.Tensor(["routing_table_size", 16, 32], "float32"), |
| 170 | + routing_table: R.Tensor(["batch_size"], "int64"), |
| 171 | + ) -> R.Tensor(["batch_size", 1, 32], "float32"): |
| 172 | + batch_size = T.int64() |
| 173 | + routing_table_size = T.int64() |
| 174 | + with R.dataflow(): |
| 175 | + reordered_weight = R.permute_dims(weight_table, [1, 0, 2]) |
| 176 | + fused_weight = R.reshape(reordered_weight, [16, routing_table_size * 32]) |
| 177 | + fused_output = R.matmul(x, fused_weight) |
| 178 | + reordered_output = R.reshape(fused_output, [batch_size, 1, routing_table_size, 32]) |
| 179 | + tabular_output = R.take(reordered_output, routing_table, axis=2) |
| 180 | + out = R.einsum([tabular_output], "ijik->ijk") |
| 181 | + R.output(out) |
| 182 | + return out |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + tvm.testing.main() |
0 commit comments