forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathop_dialect.h
More file actions
156 lines (120 loc) · 4.53 KB
/
op_dialect.h
File metadata and controls
156 lines (120 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2023 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
#include <typeindex>
#include <typeinfo>
#include "paddle/common/macros.h"
#include "paddle/fluid/pir/dialect/operator/interface/op_yaml_info.h"
#include "paddle/phi/api/ext/op_meta_info.h"
#include "paddle/pir/include/core/dialect.h"
#include "paddle/pir/include/core/ir_context.h"
#include "paddle/pir/include/core/operation.h"
#include "paddle/utils/test_macros.h"
namespace paddle {
namespace dialect {
class TEST_API OperatorDialect : public pir::Dialect {
public:
explicit OperatorDialect(pir::IrContext* context);
static const char* name() { return "pd_op"; }
void PrintType(pir::Type type, std::ostream& os) const override;
void PrintAttribute(pir::Attribute attr, std::ostream& os) const override;
pir::OpPrintFn PrintOperation(
const pir::Operation& op) const override; // NOLINT
private:
void initialize();
};
inline bool IsCustomOp(pir::Operation* op) {
std::string op_name = op->name();
return op_name.find("custom_op") != op_name.npos;
}
inline bool IsPythonOp(pir::Operation* op) {
const std::string& op_name = op->name();
return op_name.find("py_op") != op_name.npos;
}
inline bool IsCustomEngineOp(pir::Operation* op) {
std::string op_name = op->name();
return op_name.find("custom_engine") != op_name.npos;
}
inline bool IsInplaceOp(pir::Operation* op) {
std::string op_name = op->name();
return op_name.back() == '_';
}
inline bool IsTensorRTOp(pir::Operation* op) {
std::string op_name = op->name();
return op_name == "pd_op.tensorrt_engine";
}
class CustomOpDialect : public pir::Dialect {
public:
explicit CustomOpDialect(pir::IrContext* context);
static const char* name() { return "custom_op"; }
void PrintType(pir::Type type, std::ostream& os) const override;
void PrintAttribute(pir::Attribute type, std::ostream& os) const override;
pir::OpPrintFn PrintOperation(
const pir::Operation& op) const override; // NOLINT
void RegisterCustomOp(const paddle::OpMetaInfo& op_meta);
bool HasRegistered(const std::string& op_name) {
if (std::find(op_names_.begin(), op_names_.end(), op_name) !=
op_names_.end()) {
return true;
}
return false;
}
private:
std::vector<const char*> op_names_;
};
class PythonOperatorDialect : public pir::Dialect {
public:
explicit PythonOperatorDialect(pir::IrContext* context);
constexpr static const char* name() { return "py_op"; }
void PrintType(pir::Type type, std::ostream& os) const override;
void PrintAttribute(pir::Attribute type, std::ostream& os) const override;
pir::OpPrintFn PrintOperation(
const pir::Operation& op) const override; // NOLINT
void RegisterPythonOperator(const paddle::OpMetaInfo& op_meta);
bool HasRegistered(const std::string& op_name) {
if (std::find(op_names_.begin(), op_names_.end(), op_name) !=
op_names_.end()) {
return true;
}
return false;
}
private:
std::vector<const char*> op_names_;
};
class TEST_API CustomEngineDialect : public pir::Dialect {
public:
explicit CustomEngineDialect(pir::IrContext* context);
static const char* name() { return "custom_engine"; }
void PrintType(pir::Type type, std::ostream& os) const override;
void PrintAttribute(pir::Attribute type, std::ostream& os) const override;
pir::OpPrintFn PrintOperation(
const pir::Operation& op) const override; // NOLINT
// template <typename ConcreteOp>
// static void RegisterCustomEngineOp();
bool HasRegistered(const std::string& op_name) {
if (std::find(op_names_.begin(), op_names_.end(), op_name) !=
op_names_.end()) {
return true;
}
return false;
}
private:
std::vector<const char*> op_names_;
};
} // namespace dialect
} // namespace paddle
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::OperatorDialect)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::CustomOpDialect)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PythonOperatorDialect)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::CustomEngineDialect)