|
| 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 | +"""Schedule for dense operator""" |
| 19 | + |
| 20 | +from tvm import te, tir |
| 21 | +from tvm.topi import tag |
| 22 | +from ..utils import get_layout_transform_fn |
| 23 | + |
| 24 | + |
| 25 | +def qdense_compute( |
| 26 | + tensor_a, |
| 27 | + tensor_b, |
| 28 | + zero_a, |
| 29 | + scale_a, |
| 30 | + zero_b, |
| 31 | + scale_b, |
| 32 | + zero_out=None, |
| 33 | + scale_out=None, |
| 34 | + bias=None, |
| 35 | + q_dtype=None, |
| 36 | +): |
| 37 | + """Hexagon's implementation of a sliced dense operator in Topi. |
| 38 | + Uses matmul. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + tensor_a : tvm.te.Tensor |
| 43 | + data 2-D with shape [batch, in_dim] |
| 44 | +
|
| 45 | + tensor_b : tvm.te.Tensor |
| 46 | + weight 2-D with shape [in_dim, out_dim] |
| 47 | +
|
| 48 | + zero_a : integer |
| 49 | + quantization zero point for tensor a. |
| 50 | +
|
| 51 | + scale_a : float |
| 52 | + quantization scale for tensor a. |
| 53 | +
|
| 54 | + zero_b : integer |
| 55 | + quantization zero point for tensor b. |
| 56 | +
|
| 57 | + scale_b : float |
| 58 | + quantization scale for tensor b. |
| 59 | +
|
| 60 | + zero_out : Optional[integer] |
| 61 | + quantization zero point for output. |
| 62 | +
|
| 63 | + scale_out : Optional[float] |
| 64 | + quantization scale for output. |
| 65 | +
|
| 66 | + bias : Optional[tvm.te.Tensor] |
| 67 | + 1-D with shape [out_dim] |
| 68 | +
|
| 69 | + q_dtype : Optional[str] |
| 70 | + The output type. |
| 71 | +
|
| 72 | + Returns |
| 73 | + ------- |
| 74 | + mat : tvm.te.Tensor |
| 75 | + 2-D with shape [batch, out_dim] |
| 76 | +
|
| 77 | + """ |
| 78 | + if bias is not None: |
| 79 | + assert len(bias.shape) == 1 |
| 80 | + if q_dtype is None: |
| 81 | + q_dtype = tensor_a.dtype |
| 82 | + |
| 83 | + batch, in_dim = tensor_a.shape |
| 84 | + out_dim, red_dim = tensor_b.shape |
| 85 | + |
| 86 | + # cmp should be done by values |
| 87 | + assert int(in_dim) == int(red_dim) |
| 88 | + |
| 89 | + k = te.reduce_axis((0, in_dim), name="k") |
| 90 | + compute_lambda = lambda n, m: te.sum( |
| 91 | + scale_a |
| 92 | + * (tensor_a[n, k].astype("float32") - zero_a) |
| 93 | + * scale_b |
| 94 | + * (tensor_b[k, m].astype("float32") - zero_b), |
| 95 | + axis=k, |
| 96 | + ) |
| 97 | + compute_name = "qmatmul_sliced" |
| 98 | + |
| 99 | + out = te.compute( |
| 100 | + (batch, out_dim), |
| 101 | + compute_lambda, |
| 102 | + name=compute_name, |
| 103 | + attrs={"layout_free_placeholders": [tensor_b]}, |
| 104 | + ) |
| 105 | + |
| 106 | + if bias is not None: |
| 107 | + out = te.compute( |
| 108 | + (batch, out_dim), |
| 109 | + lambda i, j: out[i, j] + bias[j], |
| 110 | + tag=tag.BROADCAST, |
| 111 | + name="bias", |
| 112 | + ) |
| 113 | + |
| 114 | + # Requantization of dense |
| 115 | + if scale_out is not None: |
| 116 | + out = te.compute( |
| 117 | + (batch, out_dim), |
| 118 | + lambda *i: (out[i] / scale_out + zero_out).astype(q_dtype), |
| 119 | + name="requantize", |
| 120 | + ) |
| 121 | + |
| 122 | + return out |
| 123 | + |
| 124 | + |
| 125 | +def qdense_schedule(outs, ins, output_layout: str, input_layout: str): |
| 126 | + """Schedule for dense op. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + outs: Array of Tensor |
| 131 | + The computation graph description of dense in the format |
| 132 | + of an array of tensors. |
| 133 | +
|
| 134 | + ins: Array of Tensor |
| 135 | + Input tensors into graph. |
| 136 | +
|
| 137 | + output_layout: str |
| 138 | + Descriptor string for physical layout |
| 139 | +
|
| 140 | + input_layout: str |
| 141 | + Descriptor string for physical layout |
| 142 | +
|
| 143 | + Returns |
| 144 | + ------- |
| 145 | + sch: Schedule |
| 146 | + The computation schedule for the op. |
| 147 | + """ |
| 148 | + if not isinstance(ins, list): |
| 149 | + ins = [ins] |
| 150 | + if not isinstance(outs, list): |
| 151 | + outs = [outs] |
| 152 | + |
| 153 | + func = te.create_prim_func([*ins, *outs]) |
| 154 | + s = tir.Schedule(func) |
| 155 | + |
| 156 | + matmul = s.get_block("qmatmul_sliced") |
| 157 | + try: |
| 158 | + requantize = s.get_block("requantize") |
| 159 | + except tir.schedule.schedule.ScheduleError: |
| 160 | + requantize = None |
| 161 | + try: |
| 162 | + bias = s.get_block("bias") |
| 163 | + except tir.schedule.schedule.ScheduleError: |
| 164 | + bias = None |
| 165 | + |
| 166 | + input_transform_fn = get_layout_transform_fn(input_layout) |
| 167 | + output_transform_fn = get_layout_transform_fn(output_layout) |
| 168 | + |
| 169 | + # Transform input and output buffer |
| 170 | + s.transform_layout(matmul, ("read", 0), input_transform_fn) |
| 171 | + if requantize is not None: |
| 172 | + s.transform_layout(requantize, ("write", 0), output_transform_fn) |
| 173 | + elif bias is not None: |
| 174 | + s.transform_layout(bias, ("write", 0), output_transform_fn) |
| 175 | + else: |
| 176 | + s.transform_layout(matmul, ("write", 0), output_transform_fn) |
| 177 | + |
| 178 | + # Vectorize |
| 179 | + _, matmul_c, _ = s.get_loops(matmul) |
| 180 | + _, matmul_c_inner = s.split(matmul_c, [None, 128]) |
| 181 | + s.vectorize(matmul_c_inner) |
| 182 | + |
| 183 | + # Compute everything inline |
| 184 | + if bias is not None and requantize is not None: |
| 185 | + _, bias_c = s.get_loops(bias) |
| 186 | + s.compute_at(matmul, bias_c) |
| 187 | + _, out_c = s.get_loops(requantize) |
| 188 | + s.compute_at(bias, out_c) |
| 189 | + elif bias is not None and requantize is None: |
| 190 | + _, out_c = s.get_loops(bias) |
| 191 | + s.compute_at(matmul, out_c) |
| 192 | + |
| 193 | + return s |
0 commit comments