Skip to content

Commit

Permalink
Split canonical ops (PaddlePaddle#43)
Browse files Browse the repository at this point in the history
* move popart_canonicalization files
* split popart_canonicalization ops
  • Loading branch information
gglin001 committed Aug 10, 2021
1 parent ee3af7d commit 7234141
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 111 deletions.
13 changes: 9 additions & 4 deletions paddle/fluid/framework/ipu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
set(POPART_CANONICALIZATION_HANDLERS_SRC
"popart_canonicalization/other_ops.cpp"
set(POPART_CANONICALIZATION_SRC
"popart_canonicalization/canonicalization_utils.cc"
"popart_canonicalization/activation_ops.cc"
"popart_canonicalization/logic_ops.cc"
"popart_canonicalization/math_ops.cc"
"popart_canonicalization/nn_ops.cc"
"popart_canonicalization/tensor_ops.cc"
"popart_canonicalization/other_ops.cc"
)
cc_library(popart_canonicalization_utils SRCS popart_canonicalization_utils.cc
${POPART_CANONICALIZATION_HANDLERS_SRC} DEPS framework_proto enforce)
cc_library(popart_canonicalization_utils SRCS ${POPART_CANONICALIZATION_SRC} DEPS framework_proto enforce)

cc_library(ipu_device SRCS device.cc DEPS enforce popart)
cc_library(ipu_utils SRCS ipu_utils.cc DEPS memory framework_proto popart)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 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.

#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace {

//

} // namespace
} // namespace framework
} // namespace paddle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/framework/ipu/popart_canonicalization_utils.h"
#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"

namespace paddle {
namespace framework {
Expand Down
26 changes: 26 additions & 0 deletions paddle/fluid/framework/ipu/popart_canonicalization/logic_ops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 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.

#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace {

//

} // namespace
} // namespace framework
} // namespace paddle
69 changes: 69 additions & 0 deletions paddle/fluid/framework/ipu/popart_canonicalization/math_ops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2021 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.

#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace {

ir::Node *elementwise_add_handler(ir::Graph *graph, ir::Node *node) {
auto *op = node->Op();
auto op_desc = std::make_unique<framework::OpDesc>();
op_desc->SetType("Add");

std::vector<std::string> inputs;
inputs.push_back(op->Input("X").front());
inputs.push_back(op->Input("Y").front());
op_desc->SetInput("__inputs__", inputs);
std::vector<std::string> outputs;
outputs.push_back(op->Output("Out").front());
op_desc->SetOutput("__outputs__", outputs);

op_desc->Flush();
return graph->CreateOpNode(op_desc.get());
}

ir::Node *reduce_mean_handler(ir::Graph *graph, ir::Node *node) {
auto *op = node->Op();
auto op_desc = std::make_unique<framework::OpDesc>();
op_desc->SetType("ReduceMean");

std::vector<std::string> inputs;
inputs.push_back(op->Input("X").front());
op_desc->SetInput("__inputs__", inputs);
std::vector<std::string> outputs;
outputs.push_back(op->Output("Out").front());
op_desc->SetOutput("__outputs__", outputs);
auto reduce_all = BOOST_GET_CONST(bool, op->GetAttr("reduce_all"));
if (!reduce_all) {
auto axes_ = BOOST_GET_CONST(std::vector<int>, op->GetAttr("dim"));
auto axes = std::vector<int64_t>{axes_.begin(), axes_.end()};
op_desc->SetAttr("axes", axes);
}
auto keepdims_ = BOOST_GET_CONST(bool, op->GetAttr("keep_dim"));
auto keepdims = int64_t{keepdims_};
op_desc->SetAttr("keepdims", keepdims);

op_desc->Flush();
return graph->CreateOpNode(op_desc.get());
}

REGISTER_HANDLER(elementwise_add, elementwise_add_handler);
REGISTER_HANDLER(reduce_mean, reduce_mean_handler);

} // namespace
} // namespace framework
} // namespace paddle
65 changes: 65 additions & 0 deletions paddle/fluid/framework/ipu/popart_canonicalization/nn_ops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2021 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.

#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace {

ir::Node *conv2d_handler(ir::Graph *graph, ir::Node *node) {
auto *op = node->Op();
auto op_desc = std::make_unique<framework::OpDesc>();
op_desc->SetType("Conv");

std::vector<std::string> inputs;
inputs.push_back(op->Input("Input").front());
inputs.push_back(op->Input("Filter").front());
if (op->HasInput("Bias")) {
if (!op->Input("Bias").empty()) {
inputs.push_back(op->Input("Bias").front());
}
}
op_desc->SetInput("__inputs__", inputs);
std::vector<std::string> outputs;
outputs.push_back(op->Output("Output").front());
op_desc->SetOutput("__outputs__", outputs);

auto dilations_ = BOOST_GET_CONST(std::vector<int>, op->GetAttr("dilations"));
auto dilations = std::vector<int64_t>{dilations_.begin(), dilations_.end()};
auto group_ = BOOST_GET_CONST(int, op->GetAttr("groups"));
auto group = int64_t{group_};
auto pads_ = BOOST_GET_CONST(std::vector<int>, op->GetAttr("paddings"));
if (pads_.size() == 2) {
pads_.push_back(pads_[0]);
pads_.push_back(pads_[1]);
}
auto pads = std::vector<int64_t>{pads_.begin(), pads_.end()};
auto stride_ = BOOST_GET_CONST(std::vector<int>, op->GetAttr("strides"));
auto stride = std::vector<int64_t>{stride_.begin(), stride_.end()};
op_desc->SetAttr("dilations", dilations);
op_desc->SetAttr("group", group);
op_desc->SetAttr("pads", pads);
op_desc->SetAttr("strides", stride);

op_desc->Flush();
return graph->CreateOpNode(op_desc.get());
}

REGISTER_HANDLER(conv2d, conv2d_handler);

} // namespace
} // namespace framework
} // namespace paddle
26 changes: 26 additions & 0 deletions paddle/fluid/framework/ipu/popart_canonicalization/other_ops.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 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.

#include "paddle/fluid/framework/ipu/popart_canonicalization/canonicalization_utils.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace {

//

} // namespace
} // namespace framework
} // namespace paddle

0 comments on commit 7234141

Please sign in to comment.