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

【new ir】add ir pybind api #55745

Merged
merged 11 commits into from
Aug 2, 2023
6 changes: 5 additions & 1 deletion paddle/fluid/pybind/ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void BindProgram(py::module *m) {
void BindBlock(py::module *m) {
py::class_<Block> block(*m, "Block");
block.def("front", &Block::front, return_value_policy::reference)
.def("get_program",
Copy link
Contributor

Choose a reason for hiding this comment

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

建议:get_parent_program

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

[](Block &self) { return self.GetParentOp()->GetParentProgram(); })
.def("get_ops",
[](Block &self) -> py::list {
py::list op_list;
Expand All @@ -81,9 +83,11 @@ void BindBlock(py::module *m) {
void BindOperation(py::module *m) {
py::class_<Operation> op(*m, "Operation");
op.def("name", &Operation::name)
.def("get_parent", &Operation::GetParent, return_value_policy::reference)
.def("get_block", &Operation::GetParent, return_value_policy::reference)
Copy link
Contributor

Choose a reason for hiding this comment

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

建议:get_parent_block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

.def("num_results", &Operation::num_results)
.def("num_operands", &Operation::num_operands)
.def("result", &Operation::result)
.def("operand", &Operation::op_operand)
Copy link
Contributor

Choose a reason for hiding this comment

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

修改c++端op_operand命名为operand

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

.def("operands",
[](Operation &self) -> py::list {
py::list op_list;
Expand Down
8 changes: 7 additions & 1 deletion test/ir/new_ir/test_ir_pybind.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def test_program(self):
newir_program = get_ir_program()
newir_program.print()

block = newir_program.block()
program = block.get_program()
program.print()

self.assertEqual(newir_program, program)

def test_block(self):
newir_program = get_ir_program()
block = newir_program.block()
Expand All @@ -57,7 +63,7 @@ def test_operation(self):
matmul_op = newir_program.block().get_ops()[1]
add_op = newir_program.block().get_ops()[2]
tanh_op = newir_program.block().get_ops()[3]
parent_block = tanh_op.get_parent()
parent_block = tanh_op.get_block()
parent_ops_num = len(parent_block.get_ops())
self.assertTrue(parent_ops_num, 4)
self.assertTrue(tanh_op.num_results(), 1)
Expand Down