COMPMID-1310: Create graph validation executables.

Change-Id: I9e0b57b1b83fe5a95777cdaeddba6ecef650bafc
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/138697
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
diff --git a/src/graph/Graph.cpp b/src/graph/Graph.cpp
index e1ffeed..ed24f18 100644
--- a/src/graph/Graph.cpp
+++ b/src/graph/Graph.cpp
@@ -41,9 +41,9 @@
 
     std::unique_ptr<INode> &node = _nodes[nid];
 
-    // Remove node connections
     if(node)
     {
+        // Remove node connections
         for(auto &input_eid : node->_input_edges)
         {
             remove_connection(input_eid);
@@ -52,6 +52,10 @@
         {
             remove_connection(outpud_eid);
         }
+
+        // Remove nid from tagged nodes
+        std::vector<NodeID> &tnodes = _tagged_nodes.at(node->type());
+        tnodes.erase(std::remove(tnodes.begin(), tnodes.end(), nid), tnodes.end());
     }
 
     node = nullptr;
@@ -164,9 +168,9 @@
     return _id;
 }
 
-const std::vector<NodeID> &Graph::inputs()
+const std::vector<NodeID> &Graph::nodes(NodeType type)
 {
-    return _tagged_nodes[NodeType::Input];
+    return _tagged_nodes[type];
 }
 
 std::vector<std::unique_ptr<INode>> &Graph::nodes()
diff --git a/src/graph/GraphManager.cpp b/src/graph/GraphManager.cpp
index 10661ea..db6650c 100644
--- a/src/graph/GraphManager.cpp
+++ b/src/graph/GraphManager.cpp
@@ -27,6 +27,7 @@
 #include "arm_compute/graph/GraphContext.h"
 #include "arm_compute/graph/Logger.h"
 #include "arm_compute/graph/PassManager.h"
+#include "arm_compute/graph/TypePrinter.h"
 #include "arm_compute/graph/Utils.h"
 #include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h"
 #include "arm_compute/graph/detail/ExecutionHelpers.h"
@@ -53,7 +54,12 @@
 
     // Force target to all graph construct
     // TODO (geopin01) : Support heterogeneous execution
-    Target forced_target = is_target_supported(target) ? target : get_default_target();
+    Target forced_target = target;
+    if(!is_target_supported(target))
+    {
+        forced_target = get_default_target();
+        ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
+    }
     force_target_to_graph(graph, forced_target);
 
     // Configure all tensors
@@ -103,14 +109,23 @@
     auto it = _workloads.find(graph.id());
     ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
 
-    // Call input accessors
-    detail::call_all_input_node_accessors(it->second);
+    while(true)
+    {
+        // Call input accessors
+        if(!detail::call_all_input_node_accessors(it->second))
+        {
+            return;
+        }
 
-    // Run graph
-    detail::call_all_tasks(it->second);
+        // Run graph
+        detail::call_all_tasks(it->second);
 
-    // Call output accessors
-    detail::call_all_output_node_accessors(it->second);
+        // Call output accessors
+        if(!detail::call_all_output_node_accessors(it->second))
+        {
+            return;
+        }
+    }
 }
 
 void GraphManager::invalidate_graph(Graph &graph)
diff --git a/src/graph/Tensor.cpp b/src/graph/Tensor.cpp
index ef253fe..9850128 100644
--- a/src/graph/Tensor.cpp
+++ b/src/graph/Tensor.cpp
@@ -90,12 +90,12 @@
     }
 
     // Call accessor
-    _accessor->access_tensor(_handle->tensor());
+    bool retval = _accessor->access_tensor(_handle->tensor());
 
     // Unmap tensor
     _handle->unmap();
 
-    return true;
+    return retval;
 }
 
 void Tensor::bind_edge(EdgeID eid)
diff --git a/src/graph/TypeLoader.cpp b/src/graph/TypeLoader.cpp
new file mode 100644
index 0000000..30a3546
--- /dev/null
+++ b/src/graph/TypeLoader.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWNISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/graph/TypeLoader.h"
+
+#include "arm_compute/core/utils/misc/Utility.h"
+
+#include <map>
+
+namespace arm_compute
+{
+arm_compute::DataType data_type_from_name(const std::string &name)
+{
+    static const std::map<std::string, arm_compute::DataType> data_types =
+    {
+        { "f16", DataType::F16 },
+        { "f32", DataType::F32 },
+        { "qasymm8", DataType::QASYMM8 },
+    };
+
+    try
+    {
+        return data_types.at(arm_compute::utility::tolower(name));
+    }
+    catch(const std::out_of_range &)
+    {
+        throw std::invalid_argument(name);
+    }
+}
+
+arm_compute::DataLayout data_layout_from_name(const std::string &name)
+{
+    static const std::map<std::string, arm_compute::DataLayout> data_layouts =
+    {
+        { "nhwc", DataLayout::NHWC },
+        { "nchw", DataLayout::NCHW },
+    };
+
+    try
+    {
+        return data_layouts.at(arm_compute::utility::tolower(name));
+    }
+    catch(const std::out_of_range &)
+    {
+        throw std::invalid_argument(name);
+    }
+}
+namespace graph
+{
+Target target_from_name(const std::string &name)
+{
+    static const std::map<std::string, Target> targets =
+    {
+        { "neon", Target::NEON },
+        { "cl", Target::CL },
+        { "gles", Target::GC },
+    };
+
+    try
+    {
+        return targets.at(arm_compute::utility::tolower(name));
+    }
+    catch(const std::out_of_range &)
+    {
+        throw std::invalid_argument(name);
+    }
+}
+} // namespace graph
+} // namespace arm_compute
diff --git a/src/graph/detail/ExecutionHelpers.cpp b/src/graph/detail/ExecutionHelpers.cpp
index d68092a..6df67fc 100644
--- a/src/graph/detail/ExecutionHelpers.cpp
+++ b/src/graph/detail/ExecutionHelpers.cpp
@@ -206,15 +206,12 @@
     }
 }
 
-void call_all_input_node_accessors(ExecutionWorkload &workload)
+bool call_all_input_node_accessors(ExecutionWorkload &workload)
 {
-    for(auto &input : workload.inputs)
+    return !std::any_of(std::begin(workload.inputs), std::end(workload.inputs), [](Tensor * input_tensor)
     {
-        if(input != nullptr)
-        {
-            input->call_accessor();
-        }
-    }
+        return (input_tensor == nullptr) || !input_tensor->call_accessor();
+    });
 }
 
 void prepare_all_tasks(ExecutionWorkload &workload)
@@ -256,15 +253,15 @@
     }
 }
 
-void call_all_output_node_accessors(ExecutionWorkload &workload)
+bool call_all_output_node_accessors(ExecutionWorkload &workload)
 {
-    for(auto &output : workload.outputs)
+    bool is_valid = true;
+    std::for_each(std::begin(workload.outputs), std::end(workload.outputs), [&](Tensor * output_tensor)
     {
-        if(output != nullptr)
-        {
-            output->call_accessor();
-        }
-    }
+        is_valid = is_valid && (output_tensor != nullptr) && output_tensor->call_accessor();
+    });
+
+    return is_valid;
 }
 } // namespace detail
 } // namespace graph