Skip to content

Commit f0e4bae

Browse files
committed
several bug fixes, added normilization
1 parent f6a7ab1 commit f0e4bae

File tree

10 files changed

+227
-44
lines changed

10 files changed

+227
-44
lines changed

plugins/graph_algorithm/include/graph_algorithm/netlist_graph.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ namespace hal
154154
*/
155155
igraph_t* get_graph() const;
156156

157+
/**
158+
* @brief Get all the gates that are included in the netlist graph
159+
*
160+
* @return A vector of gates.
161+
*/
162+
const std::vector<Gate*> get_included_gates() const;
163+
157164
/**
158165
* @brief Get the gates corresponding to the specified vertices.
159166
*

plugins/graph_algorithm/python/python_bindings.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ namespace hal
187187
:rtype: hal_py.Netlist
188188
)");
189189

190+
py_netlist_graph.def("get_included_gates", &graph_algorithm::NetlistGraph::get_included_gates, R"(
191+
Get the gates included in the netlist graph.
192+
193+
:returns: The gates included in the netlsit graph.
194+
:rtype: list[hal_py.Gate]
195+
)");
196+
190197
py_netlist_graph.def(
191198
"get_gates_from_vertices",
192199
[](const graph_algorithm::NetlistGraph& self, const std::vector<u32>& vertices) -> std::optional<std::vector<Gate*>> {

plugins/graph_algorithm/src/algorithms/centrality.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace hal
2626
return ERR(res.get_error());
2727
}
2828

29-
auto res = get_harmonic_centrality(graph, &i_vertices, direction);
29+
auto res = get_harmonic_centrality(graph, &i_vertices, direction, cutoff);
3030

3131
igraph_vector_int_destroy(&i_vertices);
3232

@@ -61,7 +61,7 @@ namespace hal
6161
VECTOR(i_vertices)[i] = vertices.at(i);
6262
}
6363

64-
auto res = get_harmonic_centrality(graph, &i_vertices, direction);
64+
auto res = get_harmonic_centrality(graph, &i_vertices, direction, cutoff);
6565

6666
igraph_vector_int_destroy(&i_vertices);
6767

@@ -153,7 +153,7 @@ namespace hal
153153
return ERR(res.get_error());
154154
}
155155

156-
auto res = get_betweenness_centrality(graph, &i_vertices, directed);
156+
auto res = get_betweenness_centrality(graph, &i_vertices, directed, cutoff);
157157

158158
igraph_vector_int_destroy(&i_vertices);
159159

@@ -188,7 +188,7 @@ namespace hal
188188
VECTOR(i_vertices)[i] = vertices.at(i);
189189
}
190190

191-
auto res = get_betweenness_centrality(graph, &i_vertices, directed);
191+
auto res = get_betweenness_centrality(graph, &i_vertices, directed, cutoff);
192192

193193
igraph_vector_int_destroy(&i_vertices);
194194

plugins/graph_algorithm/src/netlist_graph.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,17 @@ namespace hal
236236
std::map<Net*, u32> global_out_to_node;
237237
for (auto* src_gate : nl_asbtr->get_target_gates())
238238
{
239-
for (auto* dst_gate : nl_asbtr->get_unique_successors(src_gate).get())
239+
const auto successors = nl_asbtr->get_unique_successors(src_gate).get();
240+
for (auto* dst_gate : successors)
240241
{
241242
VECTOR(edges)[edge_index++] = graph->m_gates_to_nodes.at(src_gate);
242243
VECTOR(edges)[edge_index++] = graph->m_gates_to_nodes.at(dst_gate);
243244
}
244245

245246
if (create_dummy_vertices)
246247
{
247-
for (auto* global_in : nl_asbtr->get_global_input_predecessors(src_gate).get())
248+
const auto global_predecessors = nl_asbtr->get_global_input_predecessors(src_gate).get();
249+
for (auto* global_in : global_predecessors)
248250
{
249251
if (global_in_to_node.find(global_in) == global_in_to_node.end())
250252
{
@@ -256,7 +258,8 @@ namespace hal
256258
VECTOR(edges)[edge_index++] = graph->m_gates_to_nodes.at(src_gate);
257259
}
258260

259-
for (auto* global_out : nl_asbtr->get_global_output_successors(src_gate).get())
261+
const auto global_successors = nl_asbtr->get_global_output_successors(src_gate).get();
262+
for (auto* global_out : global_successors)
260263
{
261264
if (global_out_to_node.find(global_out) == global_out_to_node.end())
262265
{
@@ -309,6 +312,16 @@ namespace hal
309312
return m_graph_ptr;
310313
}
311314

315+
const std::vector<Gate*> NetlistGraph::get_included_gates() const
316+
{
317+
std::vector<Gate*> gates;
318+
gates.reserve(m_gates_to_nodes.size());
319+
320+
std::transform(m_gates_to_nodes.begin(), m_gates_to_nodes.end(), std::back_inserter(gates), [](const auto& pair) { return pair.first; });
321+
322+
return gates;
323+
}
324+
312325
Result<std::vector<Gate*>> NetlistGraph::get_gates_from_vertices(const std::vector<u32>& vertices) const
313326
{
314327
std::vector<Gate*> res;

plugins/machine_learning/include/machine_learning/features/gate_feature_bulk.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,58 @@ namespace hal
1111
class BetweennessCentrality : public GateFeatureBulk
1212
{
1313
public:
14-
BetweennessCentrality(const bool directed = true, const i32 cutoff = -1) : m_directed(directed), m_cutoff(cutoff){};
14+
BetweennessCentrality(const bool directed = true, const i32 cutoff = -1, const bool normalize = true) : m_directed(directed), m_cutoff(cutoff), m_normalize(normalize){};
1515

1616
Result<std::vector<std::vector<FEATURE_TYPE>>> calculate_feature(Context& ctx, const std::vector<Gate*>& gates) const override;
1717
std::string to_string() const override;
1818

1919
private:
2020
const bool m_directed;
2121
const i32 m_cutoff;
22+
const bool m_normalize;
2223
};
2324

2425
class HarmonicCentrality : public GateFeatureBulk
2526
{
2627
public:
27-
HarmonicCentrality(const PinDirection& direction, const i32 cutoff = -1) : m_direction(direction), m_cutoff(cutoff){};
28+
HarmonicCentrality(const PinDirection& direction, const i32 cutoff = -1, const bool normalize = true) : m_direction(direction), m_cutoff(cutoff), m_normalize(normalize){};
2829

2930
Result<std::vector<std::vector<FEATURE_TYPE>>> calculate_feature(Context& ctx, const std::vector<Gate*>& gates) const override;
3031
std::string to_string() const override;
3132

3233
private:
3334
const PinDirection m_direction;
3435
const i32 m_cutoff;
36+
const bool m_normalize;
3537
};
3638

3739
class SequentialBetweennessCentrality : public GateFeatureBulk
3840
{
3941
public:
40-
SequentialBetweennessCentrality(const bool directed = true, const i32 cutoff = -1) : m_directed(directed), m_cutoff(cutoff){};
42+
SequentialBetweennessCentrality(const bool directed = true, const i32 cutoff = -1, const bool normalize = true) : m_directed(directed), m_cutoff(cutoff), m_normalize(normalize){};
4143

4244
Result<std::vector<std::vector<FEATURE_TYPE>>> calculate_feature(Context& ctx, const std::vector<Gate*>& gates) const override;
4345
std::string to_string() const override;
4446

4547
private:
4648
const bool m_directed;
4749
const i32 m_cutoff;
50+
const bool m_normalize;
4851
};
4952

5053
class SequentialHarmonicCentrality : public GateFeatureBulk
5154
{
5255
public:
53-
SequentialHarmonicCentrality(const PinDirection& direction, const i32 cutoff = -1) : m_direction(direction), m_cutoff(cutoff){};
56+
SequentialHarmonicCentrality(const PinDirection& direction, const i32 cutoff = -1, const bool normalize = true) : m_direction(direction), m_cutoff(cutoff), m_normalize(normalize){};
5457

5558
Result<std::vector<std::vector<FEATURE_TYPE>>> calculate_feature(Context& ctx, const std::vector<Gate*>& gates) const override;
5659
std::string to_string() const override;
5760

5861
private:
5962
const PinDirection m_direction;
6063
const i32 m_cutoff;
64+
const bool m_normalize;
6165
};
6266
} // namespace gate_feature
63-
} // namespace machine_learning
67+
} // namespace machine_learning
6468
} // namespace hal
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include "hal_core/utilities/result.h"
4+
5+
#include <vector>
6+
7+
namespace hal
8+
{
9+
namespace machine_learning
10+
{
11+
template<typename T>
12+
Result<std::monostate> normalize_vector_min_max(std::vector<T>& values)
13+
{
14+
// Ensure T is a numeric type
15+
static_assert(std::is_arithmetic<T>::value, "Vector elements must be numeric.");
16+
17+
if (!values.empty())
18+
{
19+
const auto min_val = *std::min_element(values.begin(), values.end());
20+
const auto max_val = *std::max_element(values.begin(), values.end());
21+
22+
// Avoid division by zero if all elements are the same
23+
if (min_val == max_val)
24+
{
25+
values.assign(values.size(), static_cast<T>(0.5));
26+
return OK({});
27+
}
28+
29+
// Apply min-max normalization
30+
for (auto& value : values)
31+
{
32+
value = (value - min_val) / (max_val - min_val);
33+
}
34+
}
35+
36+
return OK({});
37+
}
38+
39+
} // namespace machine_learning
40+
} // namespace hal

plugins/machine_learning/scripts/hal_testing/gui_test_pybinds_gate_feature.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,57 @@
44
from hal_plugins import machine_learning
55

66
# Create the feature context with the netlist
7-
fc = machine_learning.gate_feature.FeatureContext(netlist)
7+
fc = machine_learning.Context(netlist)
88

9-
# Instantiate all available gate pair features
10-
connected_global_ios = machine_learning.gate_feature.ConnectedGlobalIOs()
11-
distance_global_io = machine_learning.gate_feature.DistanceGlobalIO(hal_py.PinDirection.output)
12-
sequnetial_distance_global_io = machine_learning.gate_feature.SequentialDistanceGlobalIO(hal_py.PinDirection.output)
13-
io_degrees = machine_learning.gate_feature.IODegrees()
14-
gate_type_one_hot = machine_learning.gate_feature.GateTypeOneHot()
15-
neighboring_gate_types = machine_learning.gate_feature.NeighboringGateTypes(2, hal_py.PinDirection.output)
169

17-
# Collect all features into a list
1810
features = [
19-
connected_global_ios,
20-
distance_global_io,
21-
#sequnetial_distance_global_io,
22-
io_degrees,
23-
#gate_type_one_hot,
24-
#neighboring_gate_types,
11+
machine_learning.GateFeature.GateFeatureSingle.ConnectedGlobalIOs(),
12+
13+
machine_learning.GateFeature.GateFeatureSingle.DistanceGlobalIO(hal_py.PinDirection.output, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
14+
machine_learning.GateFeature.GateFeatureSingle.DistanceGlobalIO(hal_py.PinDirection.output, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
15+
machine_learning.GateFeature.GateFeatureSingle.DistanceGlobalIO(hal_py.PinDirection.input, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
16+
machine_learning.GateFeature.GateFeatureSingle.DistanceGlobalIO(hal_py.PinDirection.input, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
17+
18+
machine_learning.GateFeature.GateFeatureSingle.SequentialDistanceGlobalIO(hal_py.PinDirection.output, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
19+
machine_learning.GateFeature.GateFeatureSingle.SequentialDistanceGlobalIO(hal_py.PinDirection.output, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
20+
machine_learning.GateFeature.GateFeatureSingle.SequentialDistanceGlobalIO(hal_py.PinDirection.input, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
21+
machine_learning.GateFeature.GateFeatureSingle.SequentialDistanceGlobalIO(hal_py.PinDirection.input, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
22+
23+
machine_learning.GateFeature.GateFeatureSingle.IODegrees(),
24+
25+
machine_learning.GateFeature.GateFeatureSingle.GateTypeOneHot(),
26+
27+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(1, hal_py.PinDirection.output, directed=True),
28+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(2, hal_py.PinDirection.output, directed=True),
29+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(3, hal_py.PinDirection.output, directed=True),
30+
31+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(1, hal_py.PinDirection.input, directed=True),
32+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(2, hal_py.PinDirection.input, directed=True),
33+
machine_learning.GateFeature.GateFeatureSingle.NeighboringGateTypes(3, hal_py.PinDirection.input, directed=True),
34+
35+
machine_learning.GateFeature.GateFeatureBulk.BetweennessCentrality(directed = True, cutoff=-1),
36+
machine_learning.GateFeature.GateFeatureBulk.BetweennessCentrality(directed = True, cutoff=16),
37+
machine_learning.GateFeature.GateFeatureBulk.BetweennessCentrality(directed = False, cutoff=-1),
38+
machine_learning.GateFeature.GateFeatureBulk.BetweennessCentrality(directed = False, cutoff=16),
39+
machine_learning.GateFeature.GateFeatureBulk.SequentialBetweennessCentrality(directed = True, cutoff=-1),
40+
machine_learning.GateFeature.GateFeatureBulk.SequentialBetweennessCentrality(directed = True, cutoff=16),
41+
machine_learning.GateFeature.GateFeatureBulk.SequentialBetweennessCentrality(directed = False, cutoff=-1),
42+
machine_learning.GateFeature.GateFeatureBulk.SequentialBetweennessCentrality(directed = False, cutoff=16),
43+
44+
machine_learning.GateFeature.GateFeatureBulk.HarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=-1),
45+
machine_learning.GateFeature.GateFeatureBulk.HarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=16),
46+
machine_learning.GateFeature.GateFeatureBulk.HarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=-1),
47+
machine_learning.GateFeature.GateFeatureBulk.HarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=16),
48+
machine_learning.GateFeature.GateFeatureBulk.SequentialHarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=-1),
49+
machine_learning.GateFeature.GateFeatureBulk.SequentialHarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=16),
50+
machine_learning.GateFeature.GateFeatureBulk.SequentialHarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=-1),
51+
machine_learning.GateFeature.GateFeatureBulk.SequentialHarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=16),
2552
]
2653

27-
gate_a = netlist.get_gate_by_id(21)
54+
#gates = [netlist.get_gate_by_id(21)]
55+
gates = netlist.get_gates(lambda g: g.type.has_property(hal_py.sequential))
2856

2957
# Build the feature vector for the pair of gates
30-
feature_vector = machine_learning.gate_feature.build_feature_vec(fc, features, gate_a)
58+
feature_vector = machine_learning.gate_feature.build_feature_vecs(fc, features, gates)
3159

3260
print("Feature vector:", feature_vector)

plugins/machine_learning/src/features/gate_feature.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace hal
1515
{
1616
namespace gate_feature
1717
{
18-
Result<std::vector<std::vector<FEATURE_TYPE>>> build_feature_vecs(const std::vector<const GateFeatureBulk*>& features, const std::vector<Gate*>& gates)
18+
Result<std::vector<std::vector<FEATURE_TYPE>>> build_feature_vecs(const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates)
1919
{
2020
if (gates.empty())
2121
{
@@ -26,7 +26,7 @@ namespace hal
2626
return build_feature_vecs(features, gates);
2727
}
2828

29-
Result<std::vector<std::vector<FEATURE_TYPE>>> build_feature_vecs(Context& ctx, const std::vector<const GateFeatureBulk*>& features, const std::vector<Gate*>& gates)
29+
Result<std::vector<std::vector<FEATURE_TYPE>>> build_feature_vecs(Context& ctx, const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates)
3030
{
3131
std::vector<std::vector<FEATURE_TYPE>> feature_vecs(gates.size(), std::vector<FEATURE_TYPE>());
3232

@@ -50,5 +50,5 @@ namespace hal
5050
return OK(feature_vecs);
5151
}
5252
} // namespace gate_feature
53-
} // namespace machine_learning
53+
} // namespace machine_learning
5454
} // namespace hal

0 commit comments

Comments
 (0)