Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewIR] Add parser to deserialize #55695

Merged
merged 21 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 131 additions & 4 deletions paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute_storage.h"
#include "paddle/ir/core/attribute.h"
#include "paddle/ir/core/builtin_attribute.h"
#include "paddle/ir/core/ir_parser.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/enforce.h"

Expand All @@ -33,6 +34,19 @@ class IntArrayAttribute : public ir::Attribute {
return storage() < right.storage();
}

static IntArrayAttribute Parse(ir::IrParser &parser) { // NOLINT
kangguangli marked this conversation as resolved.
Show resolved Hide resolved
Token buket_token = parser.GetToken();
std::vector<int32_t> vec{};
while (parser.PeekToken().val_ != "]") {
Token val_token = parser.GetToken();
vec.push_back(atoll(val_token.val_.c_str()));
if (parser.PeekToken().val_ == "]") break;
parser.GetToken();
}
parser.GetToken();
return IntArrayAttribute::get(parser.ctx, vec);
}

const phi::IntArray &data() const;
};

Expand All @@ -45,8 +59,7 @@ class ScalarAttribute : public ir::Attribute {
(val.type_id() == ir::FloatAttribute::type_id()) ||
(val.type_id() == ir::DoubleAttribute::type_id()) ||
(val.type_id() == ir::Int32Attribute::type_id()) ||
(val.type_id() == ir::Int64Attribute::type_id()) ||
(val.type_id() == ir::StrAttribute::type_id());
(val.type_id() == ir::Int64Attribute::type_id());
}

phi::Scalar data();
Expand All @@ -62,7 +75,52 @@ class DataTypeAttribute : public ir::Attribute {
bool operator<(const DataTypeAttribute &right) const {
return storage() < right.storage();
}

static DataTypeAttribute Parse(ir::IrParser &parser) { // NOLINT
Token datatype_token = parser.GetToken();
kangguangli marked this conversation as resolved.
Show resolved Hide resolved
if (datatype_token.val_ == "bool") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::BOOL);
} else if (datatype_token.val_ == "uint8") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UINT8);
} else if (datatype_token.val_ == "int8") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::INT8);
}
if (datatype_token.val_ == "uint16") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UINT16);
} else if (datatype_token.val_ == "int16") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::INT16);
} else if (datatype_token.val_ == "uint32") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UINT32);
}
if (datatype_token.val_ == "int32") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::INT32);
} else if (datatype_token.val_ == "uint64") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UINT64);
} else if (datatype_token.val_ == "int64") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::INT64);
} else if (datatype_token.val_ == "float32") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::FLOAT32);
} else if (datatype_token.val_ == "float64") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::FLOAT64);
} else if (datatype_token.val_ == "complex64") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::COMPLEX64);
} else if (datatype_token.val_ == "complex128") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::COMPLEX128);
} else if (datatype_token.val_ == "undefined") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UNDEFINED);
} else if (datatype_token.val_ == "psting") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::PSTRING);
} else if (datatype_token.val_ == "float16") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::FLOAT16);
} else if (datatype_token.val_ == "bfloat16") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::BFLOAT16);
} else if (datatype_token.val_ == "num_data_types") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::NUM_DATA_TYPES);
} else if (datatype_token.val_ == "all_dtype") {
return DataTypeAttribute::get(parser.ctx, phi::DataType::ALL_DTYPE);
} else {
return DataTypeAttribute::get(parser.ctx, phi::DataType::UNDEFINED);
}
}
phi::DataType data() const;
};

Expand All @@ -75,7 +133,38 @@ class PlaceAttribute : public ir::Attribute {
bool operator<(const PlaceAttribute &right) const {
return storage() < right.storage();
}

static PlaceAttribute Parse(ir::IrParser &parser) { // NOLINT
Token place_identifier_token = parser.GetToken();
Token left_parenthesis_token = parser.GetToken();
Token place_token = parser.GetToken();
if (place_token.val_ == "cpu") {
parser.GetToken();
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved
return PlaceAttribute::get(parser.ctx, phi::CPUPlace{});
} else if (place_token.val_ == "gpu") {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::GPUPlace{});
} else if (place_token.val_ == "gpupinned") {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::GPUPinnedPlace{});
} else if (place_token.val_ == "xpu") {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::XPUPlace{});
} else if (place_token.val_ == "ipu") {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::IPUPlace{});
} else if (place_token.val_ == "custom") {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::CustomPlace{});
} else if (place_token.val_ == "undefined") {
parser.GetToken();
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved
parser.GetToken();
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::Place{});
} else {
parser.GetToken();
return PlaceAttribute::get(parser.ctx, phi::Place{});
}
}
phi::Place data() const;
};

Expand All @@ -89,6 +178,44 @@ class DataLayoutAttribute : public ir::Attribute {
bool operator<(const DataLayoutAttribute &right) const {
return storage() < right.storage();
}
static DataLayoutAttribute Parse(ir::IrParser &parser) { // NOLINT
Token data_layout_token = parser.GetToken();
if (data_layout_token.val_ == "UNDEFINED") {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::UNDEFINED);
} else if ((data_layout_token.val_ == "ANY") ||
(data_layout_token.val_ == "kAnyLayout")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::ANY);
} else if ((data_layout_token.val_ == "NCHW") ||
(data_layout_token.val_ == "kNCHW")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::NCHW);
} else if ((data_layout_token.val_ == "NHWC") ||
(data_layout_token.val_ == "kNHWC")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::NHWC);
} else if ((data_layout_token.val_ == "NCDHW") ||
(data_layout_token.val_ == "kNCDHW")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::NCDHW);
} else if ((data_layout_token.val_ == "NDHWC") ||
(data_layout_token.val_ == "kNDHWC")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::NDHWC);
} else if ((data_layout_token.val_ == "ONEDNN") ||
(data_layout_token.val_ == "kMKLDNN")) {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::ONEDNN);
} else if (data_layout_token.val_ == "SPARSE_COO") {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::SPARSE_COO);
} else if (data_layout_token.val_ == "SPARSE_CSR") {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::SPARSE_CSR);
} else if (data_layout_token.val_ == "PSTRING_UNION") {
return DataLayoutAttribute::get(parser.ctx,
phi::DataLayout::PSTRING_UNION);
} else if (data_layout_token.val_ == "NUM_DATA_LAYOUTS") {
return DataLayoutAttribute::get(parser.ctx,
phi::DataLayout::NUM_DATA_LAYOUTS);
} else if (data_layout_token.val_ == "ALL_LAYOUT") {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::ALL_LAYOUT);
} else {
return DataLayoutAttribute::get(parser.ctx, phi::DataLayout::UNDEFINED);
}
}

phi::DataLayout data() const;
};
Expand Down
56 changes: 52 additions & 4 deletions paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ void PaddleDialect::PrintType(ir::Type type, std::ostream &os) const {
}

void PaddleDialect::PrintAttribute(ir::Attribute attr, std::ostream &os) const {
os << "(" << attr.dialect().name();
os << '.';
if (auto int_array_attr = attr.dyn_cast<IntArrayAttribute>()) {
phi::IntArray data = int_array_attr.data();
os << "IntArray[";
os << "IntArray)"
<< "[";
const auto &inner_data = data.GetData();
ir::PrintInterleave(
inner_data.begin(),
Expand All @@ -87,16 +90,61 @@ void PaddleDialect::PrintAttribute(ir::Attribute attr, std::ostream &os) const {
[&os]() { os << ","; });
os << "]";
} else if (auto data_type_attr = attr.dyn_cast<DataTypeAttribute>()) {
os << data_type_attr.data();
os << "DataType)" << data_type_attr.data();
} else if (auto place_type_attr = attr.dyn_cast<PlaceAttribute>()) {
os << place_type_attr.data();
os << "Place)" << place_type_attr.data();
} else if (auto data_layout_attr = attr.dyn_cast<DataLayoutAttribute>()) {
os << data_layout_attr.data();
os << "DataLayout)" << data_layout_attr.data();
} else {
os << "<#AttrNotImplemented>";
}
}

ir::Type PaddleDialect::ParseType(ir::IrParser &parser) { // NOLINT
parser.ConsumeAToken("pd.tensor");
parser.ConsumeAToken("<");
std::vector<int> dim{};
Token dim_token = parser.PeekToken();
while (dim_token.token_type_ == DIGIT) {
dim_token = parser.GetToken();
dim.push_back(atoi(dim_token.val_.c_str()));
if (parser.PeekToken().val_ != "x") {
break;
}
parser.GetToken();
if (parser.PeekToken().token_type_ != DIGIT) {
break;
}
}
phi::DDim ddim = phi::make_ddim(dim);
ir::Type dtype = parser.ParseType();
std::vector<std::vector<size_t>> lod;
std::vector<size_t> lodv;
lodv.push_back(0);
lod.push_back(lodv);
parser.ConsumeAToken(">");
return DenseTensorType::get(
parser.ctx, dtype, ddim, phi::DataLayout::UNDEFINED, lod, 0);
}

ir::Attribute PaddleDialect::ParseAttribute(ir::IrParser &parser) { // NOLINT
std::string type_name = parser.GetToken().val_;
std::string attribute_name =
type_name.substr(type_name.find('.') + 1, std::string::npos);
parser.ConsumeAToken(")");
if (attribute_name == "IntArray") {
return IntArrayAttribute::Parse(parser);
} else if (attribute_name == "DataType") {
return DataTypeAttribute::Parse(parser);
} else if (attribute_name == "Place") {
return PlaceAttribute::Parse(parser);
} else if (attribute_name == "DataLayout") {
return DataLayoutAttribute::Parse(parser);
} else {
IR_THROW("No function to parse " + attribute_name + " exists!");
}
}

} // namespace dialect
} // namespace paddle

Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PaddleDialect : public ir::Dialect {

void PrintType(ir::Type type, std::ostream& os) const;
void PrintAttribute(ir::Attribute type, std::ostream& os) const;
ir::Type ParseType(ir::IrParser& parser); // NOLINT
ir::Attribute ParseAttribute(ir::IrParser& parser); // NOLINT

private:
void initialize();
Expand Down
2 changes: 2 additions & 0 deletions paddle/ir/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ set(NEWIR_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/ir")
file(GLOB IR_SRCS "*.cc")
kangguangli marked this conversation as resolved.
Show resolved Hide resolved

ir_library(ir_core SRCS ${IR_SRCS})

add_subdirectory(parser)
10 changes: 9 additions & 1 deletion paddle/ir/core/dialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ir {

class Operation;
class IrPrinter;

class IrParser;
class DialectInterface;
///
/// \brief Dialect can basically be understood as a namespace. In Dialect, we
Expand Down Expand Up @@ -145,6 +145,14 @@ class IR_API Dialect {
IR_THROW("dialect has no registered attribute printing hook");
}

virtual Type ParseType(ir::IrParser &parser) { // NOLINT
kangguangli marked this conversation as resolved.
Show resolved Hide resolved
IR_THROW("dialect has no registered type parsing hook");
}

virtual Attribute ParseAttribute(ir::IrParser &parser) { // NOLINT
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved
IR_THROW("dialect has no registered attribute parsing hook");
}

kangguangli marked this conversation as resolved.
Show resolved Hide resolved
virtual void PrintOperation(const Operation *op,
IrPrinter &printer) const; // NOLINT

Expand Down
76 changes: 76 additions & 0 deletions paddle/ir/core/ir_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 "paddle/ir/core/ir_context.h"
#include "paddle/ir/core/operation.h"
#include "paddle/ir/core/parser/lexer.h"
#include "paddle/ir/core/program.h"

using OpResultMap = std::map<string, ir::OpResult>;
using AttributeMap = std::unordered_map<std::string, ir::Attribute>;
using OpAttributeInfoMap = std::map<string, string>;

namespace ir {
class IrParser {
public:
Lexer* lexer;
IrContext* ctx;
OpResultMap opresultmap;
Builder* builder;
Token last_token;
Token cur_token;
LexSegment lex_seg;

public:
IrParser(IrContext* ctx, std::istream& is);

~IrParser();

Token GetToken();
kangguangli marked this conversation as resolved.
Show resolved Hide resolved

Token PeekToken();
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved

std::unique_ptr<Program> ParseProgram();

std::unique_ptr<Region> ParseRegion();
kangguangli marked this conversation as resolved.
Show resolved Hide resolved

void ParseBlock(Block& block); // NOLINT

Operation* ParseOperation();

OpInfo ParseOpInfo();

std::vector<std::string> ParseOpResultIndex();

std::vector<OpResult> ParseOpRandList();
xingmingyyj marked this conversation as resolved.
Show resolved Hide resolved

AttributeMap ParseAttributeMap();

std::vector<Type> ParseFunctionTypeList();

OpResult GetNullValue();

void NextLexSegment();

Type ParseType();

Attribute ParseAttribute();

string GetErrorLocationInfo();

void ConsumeAToken(string expect_token_val);
};

} // namespace ir
Loading