+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Importer.h"
-
-#include "GraphBuilder.h"
-#include "GraphBuilderContext.h"
-#include "GraphBuilderRegistry.h"
-#include "Transforms.h"
-#include "ProgressReporter.h"
-
-#include <moco/IR/Nodes/TFPlaceholder.h>
-
-#include <moco/Log.h>
-
-#include <loco/IR/Verifier.h>
-#include <locop/FormattedGraph.h>
-#include <stdex/Memory.h>
-
-#include <logo/Phase.h>
-
-#include <cassert>
-#include <sstream>
-#include <stdexcept>
-
-namespace
-{
-
-void convert_graph(const moco::tf::GraphBuilderSource &source,
- const moco::tf::ModelSignature &signature, tensorflow::GraphDef &tf_graph_def,
- loco::Graph *graph)
-{
- auto nodedef = stdex::make_unique<moco::tf::NodeDefTable>();
- auto tensor_names = stdex::make_unique<moco::tf::SymbolTable>();
- auto updates = stdex::make_unique<moco::tf::UpdateQueue>();
-
- moco::tf::GraphBuilderContext gb_context(graph, nodedef.get(), tensor_names.get(), updates.get());
-
- // Building a loco graph
- // 1. Convert all the nodes to loco::Node
- // 2. Connect inputs: set all node input(from a string) to actual node object
- // 3. Set graph input
- // 4. Create loco::Push node and set input and set graph output
-
- /**
- * @brief Prepare tensorflow::NodeDef search table from name
- */
- for (const auto &n : tf_graph_def.node())
- {
- nodedef->enroll(n.name(), &n);
- }
-
- /**
- * @brief 1. Convert all the nodes to loco::Node
- *
- * @note In each build for a TF node, four things happen
- * 1) create corresponding loco::Node(s)
- * 2) read and set the attributes to created loco::Node(s)
- * 3) register name-loco::Node(last one of Nodes) that will be used as the output
- * 4) queue a task to set the input of the loco::Node(first one of the Nodes)
- * this is done only for required nodes depending on the operator
- *
- * @example Placeholder("in") - Identity("out")
- * %1 = Pull --> 0x1001 (loco::Node* object address)
- * (symboltable: register %1, after the registeration table will contain as below;
- * "in" : 0x1001
- * )
- * (queue: this will be empty as Pull does not queue a task to set input;
- * )
- *
- * %2 = Forward --> 0x1002
- * (symboltable: register %2 and table will look like below;
- * "in" : 0x1001
- * "out" : 0x1002
- * )
- * (queue: Forward will queue a task with input "in";
- * 0x1002: {"in"}
- * )
- */
- for (const auto &n : tf_graph_def.node())
- {
- if (const auto *graph_builder = source.lookup(n.op()))
- {
- if (!graph_builder->validate(n))
- {
- throw std::runtime_error{"Invalid operator: " + n.op()};
- }
-
- graph_builder->build(n, &gb_context);
- }
- else
- {
- throw std::runtime_error{"Not supported: " + n.op()};
- }
- }
-
- /**
- * @brief 2. Connect inputs: Iterate updates and call each update input method
- *
- * @note Continue from above example graph, connecting inputs is done in following steps
- * a) iterate queue
- * b) call the input method for each update
- * c) each update has the loco::Node *node and names of the input to connect
- * node = 0x1002 and names = {"in"}
- * d) from symbol table, "in" will return 0x1001
- * e) set input of 0x1002 with 0x1001
- */
- for (auto &update : updates->queue())
- {
- update->input(tensor_names.get());
- }
-
- /**
- * @brief 3. Set graph input
- */
- for (auto input : signature.inputs())
- {
- auto node = tensor_names->node(input);
- assert(node != nullptr);
-
- auto graph_input = graph->inputs()->create();
-
- auto placeholder_node = dynamic_cast<moco::TFPlaceholder *>(node);
- assert(placeholder_node != nullptr);
-
- graph_input->name(input.nodeName());
-
- // annotate index that should be passed to loco::Pull
- moco::index(placeholder_node, graph_input->index());
-
- // Use placeholder internal shape to graph_input shape
- // Currently, signature has no shape information
- // TODO graph input shape setting may move to Frontend
- auto tensorshape = moco::tensor_shape(placeholder_node);
- graph_input->shape(stdex::make_unique<loco::TensorShape>(tensorshape));
-
- // This implementation works as "PlaceholderGraphBuilder in Op/PlaceholderGraphBuilder.cpp"
- // accepts only TF_FLOAT32 as of now.
- //
- // TODO Support other types
- graph_input->dtype(loco::DataType::FLOAT32);
- }
-
- /**
- * @brief 4. Create loco::Push node and set graph input and output
- */
- for (auto output : signature.outputs())
- {
- auto output_node = tensor_names->node(output);
- assert(output_node);
-
- // create loco::Push for output of graph
- auto push_node = graph->nodes()->create<loco::Push>();
- push_node->from(output_node); // set input of Push to output node
-
- // set the graph output name and node object
- auto graph_output = graph->outputs()->create();
- graph_output->name(output.nodeName());
- // TODO Support other types
- graph_output->dtype(loco::DataType::FLOAT32);
- loco::link(graph_output, push_node);
- }
-
- // validate graph
- assert(loco::valid(graph));
-}
-
-} // namespace
-
-namespace moco
-{
-namespace tf
-{
-
-Importer::Importer()
-{
- // DO NOTHING
-}
-
-std::unique_ptr<loco::Graph> Importer::import(const ModelSignature &signature,
- tensorflow::GraphDef &tf_graph_def) const
-{
- auto graph = loco::make_graph();
-
- const GraphBuilderSource *source_ptr = &moco::tf::GraphBuilderRegistry::get();
-
- if (_source != nullptr)
- {
- // Use user-defined GraphBuilderSource
- source_ptr = _source;
- }
-
- convert_graph(*source_ptr, signature, tf_graph_def, graph.get());
-
- return std::move(graph);
-}
-
-} // namespace tf
-} // namespace moco
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Importer.h"
-
-#include "TestHelper.h"
-
-#include "IR/TFIdentity.h"
-#include "Op/Identity.h"
-
-#include <loco.h>
-#include <plier/tf/TestHelper.h>
-
-#include <gtest/gtest.h>
-
-using namespace moco::tf::test;
-
-TEST(TensorFlowImport, Dummy) { moco::tf::Importer import; }
-
-namespace
-{
-
-// clang-format off
-const char *basic_pbtxtdata = STRING_CONTENT(
-node {
- name: "Placeholder"
- op: "Placeholder"
- attr {
- key: "dtype"
- value {
- type: DT_FLOAT
- }
- }
- attr {
- key: "shape"
- value {
- shape {
- dim {
- size: 1
- }
- dim {
- size: 2
- }
- dim {
- size: 1
- }
- dim {
- size: 2
- }
- }
- }
- }
-}
-node {
- name: "output/identity"
- op: "Identity"
- input: "Placeholder"
- attr {
- key: "T"
- value {
- type: DT_FLOAT
- }
- }
-}
-);
-// clang-format on
-
-} // namespace
-
-TEST(TensorFlowImport, load_model_withio_tf)
-{
- moco::tf::ModelSignature signature;
-
- signature.add_input(moco::tf::TensorName("Placeholder", 0));
- signature.add_output(moco::tf::TensorName("output/identity", 0));
-
- tensorflow::GraphDef graph_def;
- EXPECT_TRUE(plier::tf::parse_graphdef(basic_pbtxtdata, graph_def));
-
- moco::tf::Importer importer;
-
- std::unique_ptr<loco::Graph> graph = importer.import(signature, graph_def);
-
- // what to test:
- // - import reads Placeholder
- // - import reads Identity
- // - attribute values should match
-
- auto tfidentity = find_first_node_bytype<moco::tf::TFIdentity>(graph.get());
- ASSERT_NE(tfidentity, nullptr);
- ASSERT_NE(tfidentity->input(), nullptr);
-}