[llvm-branch-commits] [mlir] a1e3fec - Generalized op transformation logic for output tensor.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: a1e3fec79420164b7cd398872d525f03c4436e96 URL: https://github.com/llvm/llvm-project/commit/a1e3fec79420164b7cd398872d525f03c4436e96 DIFF: https://github.com/llvm/llvm-project/commit/a1e3fec79420164b7cd398872d525f03c4436e96.diff LOG: Generalized op transformation logic for output tensor. Add more op lowering test cases. Added: mlir/test/Dialect/MIOpen/lowering_ckyx_cnhw_knhw.mlir mlir/test/Dialect/MIOpen/lowering_cyxk_chwn_khwn.mlir mlir/test/Dialect/MIOpen/lowering_cyxk_cnhw_knhw.mlir Modified: mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp index 46083be58a35..f1d8e914c3ec 100644 --- a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp @@ -66,7 +66,7 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { llvm::SmallVector transformedFilterAttrs; -// TBD: set layout attribute. +// set layout attribute. // Weight tensor transformation: // - Part 1: Merge non-K dimensions to dimension 0, name it as gemmK. // - Part 2: PassThrough K dimension to dimension 1, name it as gemmM. @@ -414,7 +414,7 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { StringAttr::get("wo", op.getContext()) }, op.getContext())); transformedInputAttrs.push_back(transformedInputImmLayoutAttr); -// TBD: set output_layout attribute. +// set output_layout attribute. auto transformedInputOutputLayoutAttr = rewriter.getNamedAttr("output_layout", ArrayAttr::get({ StringAttr::get("gemmK", op.getContext()), @@ -442,49 +442,59 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { llvm::SmallVector transformedOutputAttrs; -// TBD: set layout attribute. -// TBD: Part 1: Passthrough. -llvm::SmallVector transformedOutputLayoutPart1Specs; - transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("dimensions", ArrayAttr::get({IntegerAttr::get(IntegerType::get(32, op.getContext()), 0)}, op.getContext(; -transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("names", ArrayAttr::get({StringAttr::get("gemmM", op.getContext())}, op.getContext(; - transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("transformation", StringAttr::get("PassThrough", op.getContext(; - transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("source_dimensions", -ArrayAttr::get({ - IntegerAttr::get(IntegerType::get(32, op.getContext()), 1), -}, op.getContext(; - transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("source_names", -ArrayAttr::get({ -StringAttr::get("ko", op.getContext()) -}, op.getContext(; +// set layout attribute. +// Weight tensor transformation: +// - Part 1: PassThrough K dimension to dimension 0, name it as gemmM. +// - Part 2: Merge non-K dimensions to dimension 1, name it as gemmN. +{ + llvm::SmallVector nonKDims; + IntegerAttr kDim; + llvm::SmallVector nonKDimNames; + StringAttr kDimName; + for (unsigned i = 0; i < outputLayoutAttr.size(); ++i) { +if (auto strAttr = outputLayoutAttr.getValue()[i].dyn_cast()) { + if (strAttr.getValue() == "ko") { +kDim = IntegerAttr::get(IntegerType::get(32, op.getContext()), i); +kDimName = StringAttr::get(strAttr.getValue(), op.getContext()); + } else { +nonKDims.push_back(IntegerAttr::get(IntegerType::get(32, op.getContext()), i)); +nonKDimNames.push_back(StringAttr::get(strAttr.getValue(), op.getContext())); + } +} + } + + // Part 1: Passthrough. + llvm::SmallVector transformedOutputLayoutPart1Specs; + transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("dimensions", ArrayAttr::get({IntegerAttr::get(IntegerType::get(32, op.getContext()), 0)}, op.getContext(; + transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("names", ArrayAttr::get({StringAttr::get("gemmM", op.getContext())}, op.getContext(; + transformedOutputLayoutPart1Specs.push_back(rewriter.getNamedAttr("transformation", StringAttr::get("PassThrough", op.getContext(; + transf
[llvm-branch-commits] [mlir] 40ca3b5 - Amend op parsing test case.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: 40ca3b593063c2628082dbdff6b31b8e728cb926 URL: https://github.com/llvm/llvm-project/commit/40ca3b593063c2628082dbdff6b31b8e728cb926 DIFF: https://github.com/llvm/llvm-project/commit/40ca3b593063c2628082dbdff6b31b8e728cb926.diff LOG: Amend op parsing test case. Added the following attributes: - source_layout - output_layout - gridwise_gemm_argument_position Added: Modified: mlir/test/Dialect/MIOpen/ops.mlir Removed: diff --git a/mlir/test/Dialect/MIOpen/ops.mlir b/mlir/test/Dialect/MIOpen/ops.mlir index 9b3b2e3db27e..1fc8bca38f6b 100644 --- a/mlir/test/Dialect/MIOpen/ops.mlir +++ b/mlir/test/Dialect/MIOpen/ops.mlir @@ -50,7 +50,9 @@ func @miopen_transform_1_to_1(%memref: memref) { source_dimensions = [3], source_names = ["wi"] } -] +], +source_layout = ["n", "c", "hi", "wi"], +output_layout = ["n", "c", "hipad", "wipad"] } : memref to memref return } @@ -73,9 +75,12 @@ func @miopen_transform_n_to_1(%memref : memref) { names = ["gemmM"], transformation = "passthrough", source_dimensions = [0], -source_names = ["n"] +source_names = ["k"] } -] +], +source_layout = ["k", "c", "y", "x"], +output_layout = ["gemmK", "gemmM"], +gridwise_gemm_argument_pos = 0 } : memref to memref return } @@ -116,7 +121,9 @@ func @miopen_transform_1_to_n(%memref : memref) { source_dimensions = [3], source_names = ["wipad"] } -] +], +intermediate_layout = ["n", "c", "hipad", "wipad"], +output_layout = ["n", "c", "y", "ho", "x", "wo"] } : memref to memref return } ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [mlir] 125143f - Initial commit of MIOpen dialect.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: 125143fddbefacd6f10e70fcd2f1db52ddaaeb16 URL: https://github.com/llvm/llvm-project/commit/125143fddbefacd6f10e70fcd2f1db52ddaaeb16 DIFF: https://github.com/llvm/llvm-project/commit/125143fddbefacd6f10e70fcd2f1db52ddaaeb16.diff LOG: Initial commit of MIOpen dialect. Added: mlir/include/mlir/Dialect/MIOpenOps/CMakeLists.txt mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h mlir/lib/Dialect/MIOpenOps/CMakeLists.txt mlir/lib/Dialect/MIOpenOps/DialectRegistration.cpp mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp Modified: mlir/include/mlir/Dialect/CMakeLists.txt mlir/lib/Dialect/CMakeLists.txt Removed: diff --git a/mlir/include/mlir/Dialect/CMakeLists.txt b/mlir/include/mlir/Dialect/CMakeLists.txt index f0a24de73b27..d7b725168ecc 100644 --- a/mlir/include/mlir/Dialect/CMakeLists.txt +++ b/mlir/include/mlir/Dialect/CMakeLists.txt @@ -3,6 +3,8 @@ add_subdirectory(AVX512) add_subdirectory(GPU) add_subdirectory(Linalg) add_subdirectory(LLVMIR) +add_subdirectory(LoopOps) +add_subdirectory(MIOpenOps) add_subdirectory(OpenMP) add_subdirectory(Quant) add_subdirectory(SCF) diff --git a/mlir/include/mlir/Dialect/MIOpenOps/CMakeLists.txt b/mlir/include/mlir/Dialect/MIOpenOps/CMakeLists.txt new file mode 100644 index ..53d8018c9cff --- /dev/null +++ b/mlir/include/mlir/Dialect/MIOpenOps/CMakeLists.txt @@ -0,0 +1 @@ +add_mlir_dialect(MIOpenOps MIOpenOps) diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h new file mode 100644 index ..e0c36c23a854 --- /dev/null +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h @@ -0,0 +1,36 @@ +//===- MIOpenOps.h - MIOpen MLIR Operations -*- C++ -*-===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// This file defines MIOpen memref operations. +// +//===--===// + +#ifndef MLIR_MIOPENOPS_OPS_H_ +#define MLIR_MIOPENOPS_OPS_H_ + +#include "mlir/IR/Attributes.h" +#include "mlir/IR/Builders.h" +#include "mlir/IR/Dialect.h" +#include "mlir/IR/OpDefinition.h" +#include "mlir/Transforms/LoopLikeInterface.h" + +namespace mlir { +namespace miopen { + +class MIOpenOpsDialect : public Dialect { +public: + MIOpenOpsDialect(MLIRContext *context); + static StringRef getDialectNamespace() { return "miopen"; } +}; + +//#define GET_OP_CLASSES +//#include "mlir/Dialect/MIOpenOps/MIOpenOps.h.inc" + +} // end namespace miopen +} // end namespace mlir +#endif // MLIR_MIOPENOPS_OPS_H_ diff --git a/mlir/lib/Dialect/CMakeLists.txt b/mlir/lib/Dialect/CMakeLists.txt index b309454c504e..f4141bc534c9 100644 --- a/mlir/lib/Dialect/CMakeLists.txt +++ b/mlir/lib/Dialect/CMakeLists.txt @@ -3,6 +3,8 @@ add_subdirectory(AVX512) add_subdirectory(GPU) add_subdirectory(Linalg) add_subdirectory(LLVMIR) +add_subdirectory(LoopOps) +add_subdirectory(MIOpenOps) add_subdirectory(OpenMP) add_subdirectory(Quant) add_subdirectory(SCF) diff --git a/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt new file mode 100644 index ..474196162792 --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt @@ -0,0 +1,9 @@ +file(GLOB globbed *.c *.cpp) +add_llvm_library(MLIRMIOpenOps + ${globbed} + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/MIOpenOps + ) +add_dependencies(MLIRMIOpenOps MLIRStandardOps LLVMSupport) +target_link_libraries(MLIRMIOpenOps LLVMSupport) diff --git a/mlir/lib/Dialect/MIOpenOps/DialectRegistration.cpp b/mlir/lib/Dialect/MIOpenOps/DialectRegistration.cpp new file mode 100644 index ..4e00e6902383 --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/DialectRegistration.cpp @@ -0,0 +1,13 @@ +//===- DialectRegistration.cpp - Register MIOpen dialect --===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "mlir/Dialect/MIOpenOps/MIOpenOps.h" +using namespace mlir; + +// Static initialization for MIOpen dialect registration. +static DialectRegistration MIOpenOps; diff --git a/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp new file mode 100644 index ..ced4d25e866f --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp @@ -0,0 +1,52 @@ +//===- MIOpenOps.cpp - MIOpen MLIR Operations -
[llvm-branch-commits] [mlir] f0500d1 - Add Op traversing logic into MIOpen dialect -> C++ header translator.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: f0500d18ee6f0072487526e53fc9a7bd1c1d7235 URL: https://github.com/llvm/llvm-project/commit/f0500d18ee6f0072487526e53fc9a7bd1c1d7235 DIFF: https://github.com/llvm/llvm-project/commit/f0500d18ee6f0072487526e53fc9a7bd1c1d7235.diff LOG: Add Op traversing logic into MIOpen dialect -> C++ header translator. Added: mlir/test/Dialect/MIOpen/translate.mlir Modified: mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp Removed: mlir/test/Dialect/MIOpen/CppOutput/transformed.mlir diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index b071565c2b51..4eb8d7de7181 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -15,8 +15,10 @@ #include "mlir/Dialect/StandardOps/Ops.h" #include "mlir/IR/Function.h" #include "mlir/IR/Module.h" +#include "mlir/Support/STLExtras.h" #include "mlir/Translation.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -222,6 +224,160 @@ void EmitCppEpilogue(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm: output << kCppEpiloguePart2; } +static constexpr StringLiteral kHeaderPreamblePart1 = R"( +#ifndef CK_GRIDWISE_CONVOLUTION_IMPLICIT_GEMM_V4R4_HPP +#define CK_GRIDWISE_CONVOLUTION_IMPLICIT_GEMM_V4R4_HPP + +#include "common_header.hpp" +#include "tensor_descriptor.hpp" +#include "tensor_descriptor_helper.hpp" +#include "gridwise_gemm.hpp" + +namespace ck { + +// GemmM = K +// GemmN = N * Ho * Wo +// GemmK = C * Y * X +template +)"; + +static constexpr StringLiteral kHeaderPreamblePart2 = R"( +{ +__device__ void Run(const Float* const __restrict__ p_in_global, +const Float* const __restrict__ p_wei_global, +Float* const __restrict__ p_out_global) const +{ +)"; + +static constexpr StringLiteral kHeaderPreamblePart3 = R"( +constexpr auto I0 = Number<0>{}; +constexpr auto I1 = Number<1>{}; +constexpr auto I2 = Number<2>{}; +constexpr auto I3 = Number<3>{}; + +constexpr index_t ConvStrideH = ConvStrides{}[0]; +constexpr index_t ConvStrideW = ConvStrides{}[1]; + +constexpr index_t ConvDilationH = ConvDilations{}[0]; +constexpr index_t ConvDilationW = ConvDilations{}[1]; +)"; + +static constexpr StringLiteral kHeaderEpiloguePart1 = R"( +// GEMM +constexpr auto gridwise_gemm = +GridwiseGemmTransposedANormalBNormalC_v1, + Sequence<1, 0>, + 0, + GemmABlockCopySrcDataPerRead_GemmK, + GemmABlockCopyDstDataPerWrite_GemmM, + GemmBBlockCopyThreadSliceLengths_GemmK_GemmN, + GemmBBlockCopyThreadClusterLengths_GemmK_GemmN, + Sequence<0, 1>, + Sequence<0, 1>, + 1, + GemmBBlockCopySrcDataPerRead_GemmN, + GemmBBlockCopyDstDataPerWrite_GemmN, + Sequence<0, 1, 2, 3>, + 3, + GemmCThreadCopyDstDataPerWrite_GemmN1>{}; + +gridwise_gemm.Run(p_wei_global, p_in_global, p_out_global); +} +}; + +} // namespace ck +#endif +)"; + +void EmitHeaderPreamble(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm::SmallVector &tensorDescs) { + output << kHeaderPreamblePart1; + + output << R"( +struct GridwiseConvolutionImplicitGemm_v4r4_)"; + output << layoutStr; + + output << kHeaderPreamblePart2; + + output << kHeaderPreamblePart3; + + output << '\n'; + + output << R"( +constexpr auto )" << tensorDescs[0] << " = InGlobalDesc{};"; + output << R"( +constexpr auto )" << tensorDescs[1] << " = WeiGlobalDesc{};"; + output << R"( +constexpr auto )" << tensorDescs[2] << " = OutGlobalDesc{};"; +} + +void EmitHeaderEpilogue(llvm::raw_ostream &output, llvm::SmallDenseMap &args) { + output << kHeaderEpiloguePart1; + +// Between Part1 and Part2 emit: +// decltype(wei_e_k_global_desc), +// decltype(in_e_b_global_desc), +//
[llvm-branch-commits] [mlir] ff39c4c - Add parse / print logic to MIOpen ops.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: ff39c4c709ac1603d21f7baab75dbfbb13ae6fbc URL: https://github.com/llvm/llvm-project/commit/ff39c4c709ac1603d21f7baab75dbfbb13ae6fbc DIFF: https://github.com/llvm/llvm-project/commit/ff39c4c709ac1603d21f7baab75dbfbb13ae6fbc.diff LOG: Add parse / print logic to MIOpen ops. Revise test cases along the way. Added: Modified: mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp mlir/test/Dialect/MIOpen/ops.mlir Removed: diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td index 1304f16f3b30..8ffd66647f3f 100644 --- a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td @@ -35,10 +35,36 @@ class MIOpen_Op traits = []> : let parser = [{ return ::parse$cppClass(parser, result); }]; } -def MIOpen_Conv2DOp : MIOpen_Op<"conv2d">; +def MIOpen_Conv2DOp : +MIOpen_Op<"conv2d">, +Arguments<(ins MemRefRankOf<[F32], [4]>, + MemRefRankOf<[F32], [4]>, + MemRefRankOf<[F32], [4]>)> { + let summary = "2D convolution"; + let description = [{ +The `miopen.conv2d` op computes 2D convolution. + }]; +} -def MIOpen_TransformOp : MIOpen_Op<"transform">; +def MIOpen_TransformOp : +MIOpen_Op<"transform">, +Arguments<(ins AnyMemRef)>, +Results<(outs AnyMemRef)> { + let summary = "Tensor transformation"; + let description = [{ +The `miopen.transform` op transforms tensor coordinates. + }]; +} -def MIOpen_GridwiseGemmOp : MIOpen_Op<"gridwise_gemm">; +def MIOpen_GridwiseGemmOp : +MIOpen_Op<"gridwise_gemm">, +Arguments<(ins MemRefRankOf<[F32], [2]>, + MemRefRankOf<[F32], [2]>, + MemRefRankOf<[F32], [2]>)> { + let summary = "Gridwise GEMM"; + let description = [{ +The `miopen.gridwise_gemm` op computes gridwise GEMM. + }]; +} #endif // MIOPEN_OPS diff --git a/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp index 9408e17b2831..b41423435e33 100644 --- a/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp @@ -40,8 +40,6 @@ MIOpenOpsDialect::MIOpenOpsDialect(MLIRContext *context) #define GET_OP_LIST #include "mlir/Dialect/MIOpenOps/MIOpenOps.cpp.inc" >(); - - //addInterfaces(); } //===--===// @@ -49,11 +47,19 @@ MIOpenOpsDialect::MIOpenOpsDialect(MLIRContext *context) //===--===// static ParseResult parseConv2DOp(OpAsmParser &parser, OperationState &result) { - return success(); + SmallVector ops; + SmallVector types; + return failure( + parser.parseOperandList(ops, OpAsmParser::Delimiter::Paren) || + parser.parseOptionalAttrDict(result.attributes) || + parser.parseColonTypeList(types) || + parser.resolveOperands(ops, types, parser.getNameLoc(), result.operands)); } static void print(OpAsmPrinter &p, Conv2DOp op) { - p << Conv2DOp::getOperationName(); + p << op.getOperationName() << "(" << op.getOperands() << ")"; + p.printOptionalAttrDict(op.getAttrs()); + p << " : " << op.getOperandTypes(); } static LogicalResult verify(Conv2DOp op) { @@ -65,11 +71,24 @@ static LogicalResult verify(Conv2DOp op) { //===--===// static ParseResult parseTransformOp(OpAsmParser &parser, OperationState &result) { + OpAsmParser::OperandType src; + Type srcType, dstType; + return failure( + parser.parseLParen() || + parser.parseOperand(src) || + parser.parseRParen() || + parser.parseOptionalAttrDict(result.attributes) || + parser.parseColonType(srcType) || + parser.resolveOperand(src, srcType, result.operands) || + parser.parseKeywordType("to", dstType) || + parser.addTypeToList(dstType, result.types)); return success(); } static void print(OpAsmPrinter &p, TransformOp op) { - p << TransformOp::getOperationName(); + p << op.getOperationName() << "(" << op.getOperand() << ")"; + p.printOptionalAttrDict(op.getAttrs()); + p << " : " << op.getOperand()->getType() << " to " << op.getType(); } static LogicalResult verify(TransformOp op) { @@ -81,11 +100,19 @@ static LogicalResult verify(TransformOp op) { //===--===// static ParseResult parseGridwiseGemmOp(OpAsmParser &parser, OperationState &result) { - return success(); + SmallVector ops; + SmallVector types; + return failure( + parser.parseOperandList(ops, OpAsmParser::Delimiter::Paren) || + parser.parseOptionalAttrDict(result.attributes) || + parser.parseColonTypeList(type
[llvm-branch-commits] [mlir] 1c3be7e - Add Op transform logic. Improve Op translate logic.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: 1c3be7ec0838048d0a5a8f2ebf3dfa5e831370cd URL: https://github.com/llvm/llvm-project/commit/1c3be7ec0838048d0a5a8f2ebf3dfa5e831370cd DIFF: https://github.com/llvm/llvm-project/commit/1c3be7ec0838048d0a5a8f2ebf3dfa5e831370cd.diff LOG: Add Op transform logic. Improve Op translate logic. Revise tests. Added: Modified: mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp mlir/test/Dialect/MIOpen/lowering.mlir Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index 2bd64efa77b6..cda706c4112c 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -343,12 +343,17 @@ struct GridwiseConvolutionImplicitGemm_v4r4_)"; output << kHeaderPreamblePart2; output << kHeaderPreamblePart3; output << '\n'; - output << R"( -constexpr auto )" << tensorDescs[0] << " = InGlobalDesc{};"; - output << R"( -constexpr auto )" << tensorDescs[1] << " = WeiGlobalDesc{};"; - output << R"( -constexpr auto )" << tensorDescs[2] << " = OutGlobalDesc{};"; + + // TBD: remove these interim checks. + if (tensorDescs.size() > 0) +output << R"( + constexpr auto )" << tensorDescs[0] << " = InGlobalDesc{};"; + if (tensorDescs.size() > 1) +output << R"( + constexpr auto )" << tensorDescs[1] << " = WeiGlobalDesc{};"; + if (tensorDescs.size() > 2) +output << R"( + constexpr auto )" << tensorDescs[2] << " = OutGlobalDesc{};"; output << '\n'; } @@ -358,7 +363,7 @@ void EmitHeaderEpilogue(llvm::raw_ostream &output, llvm::SmallDenseMap 1) + output << llvm::toUpper(strAttr.getValue()[1]); break; default: output << llvm::toUpper(strAttr.getValue()[0]); diff --git a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp index 2a00ed675122..27311cb8cfb9 100644 --- a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp @@ -24,6 +24,7 @@ #include "mlir/Dialect/MIOpenOps/Passes.h" #include "mlir/Dialect/StandardOps/Ops.h" #include "mlir/IR/Attributes.h" +#include "mlir/IR/Builders.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Module.h" #include "mlir/IR/Operation.h" @@ -37,6 +38,8 @@ #include "mlir/Transforms/Passes.h" #include "mlir/Support/LogicalResult.h" +#include "llvm/ADT/SmallVector.h" + using namespace mlir; struct Conv2DOpRewritePattern : public OpRewritePattern { @@ -44,15 +47,450 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { PatternMatchResult matchAndRewrite(miopen::Conv2DOp op, PatternRewriter &rewriter) const override { -rewriter.create(op.getLoc(), op.filter().getType(), op.filter()); +auto filterLayoutAttr = op.getAttrOfType("filter_layout"); +auto inputLayoutAttr = op.getAttrOfType("input_layout"); +auto outputLayoutAttr = op.getAttrOfType("output_layout"); + +// TBD: handle dilations, strides, padding. + +// Transform filter tensor. +auto filterType = op.filter().getType().dyn_cast(); +auto filterShape = filterType.getShape(); +auto filterElementType = filterType.getElementType(); + +llvm::SmallVector transformedFilterShape; +transformedFilterShape.set_size(filterShape.size() - 2); +// TBD: compute transformed filter shape dimensions. +std::fill(transformedFilterShape.begin(), transformedFilterShape.end(), -1); +auto transformedFilterMemRefType = MemRefType::get(transformedFilterShape, filterElementType); + +llvm::SmallVector transformedFilterAttrs; + +// TBD: set layout attribute. +// TBD: Merge part. +llvm::SmallVector transformedFilterLayoutPart1Specs; + transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("dimensions", ArrayAttr::get({IntegerAttr::get(IntegerType::get(32, op.getContext()), 0)}, op.getContext(; +transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("names", ArrayAttr::get({StringAttr::get("gemmK", op.getContext())}, op.getContext(; + transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("transformation", StringAttr::get("Merge", op.getContext(; + transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("source_dimensions", +ArrayAttr::get({ + IntegerAttr::get(IntegerType::get(32, op.getContext()), 1), + IntegerAttr::get(IntegerType::get(32, op.getContext()), 2), + IntegerAttr::get(IntegerTyp
[llvm-branch-commits] [mlir] 05d19a7 - Add Op traversing logic into MIOpen dialect -> C++ translator.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: 05d19a7eb4ea27d4d0e7989b145c2f7458fff54a URL: https://github.com/llvm/llvm-project/commit/05d19a7eb4ea27d4d0e7989b145c2f7458fff54a DIFF: https://github.com/llvm/llvm-project/commit/05d19a7eb4ea27d4d0e7989b145c2f7458fff54a.diff LOG: Add Op traversing logic into MIOpen dialect -> C++ translator. Added: mlir/test/Dialect/MIOpen/CppOutput/transformed.mlir Modified: mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp Removed: mlir/test/Dialect/MIOpen/CppOutput/miopencpp.mlir diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h index d3e9b8ee09a2..09d2d1166caf 100644 --- a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h @@ -33,7 +33,17 @@ class ModuleOp; /// Convert the given MLIR module into MIOpen C++ . In case of error, report it /// to the error handler registered with the MLIR context, if any (obtained from /// the MLIR module), and return `nullptr`. -std::unique_ptr translateModuleToMIOpenCPP(ModuleOp m); +std::unique_ptr translateModuleToMIOpenCpp(ModuleOp m); + +/// Convert the given MLIR module into MIOpen C++ Header. In case of error, report it +/// to the error handler registered with the MLIR context, if any (obtained from +/// the MLIR module), and return `nullptr`. +std::unique_ptr translateModuleToMIOpenHeader(ModuleOp m); + +/// Convert the given MLIR module into MIOpen C++ Solver. In case of error, report it +/// to the error handler registered with the MLIR context, if any (obtained from +/// the MLIR module), and return `nullptr`. +std::unique_ptr translateModuleToMIOpenSolver(ModuleOp m); } // namespace mlir diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt b/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt index 855985b4b945..3d37305c60e7 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt @@ -5,6 +5,7 @@ add_llvm_library(MLIRMIOpenCpp ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/MIOpenOps ) target_link_libraries(MLIRMIOpenCpp + LLVMSupport MLIRIR MLIRMIOpenOps MLIRStandardOps diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index 5fe33d695cb3..b071565c2b51 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -13,34 +13,396 @@ #include "mlir/Dialect/MIOpenOps/MIOpenCPP.h" #include "mlir/Dialect/MIOpenOps/MIOpenOps.h" #include "mlir/Dialect/StandardOps/Ops.h" - +#include "mlir/IR/Function.h" +#include "mlir/IR/Module.h" #include "mlir/Translation.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/ToolOutputFile.h" using namespace mlir; -std::unique_ptr mlir::translateModuleToMIOpenCPP(ModuleOp m) { - // Check constraints: +namespace { + +static constexpr StringLiteral kVarName[3] = {"weight", "input", "output"}; + +static constexpr int kConv2DTensorDimension = 4; + +static constexpr StringLiteral kCppPreamblePart1 = R"( +#include "common_header.hpp" +)"; + +static constexpr StringLiteral kCppPreamblePart2 = R"( +#include "float_types.h" + +extern "C" __global__ +)"; + +static constexpr StringLiteral kCppPreamblePart3 = R"( +(const FLOAT* const __restrict__ p_in_global, +const FLOAT* const __restrict__ p_wei_global, +FLOAT* const __restrict__ p_out_global) +{ +using namespace ck; + +constexpr index_t ConvStrideH = CK_PARAM_PROBLEM_CONV_STRIDE_H; +constexpr index_t ConvStrideW = CK_PARAM_PROBLEM_CONV_STRIDE_W; + +constexpr index_t ConvDilationH = CK_PARAM_PROBLEM_CONV_DILATION_H; +constexpr index_t ConvDilationW = CK_PARAM_PROBLEM_CONV_DILATION_W; + +constexpr index_t InLeftPadH = CK_PARAM_PROBLEM_IN_LEFT_PAD_H; +constexpr index_t InLeftPadW = CK_PARAM_PROBLEM_IN_LEFT_PAD_W; + +constexpr index_t InRightPadH = CK_PARAM_PROBLEM_IN_RIGHT_PAD_H; +constexpr index_t InRightPadW = CK_PARAM_PROBLEM_IN_RIGHT_PAD_W; + +constexpr index_t BlockSize = CK_PARAM_TUNABLE_BLOCK_SIZE; +constexpr index_t GridSize = CK_PARAM_DEPENDENT_GRID_SIZE; + +constexpr index_t GemmMPerBlock = CK_PARAM_TUNABLE_GEMM_M_PER_BLOCK; +constexpr index_t GemmNPerBlock = CK_PARAM_TUNABLE_GEMM_N_PER_BLOCK; +constexpr index_t GemmKPerBlock = CK_PARAM_TUNABLE_GEMM_K_PER_BLOCK; + +)"; + +static constexpr StringLiteral kCppInterlude = R"( +using ConvStrides = Sequence; +using ConvDilations = Sequence; + +using InLeftPads = Sequence; +using InRightPads = Se
[llvm-branch-commits] [mlir] 9bc39d9 - Initial commit to introduce MLIR MIOpen dialect -> MIOpen C++ translation.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: 9bc39d9e6f9e7046f99dc105c093c78fc8031011 URL: https://github.com/llvm/llvm-project/commit/9bc39d9e6f9e7046f99dc105c093c78fc8031011 DIFF: https://github.com/llvm/llvm-project/commit/9bc39d9e6f9e7046f99dc105c093c78fc8031011.diff LOG: Initial commit to introduce MLIR MIOpen dialect -> MIOpen C++ translation. Added: mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp mlir/test/Dialect/MIOpen/CppOutput/miopencpp.mlir Modified: mlir/lib/Dialect/MIOpenOps/CMakeLists.txt Removed: diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h new file mode 100644 index ..d3e9b8ee09a2 --- /dev/null +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenCPP.h @@ -0,0 +1,40 @@ +//===- MIOpenCPP.h - MLIR to C++ for MIOpen conversion --*- C++ -*-===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// This file declares the entry point for the MLIR to MIOpen C++ conversion. +// +//===--===// + +#ifndef MLIR_TARGET_MIOPEN_CPP_H +#define MLIR_TARGET_MIOPEN_CPP_H + +#include "mlir/Dialect/MIOpenOps/MIOpenOps.h" +#include "mlir/IR/Block.h" +#include "mlir/IR/Module.h" +#include "mlir/IR/Value.h" + +#include + +namespace llvm { +class StringRef; +} // namespace llvm + +namespace mlir { + +class OwningModuleRef; +class MLIRContext; +class ModuleOp; + +/// Convert the given MLIR module into MIOpen C++ . In case of error, report it +/// to the error handler registered with the MLIR context, if any (obtained from +/// the MLIR module), and return `nullptr`. +std::unique_ptr translateModuleToMIOpenCPP(ModuleOp m); + +} // namespace mlir + +#endif // MLIR_TARGET_MIOPEN_CPP_H diff --git a/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt index ba32051bbb39..b7b0c69febea 100644 --- a/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt +++ b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt @@ -7,3 +7,5 @@ add_llvm_library(MLIRMIOpenOps ) add_dependencies(MLIRMIOpenOps MLIRMIOpenOpsIncGen MLIRStandardOps LLVMSupport) target_link_libraries(MLIRMIOpenOps LLVMSupport) + +add_subdirectory(CppOutput) diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt b/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt new file mode 100644 index ..855985b4b945 --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/CMakeLists.txt @@ -0,0 +1,12 @@ +add_llvm_library(MLIRMIOpenCpp + ConvertToMIOpenCPP.cpp + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/MIOpenOps + ) +target_link_libraries(MLIRMIOpenCpp + MLIRIR + MLIRMIOpenOps + MLIRStandardOps + MLIRTranslation) + diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp new file mode 100644 index ..5fe33d695cb3 --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -0,0 +1,46 @@ +//===- ConvertToMIOpenCPP.cpp - MLIR to MIOpen C++ conversion -===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// This file implements a translation between the MLIR MIOpen dialect and C++. +// +//===--===// + +#include "mlir/Dialect/MIOpenOps/MIOpenCPP.h" +#include "mlir/Dialect/MIOpenOps/MIOpenOps.h" +#include "mlir/Dialect/StandardOps/Ops.h" + +#include "mlir/Translation.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/ToolOutputFile.h" + +using namespace mlir; + +std::unique_ptr mlir::translateModuleToMIOpenCPP(ModuleOp m) { + // Check constraints: + // + // The Module should only contain 1 function. + // The Function should only contain exactly: + // - 0 conv2d op. + // - 5 transform ops (1 for filter, 3 for input, 1 for output). + // - 1 gridwise gemm op. + m.dump(); + + return std::make_unique("Hello World"); +} + +static TranslateFromMLIRRegistration +toCPP("mlir-to-miopencpp", [](ModuleOp module, llvm::raw_ostream &output) { + auto sourceCode = mlir::translateModuleToMIOpenCPP(module); + if (!sourceCode) +return failure(); + +
[llvm-branch-commits] [mlir] 01659f3 - Generalized op transformation logic for weight tensor.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: 01659f382d0ab554d59bfd793a0f9153613c7e20 URL: https://github.com/llvm/llvm-project/commit/01659f382d0ab554d59bfd793a0f9153613c7e20 DIFF: https://github.com/llvm/llvm-project/commit/01659f382d0ab554d59bfd793a0f9153613c7e20.diff LOG: Generalized op transformation logic for weight tensor. Add test cases. Added: mlir/test/Dialect/MIOpen/lowering_kcyx_nchw_nkhw.mlir mlir/test/Dialect/MIOpen/lowering_kyxc_nhwc_nhwk.mlir Modified: mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp index 0378a4c113bf..46083be58a35 100644 --- a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp @@ -67,44 +67,53 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { llvm::SmallVector transformedFilterAttrs; // TBD: set layout attribute. -// TBD: Merge part. -llvm::SmallVector transformedFilterLayoutPart1Specs; - transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("dimensions", ArrayAttr::get({IntegerAttr::get(IntegerType::get(32, op.getContext()), 0)}, op.getContext(; -transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("names", ArrayAttr::get({StringAttr::get("gemmK", op.getContext())}, op.getContext(; - transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("transformation", StringAttr::get("Merge", op.getContext(; - transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("source_dimensions", -ArrayAttr::get({ - IntegerAttr::get(IntegerType::get(32, op.getContext()), 1), - IntegerAttr::get(IntegerType::get(32, op.getContext()), 2), - IntegerAttr::get(IntegerType::get(32, op.getContext()), 3), -}, op.getContext(; - transformedFilterLayoutPart1Specs.push_back(rewriter.getNamedAttr("source_names", -ArrayAttr::get({ -StringAttr::get("c", op.getContext()), -StringAttr::get("y", op.getContext()), -StringAttr::get("x", op.getContext()) -}, op.getContext(; - -// TBD: Passthrough part. -llvm::SmallVector transformedFilterLayoutPart2Specs; - transformedFilterLayoutPart2Specs.push_back(rewriter.getNamedAttr("dimensions", ArrayAttr::get({IntegerAttr::get(IntegerType::get(32, op.getContext()), 1)}, op.getContext(; -transformedFilterLayoutPart2Specs.push_back(rewriter.getNamedAttr("names", ArrayAttr::get({StringAttr::get("gemmM", op.getContext())}, op.getContext(; - transformedFilterLayoutPart2Specs.push_back(rewriter.getNamedAttr("transformation", StringAttr::get("PassThrough", op.getContext(; - transformedFilterLayoutPart2Specs.push_back(rewriter.getNamedAttr("source_dimensions", -ArrayAttr::get({ - IntegerAttr::get(IntegerType::get(32, op.getContext()), 0), -}, op.getContext(; - transformedFilterLayoutPart2Specs.push_back(rewriter.getNamedAttr("source_names", -ArrayAttr::get({ -StringAttr::get("k", op.getContext()) -}, op.getContext(; - -auto transformedFilterLayoutAttr = rewriter.getNamedAttr("layout", - ArrayAttr::get({ - DictionaryAttr::get(transformedFilterLayoutPart1Specs, op.getContext()), - DictionaryAttr::get(transformedFilterLayoutPart2Specs, op.getContext()) - }, op.getContext())); -transformedFilterAttrs.push_back(transformedFilterLayoutAttr); +// Weight tensor transformation: +// - Part 1: Merge non-K dimensions to dimension 0, name it as gemmK. +// - Part 2: PassThrough K dimension to dimension 1, name it as gemmM. +{ + llvm::SmallVector nonKDims; + IntegerAttr kDim; + llvm::SmallVector nonKDimNames; + StringAttr kDimName; + for (unsigned i = 0; i < filterLayoutAttr.size(); ++i) { +if (auto strAttr = filterLayoutAttr.getValue()
[llvm-branch-commits] [mlir] 0ac976c - Change emitted names to fixed "mlir".
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: 0ac976c8c13175d7f744156dba3302f7d1457073 URL: https://github.com/llvm/llvm-project/commit/0ac976c8c13175d7f744156dba3302f7d1457073 DIFF: https://github.com/llvm/llvm-project/commit/0ac976c8c13175d7f744156dba3302f7d1457073.diff LOG: Change emitted names to fixed "mlir". Added: Modified: mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp mlir/test/Dialect/MIOpen/translate.mlir Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index cda706c4112c..b0b104bd137d 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -186,13 +186,20 @@ void EmitCppPreamble(llvm::raw_ostream &output, llvm::StringRef layoutStr) { // Between Preamble Part 1 and Part 2: // #include "gridwise_convolution_implicit_gemm_v4r4_nchw_kcyx_nkhw.hpp" output << R"(#include "gridwise_convolution_implicit_gemm_v4r4_)"; - output << layoutStr << R"(.hpp")"; + + // Change to fixed "mlir". + //output << layoutStr << R"(.hpp")"; + output << "mlir" << R"(.hpp")"; + output << kCppPreamblePart2; // Between Preamble Part 2 and Par 3: //__launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_nchw_kcyx_nkhw( - output << R"( + output << R"( __launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_)"; - output << layoutStr; + // Change to fixed "mlir". + //output << layoutStr; + output << "mlir"; + output << kCppPreamblePart3; } @@ -205,7 +212,11 @@ void EmitCppEpilogue(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm: //constexpr auto gridwise_conv = GridwiseConvolutionImplicitGemm_v4r4_nchw_kcyx_nkhw output << R"( constexpr auto gridwise_conv = GridwiseConvolutionImplicitGemm_v4r4_)"; - output << layoutStr; + + // Change to fixed "mlir". + //output << layoutStr; + output << "mlir"; + output << kCppEpiloguePart1; // Between Part1 and Part2: //decltype(in_nchw_desc), @@ -339,7 +350,11 @@ void EmitHeaderPreamble(llvm::raw_ostream &output, llvm::StringRef layoutStr, ll output << kHeaderPreamblePart1; output << R"( struct GridwiseConvolutionImplicitGemm_v4r4_)"; - output << layoutStr; + + // Change to fixed "mlir". + //output << layoutStr; + output << "mlir"; + output << kHeaderPreamblePart2; output << kHeaderPreamblePart3; output << '\n'; diff --git a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp index 27311cb8cfb9..0378a4c113bf 100644 --- a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp @@ -208,8 +208,8 @@ struct Conv2DOpRewritePattern : public OpRewritePattern { ArrayAttr::get({ StringAttr::get("ni", op.getContext()), StringAttr::get("ci", op.getContext()), - StringAttr::get("hi", op.getContext()), - StringAttr::get("wi", op.getContext()) + StringAttr::get("hipad", op.getContext()), + StringAttr::get("wipad", op.getContext()) }, op.getContext())); paddedInputAttrs.push_back(paddedInputOutputLayoutAttr); auto paddedInput = rewriter.create(op.getLoc(), inputType, op.input(), paddedInputAttrs); diff --git a/mlir/test/Dialect/MIOpen/translate.mlir b/mlir/test/Dialect/MIOpen/translate.mlir index 1139100262e2..a3fedba84cda 100644 --- a/mlir/test/Dialect/MIOpen/translate.mlir +++ b/mlir/test/Dialect/MIOpen/translate.mlir @@ -1,8 +1,8 @@ // RUN: mlir-translate -mlir-to-miopen-cpp %s | FileCheck -check-prefix=MIOPEN-CPP %s // RUN: mlir-translate -mlir-to-miopen-hpp %s | FileCheck -check-prefix=MIOPEN-HPP %s -// MIOPEN-CPP: __launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_kcyx_nicihiwi_nokohowo -// MIOPEN-HPP: struct GridwiseConvolutionImplicitGemm_v4r4_kcyx_nicihiwi_nokohowo +// MIOPEN-CPP: __launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_mlir +// MIOPEN-HPP: struct GridwiseConvolutionImplicitGemm_v4r4_mlir func @miopen_transformed_conv2d(%filter : memref, %input : memref, %output : memref) { // filter tensor %filter_gemmK_gemmM = miopen.transform(%filter) { @@ -153,7 +153,7 @
[llvm-branch-commits] [mlir] fe71429 - Fix input / weight tensor translation logic in Cpp preamble.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: fe71429c14c28ee85ff94a528c59b6a1436b33ac URL: https://github.com/llvm/llvm-project/commit/fe71429c14c28ee85ff94a528c59b6a1436b33ac DIFF: https://github.com/llvm/llvm-project/commit/fe71429c14c28ee85ff94a528c59b6a1436b33ac.diff LOG: Fix input / weight tensor translation logic in Cpp preamble. Added: Modified: mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index b0b104bd137d..45bb8b3ea7ae 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -362,10 +362,10 @@ struct GridwiseConvolutionImplicitGemm_v4r4_)"; // TBD: remove these interim checks. if (tensorDescs.size() > 0) output << R"( - constexpr auto )" << tensorDescs[0] << " = InGlobalDesc{};"; + constexpr auto )" << tensorDescs[0] << " = WeiGlobalDesc{};"; if (tensorDescs.size() > 1) output << R"( - constexpr auto )" << tensorDescs[1] << " = WeiGlobalDesc{};"; + constexpr auto )" << tensorDescs[1] << " = InGlobalDesc{};"; if (tensorDescs.size() > 2) output << R"( constexpr auto )" << tensorDescs[2] << " = OutGlobalDesc{};"; ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [mlir] 7054cfc - Fix translation logic.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: 7054cfc71ac450ab5ac9ee505d1096bb1252f9c6 URL: https://github.com/llvm/llvm-project/commit/7054cfc71ac450ab5ac9ee505d1096bb1252f9c6 DIFF: https://github.com/llvm/llvm-project/commit/7054cfc71ac450ab5ac9ee505d1096bb1252f9c6.diff LOG: Fix translation logic. Added: Modified: mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp Removed: diff --git a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp index 4eb8d7de7181..2bd64efa77b6 100644 --- a/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp +++ b/mlir/lib/Dialect/MIOpenOps/CppOutput/ConvertToMIOpenCPP.cpp @@ -183,20 +183,16 @@ static constexpr StringLiteral kCppEpiloguePart2 =R"( void EmitCppPreamble(llvm::raw_ostream &output, llvm::StringRef layoutStr) { output << kCppPreamblePart1; - // Between Preamble Part 1 and Part 2: // #include "gridwise_convolution_implicit_gemm_v4r4_nchw_kcyx_nkhw.hpp" output << R"(#include "gridwise_convolution_implicit_gemm_v4r4_)"; - output << layoutStr << ".hpp"; - + output << layoutStr << R"(.hpp")"; output << kCppPreamblePart2; - // Between Preamble Part 2 and Par 3: //__launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_nchw_kcyx_nkhw( output << R"( __launch_bounds__(CK_PARAM_TUNABLE_BLOCK_SIZE, 2) void gridwise_convolution_implicit_gemm_v4r4_)"; output << layoutStr; - output << kCppPreamblePart3; } @@ -210,9 +206,7 @@ void EmitCppEpilogue(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm: output << R"( constexpr auto gridwise_conv = GridwiseConvolutionImplicitGemm_v4r4_)"; output << layoutStr; - output << kCppEpiloguePart1; - // Between Part1 and Part2: //decltype(in_nchw_desc), //decltype(wei_kcyx_desc), @@ -220,7 +214,6 @@ void EmitCppEpilogue(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm: for (auto desc : tensorDescs) { output << "decltype(" << desc << "),\n"; } - output << kCppEpiloguePart2; } @@ -344,28 +337,23 @@ static constexpr StringLiteral kHeaderEpiloguePart2 = R"( void EmitHeaderPreamble(llvm::raw_ostream &output, llvm::StringRef layoutStr, llvm::SmallVector &tensorDescs) { output << kHeaderPreamblePart1; - output << R"( struct GridwiseConvolutionImplicitGemm_v4r4_)"; output << layoutStr; - output << kHeaderPreamblePart2; - output << kHeaderPreamblePart3; - output << '\n'; - output << R"( constexpr auto )" << tensorDescs[0] << " = InGlobalDesc{};"; output << R"( constexpr auto )" << tensorDescs[1] << " = WeiGlobalDesc{};"; output << R"( constexpr auto )" << tensorDescs[2] << " = OutGlobalDesc{};"; + output << '\n'; } void EmitHeaderEpilogue(llvm::raw_ostream &output, llvm::SmallDenseMap &args) { output << kHeaderEpiloguePart1; - // Between Part1 and Part2 emit: // decltype(wei_e_k_global_desc), // decltype(in_e_b_global_desc), @@ -374,7 +362,6 @@ void EmitHeaderEpilogue(llvm::raw_ostream &output, llvm::SmallDenseMap +void EmitInterleaveArrayAttrWithSeparator(llvm::raw_ostream &os, mlir::ArrayAttr &arrayAttr, const StringRef &separator) { if (arrayAttr) { interleave(arrayAttr, os, [&](Attribute attr) { - if (auto strAttr = attr.dyn_cast()) -os << strAttr.getValue(); + if (auto typedAttr = attr.dyn_cast()) +os << typedAttr.getValue(); }, separator); } } -void EmitInterleaveCommaArrayAttrOfStringAttr(llvm::raw_ostream &os, mlir::ArrayAttr &arrayAttr) { - EmitInterleaveArrayAttrOfStringAttrWithSeparator(os, arrayAttr, ", "); +template +void EmitInterleaveCommaArrayAttr(llvm::raw_ostream &os, mlir::ArrayAttr &arrayAttr) { + EmitInterleaveArrayAttrWithSeparator(os, arrayAttr, ", "); } void ObtainModuleInfo(ModuleOp &m, std::string &layoutStr, llvm::SmallVector &tensorDescs) { @@ -511,7 +500,8 @@ std::unique_ptr mlir::translateModuleToMIOpenHeader(ModuleOp m) // Start emitting. EmitHeaderPreamble(output, layoutStr, tensorDescs); -f.walk([&output, &srcLayoutAttrCtr, &tensorDescs, &gridwiseGemmArguments](miopen::TransformOp op) { +// First iteration. Output source dimensions. +f.walk([&output, &srcLayoutAttrCtr, &tensorDescs](miopen::TransformOp op) { // get source_layout attribute. auto srcLayoutAttr = op.getAttrOfType("source_layout"); if (srcLayoutAttr) { @@ -520,10 +510,17 @@ std::unique_ptr mlir::translateModuleToMIOpenHeader(ModuleOp m) EmitLayoutString(output, srcLayout, "", "", ", "); output << '\n'; -EmitHeaderDimensionLengths(output, srcLayout, tensorDescs[srcLayoutAttrCtr]); +
[llvm-branch-commits] [mlir] 4600d68 - Initial commit to add ops into MIOpen dialect.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:19-05:00 New Revision: 4600d68a403647ea41eca5c62616d7abd77d9281 URL: https://github.com/llvm/llvm-project/commit/4600d68a403647ea41eca5c62616d7abd77d9281 DIFF: https://github.com/llvm/llvm-project/commit/4600d68a403647ea41eca5c62616d7abd77d9281.diff LOG: Initial commit to add ops into MIOpen dialect. - conv2d - transform - gridwise_gemm add dummy parse / print / verify logic. add dummy test. Added: mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td mlir/test/Dialect/MIOpen/ops.mlir Modified: mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h mlir/lib/Dialect/MIOpenOps/CMakeLists.txt mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp Removed: diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h index e0c36c23a854..47341c1637b1 100644 --- a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.h @@ -28,8 +28,8 @@ class MIOpenOpsDialect : public Dialect { static StringRef getDialectNamespace() { return "miopen"; } }; -//#define GET_OP_CLASSES -//#include "mlir/Dialect/MIOpenOps/MIOpenOps.h.inc" +#define GET_OP_CLASSES +#include "mlir/Dialect/MIOpenOps/MIOpenOps.h.inc" } // end namespace miopen } // end namespace mlir diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td new file mode 100644 index ..1304f16f3b30 --- /dev/null +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td @@ -0,0 +1,44 @@ +//===- MIOpenOps.td - MIOpen operation definitions -*- tablegen -*-===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Defines MLIR MIOpen operations. +// +//===--===// + +#ifndef MIOPEN_OPS +#define MIOPEN_OPS + +include "mlir/IR/OpBase.td" +//include "mlir/Transforms/LoopLikeInterface.td" + +def MIOpen_Dialect : Dialect { + let name = "miopen"; + let cppNamespace = ""; +} + +// Base class for MIOpen dialect ops. +class MIOpen_Op traits = []> : +Op { + // For every standard op, there needs to be a: + // * void print(OpAsmPrinter &p, ${C++ class of Op} op) + // * LogicalResult verify(${C++ class of Op} op) + // * ParseResult parse${C++ class of Op}(OpAsmParser &parser, + // OperationState &result) + // functions. + let printer = [{ return ::print(p, *this); }]; + let verifier = [{ return ::verify(*this); }]; + let parser = [{ return ::parse$cppClass(parser, result); }]; +} + +def MIOpen_Conv2DOp : MIOpen_Op<"conv2d">; + +def MIOpen_TransformOp : MIOpen_Op<"transform">; + +def MIOpen_GridwiseGemmOp : MIOpen_Op<"gridwise_gemm">; + +#endif // MIOPEN_OPS diff --git a/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt index 474196162792..ba32051bbb39 100644 --- a/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt +++ b/mlir/lib/Dialect/MIOpenOps/CMakeLists.txt @@ -5,5 +5,5 @@ add_llvm_library(MLIRMIOpenOps ADDITIONAL_HEADER_DIRS ${MLIR_MAIN_INCLUDE_DIR}/mlir/MIOpenOps ) -add_dependencies(MLIRMIOpenOps MLIRStandardOps LLVMSupport) +add_dependencies(MLIRMIOpenOps MLIRMIOpenOpsIncGen MLIRStandardOps LLVMSupport) target_link_libraries(MLIRMIOpenOps LLVMSupport) diff --git a/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp index ced4d25e866f..9408e17b2831 100644 --- a/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp +++ b/mlir/lib/Dialect/MIOpenOps/MIOpenOps.cpp @@ -36,17 +36,65 @@ namespace { MIOpenOpsDialect::MIOpenOpsDialect(MLIRContext *context) : Dialect(getDialectNamespace(), context) { -// addOperations< -//#define GET_OP_LIST -//#include "mlir/Dialect/MIOpenOps/MIOpenOps.cpp.inc" -// >(); + addOperations< +#define GET_OP_LIST +#include "mlir/Dialect/MIOpenOps/MIOpenOps.cpp.inc" + >(); //addInterfaces(); } +//===--===// +// Conv2DOp +//===--===// + +static ParseResult parseConv2DOp(OpAsmParser &parser, OperationState &result) { + return success(); +} + +static void print(OpAsmPrinter &p, Conv2DOp op) { + p << Conv2DOp::getOperationName(); +} + +static LogicalResult verify(Conv2DOp op) { + return success(); +} + +//===--===// +// TransformOp +//===--===// + +static ParseResult parseTransformOp(OpAsmParser &parser, OperationSta
[llvm-branch-commits] [mlir] e587a8a - Inital commit to add MIOpen Conv2D to Transform and GridwiseGemm transform pass.
Author: Wen-Heng (Jack) Chung Date: 2020-06-05T22:18:20-05:00 New Revision: e587a8a23c9812610dd1e79a3e1211e1f4d8aba5 URL: https://github.com/llvm/llvm-project/commit/e587a8a23c9812610dd1e79a3e1211e1f4d8aba5 DIFF: https://github.com/llvm/llvm-project/commit/e587a8a23c9812610dd1e79a3e1211e1f4d8aba5.diff LOG: Inital commit to add MIOpen Conv2D to Transform and GridwiseGemm transform pass. Added: mlir/include/mlir/Dialect/MIOpenOps/Passes.h mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp mlir/test/Dialect/MIOpen/lowering.mlir Modified: mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td Removed: diff --git a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td index 8ffd66647f3f..1f531d9176ab 100644 --- a/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td +++ b/mlir/include/mlir/Dialect/MIOpenOps/MIOpenOps.td @@ -37,9 +37,9 @@ class MIOpen_Op traits = []> : def MIOpen_Conv2DOp : MIOpen_Op<"conv2d">, -Arguments<(ins MemRefRankOf<[F32], [4]>, - MemRefRankOf<[F32], [4]>, - MemRefRankOf<[F32], [4]>)> { +Arguments<(ins MemRefRankOf<[F32], [4]>:$filter, + MemRefRankOf<[F32], [4]>:$input, + MemRefRankOf<[F32], [4]>:$output)> { let summary = "2D convolution"; let description = [{ The `miopen.conv2d` op computes 2D convolution. diff --git a/mlir/include/mlir/Dialect/MIOpenOps/Passes.h b/mlir/include/mlir/Dialect/MIOpenOps/Passes.h new file mode 100644 index ..6752b71c5598 --- /dev/null +++ b/mlir/include/mlir/Dialect/MIOpenOps/Passes.h @@ -0,0 +1,33 @@ +//===- Passes.h - Linalg pass entry points --*- C++ -*-===// +// +// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// This header file defines prototypes that expose pass constructors. +// +//===--===// + +#ifndef MLIR_DIALECT_MIOPEN_PASSES_H_ +#define MLIR_DIALECT_MIOPEN_PASSES_H_ + +#include "mlir/Support/LLVM.h" +#include "llvm/ADT/ArrayRef.h" + +namespace mlir { +class FuncOp; +class ModuleOp; +template class OpPassBase; + +namespace miopen { + +/// Create a pass to convert MIOpen conv2d operations to transform and +/// gridwise_gemm operations. +std::unique_ptr> createLowerMIOpenOpsPass(); + +} // namespace miopen +} // namespace mlir + +#endif // MLIR_DIALECT_MIOPEN_PASSES_H_ diff --git a/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp new file mode 100644 index ..2a00ed675122 --- /dev/null +++ b/mlir/lib/Dialect/MIOpenOps/LowerMIOpenOps.cpp @@ -0,0 +1,82 @@ +//===- LowerMIOpenOps.cpp - MLIR MIOpen ops lowering passes ---===// +// +// Copyright 2020 The MLIR Authors. +// +// Licensed 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. +// = +// +// This pass converts miopen.conv2d into miopen.transform and +// miopen.gridwise_gemm. +// +//===--===// + +#include "mlir/Dialect/MIOpenOps/MIOpenOps.h" +#include "mlir/Dialect/MIOpenOps/Passes.h" +#include "mlir/Dialect/StandardOps/Ops.h" +#include "mlir/IR/Attributes.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/IR/Module.h" +#include "mlir/IR/Operation.h" +#include "mlir/IR/PatternMatch.h" +#include "mlir/IR/StandardTypes.h" +#include "mlir/IR/Types.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Pass/PassManager.h" +#include "mlir/Pass/PassRegistry.h" +#include "mlir/Transforms/DialectConversion.h" +#include "mlir/Transforms/Passes.h" +#include "mlir/Support/LogicalResult.h" + +using namespace mlir; + +struct Conv2DOpRewritePattern : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + PatternMatchResult + matchAndRewrite(miopen::Conv2DOp op, PatternRewriter &rewriter) const override { +rewriter.create(op.getLoc(), op.filter().getType(), op.filter()); + +rewriter.create(op.getLoc(), op.input().getType(), op.input()); +rewriter.create(op.getLoc(), op.input().getType(), op