Skip to content

Commit

Permalink
support optional args (PaddlePaddle#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
gglin001 authored Sep 23, 2021
1 parent fdffcf2 commit 6beb6a7
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 515 deletions.
150 changes: 46 additions & 104 deletions paddle/fluid/framework/ipu/ipu_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ namespace ipu {

template <typename T>
T GetAttrAllowNull(std::string attr, OpDesc* op_desc) {
std::string type = typeid(T).name();
VLOG(10) << "body attr type is: " << type << " body attr name is: " << attr;
if (op_desc->HasAttr(attr)) {
return BOOST_GET_CONST(T, op_desc->GetAttr(attr));
} else {
VLOG(10) << "body attr not exist: " << type;
return {};
}
}

template <typename T>
nonstd::optional<T> GetOptAttrAllowNull(std::string attr, OpDesc* op_desc) {
if (op_desc->HasAttr(attr)) {
return BOOST_GET_CONST(T, op_desc->GetAttr(attr));
} else {
return nonstd::optional<T>();
}
}

Compiler::Compiler() {
builder_ = popart::Builder::create();
RegisterOpFunc();
Expand All @@ -52,6 +58,7 @@ void Compiler::RegisterOpFunc() {
#define NONE

#define ARG(Type, Name) , GetAttrAllowNull<Type>(#Name, op_desc)
#define OPT_ARG(Type, Name) , GetOptAttrAllowNull<Type>(#Name, op_desc)
#define POPART_CONST_ARG(Name) , const PopartConstant& Name
#define HOST_SIDE_CONST_ARG(Name) , const HostSideConstant& Name
#define POPART_ATTRIB_VEC_ARG(Name)
Expand All @@ -65,21 +72,21 @@ void Compiler::RegisterOpFunc() {
auto inputs = GetOpInputs(op_desc); \
auto output_names = GetOpOutputs(op_desc); \
auto debug_context = BuildDebugContext(op_desc); \
auto aiOnnxOpset1 = builder_->aiGraphcoreOpset1(); \
auto aiGraphcoreOpset = builder_->aiGraphcoreOpset1(); \
auto aiOnnxOpset = builder_->aiOnnxOpset11(); \
auto output_ids = OnnxImpl(inputs Args, debug_context); \
SetIpuIndexStage(output_ids, op_desc); \
InsertTensors(output_names, output_ids); \
}}, // NOLINT
#include "paddle/fluid/framework/ipu/supported_ops_autogen.h"
#include "paddle/fluid/framework/ipu/supported_ops.h"
};

#undef OP_DECL
// #undef OP_DECL_NO_RETURN
#undef BODY_ARG
#undef POPART_ATTRIB_VEC_ARG
#undef HOST_SIDE_CONST_ARG
#undef POPART_CONST_ARG
#undef OPT_ARG
#undef ARG
#undef NONE
#undef STRING_VEC
Expand All @@ -89,20 +96,6 @@ void Compiler::RegisterOpFunc() {
#undef FLOAT
#undef FLOAT_VEC
#undef INT_VEC

// // self register ops
// #include "paddle/fluid/framework/ipu/supported_ops_custom.h"
// name_function_.emplace("popart_reducemean", ReduceMeanHandler);
// name_function_.emplace("popart_batchnormalization", BatchNormHandler);
// name_function_.emplace("popart_constant", Constant);
// name_function_.emplace("popart_nllloss", NllLoss);
// name_function_.emplace("popart_groupnormalization", Groupnormalization);

// // used for debug
// for (auto elem : name_function_) {
// VLOG(10) << "registered in map : " << elem.first << " second "
// << &(elem.second);
// }
}

void Compiler::LowerBody(const ir::Graph* graph) {
Expand Down Expand Up @@ -150,61 +143,6 @@ void Compiler::LowerBody(const ir::Graph* graph) {
popart::TensorId result = builder_->aiOnnxOpset11().constant(*const_data);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_reducemean") {
auto inputs = GetOpInputs(op_desc);
auto axes = nonstd::optional<std::vector<int64_t>>();
if (op_desc->HasAttr("axes")) {
axes = BOOST_GET_CONST(std::vector<int64_t>, op_desc->GetAttr("axes"));
}
auto keepdims = BOOST_GET_CONST(int64_t, op_desc->GetAttr("keepdims"));
popart::TensorId result =
builder_->aiOnnxOpset11().reducemean(inputs, axes, keepdims);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_reducemin") {
auto inputs = GetOpInputs(op_desc);
auto axes = nonstd::optional<std::vector<int64_t>>();
if (op_desc->HasAttr("axes")) {
axes = BOOST_GET_CONST(std::vector<int64_t>, op_desc->GetAttr("axes"));
}
auto keepdims = BOOST_GET_CONST(int64_t, op_desc->GetAttr("keepdims"));
popart::TensorId result =
builder_->aiOnnxOpset11().reducemin(inputs, axes, keepdims);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_reducemax") {
auto inputs = GetOpInputs(op_desc);
auto axes = nonstd::optional<std::vector<int64_t>>();
if (op_desc->HasAttr("axes")) {
axes = BOOST_GET_CONST(std::vector<int64_t>, op_desc->GetAttr("axes"));
}
auto keepdims = BOOST_GET_CONST(int64_t, op_desc->GetAttr("keepdims"));
popart::TensorId result =
builder_->aiOnnxOpset11().reducemax(inputs, axes, keepdims);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_reducesum") {
auto inputs = GetOpInputs(op_desc);
auto axes = nonstd::optional<std::vector<int64_t>>();
if (op_desc->HasAttr("axes")) {
axes = BOOST_GET_CONST(std::vector<int64_t>, op_desc->GetAttr("axes"));
}
auto keepdims = BOOST_GET_CONST(int64_t, op_desc->GetAttr("keepdims"));
popart::TensorId result =
builder_->aiOnnxOpset11().reducesum(inputs, axes, keepdims);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_reduceprod") {
auto inputs = GetOpInputs(op_desc);
auto axes = nonstd::optional<std::vector<int64_t>>();
if (op_desc->HasAttr("axes")) {
axes = BOOST_GET_CONST(std::vector<int64_t>, op_desc->GetAttr("axes"));
}
auto keepdims = BOOST_GET_CONST(int64_t, op_desc->GetAttr("keepdims"));
popart::TensorId result =
builder_->aiOnnxOpset11().reduceprod(inputs, axes, keepdims);
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_batchnormalization") {
auto inputs = GetOpInputs(op_desc);
auto outputs = GetOpOutputs(op_desc);
Expand All @@ -223,35 +161,39 @@ void Compiler::LowerBody(const ir::Graph* graph) {
SetIpuIndexStage(result, op_desc);
InsertTensors(GetOpOutputs(op_desc), result);
} else if (op_type == "popart_topk") {
auto inputs = GetOpInputs(op_desc);
auto outputs= GetOpOutputs(op_desc);
int64_t axis = BOOST_GET_CONST(int64_t, op_desc->GetAttr("axis"));
int sorted_INT32 = BOOST_GET_CONST(int, op_desc->GetAttr("sorted"));
int64_t sorted = int64_t{sorted_INT32};

auto aiOnnxOpset = builder_->aiOnnxOpset11();

popart::ConvInputs result;
if (inputs.size() == 2) {
VLOG(10) << "[Compiler::LowerBody] size of inputs for <popart_topk> is 2";
result = aiOnnxOpset.topk(inputs, axis, sorted);
} else if (inputs.size() == 1) {
VLOG(10) << "[Compiler::LowerBody] size of inputs for <popart_topk> is 1";
int64_t k = BOOST_GET_CONST(int64_t, op_desc->GetAttr("k"));
// TODO(yiakwy) : reference to Opset10, 11 API
popart::TensorInfo kShape{"INT64", std::vector<int64_t>{1}};
popart::ConstVoidData kData = {&k, kShape};
auto K_t = aiOnnxOpset.constant(kData);
result = aiOnnxOpset.topk({inputs[0], K_t}, axis, sorted);
}
result[1] = aiOnnxOpset.cast({result[1]}, "INT32");
SetIpuIndexStage(result, op_desc);
VLOG(10) << "[Compiler::LowerBody] output[1]: " << outputs[1];
VLOG(10) << "[Compiler::LowerBody] output[1]: " << GetOpOutputs(op_desc)[1] << " -> " << result[1];
tensors_.emplace(GetOpOutputs(op_desc)[1], result[1]); // topk indices
VLOG(10) << "[Compiler::LowerBody] output[0]: " << outputs[0];
VLOG(10) << "[Compiler::LowerBody] output[0]: " << GetOpOutputs(op_desc)[0] << " -> " << result[0];
tensors_.emplace(GetOpOutputs(op_desc)[0], result[0]); // topk values
auto inputs = GetOpInputs(op_desc);
auto outputs = GetOpOutputs(op_desc);
int64_t axis = BOOST_GET_CONST(int64_t, op_desc->GetAttr("axis"));
int sorted_INT32 = BOOST_GET_CONST(int, op_desc->GetAttr("sorted"));
int64_t sorted = int64_t{sorted_INT32};

auto aiOnnxOpset = builder_->aiOnnxOpset11();

popart::ConvInputs result;
if (inputs.size() == 2) {
VLOG(10)
<< "[Compiler::LowerBody] size of inputs for <popart_topk> is 2";
result = aiOnnxOpset.topk(inputs, axis, sorted);
} else if (inputs.size() == 1) {
VLOG(10)
<< "[Compiler::LowerBody] size of inputs for <popart_topk> is 1";
int64_t k = BOOST_GET_CONST(int64_t, op_desc->GetAttr("k"));
// TODO(yiakwy) : reference to Opset10, 11 API
popart::TensorInfo kShape{"INT64", std::vector<int64_t>{1}};
popart::ConstVoidData kData = {&k, kShape};
auto K_t = aiOnnxOpset.constant(kData);
result = aiOnnxOpset.topk({inputs[0], K_t}, axis, sorted);
}
result[1] = aiOnnxOpset.cast({result[1]}, "INT32");
SetIpuIndexStage(result, op_desc);
VLOG(10) << "[Compiler::LowerBody] output[1]: " << outputs[1];
VLOG(10) << "[Compiler::LowerBody] output[1]: "
<< GetOpOutputs(op_desc)[1] << " -> " << result[1];
tensors_.emplace(GetOpOutputs(op_desc)[1], result[1]); // topk indices
VLOG(10) << "[Compiler::LowerBody] output[0]: " << outputs[0];
VLOG(10) << "[Compiler::LowerBody] output[0]: "
<< GetOpOutputs(op_desc)[0] << " -> " << result[0];
tensors_.emplace(GetOpOutputs(op_desc)[0], result[0]); // topk values
} else {
auto itr = name_function_.find(op_type);
if (itr != name_function_.end()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Node *sqrt_handler(Graph *graph, Node *node) {
}

Node *gelu_handler(Graph *graph, Node *node) {
return activation_op_handler(graph, node, "popart_gelu");
return activation_op_handler(graph, node, "popart_gelu_v2");
}

Node *log_softmax_handler(Graph *graph, Node *node) {
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/framework/ipu/popart_canonicalization/nn_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Node *group_norm_handler(Graph *graph, Node *node) {
std::vector<Node *> outputs_ = {GetOutputVarNode("Y", node),
GetOutputVarNode("Mean", node),
GetOutputVarNode("Variance", node)};
return CreateBaseOp(graph, node, "popart_groupnormalization", inputs_,
return CreateBaseOp(graph, node, "popart_groupnormalization_v2", inputs_,
outputs_, attrs_);
}

Expand Down Expand Up @@ -206,7 +206,7 @@ Node *layer_norm_handler(Graph *graph, Node *node) {
auto groupnorm_attrs_ =
AttributeMap{{"epsilon", epsilon_}, {"num_groups", groups_}};
auto out_Y_ = MakeVarNode(graph, node);
CreateBaseOp(graph, node, "popart_groupnormalization",
CreateBaseOp(graph, node, "popart_groupnormalization_v2",
{new_node_reshape1->outputs[0], GetInputVarNode("Scale", node),
GetInputVarNode("Bias", node)},
{out_Y_, GetOutputVarNode("Mean", node),
Expand Down
Loading

0 comments on commit 6beb6a7

Please sign in to comment.