Skip to content

Commit

Permalink
fixed error in netlist abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKlx committed Jan 13, 2025
1 parent af446e6 commit 9136dc9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

# Example of using build_feature_vec for gate_feature with all available features

from hal_plugins import machine_learning

# Create the feature context with the netlist
fc = machine_learning.Context(netlist)


features = [
#machine_learning.gate_feature.ConnectedGlobalIOs(),

machine_learning.gate_feature.DistanceGlobalIO(hal_py.PinDirection.output, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
machine_learning.gate_feature.DistanceGlobalIO(hal_py.PinDirection.output, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
machine_learning.gate_feature.DistanceGlobalIO(hal_py.PinDirection.input, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
machine_learning.gate_feature.DistanceGlobalIO(hal_py.PinDirection.input, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),

# machine_learning.gate_feature.SequentialDistanceGlobalIO(hal_py.PinDirection.output, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
# machine_learning.gate_feature.SequentialDistanceGlobalIO(hal_py.PinDirection.output, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
# machine_learning.gate_feature.SequentialDistanceGlobalIO(hal_py.PinDirection.input, directed=True, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),
# machine_learning.gate_feature.SequentialDistanceGlobalIO(hal_py.PinDirection.input, directed=False, forbidden_pin_types=[hal_py.PinType.clock, hal_py.PinType.reset, hal_py.PinType.enable]),

# machine_learning.gate_feature.IODegrees(),

# machine_learning.gate_feature.GateTypeOneHot(),

# machine_learning.gate_feature.NeighboringGateTypes(1, hal_py.PinDirection.output, directed=True),
# machine_learning.gate_feature.NeighboringGateTypes(2, hal_py.PinDirection.output, directed=True),
# machine_learning.gate_feature.NeighboringGateTypes(3, hal_py.PinDirection.output, directed=True),

# machine_learning.gate_feature.NeighboringGateTypes(1, hal_py.PinDirection.input, directed=True),
# machine_learning.gate_feature.NeighboringGateTypes(2, hal_py.PinDirection.input, directed=True),
# machine_learning.gate_feature.NeighboringGateTypes(3, hal_py.PinDirection.input, directed=True),

# machine_learning.gate_feature.BetweennessCentrality(directed = True, cutoff=-1),
# machine_learning.gate_feature.BetweennessCentrality(directed = True, cutoff=16),
# machine_learning.gate_feature.BetweennessCentrality(directed = False, cutoff=-1),
# machine_learning.gate_feature.BetweennessCentrality(directed = False, cutoff=16),
# machine_learning.gate_feature.SequentialBetweennessCentrality(directed = True, cutoff=-1),
# machine_learning.gate_feature.SequentialBetweennessCentrality(directed = True, cutoff=16),
# machine_learning.gate_feature.SequentialBetweennessCentrality(directed = False, cutoff=-1),
# machine_learning.gate_feature.SequentialBetweennessCentrality(directed = False, cutoff=16),

# machine_learning.gate_feature.HarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=-1),
# machine_learning.gate_feature.HarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=16),
# machine_learning.gate_feature.HarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=-1),
# machine_learning.gate_feature.HarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=16),
# machine_learning.gate_feature.SequentialHarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=-1),
# machine_learning.gate_feature.SequentialHarmonicCentrality(direction=hal_py.PinDirection.output, cutoff=16),
# machine_learning.gate_feature.SequentialHarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=-1),
# machine_learning.gate_feature.SequentialHarmonicCentrality(direction=hal_py.PinDirection.inout, cutoff=16),
]

gates = [netlist.get_gate_by_id(3)]

# Build the feature vector for the pair of gates
feature_vector = machine_learning.gate_feature.build_feature_vecs(fc, features, gates)

print("Feature vector:", feature_vector)
10 changes: 9 additions & 1 deletion plugins/machine_learning/src/features/gate_feature_single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ namespace hal
log_error("machine_learning", "{}", global_io_connections.get_error().get());
return false;
}

// TODO remove debug print
// if (!global_io_connections.get().empty())
// {
// std::cout << "Global IO connections: " << global_io_connections.get().front()->get_name() << std::endl;
// std::cout << "Endpoint: " << ep->get_pin()->get_name() << std::endl;
// }

return !global_io_connections.get().empty();
},
m_direction,
Expand Down Expand Up @@ -324,5 +332,5 @@ namespace hal
return OK(feature_vecs);
}
} // namespace gate_feature
} // namespace machine_learning
} // namespace machine_learning
} // namespace hal
105 changes: 57 additions & 48 deletions src/netlist/decorators/netlist_abstraction_decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,26 @@ namespace hal
// std::cout << ep_out->get_pin()->get_name() << std::endl;

new_abstraction->m_successors.insert({ep_out, {}});
new_abstraction->m_global_output_successors.insert({ep_out, {}});

const auto successors = nl_trav_dec.get_next_matching_endpoints(
ep_out,
true,
[target_gates_set](const auto& ep) { return ep->is_destination_pin() && target_gates_set.find(ep->get_gate()) != target_gates_set.end(); },
[target_gates_set](const auto& ep) {
bool found_target_gate = ep->is_destination_pin() && target_gates_set.find(ep->get_gate()) != target_gates_set.end();
if (found_target_gate)
{
return true;
}

bool found_global_output = ep->is_source_pin() && ep->get_net()->is_global_output_net();
if (found_global_output)
{
return true;
}

return false;
},
false,
exit_endpoint_filter,
entry_endpoint_filter);
Expand All @@ -59,66 +75,59 @@ namespace hal

for (Endpoint* ep : successors.get())
{
new_abstraction->m_successors.at(ep_out).push_back(ep);
}
}

// gather all global output succesors
for (Endpoint* ep_out : gate->get_fan_out_endpoints())
{
new_abstraction->m_global_output_successors.insert({ep_out, {}});

const auto destinations = nl_trav_dec.get_next_matching_endpoints(
ep_out, true, [](const auto& ep) { return ep->is_source_pin() && ep->get_net()->is_global_output_net(); }, false, exit_endpoint_filter, entry_endpoint_filter);

if (destinations.is_error())
{
return ERR_APPEND(destinations.get_error(),
"cannot build netlist abstraction: failed to gather global succesor endpoints for gate " + gate->get_name() + " with ID " + std::to_string(gate->get_id()));
}

for (const auto* ep : destinations.get())
{
new_abstraction->m_global_output_successors.at(ep_out).push_back({ep->get_net()});
if (ep->is_destination_pin())
{
new_abstraction->m_successors.at(ep_out).push_back(ep);
}
else if (ep->is_source_pin())
{
new_abstraction->m_global_output_successors.at(ep_out).push_back(ep->get_net());
}
}
}

// gather all predecessors
for (Endpoint* ep_in : gate->get_fan_in_endpoints())
{
new_abstraction->m_predecessors.insert({ep_in, {}});
new_abstraction->m_global_input_predecessors.insert({ep_in, {}});

const auto predecessors = nl_trav_dec.get_next_matching_endpoints(
ep_in, false, [target_gates_set](const auto& ep) { return ep->is_source_pin() && target_gates_set.find(ep->get_gate()) != target_gates_set.end(); });

if (predecessors.is_error())
{
return ERR_APPEND(predecessors.get_error(),
"cannot build netlist abstraction: failed to gather predecessor endpoints for gate " + gate->get_name() + " with ID " + std::to_string(gate->get_id()));
}
const auto predecessors = nl_trav_dec.get_next_matching_endpoints(ep_in,
false,
[target_gates_set](const auto& ep) {
bool found_target_gate = ep->is_source_pin() && target_gates_set.find(ep->get_gate()) != target_gates_set.end();
if (found_target_gate)
{
return true;
}

for (Endpoint* ep : predecessors.get())
{
new_abstraction->m_predecessors.at(ep_in).push_back(ep);
}
}
bool found_global_input = ep->is_destination_pin() && ep->get_net()->is_global_input_net();
if (found_global_input)
{
return true;
}

// gather all global input predecessors
for (Endpoint* ep_in : gate->get_fan_in_endpoints())
{
new_abstraction->m_global_input_predecessors.insert({ep_in, {}});
return false;
}

const auto predecessors = nl_trav_dec.get_next_matching_endpoints(ep_in, false, [](const auto& ep) { return ep->is_destination_pin() && ep->get_net()->is_global_input_net(); });
);

if (predecessors.is_error())
{
return ERR_APPEND(predecessors.get_error(),
"cannot build netlist abstraction: failed to gather global predecessor endpoints for gate " + gate->get_name() + " with ID " + std::to_string(gate->get_id()));
"cannot build netlist abstraction: failed to gather predecessor endpoints for gate " + gate->get_name() + " with ID " + std::to_string(gate->get_id()));
}

for (const auto* ep : predecessors.get())
for (Endpoint* ep : predecessors.get())
{
new_abstraction->m_global_input_predecessors.at(ep_in).push_back({ep->get_net()});
if (ep->is_source_pin())
{
new_abstraction->m_predecessors.at(ep_in).push_back(ep);
}
else if (ep->is_destination_pin())
{
new_abstraction->m_global_input_predecessors.at(ep_in).push_back(ep->get_net());
}
}
}
}
Expand Down Expand Up @@ -396,11 +405,11 @@ namespace hal
for (const auto& exit_ep : current)
{
// currently only works for input and output pins
if (exit_ep->get_pin()->get_direction() != PinDirection::output && exit_ep->get_pin()->get_direction() != PinDirection::input)
{
return ERR("failed to get shortest path distance: found endpoint at gate " + exit_ep->get_gate()->get_name() + " with ID " + std::to_string(exit_ep->get_gate()->get_id())
+ " and pin " + exit_ep->get_pin()->get_name() + " with direction " + enum_to_string(exit_ep->get_pin()->get_direction()) + " that is currently unhandled");
}
// if (exit_ep->get_pin()->get_direction() != PinDirection::output && exit_ep->get_pin()->get_direction() != PinDirection::input)
// {
// return ERR("failed to get shortest path distance: found endpoint at gate " + exit_ep->get_gate()->get_name() + " with ID " + std::to_string(exit_ep->get_gate()->get_id())
// + " and pin " + exit_ep->get_pin()->get_name() + " with direction " + enum_to_string(exit_ep->get_pin()->get_direction()) + " that is currently unhandled");
// }

const auto entry_eps = (exit_ep->get_pin()->get_direction() == PinDirection::output) ? m_abstraction.get_successors(exit_ep) : m_abstraction.get_predecessors(exit_ep);
if (entry_eps.is_error())
Expand Down

0 comments on commit 9136dc9

Please sign in to comment.