|
| 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 | +# pylint: disable=invalid-name,unused-variable,unused-argument |
| 18 | +"""Tensorcore alter op and legalize functions for cuda backend""" |
| 19 | + |
| 20 | +import logging |
| 21 | +import math |
| 22 | +from tvm import relay |
| 23 | + |
| 24 | +from .. import nn |
| 25 | + |
| 26 | +logger = logging.getLogger("topi") |
| 27 | + |
| 28 | + |
| 29 | +@nn.batch_matmul_legalize.register("cuda") |
| 30 | +def _batch_matmul_legalize(attrs, inputs, arg_types): |
| 31 | + """Legalizes batch_matmul op. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + attrs : tvm.ir.Attrs |
| 36 | + Attributes of current convolution |
| 37 | + inputs : list of tvm.relay.Expr |
| 38 | + The args of the Relay expr to be legalized |
| 39 | + arg_types : list of types |
| 40 | + List of input and output types |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + result : tvm.relay.Expr |
| 45 | + The legalized expr |
| 46 | + """ |
| 47 | + # Collect the input tensors. |
| 48 | + x_tensor, y_tensor = arg_types[0], arg_types[1] |
| 49 | + dtype = x_tensor.dtype |
| 50 | + |
| 51 | + # Collect the output tensor. |
| 52 | + output_tensor = arg_types[2] |
| 53 | + |
| 54 | + # Collect the input exprs. |
| 55 | + x, y = inputs |
| 56 | + |
| 57 | + # Pad input and output channels to use tensorcore schedule. |
| 58 | + if dtype in ["float16"]: # todo: support int8/int4 |
| 59 | + B, M, K = x_tensor.shape |
| 60 | + B, N, K = y_tensor.shape |
| 61 | + M = M.value |
| 62 | + K = K.value |
| 63 | + N = N.value |
| 64 | + |
| 65 | + # The shape of (M, K, N) must be multiple of (16, 16, 16) or (32, 16, 8) or (8, 16, 32) |
| 66 | + if ( |
| 67 | + (M % 8 == 0 and K % 16 == 0 and N % 32 == 0) |
| 68 | + or (M % 16 == 0 and K % 16 == 0 and N % 16 == 0) |
| 69 | + or (M % 32 == 0 and K % 16 == 0 and N % 8 == 0) |
| 70 | + ): |
| 71 | + # no need to pad |
| 72 | + return None |
| 73 | + |
| 74 | + (dm, dk, dn), extra_flops = pad_to_tensorcore(M, K, N) |
| 75 | + |
| 76 | + if extra_flops > 2: |
| 77 | + logger.info("batch_matmul pad_to_tensorcore skipped, extra_flops %s", extra_flops) |
| 78 | + return None |
| 79 | + |
| 80 | + logger.info("batch_matmul pad_to_tensorcore, extra_flops %s", extra_flops) |
| 81 | + if dm or dk: |
| 82 | + x_ = relay.nn.pad(x, pad_width=((0, 0), (0, dm), (0, dk))) |
| 83 | + else: |
| 84 | + x_ = x |
| 85 | + if dn or dk: |
| 86 | + y_ = relay.nn.pad(y, pad_width=((0, 0), (0, dn), (0, dk))) |
| 87 | + else: |
| 88 | + y_ = y |
| 89 | + out_ = relay.nn.batch_matmul(x_, y_) |
| 90 | + if dm or dn: |
| 91 | + original_out_shape = [x.value for x in output_tensor.shape] |
| 92 | + out = relay.strided_slice(out_, begin=[0, 0, 0], end=original_out_shape) |
| 93 | + else: |
| 94 | + out = out_ |
| 95 | + return out |
| 96 | + return None |
| 97 | + |
| 98 | + |
| 99 | +@nn.dense_legalize.register("cuda") |
| 100 | +def _dense_legalize(attrs, inputs, arg_types): |
| 101 | + """Legalizes dense op. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + attrs : tvm.ir.Attrs |
| 106 | + Attributes of current convolution |
| 107 | + inputs : list of tvm.relay.Expr |
| 108 | + The args of the Relay expr to be legalized |
| 109 | + types : list of types |
| 110 | + List of input and output types |
| 111 | +
|
| 112 | + Returns |
| 113 | + ------- |
| 114 | + result : tvm.relay.Expr |
| 115 | + The legalized expr |
| 116 | + """ |
| 117 | + # Collect the input tensors. |
| 118 | + x_tensor, y_tensor = arg_types[0], arg_types[1] |
| 119 | + dtype = x_tensor.dtype |
| 120 | + |
| 121 | + # Collect the output tensor. |
| 122 | + output_tensor = arg_types[2] |
| 123 | + |
| 124 | + # Collect the input exprs. |
| 125 | + x, y = inputs |
| 126 | + |
| 127 | + # Pad input and output channels to use tensorcore schedule. |
| 128 | + if dtype in ["float16"]: # todo: support int8/int4 |
| 129 | + M, K = x_tensor.shape |
| 130 | + N, K = y_tensor.shape |
| 131 | + try: |
| 132 | + M = M.value |
| 133 | + K = K.value |
| 134 | + N = N.value |
| 135 | + except AttributeError: |
| 136 | + # todo: deal with unfixed shape when compiling wdl model |
| 137 | + return None |
| 138 | + |
| 139 | + # The shape of (M, K, N) must be multiple of (16, 16, 16) or (32, 16, 8) or (8, 16, 32) |
| 140 | + if ( |
| 141 | + (M % 8 == 0 and K % 16 == 0 and N % 32 == 0) |
| 142 | + or (M % 16 == 0 and K % 16 == 0 and N % 16 == 0) |
| 143 | + or (M % 32 == 0 and K % 16 == 0 and N % 8 == 0) |
| 144 | + ): |
| 145 | + # no need to pad |
| 146 | + return None |
| 147 | + |
| 148 | + (dm, dk, dn), extra_flops_ratio = pad_to_tensorcore(M, K, N) |
| 149 | + |
| 150 | + if extra_flops_ratio > 2: |
| 151 | + logger.info("dense pad_to_tensorcore skipped, extra_flops_ratio %s", extra_flops_ratio) |
| 152 | + return None |
| 153 | + |
| 154 | + logger.info("dense pad_to_tensorcore, extra_flops_ratio %s", extra_flops_ratio) |
| 155 | + |
| 156 | + if dm or dk: |
| 157 | + x_ = relay.nn.pad(x, pad_width=((0, dm), (0, dk))) |
| 158 | + else: |
| 159 | + x_ = x |
| 160 | + if dn or dk: |
| 161 | + y_ = relay.nn.pad(y, pad_width=((0, dn), (0, dk))) |
| 162 | + else: |
| 163 | + y_ = y |
| 164 | + out_ = relay.nn.dense(x_, y_) |
| 165 | + if dm or dn: |
| 166 | + original_out_shape = [x.value for x in output_tensor.shape] |
| 167 | + out = relay.strided_slice(out_, begin=[0, 0], end=original_out_shape) |
| 168 | + else: |
| 169 | + out = out_ |
| 170 | + return out |
| 171 | + return None |
| 172 | + |
| 173 | + |
| 174 | +def pad_to_tensorcore(M, K, N): |
| 175 | + """pad shape to enable tensorcore""" |
| 176 | + candidates = [(16, 16, 16), (32, 16, 8), (8, 16, 32)] |
| 177 | + |
| 178 | + flops = M * K * N |
| 179 | + extra_flops = math.inf |
| 180 | + best_pad = (0, 0, 0) |
| 181 | + for padding in candidates: |
| 182 | + dm, dk, dn = _pad_to(M, K, N, padding) |
| 183 | + e = (M + dm) * (N + dn) * (K + dk) - M * N * K |
| 184 | + # print(dm, dk, dn, e, flops) |
| 185 | + if e < extra_flops: |
| 186 | + extra_flops = e |
| 187 | + best_pad = (dm, dk, dn) |
| 188 | + return best_pad, extra_flops / flops |
| 189 | + |
| 190 | + |
| 191 | +def _pad_to(M, K, N, PADDING): |
| 192 | + dm, dk, dn = 0, 0, 0 |
| 193 | + |
| 194 | + if M % PADDING[0] != 0: |
| 195 | + M_ = ((M + PADDING[0]) // PADDING[0]) * PADDING[0] |
| 196 | + dm = M_ - M |
| 197 | + if K % PADDING[1] != 0: |
| 198 | + K_ = ((K + PADDING[1]) // PADDING[1]) * PADDING[1] |
| 199 | + dk = K_ - K |
| 200 | + if N % PADDING[2] != 0: |
| 201 | + N_ = ((N + PADDING[2]) // PADDING[2]) * PADDING[2] |
| 202 | + dn = N_ - N |
| 203 | + |
| 204 | + return dm, dk, dn |
0 commit comments