Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix pybind, ComparisionKind enum export (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
hp03 committed Dec 10, 2021
1 parent 70b04f0 commit a0361cb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
19 changes: 10 additions & 9 deletions cinn/hlir/op/reduction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,18 @@ std::vector<shape_t> InferShapeForReduction(const std::vector<shape_t> &inputs_s
if (attrs.find("keep_dim") != attrs.end()) {
keep_dim = absl::get<bool>(attrs.at("keep_dim"));
}
CHECK(!dim.empty()) << "should have reduce dim, please check!";
CHECK_LE(dim.size(), inputs_shape[0].size()) << "reduce dim should no more than the input size";
std::vector<int> out_shapes;
auto ndim = inputs_shape[0].size();
for (size_t i = 0; i < ndim; ++i) {
if (std::find(dim.begin(), dim.end(), i) != dim.end()) {
if (keep_dim) {
out_shapes.push_back(1);
if (!dim.empty()) {
CHECK_LE(dim.size(), inputs_shape[0].size()) << "reduce dim should no more than the input size";
auto ndim = inputs_shape[0].size();
for (size_t i = 0; i < ndim; ++i) {
if (std::find(dim.begin(), dim.end(), i) != dim.end()) {
if (keep_dim) {
out_shapes.push_back(1);
}
} else {
out_shapes.push_back(inputs_shape[0][i]);
}
} else {
out_shapes.push_back(inputs_shape[0][i]);
}
}

Expand Down
24 changes: 21 additions & 3 deletions cinn/pybind/frontend.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,24 @@ void BindFrontend(pybind11::module *m) {
py::arg("padding_algorithm") = "EXPLICIT")
.def("sum", &NetBuilder::sum, py::arg("inputs"));

py::enum_<ComparisonKind>(*m, "ComparisonKind")
.value("kUnk", ComparisonKind::kUnk)
.value("kEq", ComparisonKind::kEq)
.value("kNe", ComparisonKind::kNe)
.value("kGe", ComparisonKind::kGe)
.value("kGt", ComparisonKind::kGt)
.value("kLe", ComparisonKind::kLe)
.value("kLt", ComparisonKind::kLt)
.export_values();

py::enum_<ReduceKind>(*m, "ReduceKind")
.value("kUnk", ReduceKind::kUnk)
.value("kSum", ReduceKind::kSum)
.value("kProd", ReduceKind::kProd)
.value("kMax", ReduceKind::kMax)
.value("kMin", ReduceKind::kMin)
.export_values();

py::class_<CinnBuilder, BaseBuilder>(*m, "CinnBuilder")
.def(py::init<const std::string &>(), py::arg("name") = "")
.def("const_scalar", &CinnBuilder::ConstScalar<bool>)
Expand All @@ -455,12 +473,12 @@ void BindFrontend(pybind11::module *m) {
py::arg("data_format") = "NCHW",
py::arg("padding_algorithm") = "EXPLICIT",
py::arg("output_shape") = std::vector<int>{})
.def("compare", &CinnBuilder::Compare, py::arg("lhs"), py::arg("rhs"), py::arg("kind"))
.def("compare", &CinnBuilder::Compare, py::arg("lhs"), py::arg("rhs"), py::arg("kind") = ComparisonKind::kEq)
.def("reduce",
&CinnBuilder::Reduce,
py::arg("operand"),
py::arg("kind"),
py::arg("dim"),
py::arg("kind") = ReduceKind::kSum,
py::arg("dim") = std::vector<int>{},
py::arg("keep_dim") = false)
.def("broadcast_to",
&CinnBuilder::BroadcastTo,
Expand Down
26 changes: 26 additions & 0 deletions python/tests/test_cinnbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@
enable_gpu = sys.argv.pop()


class TestCinnBuildBasic(unittest.TestCase):
def setUp(self):
pass

def test_compare(self):
builder = CinnBuilder("test_compare")
a = builder.create_input(Float(32), (1, 24, 56, 56), "A")
b = builder.create_input(Float(32), (1, 24, 56, 56), "B")
# default compare kind is ComparisonKind.kEq
c = builder.compare(a, b)
d = builder.compare(a, c, ComparisonKind.kNe)
prog = builder.build()
for i in range(prog.size()):
print(prog[i])

def test_reduce(self):
builder = CinnBuilder("test_compare")
a = builder.create_input(Float(32), (1, 24, 56, 56), "A")
b = builder.reduce(a)
c = builder.reduce(a, ReduceKind.kMax)
d = builder.add(b, c)
prog = builder.build()
for i in range(prog.size()):
print(prog[i])


class TestCinnBuilder(unittest.TestCase):
def setUp(self):
if enable_gpu == "ON":
Expand Down

0 comments on commit a0361cb

Please sign in to comment.