Skip to content

Commit f6f41cd

Browse files
committed
fine tuned python bindings and to_string() methods
1 parent 0e84390 commit f6f41cd

File tree

9 files changed

+228
-169
lines changed

9 files changed

+228
-169
lines changed

plugins/machine_learning/include/machine_learning/features/gate_feature.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ namespace hal
5959

6060
private:
6161
const PinDirection m_direction;
62-
const std::vector<PinType> m_forbidden_pin_types;
6362
const bool m_directed;
63+
const std::vector<PinType> m_forbidden_pin_types;
6464
};
6565

6666
class SequentialDistanceGlobalIO : public GateFeature
@@ -74,8 +74,8 @@ namespace hal
7474

7575
private:
7676
const PinDirection m_direction;
77-
const std::vector<PinType> m_forbidden_pin_types;
7877
const bool m_directed;
78+
const std::vector<PinType> m_forbidden_pin_types;
7979
};
8080

8181
class IODegrees : public GateFeature

plugins/machine_learning/include/machine_learning/features/gate_pair_feature.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,14 @@ namespace hal
131131
const std::vector<PinType> m_forbidden_pin_types;
132132
};
133133

134-
// feature ideas:
135-
136-
// distance to each other in a sequential only netlist
137-
// shared neighbors in a sequential only netlist
138-
139134
Result<std::vector<u32>> build_feature_vec(const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
140135
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
141136

142-
Result<std::vector<u32>> build_feature_vec(const std::vector<const GatePairFeature*>& features, const std::pair<const Gate*, const Gate*>& gate_pair);
143-
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::pair<const Gate*, const Gate*>& gate_pair);
137+
Result<std::vector<u32>> build_feature_vec(const std::vector<const GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair);
138+
Result<std::vector<u32>> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair);
144139

145-
Result<std::vector<std::vector<u32>>> build_feature_vecs(const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<const Gate*, const Gate*>>& gate_pairs);
146-
Result<std::vector<std::vector<u32>>>
147-
build_feature_vecs(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<const Gate*, const Gate*>>& gate_pairs);
140+
Result<std::vector<std::vector<u32>>> build_feature_vecs(const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs);
141+
Result<std::vector<std::vector<u32>>> build_feature_vecs(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs);
148142
} // namespace gate_pair_feature
149143
} // namespace machine_learning
150144
} // namespace hal

plugins/machine_learning/include/machine_learning/labels/gate_pair_label.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ namespace hal
103103
* @param[in] gates - The gates to be paired.
104104
* @returns A vector of gate pairs on success, an error otherwise.
105105
*/
106-
virtual Result<std::vector<std::pair<const Gate*, const Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const = 0;
106+
virtual Result<std::vector<std::pair<Gate*, Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const = 0;
107107

108108
/**
109109
* @brief Calculate labels for a given gate pair.
@@ -127,7 +127,9 @@ namespace hal
127127
* @param[in] lc - The labeling context.
128128
* @returns A pair containing gate pairs and corresponding labels on success, an error otherwise.
129129
*/
130-
virtual Result<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(LabelContext& lc) const = 0;
130+
virtual Result<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(const Netlist* netlist) const = 0;
131+
132+
virtual std::string to_string() const = 0;
131133
};
132134

133135
/**
@@ -140,12 +142,19 @@ namespace hal
140142
/**
141143
* @brief Default constructor.
142144
*/
143-
SharedSignalGroup(){};
145+
SharedSignalGroup(const PinDirection& direction, const u32 min_pair_count, const double negative_to_positive_factor)
146+
: m_direction(direction), m_min_pair_count(min_pair_count), m_negative_to_positive_factor(negative_to_positive_factor){};
144147

145-
Result<std::vector<std::pair<const Gate*, const Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
148+
Result<std::vector<std::pair<Gate*, Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
146149
Result<std::vector<u32>> calculate_label(LabelContext& lc, const Gate* g_a, const Gate* g_b) const override;
147150
Result<std::vector<std::vector<u32>>> calculate_labels(LabelContext& lc, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) const override;
148-
Result<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(LabelContext& lc) const override;
151+
Result<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(const Netlist* netlist) const override;
152+
std::string to_string() const override;
153+
154+
private:
155+
const PinDirection m_direction;
156+
const u32 m_min_pair_count;
157+
const double m_negative_to_positive_factor;
149158
};
150159

151160
/**
@@ -160,10 +169,11 @@ namespace hal
160169
*/
161170
SharedConnection(){};
162171

163-
Result<std::vector<std::pair<const Gate*, const Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
172+
Result<std::vector<std::pair<Gate*, Gate*>>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
164173
Result<std::vector<u32>> calculate_label(LabelContext& lc, const Gate* g_a, const Gate* g_b) const override;
165174
Result<std::vector<std::vector<u32>>> calculate_labels(LabelContext& lc, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) const override;
166-
Result<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(LabelContext& lc) const override;
175+
Result<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> calculate_labels(const Netlist* netlist) const override;
176+
std::string to_string() const override;
167177
};
168178
} // namespace gate_pair_label
169179
} // namespace machine_learning

plugins/machine_learning/python/python_bindings.cpp

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,35 @@ namespace hal
112112
:rtype: hal_py.machine_learning.graph.NetlistGraph
113113
)");
114114

115+
m.def(
116+
"construct_sequential_netlist_graph",
117+
[](const Netlist* nl, const std::vector<Gate*>& gates, const machine_learning::GraphDirection& dir) -> std::optional<machine_learning::NetlistGraph> {
118+
auto res = machine_learning::construct_sequential_netlist_graph(nl, gates, dir);
119+
if (res.is_ok())
120+
{
121+
return res.get();
122+
}
123+
else
124+
{
125+
log_error("python_context", "error encountered while constructing sequential netlist graph:\n{}", res.get_error().get());
126+
return std::nullopt;
127+
}
128+
},
129+
py::arg("nl"),
130+
py::arg("gates"),
131+
py::arg("dir"),
132+
R"(
133+
Constructs a sequential netlist graph representation. The connections are an edge list of indices representing the position of the gates in the gates vector.
134+
135+
This function constructs a sequential graph representation of the netlist, capturing only the sequential dependencies between gates.
136+
137+
:param hal_py.Netlist nl: The netlist to operate on.
138+
:param list[hal_py.Gate] gates: The order of the gates, needed for the index representation.
139+
:param machine_learning.GraphDirection dir: The direction of the graph.
140+
:returns: A NetlistGraph representing the sequential connections within the netlist on success, None otherwise.
141+
:rtype: machine_learning.NetlistGraph or None
142+
)");
143+
115144
// Bindings for annotate_netlist_graph
116145
m.def("annotate_netlist_graph",
117146
&machine_learning::annotate_netlist_graph,
@@ -1099,7 +1128,7 @@ namespace hal
10991128

11001129
py_gate_pair_feature.def(
11011130
"build_feature_vec",
1102-
[](const std::vector<const machine_learning::gate_pair_feature::GatePairFeature*>& features, const std::pair<const Gate*, const Gate*>& gate_pair) -> std::optional<std::vector<u32>> {
1131+
[](const std::vector<const machine_learning::gate_pair_feature::GatePairFeature*>& features, const std::pair<Gate*, Gate*>& gate_pair) -> std::optional<std::vector<u32>> {
11031132
auto res = machine_learning::gate_pair_feature::build_feature_vec(features, gate_pair);
11041133
if (res.is_ok())
11051134
{
@@ -1126,7 +1155,7 @@ namespace hal
11261155
"build_feature_vec",
11271156
[](machine_learning::gate_pair_feature::FeatureContext& fc,
11281157
const std::vector<const machine_learning::gate_pair_feature::GatePairFeature*>& features,
1129-
const std::pair<const Gate*, const Gate*>& gate_pair) -> std::optional<std::vector<u32>> {
1158+
const std::pair<Gate*, Gate*>& gate_pair) -> std::optional<std::vector<u32>> {
11301159
auto res = machine_learning::gate_pair_feature::build_feature_vec(fc, features, gate_pair);
11311160
if (res.is_ok())
11321161
{
@@ -1154,7 +1183,7 @@ namespace hal
11541183
py_gate_pair_feature.def(
11551184
"build_feature_vecs",
11561185
[](const std::vector<const machine_learning::gate_pair_feature::GatePairFeature*>& features,
1157-
const std::vector<std::pair<const Gate*, const Gate*>>& gate_pairs) -> std::optional<std::vector<std::vector<u32>>> {
1186+
const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) -> std::optional<std::vector<std::vector<u32>>> {
11581187
auto res = machine_learning::gate_pair_feature::build_feature_vecs(features, gate_pairs);
11591188
if (res.is_ok())
11601189
{
@@ -1181,7 +1210,7 @@ namespace hal
11811210
"build_feature_vecs",
11821211
[](machine_learning::gate_pair_feature::FeatureContext& fc,
11831212
const std::vector<const machine_learning::gate_pair_feature::GatePairFeature*>& features,
1184-
const std::vector<std::pair<const Gate*, const Gate*>>& gate_pairs) -> std::optional<std::vector<std::vector<u32>>> {
1213+
const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) -> std::optional<std::vector<std::vector<u32>>> {
11851214
auto res = machine_learning::gate_pair_feature::build_feature_vecs(fc, features, gate_pairs);
11861215
if (res.is_ok())
11871216
{
@@ -1297,7 +1326,7 @@ namespace hal
12971326
py_gate_pair_label_class.def(
12981327
"calculate_gate_pairs",
12991328
[](const machine_learning::gate_pair_label::GatePairLabel& self, machine_learning::gate_pair_label::LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates)
1300-
-> std::optional<std::vector<std::pair<const Gate*, const Gate*>>> {
1329+
-> std::optional<std::vector<std::pair<Gate*, Gate*>>> {
13011330
auto res = self.calculate_gate_pairs(lc, nl, gates);
13021331
if (res.is_ok())
13031332
{
@@ -1378,9 +1407,8 @@ namespace hal
13781407

13791408
py_gate_pair_label_class.def(
13801409
"calculate_labels",
1381-
[](const machine_learning::gate_pair_label::GatePairLabel& self,
1382-
machine_learning::gate_pair_label::LabelContext& lc) -> std::optional<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> {
1383-
auto res = self.calculate_labels(lc);
1410+
[](const machine_learning::gate_pair_label::GatePairLabel& self, const Netlist* netlist) -> std::optional<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> {
1411+
auto res = self.calculate_labels(netlist);
13841412
if (res.is_ok())
13851413
{
13861414
return res.get();
@@ -1395,26 +1423,43 @@ namespace hal
13951423
R"(
13961424
Calculate both gate pairs and their labels within the labeling context.
13971425
1398-
:param machine_learning.gate_pair_label.LabelContext lc: The labeling context.
1426+
:param hal_py.netlist: The netlist to create labels for.
13991427
:returns: A pair containing gate pairs and corresponding labels on success, None otherwise.
14001428
:rtype: tuple[list[tuple[hal_py.Gate, hal_py.Gate]], list[list[int]]] or None
14011429
)");
14021430

1431+
py_gate_pair_label_class.def("to_string",
1432+
&machine_learning::gate_pair_label::GatePairLabel::to_string,
1433+
R"(
1434+
Get the string representation of the gate pair label.
1435+
1436+
:returns: The string representation.
1437+
:rtype: str
1438+
)");
1439+
14031440
py::class_<machine_learning::gate_pair_label::SharedSignalGroup, machine_learning::gate_pair_label::GatePairLabel> py_shared_signal_group(py_gate_pair_label,
14041441
"SharedSignalGroup",
14051442
R"(
14061443
Labels gate pairs based on shared signal groups.
14071444
)");
14081445

1409-
py_shared_signal_group.def(py::init<>(),
1446+
py_shared_signal_group.def(py::init<const PinDirection&, const u32, const double>(),
1447+
py::arg("direction"),
1448+
py::arg("min_pair_count"),
1449+
py::arg("negative_to_positive_factor"),
14101450
R"(
1411-
Default constructor.
1412-
)");
1451+
Construct a SharedSignalGroup.
1452+
1453+
:param hal_py.PinDirection direction: The pin direction.
1454+
:param int min_pair_count: The minimum pair count.
1455+
:param float negative_to_positive_factor: The negative to positive factor.
1456+
)");
1457+
;
14131458

14141459
py_shared_signal_group.def(
14151460
"calculate_gate_pairs",
14161461
[](const machine_learning::gate_pair_label::SharedSignalGroup& self, machine_learning::gate_pair_label::LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates)
1417-
-> std::optional<std::vector<std::pair<const Gate*, const Gate*>>> {
1462+
-> std::optional<std::vector<std::pair<Gate*, Gate*>>> {
14181463
auto res = self.calculate_gate_pairs(lc, nl, gates);
14191464
if (res.is_ok())
14201465
{
@@ -1497,8 +1542,8 @@ namespace hal
14971542
py_shared_signal_group.def(
14981543
"calculate_labels",
14991544
[](const machine_learning::gate_pair_label::SharedSignalGroup& self,
1500-
machine_learning::gate_pair_label::LabelContext& lc) -> std::optional<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> {
1501-
auto res = self.calculate_labels(lc);
1545+
const Netlist* netlist) -> std::optional<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> {
1546+
auto res = self.calculate_labels(netlist);
15021547
if (res.is_ok())
15031548
{
15041549
return res.get();
@@ -1513,11 +1558,20 @@ namespace hal
15131558
R"(
15141559
Calculate both gate pairs and their labels based on shared signal groups within the labeling context.
15151560
1516-
:param machine_learning.gate_pair_label.LabelContext lc: The labeling context.
1561+
:param hal_py.netlist: The netlist to create labels for.
15171562
:returns: A pair containing gate pairs and corresponding labels on success, None otherwise.
15181563
:rtype: tuple[list[tuple[hal_py.Gate, hal_py.Gate]], list[list[int]]] or None
15191564
)");
15201565

1566+
py_shared_signal_group.def("to_string",
1567+
&machine_learning::gate_pair_label::SharedSignalGroup::to_string,
1568+
R"(
1569+
Get the string representation of the gate pair label.
1570+
1571+
:returns: The string representation.
1572+
:rtype: str
1573+
)");
1574+
15211575
py::class_<machine_learning::gate_pair_label::SharedConnection, machine_learning::gate_pair_label::GatePairLabel> py_shared_connection(py_gate_pair_label,
15221576
"SharedConnection",
15231577
R"(
@@ -1532,7 +1586,7 @@ namespace hal
15321586
py_shared_connection.def(
15331587
"calculate_gate_pairs",
15341588
[](const machine_learning::gate_pair_label::SharedConnection& self, machine_learning::gate_pair_label::LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates)
1535-
-> std::optional<std::vector<std::pair<const Gate*, const Gate*>>> {
1589+
-> std::optional<std::vector<std::pair<Gate*, Gate*>>> {
15361590
auto res = self.calculate_gate_pairs(lc, nl, gates);
15371591
if (res.is_ok())
15381592
{
@@ -1615,8 +1669,8 @@ namespace hal
16151669
py_shared_connection.def(
16161670
"calculate_labels",
16171671
[](const machine_learning::gate_pair_label::SharedConnection& self,
1618-
machine_learning::gate_pair_label::LabelContext& lc) -> std::optional<std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>>> {
1619-
auto res = self.calculate_labels(lc);
1672+
const Netlist* netlist) -> std::optional<std::pair<std::vector<std::pair<Gate*, Gate*>>, std::vector<std::vector<u32>>>> {
1673+
auto res = self.calculate_labels(netlist);
16201674
if (res.is_ok())
16211675
{
16221676
return res.get();
@@ -1631,11 +1685,20 @@ namespace hal
16311685
R"(
16321686
Calculate both gate pairs and their labels based on shared connections within the labeling context.
16331687
1634-
:param machine_learning.gate_pair_label.LabelContext lc: The labeling context.
1688+
:param hal_py.netlist: The netlist to create labels for.
16351689
:returns: A pair containing gate pairs and corresponding labels on success, None otherwise.
16361690
:rtype: tuple[list[tuple[hal_py.Gate, hal_py.Gate]], list[list[int]]] or None
16371691
)");
16381692

1693+
py_shared_connection.def("to_string",
1694+
&machine_learning::gate_pair_label::SharedConnection::to_string,
1695+
R"(
1696+
Get the string representation of the gate pair label.
1697+
1698+
:returns: The string representation.
1699+
:rtype: str
1700+
)");
1701+
16391702
#ifndef PYBIND11_MODULE
16401703
return m.ptr();
16411704
#endif // PYBIND11_MODULE

0 commit comments

Comments
 (0)