jwfromm commented on a change in pull request #5099: [TOPI][Tensor Core] Conv2d and Dense ops support on Tensor Core URL: https://github.com/apache/incubator-tvm/pull/5099#discussion_r395911192
########## File path: topi/python/topi/cuda/conv2d_nhwc_tensorcore.py ########## @@ -0,0 +1,361 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=invalid-name, too-many-locals, too-many-arguments +# pylint: disable=too-many-statements, unused-argument +"""Tensorcore template for cuda backend""" +import numpy as np +import tvm +from tvm import te +from tvm import autotvm +from ..util import get_const_tuple, traverse_inline, simplify +from ..nn.pad import pad +from ..nn.util import get_pad_tuple +from .tensor_intrin import intrin_wmma_load_matrix_A +from .tensor_intrin import intrin_wmma_load_matrix_W +from .tensor_intrin import intrin_wmma_store_matrix + + +def intrin_wmma_gemm(strides_A, strides_W, strides_Conv, shape, out_dtype): + """Intrin for wmma fill_fragment and mma_sync""" + wmma_m, wmma_n, wmma_k = shape + A = te.placeholder((wmma_m, 1, 1, wmma_k), name='A', dtype='float16') + B = te.placeholder((wmma_k, wmma_n), name='B', dtype='float16') + k = te.reduce_axis((0, wmma_k), name="k") + C = te.compute((wmma_m, 1, 1, wmma_n), + lambda ii, t0, t1, jj: + te.sum(A[ii, t0, t1, k].astype(out_dtype) * \ + B[k, jj].astype(out_dtype), axis=k), + name='C') + BA = tvm.tir.decl_buffer(A.shape, A.dtype, name='BA', + scope='wmma.matrix_a', data_alignment=32, + offset_factor=8, strides=strides_A) + BB = tvm.tir.decl_buffer(B.shape, B.dtype, name='BB', + scope='wmma.matrix_b', data_alignment=32, + offset_factor=8, strides=strides_W) + BC = tvm.tir.decl_buffer(C.shape, C.dtype, name='BC', + scope='wmma.accumulator', data_alignment=32, + offset_factor=8, strides=strides_Conv) + + def intrin_func(ins, outs): + BA, BB = ins + BC, = outs + + def warp_idnex(offset, row, col): + row = row * col + return offset // row + offset % row // col + + warp_index_A = warp_idnex(BA.elem_offset, wmma_m, wmma_k) + warp_index_B = warp_idnex(BB.elem_offset, wmma_k, wmma_n) + warp_index_C = warp_idnex(BC.elem_offset, wmma_m, wmma_n) + + def init(): + ib = tvm.tir.ir_builder.create() + ib.emit( + tvm.tir.call_intrin('handle', 'tvm_fill_fragment', BC.data, wmma_m, wmma_n, wmma_k, + warp_index_C, 0.0)) + return ib.get() + + def update(): + ib = tvm.tir.ir_builder.create() + ib.emit(tvm.tir.call_intrin('handle', 'tvm_mma_sync', + BC.data, warp_index_C, + BA.data, warp_index_A, + BB.data, warp_index_B, + BC.data, warp_index_C)) + return ib.get() + + return update(), init(), update() + + return te.decl_tensor_intrin(C.op, intrin_func, binds={A: BA, B: BB, C: BC}) Review comment: The two `intrin_wmma_gemm` functions in conv2d and dense are extremely similar. Do you think it makes sense to instead have one more general gemm intrinsic for both or is there something preventing that? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services