Skip to content

Commit

Permalink
[Subgraph] rename GraphRepresentation to GraphLayerNodeRepresentation
Browse files Browse the repository at this point in the history
- This commit renames the alias for `GraphRepresentation` to
  `GraphLayerNodeRepresentation`
- The reason we do this is to update the GraphRepresentation as the
  serialized SubGraphBase pointer, not LayerNode

Signed-off-by: Eunju Yang <[email protected]>
  • Loading branch information
EunjuYang committed Mar 6, 2025
1 parent deeae64 commit d19df9c
Show file tree
Hide file tree
Showing 38 changed files with 179 additions and 165 deletions.
12 changes: 5 additions & 7 deletions nntrainer/compiler/activation_realizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ namespace nntrainer {

ActivationRealizer::~ActivationRealizer() {}

GraphRepresentation
ActivationRealizer::realize(const GraphRepresentation &reference) {
GraphRepresentation processed;
GraphLayerNodeRepresentation
ActivationRealizer::realize(const GraphLayerNodeRepresentation &reference) {
GraphLayerNodeRepresentation processed;
processed.reserve(reference.size());

std::unordered_map<std::string /**< layer_name */,
Expand Down Expand Up @@ -73,15 +73,13 @@ ActivationRealizer::realize(const GraphRepresentation &reference) {
if (auto iter = remap_table.find(name); iter != remap_table.end()) {
name = iter->second;
}
})
.realize(processed);
}).realize(processed);
processed =
RemapRealizer([&recovery_table](std::string &name, unsigned &idx) {
if (auto iter = recovery_table.find(name); iter != recovery_table.end()) {
name = iter->second;
}
})
.realize(processed);
}).realize(processed);

return processed;
}
Expand Down
3 changes: 2 additions & 1 deletion nntrainer/compiler/activation_realizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ActivationRealizer final : public GraphRealizer {
* @brief graph realizer creates a new graph based on the reference
*
*/
GraphRepresentation realize(const GraphRepresentation &reference) override;
GraphLayerNodeRepresentation
realize(const GraphLayerNodeRepresentation &reference) override;
};

} // namespace nntrainer
Expand Down
5 changes: 3 additions & 2 deletions nntrainer/compiler/bn_realizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace nntrainer {

static constexpr size_t SINGLE_INOUT_IDX = 0;

GraphRepresentation BnRealizer::realize(const GraphRepresentation &reference) {
GraphLayerNodeRepresentation
BnRealizer::realize(const GraphLayerNodeRepresentation &reference) {
std::unordered_map<std::string, LayerNode *> existing_nodes;
std::vector<LayerNode *> bn_layers;

Expand Down Expand Up @@ -64,7 +65,7 @@ GraphRepresentation BnRealizer::realize(const GraphRepresentation &reference) {
}
}

GraphRepresentation processed;
GraphLayerNodeRepresentation processed;
for (auto &node : reference) {
if (!istrequal(node->getType(), "batch_normalization")) {
processed.push_back(node);
Expand Down
7 changes: 4 additions & 3 deletions nntrainer/compiler/bn_realizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class BnRealizer final : public GraphRealizer {
/**
* @brief graph realizer creates a shallow copied graph based on the reference
* @note bn realizer removes batch normalization layers from
* GraphRepresentation
* @param reference GraphRepresentation to be realized
* GraphLayerNodeRepresentation
* @param reference GraphLayerNodeRepresentation to be realized
* @throw std::invalid_argument if graph is ill formed
*
*/
GraphRepresentation realize(const GraphRepresentation &reference) override;
GraphLayerNodeRepresentation
realize(const GraphLayerNodeRepresentation &reference) override;
};

} // namespace nntrainer
Expand Down
36 changes: 18 additions & 18 deletions nntrainer/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
* appropriate compiler and interpreter
* For example, if istream would be from a a.tflite file,
*
* GraphRepresentation g;
* GraphLayerNodeRepresentation g;
* GraphCompiler * compiler = new NNTrainerCPUCompiler;
*
* ExecutableGraph eg = compiler->compile(g);
*
*
* +-------+--+--------+
* |GraphRepresentation|
* +-------+-----------+
* | ^
* compile() | |
* | |
* (Compiler)
* | |
* | | decompile()
* v |
* +--------+------+
* |ExecutableGraph|
* +---------------+
* +-----------+---+------------+
* |GraphLayerNodeRepresentation|
* +-------+--------------------+
* | ^
* compile() | |
* | |
* (Compiler)
* | |
* | | decompile()
* v |
* +--------+------+
* |ExecutableGraph|
* +---------------+
*
*/
#ifndef __COMPILER_H__
Expand Down Expand Up @@ -59,16 +59,16 @@ class GraphCompiler {
* @param representation graph representation
* @param file ifstream to serialize graph
*/
virtual std::shared_ptr<ExecutableGraph>
compile(std::shared_ptr<const GraphRepresentation> representation) = 0;
virtual std::shared_ptr<ExecutableGraph> compile(
std::shared_ptr<const GraphLayerNodeRepresentation> representation) = 0;

/**
* @brief deserialize graph from a file stream
*
* @param executable executable graph
* @return GraphRepresentation graph representation
* @return GraphLayerNodeRepresentation graph representation
*/
virtual std::shared_ptr<GraphRepresentation>
virtual std::shared_ptr<GraphLayerNodeRepresentation>
decompile(std::shared_ptr<ExecutableGraph> executable) = 0;
};

Expand Down
2 changes: 1 addition & 1 deletion nntrainer/compiler/compiler_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace nntrainer {
class LayerNode;
class NetworkGraph;

using GraphRepresentation = std::vector<std::shared_ptr<LayerNode>>;
using GraphLayerNodeRepresentation = std::vector<std::shared_ptr<LayerNode>>;
using ExecutableGraph = NetworkGraph;

} // namespace nntrainer
Expand Down
4 changes: 2 additions & 2 deletions nntrainer/compiler/flatbuffer_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class FlatBufferInterpreter : public GraphInterpreter {
/**
* @copydoc GraphInterpreter::serialize(const std::string &out)
*/
void serialize(const GraphRepresentation &representation,
void serialize(const GraphLayerNodeRepresentation &representation,
const std::string &out) override;

/**
* @copydoc GraphInterpreter::deserialize(const std::string &in)
*/
GraphRepresentation deserialize(const std::string &in) override;
GraphLayerNodeRepresentation deserialize(const std::string &in) override;

private:
AppContext &app_context;
Expand Down
12 changes: 5 additions & 7 deletions nntrainer/compiler/flatten_realizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace nntrainer {

FlattenRealizer::~FlattenRealizer() {}

GraphRepresentation
FlattenRealizer::realize(const GraphRepresentation &reference) {
GraphRepresentation processed;
GraphLayerNodeRepresentation
FlattenRealizer::realize(const GraphLayerNodeRepresentation &reference) {
GraphLayerNodeRepresentation processed;
processed.reserve(reference.size());

std::unordered_map<std::string /**< layer_name */,
Expand Down Expand Up @@ -59,15 +59,13 @@ FlattenRealizer::realize(const GraphRepresentation &reference) {
if (auto iter = remap_table.find(name); iter != remap_table.end()) {
name = iter->second;
}
})
.realize(processed);
}).realize(processed);
processed =
RemapRealizer([&recovery_table](std::string &name, unsigned &idx) {
if (auto iter = recovery_table.find(name); iter != recovery_table.end()) {
name = iter->second;
}
})
.realize(processed);
}).realize(processed);

return processed;
}
Expand Down
3 changes: 2 additions & 1 deletion nntrainer/compiler/flatten_realizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class FlattenRealizer final : public GraphRealizer {
* @brief graph realizer creates a new graph based on the reference
*
*/
GraphRepresentation realize(const GraphRepresentation &reference) override;
GraphLayerNodeRepresentation
realize(const GraphLayerNodeRepresentation &reference) override;
};

} // namespace nntrainer
Expand Down
11 changes: 6 additions & 5 deletions nntrainer/compiler/ini_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ referenced
* @return std::vector<std::shared_ptr<Layer>> mergeable graph
*/
// std::vector<std::shared_ptr<LayerNode>>
// getMergeableGraph(const GraphRepresentation& graph,
// getMergeableGraph(const GraphLayerNodeRepresentation& graph,
// dictionary *ini, const std::string &sec_name) {
// std::string input_layer =
// iniparser_getstring(ini, (sec_name + ":InputLayer").c_str(), "");
Expand Down Expand Up @@ -263,8 +263,8 @@ referenced

} // namespace

void IniGraphInterpreter::serialize(const GraphRepresentation &representation,
const std::string &out) {
void IniGraphInterpreter::serialize(
const GraphLayerNodeRepresentation &representation, const std::string &out) {

std::vector<IniSection> sections;
for (auto iter = representation.cbegin(); iter != representation.cend();
Expand All @@ -281,7 +281,8 @@ void IniGraphInterpreter::serialize(const GraphRepresentation &representation,
ini.save_ini(out);
}

GraphRepresentation IniGraphInterpreter::deserialize(const std::string &in) {
GraphLayerNodeRepresentation
IniGraphInterpreter::deserialize(const std::string &in) {
NNTR_THROW_IF(in.empty(), std::invalid_argument)
<< FUNC_TAG << "given in file is empty";

Expand All @@ -298,7 +299,7 @@ GraphRepresentation IniGraphInterpreter::deserialize(const std::string &in) {
NNTR_THROW_IF_CLEANUP(num_ini_sec < 0, std::invalid_argument, freedict)
<< FUNC_TAG << "invalid number of sections.";

GraphRepresentation graph;
GraphLayerNodeRepresentation graph;

try {
ml_logi("==========================parsing ini...");
Expand Down
6 changes: 3 additions & 3 deletions nntrainer/compiler/ini_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class IniGraphInterpreter : public GraphInterpreter {
virtual ~IniGraphInterpreter();

/**
* @copydoc GraphInterpreter::serialize(const GraphRepresentation
* @copydoc GraphInterpreter::serialize(const GraphLayerNodeRepresentation
* representation, const std::string &out)
*/
void serialize(const GraphRepresentation &representation,
void serialize(const GraphLayerNodeRepresentation &representation,
const std::string &out) override;

/**
* @copydoc GraphInterpreter::deserialize(const std::string &in)
*/
GraphRepresentation deserialize(const std::string &in) override;
GraphLayerNodeRepresentation deserialize(const std::string &in) override;

private:
AppContext app_context;
Expand Down
7 changes: 3 additions & 4 deletions nntrainer/compiler/input_realizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
namespace nntrainer {
InputRealizer::InputRealizer(const std::vector<Connection> &start_conns,
const std::vector<Connection> &input_conns) :
start_conns(start_conns),
input_conns(input_conns) {
start_conns(start_conns), input_conns(input_conns) {
NNTR_THROW_IF(start_conns.size() != input_conns.size(), std::invalid_argument)
<< "start connection size is not same input_conns size";
}

InputRealizer::~InputRealizer() {}

GraphRepresentation
InputRealizer::realize(const GraphRepresentation &reference) {
GraphLayerNodeRepresentation
InputRealizer::realize(const GraphLayerNodeRepresentation &reference) {
std::unordered_map<std::string, LayerNode *> existing_nodes;

std::transform(
Expand Down
3 changes: 2 additions & 1 deletion nntrainer/compiler/input_realizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class InputRealizer final : public GraphRealizer {
* @throw std::invalid_argument if graph is ill formed
*
*/
GraphRepresentation realize(const GraphRepresentation &reference) override;
GraphLayerNodeRepresentation
realize(const GraphLayerNodeRepresentation &reference) override;

private:
std::vector<Connection> start_conns;
Expand Down
38 changes: 19 additions & 19 deletions nntrainer/compiler/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* @file interpreter.h
* @date 01 April 2021
* @brief NNTrainer interpreter that reads and generates a graphRepresentation
from a file
* @brief NNTrainer interpreter that reads and generates a
GraphLayerNodeRepresentation from a file
* @see https://github.com/nnstreamer/nntrainer
* @author Jihoon Lee <[email protected]>
* @bug No known bugs except for NYI items
Expand All @@ -15,25 +15,25 @@
interpreter
* For example, if istream would be from a a.tflite file,
*
* GraphRepresentation g;
* GraphLayerNodeRepresentation g;
* GraphInterpreter * interpreter = new TfliteInterpreter;
*
* std::ifstream f = std::open("a.tflite");
* g = interpreter->serialize(f);
*
* +--------+
* |iostream|
* +--+--+--+
* ^ |
* serialize()| |
* | |
* (Interpreter)
* | |
* | | deserialize()
* | v
* +-------+--+--------+
* |GraphRepresentation|
* +-------+-----------+
* +--------+
* |iostream|
* +--+--+--+
* ^ |
* serialize()| |
* | |
* (Interpreter)
* | |
* | | deserialize()
* | v
* +-----------+---+------------+
* |GraphLayerNodeRepresentation|
* +-----------+---+------------+
*
*/
#ifndef __INTERPRETER_H__
Expand All @@ -60,7 +60,7 @@ class GraphInterpreter {
* @param representation graph representation
* @param out output file name
*/
virtual void serialize(const GraphRepresentation &representation,
virtual void serialize(const GraphLayerNodeRepresentation &representation,
const std::string &out) = 0;

/**
Expand All @@ -86,9 +86,9 @@ class GraphInterpreter {
* @brief deserialize graph from a stream
*
* @param in input file name
* @return GraphRepresentation graph representation
* @return GraphLayerNodeRepresentation graph representation
*/
virtual GraphRepresentation deserialize(const std::string &in) = 0;
virtual GraphLayerNodeRepresentation deserialize(const std::string &in) = 0;
};

} // namespace nntrainer
Expand Down
6 changes: 3 additions & 3 deletions nntrainer/compiler/loss_realizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace nntrainer {

static constexpr size_t SINGLE_IN_IDX = 0;

GraphRepresentation
LossRealizer::realize(const GraphRepresentation &reference) {
GraphLayerNodeRepresentation
LossRealizer::realize(const GraphLayerNodeRepresentation &reference) {
/// @todo support more loss layers
/// @note Some layers need to consider not removing all semantics.
/// For example, When CrossEntropySigmoidLossLayer needs to be removed,
Expand Down Expand Up @@ -61,7 +61,7 @@ LossRealizer::realize(const GraphRepresentation &reference) {
}
}

GraphRepresentation processed;
GraphLayerNodeRepresentation processed;
for (auto &node : reference) {
if (loss_type.find(node->getType()) == loss_type.end()) {
processed.push_back(node);
Expand Down
Loading

0 comments on commit d19df9c

Please sign in to comment.