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_r395914065
 
 

 ##########
 File path: topi/python/topi/cuda/dense_tensorcore.py
 ##########
 @@ -0,0 +1,290 @@
+# 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-statements, 
unused-argument
+"""Compute and Schedule definition for dense tensorcore with cuda backend"""
+from __future__ import absolute_import as _abs
+import tvm
+from tvm import te
+import tvm.autotvm as autotvm
+from .. import tag
+from ..util import traverse_inline, get_const_tuple
+from .tensor_intrin import intrin_wmma_load_matrix_A, \
+        intrin_wmma_load_matrix_W, intrin_wmma_store_matrix
+
+
+@autotvm.register_topi_compute("dense_tensorcore.cuda")
+def dense_tensorcore(cfg, data, weight, bias=None, out_dtype=None):
+    """Dense tensorcore operator on CUDA"""
+    matmul = dense_tensorcore_cuda(data, weight, bias, out_dtype)
+    return matmul
+
+
+@autotvm.register_topi_schedule("dense_tensorcore.cuda")
+def schedule_dense_tensorcore(cfg, outs):
+    """Schedule dense operator using Tensorcore"""
+    outs = [outs] if isinstance(outs, te.tensor.Tensor) else outs
+    s = te.create_schedule([x.op for x in outs])
+
+    def _callback(op):
+        if op.tag == 'dense_tensorcore':
+            _schedule_dense_tensorcore(cfg, s, op.output(0))
+    traverse_inline(s, outs[0].op, _callback)
+    return s
+
+
+def dense_tensorcore_cuda(data, weight, bias=None, out_dtype=None):
+    """Dense tensorcore operator on CUDA"""
+    assert len(data.shape) == 2 and len(weight.shape) == 2, \
+        "only support 2-dim dense"
+    if bias is not None:
+        assert len(bias.shape) == 1
+    if out_dtype is None:
+        out_dtype = data.dtype
+    batch, in_dim = data.shape
+    out_dim, _ = weight.shape
+    k = te.reduce_axis((0, in_dim), name='k')
+    data_16 = te.compute((batch, in_dim), lambda b, i: data[b, 
i].astype('float16'))
+    weight_16 = te.compute((out_dim, in_dim), lambda o, i: weight[o, 
i].astype('float16'))
+    matmul = te.compute((batch, out_dim), \
+                         lambda i, j: te.sum(data_16[i, k].astype(out_dtype) * 
\
+                                              weight_16[j, 
k].astype(out_dtype), axis=k), \
+                         name='T_dense', tag='dense_tensorcore')
+    if bias is not None:
+        matmul = te.compute((batch, out_dim), \
+                             lambda i, j: matmul[i, j] + 
bias[j].astype(out_dtype), \
+                             tag=tag.BROADCAST)
+    return matmul
+
+
+def _schedule_dense_tensorcore(cfg, s, C):
+    """Schedule dense operator using Tensorcore"""
+    A, B = s[C].op.input_tensors
+    batch, out_dim = get_const_tuple(C.shape)
+    out_dtype = C.dtype
+    s[A].compute_inline()
+    s[B].compute_inline()
+
+    # Explicit memory access
+    AS = s.cache_read(A, 'shared', [C])
+    BS = s.cache_read(B, 'shared', [C])
+    AF = s.cache_read(AS, 'wmma.matrix_a', [C])
+    BF = s.cache_read(BS, 'wmma.matrix_b', [C])
+    CF = s.cache_write(C, 'wmma.accumulator')
+    CS = s.cache_read(CF, 'shared', [C])
+
+    # fallback support
+    target = tvm.target.Target.current()
+    if cfg.is_fallback:
+        ref_log = autotvm.tophub.load_reference_log(
+            target.target_name, target.model, 'dense_tensorcore.cuda')
+        cfg.fallback_with_reference_log(ref_log)
+
+    # Deal with op fusion, such as bias and relu
+    if C.op not in s.outputs:
+        s[C].compute_inline()
+        C = s.outputs[0].output(0)
+
+    # create tuning space
+    cfg.define_knob("block_row_warps", [1, 2, 4])
+    cfg.define_knob("block_col_warps", [1, 2, 4])
+    cfg.define_knob("warp_row_tiles", [1, 2, 4])
+    cfg.define_knob("warp_col_tiles", [1, 2, 4])
+    cfg.define_knob("chunk", [1, 2, 4, 8])
+    cfg.define_knob("offset", [0, 8])
+    cfg.define_knob("offsetCS", [0, 8])
+    cfg.define_knob("vec", [1, 2, 4, 8])
+
+    #Make it available by default
 
 Review comment:
   Unclear what this comment means.

----------------------------------------------------------------
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

Reply via email to