diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index 62e54e26eda990e32a62c4ba99070b78d5c6275d..c8253effe8488946dfaa3c3bd4812c73d7f938d8 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 ce38c53617c711e25bf559f8aa668e5da253955d..757d47a8de43e2a394ad5296e617ed6ed94078f3 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 0000000000000000000000000000000000000000..98910d8d0ecf0b99bd1eb8b860ed573ae88ef203 --- /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 0000000000000000000000000000000000000000..388de858b6572ea5900851b170d09589387c0b05 --- /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 0000000000000000000000000000000000000000..21a1f6b34f6a5f33bd82c4e78669ee24221a08f1 --- /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 0000000000000000000000000000000000000000..319760973cd90c667793e29761c030141990c242 --- /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 9afefc0158715bcd17f26447631d69441b445c13..090f1aea289109feda54b12131daf2993ea4e5e0 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 f3b85ae4b5d0b9c93dfc4d0a1d9530c1e18da925..7cf5b2fb20f527eefe31f817c7fe85c7864c8669 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 ca61ddc0b7053dce34b115dc443e872206960631..02511b21e4792bb37c416093a7c272090eae44c1 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