-
Notifications
You must be signed in to change notification settings - Fork 6
✨ Refactored Cut View on AIG #368
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're exposing cuts, we should also expose cut enumeration algorithms: https://mockturtle.readthedocs.io/en/latest/algorithms/cut_enumeration.html I haven't put much thought into how exactly the architecture would look best for |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||||||
| #include <fmt/format.h> | ||||||||||
| #include <mockturtle/networks/sequential.hpp> | ||||||||||
| #include <mockturtle/traits.hpp> | ||||||||||
| #include <mockturtle/views/cut_view.hpp> | ||||||||||
| #include <mockturtle/views/depth_view.hpp> | ||||||||||
| #include <mockturtle/views/fanout_view.hpp> | ||||||||||
| #include <mockturtle/views/names_view.hpp> | ||||||||||
|
|
@@ -22,6 +23,7 @@ | |||||||||
|
|
||||||||||
| #include <algorithm> | ||||||||||
| #include <cctype> | ||||||||||
| #include <cstddef> | ||||||||||
| #include <cstdint> | ||||||||||
| #include <exception> | ||||||||||
| #include <functional> | ||||||||||
|
|
@@ -529,6 +531,131 @@ Preserves only combinational structure and does not capture augmented view metad | |||||||||
| }, | ||||||||||
| nb::arg("n"), R"pb(Returns fanout nodes of node ``n``.)pb"); | ||||||||||
|
|
||||||||||
| using CutNtk = mockturtle::cut_view<Ntk>; | ||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| auto clear_visited = [](const Ntk& ntk) { ntk.foreach_node([&ntk](const auto& n) { ntk.set_visited(n, 0); }); }; | ||||||||||
|
|
||||||||||
| nb::class_<CutNtk>(m, "Cut", | ||||||||||
| R"pb(Implements an isolated view on a single cut in a network. | ||||||||||
|
Comment on lines
+538
to
+539
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know I proposed renaming this to
Long story short, we won't get around parameterizing this type with the network name to avoid collisions; we just do it slightly differently than before. And we keep the non-inheritance.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation and tests will have to be adjusted accordingly. |
||||||||||
|
|
||||||||||
| This view creates a network from a single cut with a single output `root` | ||||||||||
| and a set of `leaves`. This is a standalone structural descriptor; it does | ||||||||||
| not inherit from the network type and only exposes read-only inspection | ||||||||||
| methods. | ||||||||||
|
|
||||||||||
| Note: | ||||||||||
| This view clears all nodes' visited flags before construction to ensure | ||||||||||
| the cut is constructed correctly. The view guarantees that all nodes in | ||||||||||
| the view will have a 0 visited flag after construction.)pb") | ||||||||||
|
Comment on lines
+534
to
+549
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is exactly what I had in mind. Thanks for changing the architecture! Feels much cleaner to me. What do you think? A few things we must still adjust, though, or we will run into issues once we add more network types. |
||||||||||
| .def( | ||||||||||
| "__init__", | ||||||||||
| [clear_visited](CutNtk* self, const Ntk& ntk, const std::vector<Node>& leaves, const Signal& root) | ||||||||||
| { | ||||||||||
| clear_visited(ntk); | ||||||||||
| new (self) CutNtk(ntk, leaves, root); | ||||||||||
| }, | ||||||||||
| nb::arg("ntk"), nb::arg("leaves"), nb::arg("root"), | ||||||||||
| R"pb(Creates a cut view from a network, leaf nodes, and root signal. | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| ntk: The base network. | ||||||||||
| leaves: Vector of leaf nodes (boundary of the cut). | ||||||||||
| root: The root signal (output) of the cut.)pb") | ||||||||||
| .def( | ||||||||||
| "__init__", | ||||||||||
| [clear_visited](CutNtk* self, const Ntk& ntk, const std::vector<Signal>& leaves, const Signal& root) | ||||||||||
| { | ||||||||||
| clear_visited(ntk); | ||||||||||
| new (self) CutNtk(ntk, leaves, root); | ||||||||||
| }, | ||||||||||
| nb::arg("ntk"), nb::arg("leaves"), nb::arg("root"), | ||||||||||
| R"pb(Creates a cut view from a network, leaf signals, and root signal. | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| ntk: The base network. | ||||||||||
| leaves: Vector of leaf signals (boundary of the cut). | ||||||||||
| root: The root signal (output) of the cut.)pb") | ||||||||||
| .def( | ||||||||||
| "clone", [](const CutNtk& ntk) { return CutNtk{ntk}; }, R"pb(Creates a structural copy of the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "__copy__", [](const CutNtk& ntk) { return CutNtk{ntk}; }, R"pb(Returns a shallow copy of the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "__deepcopy__", [](const CutNtk& ntk, const nb::dict&) { return CutNtk{ntk}; }, nb::arg("memo"), | ||||||||||
| R"pb(Returns a deep copy of the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "nodes", [](const CutNtk& ntk) { return collect_nodes(ntk); }, | ||||||||||
| R"pb(Returns a list of all nodes in the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "gates", | ||||||||||
| [](const CutNtk& ntk) | ||||||||||
| { | ||||||||||
| std::vector<Node> gates{}; | ||||||||||
| gates.reserve(static_cast<size_t>(ntk.num_gates())); | ||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use this consistently. |
||||||||||
| ntk.foreach_gate([&gates](const auto& g) { gates.push_back(g); }); | ||||||||||
| return gates; | ||||||||||
| }, | ||||||||||
| R"pb(Returns a list of all gate nodes in the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "pis", | ||||||||||
| [](const CutNtk& ntk) | ||||||||||
| { | ||||||||||
| std::vector<Node> pis{}; | ||||||||||
| pis.reserve(static_cast<size_t>(ntk.num_pis())); | ||||||||||
| ntk.foreach_pi([&pis](const auto& pi) { pis.push_back(pi); }); | ||||||||||
| return pis; | ||||||||||
| }, | ||||||||||
| R"pb(Returns a list of all primary input (leaf) nodes in the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "pos", | ||||||||||
| [](const CutNtk& ntk) | ||||||||||
| { | ||||||||||
| std::vector<Signal> pos{}; | ||||||||||
| pos.reserve(static_cast<size_t>(ntk.num_pos())); | ||||||||||
| ntk.foreach_po([&pos](const auto& po) { pos.push_back(po); }); | ||||||||||
| return pos; | ||||||||||
| }, | ||||||||||
| R"pb(Returns a list containing the root signal of the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "is_pi", [](const CutNtk& ntk, const Node& n) { return ntk.is_pi(n); }, nb::arg("n"), | ||||||||||
| R"pb(Returns whether ``n`` is a primary input (leaf) in the cut view.)pb") | ||||||||||
| .def_prop_ro( | ||||||||||
| "size", [](const CutNtk& ntk) { return ntk.size(); }, R"pb(Number of nodes in the cut view.)pb") | ||||||||||
| .def_prop_ro( | ||||||||||
| "num_pis", [](const CutNtk& ntk) { return ntk.num_pis(); }, | ||||||||||
| R"pb(Number of primary inputs (leaves) in the cut view.)pb") | ||||||||||
| .def_prop_ro( | ||||||||||
| "num_pos", [](const CutNtk& ntk) { return ntk.num_pos(); }, | ||||||||||
| R"pb(Number of primary outputs (always 1 for cut view).)pb") | ||||||||||
| .def_prop_ro( | ||||||||||
| "num_gates", [](const CutNtk& ntk) { return ntk.num_gates(); }, | ||||||||||
| R"pb(Number of logic gates in the cut view.)pb") | ||||||||||
| .def( | ||||||||||
| "node_to_index", [](const CutNtk& ntk, const Node& n) { return ntk.node_to_index(n); }, nb::arg("n"), | ||||||||||
| R"pb(Returns the integer index of a node.)pb") | ||||||||||
| .def( | ||||||||||
| "index_to_node", [](const CutNtk& ntk, const uint32_t index) { return ntk.index_to_node(index); }, | ||||||||||
| nb::arg("index"), R"pb(Returns the node for an index.)pb") | ||||||||||
| .def( | ||||||||||
| "to_index_list", | ||||||||||
| [](const CutNtk& ntk) | ||||||||||
| { | ||||||||||
| aigverse::aig_index_list il{}; | ||||||||||
| mockturtle::encode(il, ntk); | ||||||||||
| return il; | ||||||||||
| }, | ||||||||||
| R"pb(Converts the cut view to an index-list encoding. | ||||||||||
|
|
||||||||||
| Only the cut's restricted node set is encoded. The resulting index list | ||||||||||
| can be decoded into a standalone Aig via ``AigIndexList.to_aig()``. | ||||||||||
|
|
||||||||||
| Returns: | ||||||||||
| The corresponding index-list representation.)pb", | ||||||||||
| nb::rv_policy::move) | ||||||||||
| .def( | ||||||||||
| "__repr__", [](const CutNtk& ntk) | ||||||||||
| { return fmt::format("Cut(leaves={}, gates={}, size={})", ntk.num_pis(), ntk.num_gates(), ntk.size()); }, | ||||||||||
| R"pb(Returns a developer-friendly string representation.)pb"); | ||||||||||
|
|
||||||||||
| using Register = mockturtle::register_t; // NOLINT(readability-identifier-naming) | ||||||||||
| nb::class_<Register>(m, fmt::format("{}Register", network_name).c_str(), | ||||||||||
| R"pb(Represents metadata for one sequential register.)pb") | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.