From 2c7f6e6d37f322c7cc71c93d6dcf63c73e3db7a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=8E=E5=86=AC?= <78149749+winter-wang@users.noreply.github.com> Date: Wed, 16 Feb 2022 10:33:58 +0800 Subject: [PATCH] [infrt] add infrt dialect ir. test=develop (#39455) --- paddle/infrt/CMakeLists.txt | 2 +- paddle/infrt/dialect/CMakeLists.txt | 2 +- paddle/infrt/dialect/infrt/CMakeLists.txt | 7 ++ paddle/infrt/dialect/infrt/infrt_dialect.cc | 92 +++++++++++++++++++++ paddle/infrt/dialect/infrt/infrt_dialect.h | 29 +++++++ paddle/infrt/dialect/infrt/infrt_ops.td | 52 ++++++++++++ paddle/infrt/dialect/init_infrt_dialects.cc | 2 + paddle/infrt/dialect/pd_op_base.td | 6 +- paddle/infrt/dialect/pd_ops.cc | 1 + paddle/infrt/tests/dialect/paddle_ops.mlir | 2 +- 10 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 paddle/infrt/dialect/infrt/CMakeLists.txt create mode 100644 paddle/infrt/dialect/infrt/infrt_dialect.cc create mode 100644 paddle/infrt/dialect/infrt/infrt_dialect.h create mode 100644 paddle/infrt/dialect/infrt/infrt_ops.td diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index 62e54e26eda..c8253effe84 100644 --- a/paddle/infrt/CMakeLists.txt +++ b/paddle/infrt/CMakeLists.txt @@ -107,6 +107,6 @@ endif() cc_library(infrt SHARED SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto infrt_naive) cc_library(infrt_static SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto) -add_dependencies(infrt ${infrt_mlir_incs}) +add_dependencies(infrt ${infrt_mlir_incs} mlir-headers) add_custom_target(test_infrt_exec DEPENDS ${INFRT_TEST_TARGETS}) diff --git a/paddle/infrt/dialect/CMakeLists.txt b/paddle/infrt/dialect/CMakeLists.txt index ce38c53617c..757d47a8de4 100644 --- a/paddle/infrt/dialect/CMakeLists.txt +++ b/paddle/infrt/dialect/CMakeLists.txt @@ -31,9 +31,9 @@ target_link_libraries(infrtopt infrt) add_executable(print-ir print_ir.cc) target_link_libraries(print-ir infrt ${mlir_libs}) add_dependencies(print-ir pd_ops_inc) - cc_test_tiny(test_infrt_mlir_loader SRCS mlir_loader_test.cc DEPS infrt ${MLIR_IR_LIBS}) +add_subdirectory(infrt) add_subdirectory(tensorrt) if (INFRT_WITH_PTEN) diff --git a/paddle/infrt/dialect/infrt/CMakeLists.txt b/paddle/infrt/dialect/infrt/CMakeLists.txt new file mode 100644 index 00000000000..98910d8d0ec --- /dev/null +++ b/paddle/infrt/dialect/infrt/CMakeLists.txt @@ -0,0 +1,7 @@ +core_gather_headers() + +gather_srcs(infrt_src SRCS + infrt_dialect.cc + ) + +add_mlir_dialect(infrt_ops Infrt) diff --git a/paddle/infrt/dialect/infrt/infrt_dialect.cc b/paddle/infrt/dialect/infrt/infrt_dialect.cc new file mode 100644 index 00000000000..388de858b65 --- /dev/null +++ b/paddle/infrt/dialect/infrt/infrt_dialect.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// 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. + +#include "paddle/infrt/dialect/infrt/infrt_dialect.h" + +#include +#include +#include +#include "paddle/infrt/dialect/dense_tensor.h" +#include "paddle/infrt/dialect/infrt/infrt_opsDialect.cpp.inc" + +#define GET_TYPEDEF_CLASSES +#include "paddle/infrt/dialect/infrt/infrt_opsTypes.cpp.inc" + +#define GET_OP_CLASSES +#include "paddle/infrt/dialect/infrt/infrt_ops.cpp.inc" + +namespace infrt { + +void InfrtDialect::initialize() { + addTypes< +#define GET_TYPEDEF_LIST +#include "paddle/infrt/dialect/infrt/infrt_opsTypes.cpp.inc" // NOLINT + >(); + addOperations< +#define GET_OP_LIST +#include "paddle/infrt/dialect/infrt/infrt_ops.cpp.inc" // NOLINT + >(); +} + +/// Parse a type registered to this dialect. +mlir::Type InfrtDialect::parseType(::mlir::DialectAsmParser &parser) const { + llvm::StringRef keyword; + if (parser.parseKeyword(&keyword)) return nullptr; + // parse TensorType, for example: !infrt.lod_tensor<3x64x3x3xf32,5> + // 5 is the lod_level + if (keyword == "lod_tensor") { + // Parse the size and elementType. + llvm::SmallVector shape; + mlir::Type elementType; + int32_t lod_level = 0; + // parse "<" + if (parser.parseLess()) return nullptr; + + if (parser.parseDimensionList(shape)) return nullptr; + + // Parse the element type. + if (parser.parseType(elementType)) return nullptr; + // parse "," + if (parser.parseComma()) return nullptr; + + // llvm::APInt lod_level; + if (parser.parseInteger(lod_level)) return nullptr; + + // parse ">" + if (parser.parseGreater()) return nullptr; + + return LoDTensorType::get( + parser.getContext(), shape, elementType, lod_level); + } + // Todo: parse other type + return mlir::Type(); +} + +void InfrtDialect::printType(::mlir::Type type, + ::mlir::DialectAsmPrinter &os) const { + // print TensorType, for example: !infrt.tensor + if (type.isa()) { + auto lodTensorType = type.cast(); + os << "lod_tensor<"; + auto shape = lodTensorType.getShape(); + for (auto dim = shape.begin(), e = shape.end() - 1; dim != e; ++dim) + os << *dim << 'x'; + os << shape.back() << 'x' << lodTensorType.getElementType() << ", " + << lodTensorType.getLod_level() << ">"; + return; + } + llvm_unreachable("unknown infrt type."); +} + +} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/infrt_dialect.h b/paddle/infrt/dialect/infrt/infrt_dialect.h new file mode 100644 index 00000000000..21a1f6b34f6 --- /dev/null +++ b/paddle/infrt/dialect/infrt/infrt_dialect.h @@ -0,0 +1,29 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// 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. + +#pragma once + +//===----------------------------------------------------------------------===// +// Dialect +//===----------------------------------------------------------------------===// +#include +#include +#include +#include + +#include "paddle/infrt/dialect/infrt/infrt_opsDialect.h.inc" +#define GET_TYPEDEF_CLASSES +#include "paddle/infrt/dialect/infrt/infrt_opsTypes.h.inc" +#define GET_OP_CLASSES +#include "paddle/infrt/dialect/infrt/infrt_ops.h.inc" diff --git a/paddle/infrt/dialect/infrt/infrt_ops.td b/paddle/infrt/dialect/infrt/infrt_ops.td new file mode 100644 index 00000000000..319760973cd --- /dev/null +++ b/paddle/infrt/dialect/infrt/infrt_ops.td @@ -0,0 +1,52 @@ +#ifndef Infrt_OpS +#define Infrt_OpS + +include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffectInterfaces.td" + +def Infrt_Dialect : Dialect { + let summary = + "A dialect containing the Infrt Attributes, Operations, and Types"; + + let name = "Infrt"; + let cppNamespace = "::infrt"; +} + +// Type definitions + +// Base class for Infrt dialect types. +class Infrt_Type traits = [], + string baseCppClass = "::mlir::Type"> + : TypeDef { +} + +def LoDTensor : Infrt_Type<"LoDTensor"> { + let summary = "infrt lod tensor"; + let description = [{lod_tensor<3x64x3x3xf32, 3>}]; + let parameters = (ins + ArrayRefParameter<"int64_t">:$shape, + "mlir::Type":$elementType, + "int32_t":$lod_level + ); +} + +// Op definition +class Infrt_Op traits = []> : Op { + + // Each registered op needs to provide all of a printer, parser and verifier. + // let printer = [{ return infrt::print(p, *this); }]; + // let verifier = [{ return infrt::verify(*this); }]; + // let parser = [{ return infrt::parse$cppClass(parser, result); }]; +} + +// def InfRT_KernelOp : Infrt_Op<"kernel", [NoSideEffect]> { +// let summary = "kernel op"; +// let description = [{ +// kernel op! +// }]; +// let arguments = (ins StrAttr:$name, PD_Tensor:$X, PD_Tensor:$Y, DefaultValuedAttr:$Alpha, DefaultValuedAttr:$Beta); +// +// let results = (outs PD_Tensor:$Out); +// } + +#endif // Infrt_OpS diff --git a/paddle/infrt/dialect/init_infrt_dialects.cc b/paddle/infrt/dialect/init_infrt_dialects.cc index 9afefc01587..090f1aea289 100644 --- a/paddle/infrt/dialect/init_infrt_dialects.cc +++ b/paddle/infrt/dialect/init_infrt_dialects.cc @@ -18,6 +18,7 @@ #include "paddle/infrt/dialect/basic_kernels.h" #include "paddle/infrt/dialect/dense_tensor.h" +#include "paddle/infrt/dialect/infrt/infrt_dialect.h" #include "paddle/infrt/dialect/infrt_base.h" #include "paddle/infrt/dialect/pd_ops.h" #include "paddle/infrt/dialect/pten/infrt_pten_tensor.h" @@ -28,6 +29,7 @@ namespace infrt { void registerCinnDialects(mlir::DialectRegistry ®istry) { // NOLINT registry.insert, "pd.dtype">; -def PD_Tensor : TensorOf<[PD_ElementType]>; +// def PD_Tensor : TensorOf<[PD_ElementType]>; +def PD_Tensor1 : TensorOf<[PD_ElementType]>; + +def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor],"pd.ttype">; def PD_Tensor_Array : VectorOf<[PD_Tensor]>; diff --git a/paddle/infrt/dialect/pd_ops.cc b/paddle/infrt/dialect/pd_ops.cc index f3b85ae4b5d..7cf5b2fb20f 100644 --- a/paddle/infrt/dialect/pd_ops.cc +++ b/paddle/infrt/dialect/pd_ops.cc @@ -16,6 +16,7 @@ #include #include +#include "paddle/infrt/dialect/infrt/infrt_dialect.h" #include "paddle/infrt/dialect/infrt_base.h" #define GET_OP_CLASSES diff --git a/paddle/infrt/tests/dialect/paddle_ops.mlir b/paddle/infrt/tests/dialect/paddle_ops.mlir index ca61ddc0b70..02511b21e47 100644 --- a/paddle/infrt/tests/dialect/paddle_ops.mlir +++ b/paddle/infrt/tests/dialect/paddle_ops.mlir @@ -3,7 +3,7 @@ func @ops() { %a = pd.feed() {name="input0"} : tensor %b = pd.feed() {name="input1"}: tensor - + %d = pd.feed() {name="input3"}: !Infrt.lod_tensor<3x4x9xf32, 0> %c = "pd.matmul"(%a, %b) {transpose_x=true, transpose_y=false} : (tensor, tensor) -> tensor infrt.return -- GitLab