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 all 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
105 changes: 104 additions & 1 deletion paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace paddle {
namespace dialect {
const phi::IntArray& IntArrayAttribute::data() const {
const phi::IntArray &IntArrayAttribute::data() const {
return storage()->GetAsKey();
}

Expand Down Expand Up @@ -48,6 +48,109 @@ phi::Scalar ScalarAttribute::data() {
}
}

IntArrayAttribute IntArrayAttribute::Parse(ir::IrParser &parser) { // NOLINT
Token buket_token = parser.ConsumeToken();
std::vector<int32_t> vec{};
while (parser.PeekToken().val_ != "]") {
Token val_token = parser.ConsumeToken();
vec.push_back(atoll(val_token.val_.c_str()));
if (parser.PeekToken().val_ == "]") break;
parser.ConsumeToken();
}
parser.ConsumeToken();
return IntArrayAttribute::get(parser.ctx, vec);
}

// Parse a DataTypeAttribute
// DataTypeAttribute := bool|uint8|int8|uint16|int16|uint32
// |int32|uint64|int64|float32|complex64
// |complex128|Undefined|psting|flaot16
// |bfloat16|num_data_types|all_dtype
DataTypeAttribute DataTypeAttribute::Parse(ir::IrParser &parser) { // NOLINT
std::unordered_map<std::string, phi::DataType> StringToDataType{
{"bool", phi::DataType::BOOL},
{"uint8", phi::DataType::UINT8},
{"int8", phi::DataType::INT8},
{"uint16", phi::DataType::UINT16},
{"int16", phi::DataType::INT16},
{"uint32", phi::DataType::UINT32},
{"int32", phi::DataType::INT32},
{"uint64", phi::DataType::UINT64},
{"int64", phi::DataType::INT64},
{"float32", phi::DataType::FLOAT32},
{"complex64", phi::DataType::COMPLEX64},
{"complex128", phi::DataType::COMPLEX128},
{"Undefined", phi::DataType::UNDEFINED},
{"psting", phi::DataType::PSTRING},
{"float16", phi::DataType::FLOAT16},
{"bfloat16", phi::DataType::BFLOAT16},
{"float64", phi::DataType::FLOAT64}};
std::string datatype_token_val = parser.ConsumeToken().val_;
IR_ENFORCE(StringToDataType.count(datatype_token_val) > 0,
datatype_token_val + " is not defined in DataType." +
parser.GetErrorLocationInfo());
return DataTypeAttribute::get(parser.ctx,
StringToDataType[datatype_token_val]);
}

// Parse a PlaceAttribute
// PlaceAttribute := Place(cpu)|Place(gpu:0)|Place(gpu_pinned)
// |Place(xpu:0)|Place(ipu:0)|Place(:0)|undefined
PlaceAttribute PlaceAttribute::Parse(ir::IrParser &parser) { // NOLINT
std::unordered_map<std::string, phi::Place> StringToPlace{
{"cpu", phi::CPUPlace{}},
{"gpu", phi::GPUPlace{}},
{"gpu_pinned", phi::GPUPinnedPlace{}},
{"xpu", phi::XPUPlace{}},
{"ipu", phi::IPUPlace{}},
{":", phi::CustomPlace{}},
{"undefined", phi::Place{}}};
parser.ConsumeAToken("Place");
parser.ConsumeAToken("(");
std::string place_token_val = parser.ConsumeToken().val_;
IR_ENFORCE(StringToPlace.count(place_token_val) > 0,
place_token_val + " is not defined in Place." +
parser.GetErrorLocationInfo());
if (parser.PeekToken().val_ == ":") {
parser.ConsumeAToken(":");
parser.ConsumeToken();
} else if (place_token_val == ":") {
parser.ConsumeToken();
}
parser.ConsumeAToken(")");
return PlaceAttribute::get(parser.ctx, StringToPlace[place_token_val]);
}

// Parse a DataLayoutAttribute
// DataLayoutAttribute := NHWC|NCHW|Undefined(0)|ONEDNN
// |SPARSE_COO|SPARSE_CSR|NDHWC
// |NCDHW|PSTRING_UNION|STRIDED
DataLayoutAttribute DataLayoutAttribute::Parse(
ir::IrParser &parser) { // NOLINT
std::unordered_map<std::string, phi::DataLayout> StringToDataLayout{
{"NHWC", phi::DataLayout::kNHWC},
{"NCHW", phi::DataLayout::kNCHW},
{"Undefined", phi::DataLayout::kAnyLayout},
{"ONEDNN", phi::DataLayout::ONEDNN},
{"SPARSE_COO", phi::DataLayout::SPARSE_COO},
{"SPARSE_CSR", phi::DataLayout::SPARSE_CSR},
{"NDHWC", phi::DataLayout::kNDHWC},
{"NCDHW", phi::DataLayout::kNCDHW},
{"PSTRING_UNION", phi::DataLayout::PSTRING_UNION},
{"STRIDED", phi::DataLayout::STRIDED}};
std::string datalayout_token_val = parser.ConsumeToken().val_;
IR_ENFORCE(StringToDataLayout.count(datalayout_token_val) > 0,
datalayout_token_val + " is not defined in DataLayout." +
parser.GetErrorLocationInfo());
if (datalayout_token_val == "Undefined") {
parser.ConsumeAToken("(");
parser.ConsumeAToken("AnyLayout");
parser.ConsumeAToken(")");
}
return DataLayoutAttribute::get(parser.ctx,
StringToDataLayout[datalayout_token_val]);
}

} // namespace dialect
} // namespace paddle

Expand Down
8 changes: 8 additions & 0 deletions paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.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 @@ -34,6 +35,8 @@ class IntArrayAttribute : public ir::Attribute {
return storage() < right.storage();
}

static IntArrayAttribute Parse(ir::IrParser &parser); // NOLINT

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

Expand Down Expand Up @@ -68,6 +71,8 @@ class DataTypeAttribute : public ir::Attribute {
return storage() < right.storage();
}

static DataTypeAttribute Parse(ir::IrParser &parser); // NOLINT

phi::DataType data() const;
};

Expand All @@ -81,6 +86,8 @@ class PlaceAttribute : public ir::Attribute {
return storage() < right.storage();
}

static PlaceAttribute Parse(ir::IrParser &parser); // NOLINT

phi::Place data() const;
};

Expand All @@ -95,6 +102,7 @@ class DataLayoutAttribute : public ir::Attribute {
return storage() < right.storage();
}

static DataLayoutAttribute Parse(ir::IrParser &parser); // NOLINT
phi::DataLayout data() const;
};

Expand Down
59 changes: 55 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 @@ -80,9 +80,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 @@ -91,16 +94,64 @@ 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.ConsumeToken();
dim.push_back(atoi(dim_token.val_.c_str()));
std::string peek_token_val = parser.PeekToken().val_;
if (peek_token_val[0] != 'x') {
break;
}
parser.ConsumeToken();
parser.lexer->Unget(peek_token_val.size() - 1);
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.ConsumeToken().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!" +
parser.GetErrorLocationInfo());
}
}

void PaddleDialect::PrintOperation(ir::Operation *op,
ir::IrPrinter &printer) const {
if (auto if_op = op->dyn_cast<IfOp>()) {
Expand Down
3 changes: 3 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 @@ -25,6 +25,9 @@ class PaddleDialect : public ir::Dialect {

static const char* name() { return "pd"; }

ir::Type ParseType(ir::IrParser& parser) override; // NOLINT
ir::Attribute ParseAttribute(ir::IrParser& parser) override; // NOLINT

void PrintType(ir::Type type, std::ostream& os) const override;
void PrintAttribute(ir::Attribute type, std::ostream& os) const override;

Expand Down
4 changes: 4 additions & 0 deletions paddle/ir/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ set(NEWIR_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/ir")

file(GLOB IR_SRCS "*.cc")
kangguangli marked this conversation as resolved.
Show resolved Hide resolved

file(GLOB IR_PARSER_SRCS "parser/*.cc")

list(APPEND IR_SRCS ${IR_PARSER_SRCS})

ir_library(ir_core SRCS ${IR_SRCS} DEPS ddim)
2 changes: 2 additions & 0 deletions paddle/ir/core/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class IR_API Attribute {
/// @param os
void Print(std::ostream &os) const;

static Attribute Parse(std::istream &is, IrContext *ctx);

///
/// \brief Methods for type judgment and cast.
///
Expand Down
14 changes: 13 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,9 +145,21 @@ class IR_API Dialect {
IR_THROW("dialect has no registered attribute printing hook");
}

virtual Type ParseType(IrParser &parser) { // NOLINT
IR_THROW("dialect has no registered type parsing hook");
}

virtual Attribute ParseAttribute(IrParser &parser) { // NOLINT
IR_THROW("dialect has no registered attribute parsing hook");
}

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

virtual Operation ParseOperation(IrParser &parser) { // NOLINT
IR_THROW("dialect has no registered operation parsing hook");
}

private:
Dialect(const Dialect &) = delete;

Expand Down
71 changes: 71 additions & 0 deletions paddle/ir/core/ir_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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<std::string, ir::OpResult>;
using AttributeMap = std::unordered_map<std::string, ir::Attribute>;
using OpAttributeInfoMap = std::map<std::string, std::string>;

namespace ir {
class IrParser {
public:
std::unique_ptr<Lexer> lexer;
IrContext* ctx;
OpResultMap opresultmap;
std::unique_ptr<Builder> builder;

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

~IrParser() = default;
kangguangli marked this conversation as resolved.
Show resolved Hide resolved

Token ConsumeToken();

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

std::unique_ptr<Program> ParseProgram();

void ParseRegion(Region& region); // NOLINT

void ParseBlock(Block& block); // NOLINT

Operation* ParseOperation();

OpInfo ParseOpInfo();

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

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

AttributeMap ParseAttributeMap();

std::vector<Type> ParseTypeList();

OpResult GetNullValue();

Type ParseType();

Attribute ParseAttribute();

std::string GetErrorLocationInfo();

void ConsumeAToken(std::string expect_token_val);
};

} // namespace ir
Loading