Skip to content

Commit

Permalink
first version of the machine learning plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKlx committed Nov 5, 2024
1 parent 07a5251 commit bc64da8
Show file tree
Hide file tree
Showing 26 changed files with 3,256 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
!liberty_parser/**/*
!logic_evaluator*
!logic_evaluator/**/*
!machine_learning*
!machine_learning/**/*
!module_identification*
!module_identification/**/*
!netlist_preprocessing*
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions plugins/machine_learning/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
option(PL_MACHINE_LEARNING "PL_MACHINE_LEARNING" OFF)

if(PL_MACHINE_LEARNING OR BUILD_ALL_PLUGINS)
file(GLOB_RECURSE MACHINE_LEARNING_INC ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
file(GLOB_RECURSE MACHINE_LEARNING_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE MACHINE_LEARNING_PYTHON_SRC ${CMAKE_CURRENT_SOURCE_DIR}/python/*.cpp)

hal_add_plugin(machine_learning
SHARED
HEADER ${MACHINE_LEARNING_INC}
SOURCES ${MACHINE_LEARNING_SRC} ${MACHINE_LEARNING_PYTHON_SRC}
LINK_LIBRARIES nlohmann_json::nlohmann_json
)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "hal_core/defines.h"
#include "hal_core/netlist/decorators/netlist_abstraction_decorator.h"

#include <optional>
#include <vector>

namespace hal
{
/* Forward declaration */
class Gate;
class Netlist;

namespace machine_learning
{
namespace gate_feature
{

struct FeatureContext
{
public:
FeatureContext() = delete;
FeatureContext(const Netlist* netlist) : nl(netlist) {};

const NetlistAbstraction& get_sequential_abstraction();
const std::vector<GateTypeProperty>& get_possible_gate_type_properties();

const Netlist* nl;

private:
std::optional<NetlistAbstraction> m_seqential_abstraction;
std::optional<std::vector<GateTypeProperty>> m_possible_gate_type_properties;
};

class GateFeature
{
public:
virtual std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const = 0;
virtual std::string get_name() const = 0;
};

class ConnectedGlobalIOs : public GateFeature
{
public:
ConnectedGlobalIOs() {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;
};

class DistanceGlobalIO : public GateFeature
{
public:
DistanceGlobalIO(const PinDirection& direction) : m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;

private:
const PinDirection m_direction;
};

class SequentialDistanceGlobalIO : public GateFeature
{
public:
SequentialDistanceGlobalIO(const PinDirection& direction) : m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;

private:
const PinDirection m_direction;
};

class IODegrees : public GateFeature
{
public:
IODegrees() {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;
};

class GateTypeOneHot : public GateFeature
{
public:
GateTypeOneHot() {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;
};

class NeighboringGateTypes : public GateFeature
{
public:
NeighboringGateTypes(const u32 depth, const PinDirection& direction) : m_depth(depth), m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g) const override;
std::string get_name() const override;

private:
const u32 m_depth;
const PinDirection m_direction;
};

// Feature ideas

// number of sequential predecessors/successors
// graph metrics (centrality)

// distance to global io in sequential only netlist

// distance to nearest type/module (e.g. RAM, DSP)
// distance to nearest shift register
// distance to nearest bus register

std::vector<u32> build_feature_vec(const std::vector<const GateFeature*>& features, const Gate* g);
std::vector<u32> build_feature_vec(FeatureContext& fc, const std::vector<const GateFeature*>& features, const Gate* g);

std::vector<std::vector<u32>> build_feature_vecs(const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates);
std::vector<std::vector<u32>> build_feature_vecs(FeatureContext& fc, const std::vector<const GateFeature*>& features, const std::vector<Gate*>& gates);
} // namespace gate_feature
} // namespace machine_learning
} // namespace hal
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once

#include "hal_core/defines.h"
#include "hal_core/netlist/decorators/netlist_abstraction_decorator.h"
#include "machine_learning/types.h"

#include <optional>
#include <vector>

namespace hal
{
/* Forward declaration */
class Gate;
class Netlist;

namespace machine_learning
{
namespace gate_pair_feature
{

struct FeatureContext
{
public:
FeatureContext() = delete;
FeatureContext(const Netlist* netlist) : nl(netlist) {};

const NetlistAbstraction& get_sequential_abstraction();

const Netlist* nl;

private:
std::optional<NetlistAbstraction> m_seqential_abstraction;
};

class GatePairFeature
{
public:
virtual std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const = 0;
virtual std::string get_name() const = 0;
};

class LogicalDistance : public GatePairFeature
{
public:
LogicalDistance(const PinDirection direction) : m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;

private:
const PinDirection m_direction;
};

class SequentialDistance : public GatePairFeature
{
public:
SequentialDistance(const PinDirection direction) : m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;

private:
const PinDirection m_direction;
};

class PhysicalDistance : public GatePairFeature
{
public:
PhysicalDistance() {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;
};

class SharedControlSignals : public GatePairFeature
{
public:
SharedControlSignals() {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;
};

class SharedSequentialNeighbors : public GatePairFeature
{
public:
SharedSequentialNeighbors(const u32 depth, const PinDirection direction) : m_depth(depth), m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;

private:
const u32 m_depth;
const PinDirection m_direction;
};

class SharedNeighbors : public GatePairFeature
{
public:
SharedNeighbors(const u32 depth, const PinDirection direction) : m_depth(depth), m_direction(direction) {};

std::vector<u32> calculate_feature(FeatureContext& fc, const Gate* g_a, const Gate* g_b) const override;
std::string get_name() const override;

private:
const u32 m_depth;
const PinDirection m_direction;
};

// feature ideas:

// distance to each other in a sequential only netlist
// shared neighbors in a sequential only netlist

std::vector<u32> build_feature_vec(const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);
std::vector<u32> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const Gate* g_a, const Gate* g_b);

std::vector<u32> build_feature_vec(const std::vector<const GatePairFeature*>& features, const std::pair<const Gate*, const Gate*>& gate_pair);
std::vector<u32> build_feature_vec(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::pair<const Gate*, const Gate*>& gate_pair);

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);
std::vector<std::vector<u32>>
build_feature_vecs(FeatureContext& fc, const std::vector<const GatePairFeature*>& features, const std::vector<std::pair<const Gate*, const Gate*>>& gate_pairs);
} // namespace gate_pair_feature
} // namespace machine_learning
} // namespace hal
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "hal_core/defines.h"
#include "machine_learning/types.h"

#include <vector>

namespace hal
{
class Netlist;

namespace machine_learning
{
namespace graph
{
struct NetlistGraph
{
std::pair<std::vector<u32>, std::vector<u32>> edge_list;
GraphDirection direction;
};

NetlistGraph construct_netlist_graph(const Netlist* nl, const std::vector<Gate*>& gates, const GraphDirection& dir);

void annotate_netlist_graph(Netlist* nl, const std::vector<Gate*>& gates, const NetlistGraph& nlg, const std::vector<std::vector<u32>>& node_features);
} // namespace graph
} // namespace machine_learning
} // namespace hal
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include "hal_core/defines.h"
#include "machine_learning/types.h"

#include <map>
#include <optional>
#include <vector>

namespace hal
{
/* Forward declaration */
class Gate;
class Netlist;

namespace machine_learning
{
namespace gate_pair_label
{
struct MultiBitInformation
{
std::map<const std::pair<const std::string, const std::string>, std::vector<const Gate*>> word_to_gates;
std::map<const Gate*, std::vector<std::pair<const std::string, const std::string>>> gate_to_words;
};

struct LabelContext
{
LabelContext() = delete;
LabelContext(const Netlist* netlist, const std::vector<Gate*>& gates) : nl(netlist), gates{gates} {};

const MultiBitInformation& get_multi_bit_information();

const Netlist* nl;
const std::vector<Gate*> gates;
std::optional<MultiBitInformation> mbi;
};

class GatePairLabel
{
public:
virtual std::vector<std::pair<const Gate*, const Gate*>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const = 0;
virtual std::vector<u32> calculate_label(LabelContext& lc, const Gate* g_a, const Gate* g_b) const = 0;
virtual std::vector<std::vector<u32>> calculate_labels(LabelContext& lc, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) const = 0;

virtual std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>> calculate_labels(LabelContext& lc) const = 0;
};

class SharedSignalGroup : public GatePairLabel
{
public:
SharedSignalGroup() {};

std::vector<std::pair<const Gate*, const Gate*>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
std::vector<u32> calculate_label(LabelContext& lc, const Gate* g_a, const Gate* g_b) const override;
std::vector<std::vector<u32>> calculate_labels(LabelContext& lc, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) const override;

std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>> calculate_labels(LabelContext& lc) const override;
};

class SharedConnection : public GatePairLabel
{
public:
SharedConnection() {};

std::vector<std::pair<const Gate*, const Gate*>> calculate_gate_pairs(LabelContext& lc, const Netlist* nl, const std::vector<Gate*>& gates) const override;
std::vector<u32> calculate_label(LabelContext& lc, const Gate* g_a, const Gate* g_b) const override;
std::vector<std::vector<u32>> calculate_labels(LabelContext& lc, const std::vector<std::pair<Gate*, Gate*>>& gate_pairs) const override;

std::pair<std::vector<std::pair<const Gate*, const Gate*>>, std::vector<std::vector<u32>>> calculate_labels(LabelContext& lc) const override;
};
} // namespace gate_pair_label
} // namespace machine_learning
} // namespace hal
Loading

0 comments on commit bc64da8

Please sign in to comment.