masahi commented on a change in pull request #9261:
URL: https://github.com/apache/tvm/pull/9261#discussion_r737951454



##########
File path: python/tvm/contrib/cutlass/gen_gemm.py
##########
@@ -0,0 +1,335 @@
+# 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
+"""Kernel generator and profiler for CUTLASS."""
+import os
+import re
+import tempfile
+import subprocess
+from .gemm_operation import GemmOperation, EmitGemmInstance
+from .gemm_profiler import GemmProfilerEmitter
+from .library import (
+    EpilogueFunctor,
+    SwizzlingFunctor,
+    TensorDescription,
+    DataTypeTag,
+    LayoutType,
+    MathInstruction,
+    DataType,
+    OpcodeClass,
+    MathOperation,
+    TileDescription,
+)
+
+
+def create_gemm_operator(
+    layouts,
+    tile_descriptions,
+    data_type,
+    alignment_constraints,
+    epilogue_functor=EpilogueFunctor.LinearCombination,
+    swizzling_functor=SwizzlingFunctor.Identity8,
+):
+    """Exhaustively instantiate all kernels from a given configuration."""
+    ret = []
+    kernel_emitter = EmitGemmInstance()
+    profiler_emitter = GemmProfilerEmitter()
+
+    element_a, element_b, element_c, element_epilogue = data_type
+
+    for layout in layouts:
+        for tile_description in tile_descriptions:
+            for alignment in alignment_constraints:
+                alignment_c = min(8, alignment)
+
+                A = TensorDescription(element_a, layout[0], alignment)
+                B = TensorDescription(element_b, layout[1], alignment)
+                C = TensorDescription(element_c, layout[2], alignment_c)
+
+                op_entry = {}
+                op = GemmOperation(
+                    tile_description.minimum_compute_capability,
+                    tile_description,
+                    A,
+                    B,
+                    C,
+                    element_epilogue,
+                    epilogue_functor,
+                    swizzling_functor,
+                )
+                op_bias = GemmOperation(
+                    tile_description.minimum_compute_capability,
+                    tile_description,
+                    A,
+                    B,
+                    C,
+                    element_epilogue,
+                    EpilogueFunctor.LinearCombinationBias,
+                    swizzling_functor,
+                )
+                op_bias_relu = GemmOperation(
+                    tile_description.minimum_compute_capability,
+                    tile_description,
+                    A,
+                    B,
+                    C,
+                    element_epilogue,
+                    EpilogueFunctor.LinearCombinationRelu,
+                    swizzling_functor,
+                )
+                op_bias_gelu = GemmOperation(
+                    tile_description.minimum_compute_capability,
+                    tile_description,
+                    A,
+                    B,
+                    C,
+                    element_epilogue,
+                    EpilogueFunctor.LinearCombinationGelu,
+                    swizzling_functor,
+                )
+
+                kernel_emitter = EmitGemmInstance()
+                op_entry["op"] = op
+                op_entry["name"] = op.procedural_name()
+                op_entry["opdef"] = kernel_emitter.emit(op)
+                op_entry["opdef_bias"] = kernel_emitter.emit(op_bias, 
no_beta_scaling=True)
+                op_entry["opdef_bias_relu"] = kernel_emitter.emit(
+                    op_bias_relu, no_beta_scaling=True
+                )
+                op_entry["opdef_bias_gelu"] = kernel_emitter.emit(op_bias_gelu)
+                op_entry["src"] = profiler_emitter.emit(
+                    op.procedural_name(),
+                    op_entry["opdef"],
+                    DataTypeTag[element_a],
+                    DataTypeTag[element_b],
+                    DataTypeTag[element_c],
+                    op.leading_dim(),
+                )
+                op_entry["runtime"] = 9999999
+                ret.append(op_entry)
+    return ret
+
+
+def generate_tensor_op_common(math_instructions, alignment_constraints, 
get_tile_descriptions):
+    """Common kernel generator to be used by archtecture specific 
generators."""
+    ops = []
+    layouts = [
+        (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor),
+    ]
+    for math_inst in math_instructions:
+        tile_descriptions = get_tile_descriptions(math_inst)
+        data_type = [
+            math_inst.element_a,
+            math_inst.element_b,
+            math_inst.element_accumulator,
+            math_inst.element_accumulator,
+        ]
+
+        out = create_gemm_operator(layouts, tile_descriptions, data_type, 
alignment_constraints)
+
+        ops.extend(out)
+
+    return ops
+
+
+def generate_sm75_tensor_op_1688(out_dtype):
+    """Generate GEMM kernels for Turing."""
+    assert out_dtype in ["float32", "float16"]
+    math_instructions = {
+        "float32": [
+            MathInstruction(
+                [16, 8, 8],
+                DataType.f16,
+                DataType.f16,
+                DataType.f32,
+                OpcodeClass.TensorOp,
+                MathOperation.multiply_add,
+            )
+        ],
+        "float16": [
+            MathInstruction(
+                [16, 8, 8],
+                DataType.f16,
+                DataType.f16,
+                DataType.f16,
+                OpcodeClass.TensorOp,
+                MathOperation.multiply_add,
+            )
+        ],
+    }[out_dtype]

Review comment:
       A bit confused about your comment. Here, I'm selecting either fp32 or 
fp16 accum / output kernels based on `out_dtype` of Relay `dense` op, assuming 
that `accum_dtype == out_dtype`. If the user requests fp32 output dtype, we 
must select fp32 accumulation, don't we? 




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

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to