$(NNTRAINER_ROOT)/nntrainer/compiler/ini_interpreter.cpp \
$(NNTRAINER_ROOT)/nntrainer/compiler/flatten_realizer.cpp \
$(NNTRAINER_ROOT)/nntrainer/compiler/recurrent_realizer.cpp \
+ $(NNTRAINER_ROOT)/nntrainer/compiler/remap_realizer.cpp \
$(NNTRAINER_ROOT)/nntrainer/app_context.cpp
ifeq ($(ENABLE_TFLITE_INTERPRETER), 1)
compiler_sources = [
'ini_interpreter.cpp',
'flatten_realizer.cpp',
- 'recurrent_realizer.cpp'
+ 'recurrent_realizer.cpp',
+ 'remap_realizer.cpp'
]
compiler_headers = []
--- /dev/null
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file remap_realizer.h
+ * @date 12 October 2021
+ * @brief NNTrainer graph realizer which realizes identifer to a new identifier
+ * @see https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+#include <remap_realizer.h>
+
+#include <layer_node.h>
+namespace nntrainer {
+
+RemapRealizer::RemapRealizer(
+ std::function<void(std::string &)> remap_function) :
+ remap_fn(remap_function) {
+ if (!remap_fn) {
+ throw std::invalid_argument("remap function is not given!");
+ }
+}
+
+RemapRealizer::~RemapRealizer() {}
+
+GraphRepresentation
+RemapRealizer::realize(const GraphRepresentation &reference) {
+ GraphRepresentation processed(reference.begin(), reference.end());
+ for (auto &node : processed) {
+ node->remapIdentifiers(remap_fn);
+ }
+ return processed;
+}
+
+} // namespace nntrainer
--- /dev/null
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file remap_realizer.h
+ * @date 12 October 2021
+ * @brief NNTrainer graph realizer which realizes identifer to a new identifier
+ * @see https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+#ifndef __REMAP_REALIZER_H__
+#define __REMAP_REALIZER_H__
+
+#include <functional>
+#include <memory>
+#include <vector>
+
+#include <realizer.h>
+
+namespace nntrainer {
+
+/**
+ * @brief Graph realizer class which remaps identifiers inside the graph
+ * representation, remap_function will be applied for all the layers identifier
+ * visible
+ *
+ */
+class RemapRealizer final : public GraphRealizer {
+public:
+ RemapRealizer(std::function<void(std::string &)> remap_function);
+ /**
+ * @brief Destroy the Graph Realizer object
+ *
+ */
+ ~RemapRealizer();
+
+ /**
+ * @brief graph realizer creates a new graph based on the reference
+ *
+ */
+ GraphRepresentation realize(const GraphRepresentation &reference) override;
+
+private:
+ std::function<void(std::string &)> remap_fn;
+};
+
+} // namespace nntrainer
+
+#endif // __REMAP_REALIZER_H__
print(out, flags);
}
+void LayerNode::remapIdentifiers(std::function<void(std::string &)> remap_fn) {
+ NNTR_THROW_IF(isFinalized(), std::invalid_argument)
+ << "cannot remap identifiers after finalized";
+
+ auto &name = std::get<props::Name>(*layer_node_props);
+ if (!name.empty()) {
+ remap_fn(name.get());
+ }
+
+ auto &shared_from = std::get<props::SharedFrom>(*layer_node_props);
+ if (!shared_from.empty()) {
+ remap_fn(shared_from.get());
+ }
+
+ auto &input_layers =
+ std::get<std::vector<props::InputLayer>>(*layer_node_props);
+
+ for (auto &input_layer : input_layers) {
+ remap_fn(input_layer);
+ }
+
+ for (auto &output_layer : output_layers) {
+ remap_fn(output_layer);
+ }
+}
+
void LayerNode::printShapeInfo(std::ostream &out) {
for (unsigned int idx = 0; idx < getNumInputs(); ++idx) {
out << "input " << run_context->getInput(idx).getDim();
void printPreset(std::ostream &out,
PrintPreset preset = PrintPreset::PRINT_SUMMARY);
+ /**
+ * @brief remap identifier inside layer node
+ *
+ * @param remap_fn function to remap
+ */
+ void remapIdentifiers(std::function<void(std::string &)> remap_fn);
+
private:
std::unique_ptr<nntrainer::Layer>
layer; /**< The actual object in the graph node */
#include <flatten_realizer.h>
#include <realizer.h>
#include <recurrent_realizer.h>
+#include <remap_realizer.h>
#include <compiler_test_util.h>
realizeAndEqual(r, {input1}, {input1});
}
+
+TEST(RemapRealizer, remap_p) {
+
+ RemapRealizer r([](std::string &name) { name = "scoped/" + name; });
+
+ LayerRepresentation input1 = {
+ "fully_connected", {"name=layer1", "flatten=true", "input_layers=1,2"}};
+
+ LayerRepresentation expected1 = {
+ "fully_connected",
+ {"name=scoped/layer1", "flatten=true", "input_layers=scoped/1,scoped/2"}};
+
+ realizeAndEqual(r, {input1}, {expected1});
+}