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

Support selected rows new ir #54987

Merged
merged 38 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e7514d3
refine program translator
kangguangli Jun 16, 2023
3dca434
fix warning: not override
kangguangli Jun 19, 2023
fbc5d68
fix bug
kangguangli Jun 19, 2023
beeb0b3
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into refacto…
kangguangli Jun 25, 2023
7fe49a2
merge new modifications
kangguangli Jun 25, 2023
4ba7532
modify by reviews
kangguangli Jun 25, 2023
c1b0b2c
resolve conflicts
kangguangli Jun 26, 2023
2eb2ad8
resolve conflicts
kangguangli Jun 26, 2023
db54984
fix
kangguangli Jun 26, 2023
0c8983f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
kangguangli Jun 26, 2023
5344ea9
fix
kangguangli Jun 26, 2023
a8bc7f2
update
phlrain Jun 27, 2023
2526698
Merge commit 'refs/pull/54719/head' of https://github.com/PaddlePaddl…
phlrain Jun 27, 2023
49f5666
support selected rows
phlrain Jun 29, 2023
0ebd6c2
update
phlrain Jun 30, 2023
fd06a07
add selectrows
zhangbo9674 Jun 30, 2023
f21c840
fix bug
zhangbo9674 Jun 30, 2023
960f14b
add ut
zhangbo9674 Jul 1, 2023
36d9457
refine code
zhangbo9674 Jul 1, 2023
742d6e3
sovle conflict
zhangbo9674 Jul 1, 2023
bf7d2a1
solve conflict
zhangbo9674 Jul 3, 2023
7b75794
refien code
zhangbo9674 Jul 3, 2023
eb41565
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 3, 2023
c33099a
Merge commit 'refs/pull/55041/head' of https://github.com/PaddlePaddl…
phlrain Jul 3, 2023
6bf20a2
update
phlrain Jul 3, 2023
2c50f8d
update
phlrain Jul 3, 2023
5c18036
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 3, 2023
3b6efd6
support selected rows
phlrain Jul 4, 2023
f5ee2c9
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 4, 2023
584537d
support selected rows
phlrain Jul 4, 2023
9dd2eeb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 4, 2023
be347c2
support dense tensor
phlrain Jul 4, 2023
c13179d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 6, 2023
e16139e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 10, 2023
4d80947
remove useless code
phlrain Jul 10, 2023
3a6016a
polish code
phlrain Jul 10, 2023
078b685
remote standalone executor test
phlrain Jul 11, 2023
c2e1641
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
phlrain Jul 11, 2023
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
1 change: 1 addition & 0 deletions paddle/fluid/ir/dialect/kernel_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ PaddleKernelDialect::PaddleKernelDialect(ir::IrContext *context)

void PaddleKernelDialect::initialize() {
RegisterTypes<paddle::dialect::AllocatedDenseTensorType>();
RegisterTypes<paddle::dialect::AllocatedSelectedRowsType>();
RegisterOps<dialect::PhiKernelOp>();

RegisterAttributes<paddle::dialect::KernelAttribute>();
Expand Down
25 changes: 25 additions & 0 deletions paddle/fluid/ir/dialect/kernel_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,32 @@ const size_t& AllocatedDenseTensorType::offset() const {
return storage()->dense_tensor_type_.offset();
}

const phi::Place& AllocatedSelectedRowsType::place() const {
return storage()->place_;
}

const ir::Type& AllocatedSelectedRowsType::dtype() const {
return storage()->selected_rows_type_.dtype();
}

const phi::DDim& AllocatedSelectedRowsType::dims() const {
return storage()->selected_rows_type_.dims();
}

const phi::DataLayout& AllocatedSelectedRowsType::data_layout() const {
return storage()->selected_rows_type_.data_layout();
}

const phi::LoD& AllocatedSelectedRowsType::lod() const {
return storage()->selected_rows_type_.lod();
}

const size_t& AllocatedSelectedRowsType::offset() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于size_t、int、double这种数据类型,直接返回copy就可以,在函数里也是直接传值的

return storage()->selected_rows_type_.offset();
}

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType)
IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedSelectedRowsType)
46 changes: 43 additions & 3 deletions paddle/fluid/ir/dialect/kernel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

namespace paddle {
namespace dialect {
///
/// \brief Define built-in parametric types.
///

class AllocatedDenseTensorType : public ir::Type {
public:
using Type::Type;
Expand Down Expand Up @@ -64,7 +62,49 @@ class AllocatedDenseTensorType : public ir::Type {
const size_t &offset() const;
};

class AllocatedSelectedRowsType : public ir::Type {
public:
using Type::Type;

DECLARE_TYPE_UTILITY_FUNCTOR(AllocatedSelectedRowsType,
AllocatedSelectedRowsTypeStorage);

static AllocatedSelectedRowsType get(ir::IrContext *ctx,
const phi::Place &place,
dialect::SelectedRowsType type) {
return ir::TypeManager::template get<AllocatedSelectedRowsType>(
ctx, place, type);
}

static AllocatedSelectedRowsType get(ir::IrContext *ctx,
const phi::Place &place,
const ir::Type &dtype,
const phi::DDim &dims,
const phi::DataLayout &layout,
const phi::LoD &lod,
size_t offset) {
dialect::SelectedRowsType dense_tensor_type =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dialect::SelectedRowsType dense_tensor_type =
dialect::SelectedRowsType type =

这里copy后没有改过来吧?

dialect::SelectedRowsType::get(ctx, dtype, dims, layout, lod, offset);

return ir::TypeManager::template get<AllocatedSelectedRowsType>(
ctx, place, dense_tensor_type);
}

const phi::Place &place() const;

const ir::Type &dtype() const;

const phi::DDim &dims() const;

const phi::DataLayout &data_layout() const;

const phi::LoD &lod() const;

const size_t &offset() const;
};

} // namespace dialect
} // namespace paddle

IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedDenseTensorType)
IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AllocatedSelectedRowsType)
62 changes: 62 additions & 0 deletions paddle/fluid/ir/dialect/kernel_type_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,67 @@ struct AllocatedDenseTensorTypeStorage : public ir::TypeStorage {
dialect::DenseTensorType dense_tensor_type_;
};

///
/// \brief Define Parametric TypeStorage for AllocatedSelectedRowsTypeStorage.
///
///
struct AllocatedSelectedRowsTypeStorage : public ir::TypeStorage {
using Place = phi::Place;
///
/// \brief Declare ParamKey according to parameter type.
///
using ParamKey = std::tuple<phi::Place, dialect::SelectedRowsType>;

AllocatedSelectedRowsTypeStorage(const phi::Place& place,
const dialect::SelectedRowsType& type)
: place_(place), selected_rows_type_(type) {}

///
/// \brief Each derived TypeStorage must define a Construct method, which
/// StorageManager uses to construct a derived TypeStorage.
///
static AllocatedSelectedRowsTypeStorage* Construct(const ParamKey& key) {
return new AllocatedSelectedRowsTypeStorage(std::get<0>(key),
std::get<1>(key));
}

///
/// \brief Each derived TypeStorage must provide a HashValue method.
///
static std::size_t HashValue(const ParamKey& key) {
std::size_t hash_value = 791;
// hash place
hash_value = ir::hash_combine(hash_value, std::get<0>(key).HashValue());

// hash dtype
auto selected_rows_type = std::get<1>(key);
hash_value = ir::hash_combine(hash_value,
dialect::DenseTensorTypeStorage::HashValue(
dialect::DenseTensorTypeStorage::ParamKey(
selected_rows_type.dtype(),
selected_rows_type.dims(),
selected_rows_type.data_layout(),
selected_rows_type.lod(),
selected_rows_type.offset())));
return hash_value;
}

///
/// \brief Each derived TypeStorage needs to overload operator==.
///
bool operator==(const ParamKey& key) const {
return ParamKey(place_, selected_rows_type_) == key;
}

ParamKey GetAsKey() const { return ParamKey(place_, selected_rows_type_); }

///
/// \brief AllocatedSelectedRowsTypeStorage include five parameters: place,
/// SelectedRowsType
///
phi::Place place_;
dialect::SelectedRowsType selected_rows_type_;
};

} // namespace dialect
} // namespace paddle
24 changes: 16 additions & 8 deletions paddle/fluid/ir/dialect/pd_dialect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,23 @@ void PaddleDialect::initialize() {
}

void PaddleDialect::PrintType(ir::Type type, std::ostream &os) const {
DenseTensorType tensor_type = type.dyn_cast<DenseTensorType>();

os << "tensor<";
for (auto d : phi::vectorize(tensor_type.dims())) {
os << d;
os << "x";
if (auto tensor_type = type.dyn_cast<DenseTensorType>()) {
os << "tensor<";
for (auto d : phi::vectorize(tensor_type.dims())) {
os << d;
os << "x";
}
tensor_type.dtype().Print(os);
os << ">";
} else if (auto selected_rows_type = type.dyn_cast<SelectedRowsType>()) {
os << "selectedrows<";
for (auto d : phi::vectorize(selected_rows_type.dims())) {
os << d;
os << "x";
}
selected_rows_type.dtype().Print(os);
os << ">";
}
tensor_type.dtype().Print(os);
os << ">";
}

void PaddleDialect::PrintAttribute(ir::Attribute attr, std::ostream &os) const {
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/ir/dialect/pd_op.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@
no_need_buffer: null
data_transform: null
infer_meta:
func: UnchangedInferMeta
func: EmbeddingGradSparseInferMeta
param: [weight]
kernel:
func: [embedding_grad_sparse]
func: [embedding_sparse_grad]
param: [x, weight, out_grad, padding_idx, sparse]
backend: null
layout: null
data_type:
ordered: false
candidates: [weight]
to_complex_flag: [false]
dispatch: {embedding_grad_sparse: null}
dispatch: {embedding_sparse_grad: null}
force_backend: null
inplace: null
view: null
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void BuildValue(ir::Value value,
var->GetMutable<phi::DenseTensor>();
} else if (value.type().isa<paddle::dialect::AllocatedDenseTensorType>()) {
var->GetMutable<phi::DenseTensor>();
} else if (value.type().isa<paddle::dialect::AllocatedSelectedRowsType>()) {
var->GetMutable<phi::SelectedRows>();
} else if (value.type().isa<ir::VectorType>()) {
auto tensor_array = var->GetMutable<paddle::framework::TensorRefArray>();
for (size_t i = 0; i < value.type().dyn_cast<ir::VectorType>().size();
Expand Down
32 changes: 28 additions & 4 deletions paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "paddle/phi/core/kernel_context.h"

#include "paddle/fluid/ir/dialect/kernel_attribute.h"
#include "paddle/fluid/ir/dialect/kernel_type.h"
#include "paddle/fluid/ir/dialect/pd_attribute.h"
#include "paddle/fluid/ir/interface/op_yaml_info_parser.h"
#include "paddle/phi/core/infermeta_utils.h"
Expand Down Expand Up @@ -238,6 +239,23 @@ void BuildPhiContext(
} else if (attr_type_name == "ir::ArrayAttribute<ir::Int64Attribute>") {
auto array_list = attr_map[t].dyn_cast<ir::ArrayAttribute>().data();

std::vector<int64_t> vec_res;
if (array_list.size() > 0) {
PADDLE_ENFORCE_EQ(
array_list[0].isa<ir::Int64Attribute>(),
true,
phi::errors::PreconditionNotMet(
"Element in array list MUST be ir::Int64Attribute "));

for (size_t i = 0; i < array_list.size(); ++i) {
vec_res.push_back(
array_list[i].dyn_cast<ir::Int64Attribute>().data());
}
}
ctx->EmplaceBackAttr(vec_res);
} else if (attr_type_name == "ir::ArrayAttribute<ir::Int64Attribute>") {
auto array_list = attr_map[t].dyn_cast<ir::ArrayAttribute>().data();

std::vector<int64_t> vec_res;
if (array_list.size() > 0) {
PADDLE_ENFORCE_EQ(
Expand Down Expand Up @@ -280,13 +298,19 @@ void BuildPhiContext(
for (size_t i = 0; i < op->num_results(); ++i) {
ir::Value out_ptr = op->result(i);
auto name = name_map.at(out_ptr);
if (out_ptr.type()) {
ctx->EmplaceBackOutput(OutType(const_cast<phi::DenseTensor*>(
&(inner_scope->FindVar(name)->Get<phi::DenseTensor>()))));
} else {
auto out_type = out_ptr.type();
if (!out_type) {
phi::DenseTensor* ptr = nullptr;
OutType out_ptr(ptr);
ctx->EmplaceBackOutput(out_ptr);
} else if (out_type.isa<paddle::dialect::AllocatedDenseTensorType>()) {
ctx->EmplaceBackOutput(OutType(const_cast<phi::DenseTensor*>(
&(scope->Var(name)->Get<phi::DenseTensor>()))));
} else if (out_type.isa<paddle::dialect::AllocatedSelectedRowsType>()) {
ctx->EmplaceBackOutput(OutType(const_cast<phi::SelectedRows*>(
&(scope->Var(name)->Get<phi::SelectedRows>()))));
} else {
PADDLE_THROW("not support type");
}

if (output_map != nullptr) {
Expand Down
9 changes: 9 additions & 0 deletions paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ std::unique_ptr<ir::Program> PdOpLowerToKernelPass(ir::Program* prog) {

ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types);
op_output_types.push_back(t1);
} else if (result_type.isa<dialect::SelectedRowsType>()) {
auto allocated_selected_rows_dtype =
paddle::dialect::AllocatedSelectedRowsType::get(
ctx,
phi::TransToPhiPlace(kernel_key.backend()),
result_type.dyn_cast<dialect::SelectedRowsType>());
op_output_types.push_back(allocated_selected_rows_dtype);
} else {
PADDLE_THROW(phi::errors::Unimplemented(
"Result type only support DenseTensorType and VectorType"));
Expand Down Expand Up @@ -322,6 +329,8 @@ std::unique_ptr<ir::Program> PdOpLowerToKernelPass(ir::Program* prog) {
}
} else if (new_in_type.isa<ir::VectorType>()) {
// [ todo need update here, support combine data transfomer]
} else if (new_in_type.isa<dialect::AllocatedSelectedRowsType>()) {
// do nothing here
} else {
PADDLE_THROW(phi::errors::Unimplemented(
"only support allocated dense tensor type for now"));
Expand Down
36 changes: 36 additions & 0 deletions paddle/fluid/ir_adaptor/translator/op_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,41 @@ struct RnnOpTranscriber : public OpTranscriber {
};
};

struct EmbeddingGradOpTranscriber : public OpTranscriber {
void HandleNonexistentAttribute(ir::IrContext* ctx,
ir::AttributeMap* attribute_map,
const OpAttributeInfo& info) override {
if (info.name == "padding_idx") {
(*attribute_map)[info.name] = ir::Int64Attribute::get(ctx, -1);
} else if (info.name == "sparse") {
(*attribute_map)[info.name] = ir::BoolAttribute::get(ctx, false);
}
}

ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) override {
std::string target_op_name =
kTargetDialectPrefix + OpNameCompatibleMapping(op_desc.Type());

bool is_sparse = paddle::get<bool>(op_desc.GetAttr("is_sparse"));

if (is_sparse) {
target_op_name = "pd.embedding_grad_sparse";
} else {
target_op_name = "pd.embedding_grad_dense";
}
VLOG(6) << "[op name normalizing: " << op_desc.Type() << " to "
<< target_op_name;
auto op_info = ctx->GetRegisteredOpInfo(target_op_name);
if (!op_info) {
IR_THROW("Op %d should have corresponding OpInfo %d",
op_desc.Type(),
target_op_name);
}

return op_info;
}
};

struct FeedOpTranscriber : public OpTranscriber {
ir::AttributeMap TranslateOpAttribute(
ir::IrContext* ctx,
Expand Down Expand Up @@ -959,6 +994,7 @@ OpTranslator::OpTranslator() {
special_handlers["fetch_v2"] = FetchOpTranscriber();
special_handlers["cast"] = CastOpTranscriber();
special_handlers["lookup_table_v2"] = EmbeddingOpTranscriber();
special_handlers["lookup_table_v2_grad"] = EmbeddingGradOpTranscriber();
special_handlers["assign_value"] = AssignValueOpTranscriber();
special_handlers["increment"] = IncrementOpTranscriber();
special_handlers["rnn"] = RnnOpTranscriber();
Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/ir_adaptor/translator/program_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void ProgramTranslator::Translate() {
platform::errors::PreconditionNotMet(
"Not support multi block ProgramDesc translated, now has %d blocks",
legacy_program_->Size()));

for (size_t block_idx = 0; block_idx < legacy_program_->Size(); block_idx++) {
const BlockDesc& block = legacy_program_->Block(block_idx);
GetParameterForSingleBlock(block);
Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/ir_adaptor/translator/translate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ std::unique_ptr<Program> TranslateLegacyProgramToProgram(
ir::IrContext* ctx = ir::IrContext::Instance();
ctx->GetOrRegisterDialect<dialect::PaddleDialect>();
auto program = std::make_unique<Program>(ctx);

translator::ProgramTranslator program_translator(&legacy_program,
program.get());
program_translator.Translate();
Expand Down
Loading