forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize Paddle MLIR Dialect (PaddlePaddle#308)
- Loading branch information
Showing
13 changed files
with
318 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
func @ops() { | ||
%a = pd.Feed() : tensor<?xf32> | ||
%b = pd.Feed() : tensor<?xf32> | ||
|
||
%c = "pd.Matmul"(%a, %b) {transpose_x=true, transpose_y=false} : (tensor<?xf32>, tensor<?xf32>) -> tensor<?xf32> | ||
|
||
cinn.return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This file defines some basic elements of Paddle(alias pd) dialect. | ||
// We learned much from TensorFlow mlir dialect https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td | ||
|
||
#ifndef PD_OP_BASE | ||
#define PD_OP_BASE | ||
|
||
include "mlir/IR/OpBase.td" | ||
include "mlir/Interfaces/SideEffectInterfaces.td" | ||
|
||
def PD_Dialect : Dialect { | ||
let name = "pd"; | ||
|
||
let description = [{ | ||
The PaddlePaddle dialect. | ||
|
||
This dialect contains the PaddlePaddle operators. | ||
}]; | ||
|
||
let cppNamespace = "::mlir::PD"; | ||
} | ||
|
||
class PD_Op<string mnemonic, list<OpTrait> traits = []> : | ||
Op<PD_Dialect, mnemonic, traits>; | ||
|
||
|
||
class PD_PaddleAttr <string name, string description> : | ||
Attr<CPred<"$_self.isa<mlir::PD::" # name # "Attr>()">, | ||
"PaddlePaddle " # description # " attribute">; | ||
|
||
|
||
//===----------------------------------------------------------------------===// | ||
// PaddlePaddle type definitions | ||
//===----------------------------------------------------------------------===// | ||
|
||
def PD_PDDialectType : Type<CPred<"$_self.isa<mlir::PD::PDType>()">, "PaddlePaddle type">; | ||
|
||
class PD_PaddleType <string name, string description> : | ||
Type<CPred<"$_self.isa<mlir::PD::" # name #"Type>()">, | ||
"Paddle " # description # " type">, | ||
BuildableType<"getType<mlir::PD::" # name # "Type>()">; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Integer types | ||
def PD_Bool : AnyTypeOf<[I<1>], "bool">; | ||
def PD_Int8 : AnyTypeOf<[I8], "8-bit integer">; | ||
def PD_Int16 : AnyTypeOf<[I16], "16-bit integer">; | ||
def PD_Int32 : AnyTypeOf<[I32], "32-bit integer">; | ||
def PD_Int64 : AnyTypeOf<[I64], "64-bit integer">; | ||
|
||
def PD_UInt8 : AnyTypeOf<[UI<8>], "8-bit unsigned integer">; | ||
def PD_UInt16 : AnyTypeOf<[UI<16>], "16-bit unsigned integer">; | ||
def PD_UInt32 : AnyTypeOf<[UI<32>], "32-bit unsigned integer">; | ||
def PD_UInt64 : AnyTypeOf<[UI<64>], "64-bit unsigned integer">; | ||
|
||
def PD_SInt : AnyTypeOf<[PD_Int8, PD_Int16, PD_Int32, PD_Int64], "signed integer">; | ||
def PD_UInt : AnyTypeOf<[PD_UInt8, PD_UInt16, PD_UInt32, PD_UInt64], "unsigned integer">; | ||
def PD_Int : AnyTypeOf<[PD_SInt, PD_UInt], "integer">; | ||
|
||
// Float types | ||
def PD_Float16 : AnyTypeOf<[F16], "16-bit float">; | ||
def PD_Float32 : AnyTypeOf<[F32], "32-bit float">; | ||
def PD_Float64 : AnyTypeOf<[F64], "64-bit float">; | ||
|
||
def PD_Float : AnyTypeOf<[PD_Float16, PD_Float32, PD_Float64], "floating-point">; | ||
|
||
|
||
// Tensor types | ||
|
||
def PD_ElementType : Type<Or<[PD_Float.predicate, | ||
PD_Bool.predicate, | ||
PD_Int.predicate]>, | ||
"pd.dtype">; | ||
|
||
def PD_Tensor : TensorOf<[PD_ElementType]>; | ||
|
||
|
||
#endif // PD_OP_BASE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "cinnrt/dialect/pd_ops.h" | ||
|
||
namespace mlir { | ||
namespace PD { | ||
|
||
#define GET_OP_CLASSES | ||
#include "cinnrt/dialect/pd_ops.hpp.inc" | ||
#undef GET_OP_CLASSES | ||
|
||
PaddleDialect::PaddleDialect(MLIRContext *context) : Dialect("pd", context, TypeID::get<PaddleDialect>()) { | ||
addOperations< | ||
#define GET_OP_LIST | ||
#include "cinnrt/dialect/pd_ops.cpp.inc" | ||
>(); | ||
#undef GET_OP_LIST | ||
|
||
// Support unknown operations because not all Paddle operations are registered. | ||
allowUnknownOperations(); | ||
} | ||
|
||
#define GET_OP_CLASSES | ||
#include "cinnrt/dialect/pd_ops.cpp.inc" | ||
#undef GET_OP_CLASSES | ||
|
||
} // namespace PD | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "mlir/Dialect/Traits.h" | ||
#include "mlir/IR/Attributes.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/Dialect.h" | ||
#include "mlir/IR/Function.h" | ||
#include "mlir/IR/Matchers.h" | ||
#include "mlir/IR/Module.h" | ||
#include "mlir/IR/OpImplementation.h" | ||
#include "mlir/IR/StandardTypes.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/Interfaces/CallInterfaces.h" | ||
#include "mlir/Interfaces/DerivedAttributeOpInterface.h" | ||
#include "mlir/Interfaces/InferTypeOpInterface.h" | ||
#include "mlir/Interfaces/LoopLikeInterface.h" | ||
#include "mlir/Interfaces/SideEffectInterfaces.h" | ||
|
||
namespace mlir { | ||
namespace PD { | ||
|
||
class PaddleDialect : public Dialect { | ||
public: | ||
explicit PaddleDialect(MLIRContext* context); | ||
|
||
static StringRef getDialectNamespace() { return "PD"; } | ||
|
||
Type parseType(DialectAsmParser& parser) const override { return Dialect::parseType(parser); } | ||
void printType(Type type, DialectAsmPrinter& printer) const override { Dialect::printType(type, printer); } | ||
}; | ||
|
||
} // namespace PD | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef PD_OPS | ||
#define PD_OPS | ||
|
||
include "mlir/Interfaces/InferTypeOpInterface.td" | ||
include "mlir/Interfaces/LoopLikeInterface.td" | ||
include "mlir/IR/OpBase.td" | ||
include "cinnrt/dialect/pd_op_base.td" | ||
|
||
def PD_FeedOp : PD_Op<"Feed", [NoSideEffect]> { | ||
let summary = "Feed Op"; | ||
|
||
let description = [{ | ||
Feed a tensor into the model. | ||
}]; | ||
|
||
let arguments = (ins); | ||
let results = (outs PD_Tensor:$out); | ||
|
||
let assemblyFormat = [{ | ||
`(` `)` attr-dict `:` type($out) | ||
}]; | ||
} | ||
|
||
def PD_AbsOp : PD_Op<"Abs", [NoSideEffect]> { | ||
let summary = "Computes the absolute value of a tensor"; | ||
|
||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x); | ||
let results = (outs PD_Tensor:$y); | ||
} | ||
|
||
def PD_ReluOp : PD_Op<"Relu", [NoSideEffect]> { | ||
let summary = "Computes the Relu of a tensor"; | ||
|
||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x); | ||
let results = (outs PD_Tensor:$y); | ||
} | ||
|
||
def PD_Relu6Op : PD_Op<"Relu6", [NoSideEffect]> { | ||
let summary = "Computes the Relu6 of a tensor"; | ||
|
||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x); | ||
let results = (outs PD_Tensor:$y); | ||
} | ||
|
||
def PD_ElementwiseAdd : PD_Op<"ElementwiseAdd", [NoSideEffect]> { | ||
let summary = "ElementwiseAdd Op"; | ||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x, PD_Tensor:$y, I32Attr:$axis); | ||
let results = (outs PD_Tensor:$out); | ||
} | ||
|
||
def PD_ElementwiseMul : PD_Op<"ElementwiseMul", [NoSideEffect]> { | ||
let summary = "ElementwiseMul Op"; | ||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x, PD_Tensor:$y, I32Attr:$axis); | ||
let results = (outs PD_Tensor:$out); | ||
} | ||
|
||
def PD_MatmulOp : PD_Op<"Matmul", [NoSideEffect]> { | ||
let summary = "Computes the matrix mulplication result of two tensors"; | ||
let description = [{ | ||
}]; | ||
|
||
let arguments = (ins PD_Tensor:$x, PD_Tensor:$y, | ||
DefaultValuedAttr<BoolAttr, "false">:$transpose_x, | ||
DefaultValuedAttr<BoolAttr, "false">:$transpose_y, | ||
DefaultValuedAttr<F32Attr, "1.0">:$alpha); | ||
let results = (outs PD_Tensor:$out); | ||
|
||
//let hasCanonicalizer = 1; | ||
} | ||
|
||
#endif // PD_OPS |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// This file defines the types used in PaddlePaddle MLIR dialect. | ||
// We borrowed much ideas from tensorflow mlir dialect (tf_types.h in tensorflow). | ||
|
||
#pragma once | ||
|
||
#include "mlir/IR/Diagnostics.h" | ||
#include "mlir/IR/Location.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "mlir/IR/StandardTypes.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/IR/Types.h" | ||
|
||
namespace mlir { | ||
namespace PD { | ||
|
||
class PaddleType : public Type { | ||
public: | ||
using Type::Type; | ||
|
||
static bool classof(Type type); | ||
}; | ||
|
||
namespace detail { | ||
|
||
template <typename Derived> | ||
class PaddleTypeImpl : public Type::TypeBase<Derived, PaddleType, TypeStorage> { | ||
public: | ||
using Base = typename Type::TypeBase<Derived, PaddleType, TypeStorage>; | ||
using PDBase = PaddleTypeImpl<Derived>; | ||
using Base::Base; | ||
}; | ||
|
||
} // namespace detail | ||
|
||
#define HANDLE_PD_TYPE(pdtype, enumerant, name) \ | ||
class pdtype##Type : public detail::PaddleTypeImpl<pdtype##Type> { \ | ||
public: \ | ||
using PDBase::PDBase; \ | ||
}; | ||
|
||
} // namespace PD | ||
} // namespace mlir |
Oops, something went wrong.