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

Polish CompileTime InferShape #4660

Merged
merged 4 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions paddle/framework/op_desc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/framework/op_desc.h"
#include <functional>
#include <unordered_map>
#include "paddle/framework/block_desc.h"
#include "paddle/framework/operator.h"

namespace paddle {
namespace framework {
Expand Down Expand Up @@ -184,5 +187,37 @@ void OpDescBind::Sync() {
need_update_ = false;
}
}

using InferShapeFuncMap =
std::unordered_map<std::string /*op_type*/,
std::function<void(InferShapeContext *)>>;

static InferShapeFuncMap &InferShapeFuncs() {
static InferShapeFuncMap *g_map = nullptr;
if (g_map == nullptr) {
g_map = new InferShapeFuncMap();
auto &info_map = OpInfoMap::Instance();
// all registered kernels
for (auto &pair : OperatorWithKernel::AllOpKernels()) {
auto &info = info_map.Get(pair.first);
auto op =
static_cast<OperatorWithKernel *>(info.Creator()("", {}, {}, {}));
Copy link
Member

Choose a reason for hiding this comment

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

can we directly use the creator with no input/output? we call https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/operator.h#L139 to check if all the input/output is set.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since type is empty, the checks will be ignored.

g_map->insert(
{pair.first, [op](InferShapeContext *ctx) { op->InferShape(ctx); }});
}
}
return *g_map;
}

void OpDescBind::InferShape(const BlockDescBind &block) const {
auto &funcs = InferShapeFuncs();
auto it = funcs.find(this->Type());
if (it == funcs.end()) {
PADDLE_THROW("Operator %s has not been registered", this->Type());
}
CompileTimeInferShapeContext ctx(*this, block);
it->second(&ctx);
}

} // namespace framework
} // namespace paddle
2 changes: 2 additions & 0 deletions paddle/framework/op_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class OpDescBind {
return &this->attrs_;
}

void InferShape(const BlockDescBind &block) const;

private:
template <typename MapType>
static std::vector<typename MapType::key_type> MapKeys(const MapType &map) {
Expand Down
3 changes: 2 additions & 1 deletion paddle/pybind/protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ void BindOpDesc(py::module &m) {
.def("set_attr", &OpDescBind::SetAttr)
.def("attr", &OpDescBind::GetAttr)
.def("set_block_attr", &OpDescBind::SetBlockAttr)
.def("get_block_attr", &OpDescBind::GetBlockAttr);
.def("get_block_attr", &OpDescBind::GetBlockAttr)
.def("infer_shape", &OpDescBind::InferShape);
}

} // namespace pybind
Expand Down
15 changes: 0 additions & 15 deletions paddle/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,6 @@ All parameter, weight, gradient are variables in Paddle.
desc.InitializationErrorString());
return OpRegistry::CreateOp(desc);
})
.def_static("infer_shape",
[](OpDescBind &op_desc, BlockDescBind &block) {
auto op = OpRegistry::CreateOp(*op_desc.Proto());
auto *op_with_kernel =
dynamic_cast<OperatorWithKernel *>(op.get());
if (op_with_kernel != nullptr) {
auto ctx = CompileTimeInferShapeContext(op_desc, block);
op_with_kernel->InferShape(&ctx);
} else {
PADDLE_THROW(
"OP(%s) is not type of OperatorWithKernel, "
"should not call this function",
op_desc.Type());
}
})
.def("backward",
[](const OperatorBase &forwardOp,
const std::unordered_set<std::string> &no_grad_vars) {
Expand Down
6 changes: 3 additions & 3 deletions python/paddle/v2/framework/tests/test_infer_shape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

import paddle.v2.framework.core as core
from paddle.v2.framework.op import Operator


class TestInferShape(unittest.TestCase):
Expand All @@ -26,7 +26,7 @@ def test_sum_op(self):
sum_op_desc.set_input("X", ["x1", "x2"])
sum_op_desc.set_output("Out", ["out"])

core.Operator.infer_shape(sum_op_desc, block)
sum_op_desc.infer_shape(block)
self.assertEqual(out.shape(), shape)

def test_mul_op(self):
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_mul_op(self):
mul_op_desc.set_attr("x_num_col_dims", 1)
mul_op_desc.set_attr("y_num_col_dims", 1)

core.Operator.infer_shape(mul_op_desc, block)
mul_op_desc.infer_shape(block)
self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])


Expand Down