From d9eb27597eabe5b7c17520f4f9b3f8a282d72573 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Tue, 3 Apr 2018 13:44:29 +0100 Subject: COMPMID-797: Switch to new graph. - Cleaned up build system Change-Id: If2faa27ee5b31fa8b972836960ab3ef671059c8d Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/126435 Tested-by: Jenkins Reviewed-by: Pablo Tello --- arm_compute/graph/Graph.h | 294 +++++++++++++++++++++++++++++++--------------- 1 file changed, 201 insertions(+), 93 deletions(-) (limited to 'arm_compute/graph/Graph.h') diff --git a/arm_compute/graph/Graph.h b/arm_compute/graph/Graph.h index 72130878f8..16f5f97986 100644 --- a/arm_compute/graph/Graph.h +++ b/arm_compute/graph/Graph.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -24,130 +24,238 @@ #ifndef __ARM_COMPUTE_GRAPH_GRAPH_H__ #define __ARM_COMPUTE_GRAPH_GRAPH_H__ -#include "arm_compute/core/CL/CLTypes.h" +#include "arm_compute/graph/Edge.h" #include "arm_compute/graph/INode.h" -#include "arm_compute/graph/ITensorObject.h" -#include "arm_compute/graph/SubTensor.h" #include "arm_compute/graph/Tensor.h" #include "arm_compute/graph/Types.h" + +#include "support/Mutex.h" #include "support/ToolchainSupport.h" +#include #include +#include +#include +#include +#include namespace arm_compute { -class IFunction; - namespace graph { -/** Graph class */ +/** Graph class + * + * Represents a multiple source - multiple sink directed graph + */ class Graph final { public: - /** Constructor */ - Graph(); - /** Destructor */ - ~Graph(); - /** Prevent instances from being copy constructed */ + Graph() = default; + /** Constructor + * + * @param[in] id Graph identification number. Can be used to differentiate between graphs. Default value 0 + * @param[in] name Graph name. Default value empty string + */ + Graph(GraphID id, std::string name); + /** Prevent instances of this class from being copied (As this class contains pointers) */ Graph(const Graph &) = delete; - /** Prevent instances from being copy assigned */ - const Graph &operator=(const Graph &) = delete; - /** Prevent instances from being move constructed */ - Graph(Graph &&) = delete; - /** Prevent instances from being move assigned */ - Graph &operator=(Graph &&) = delete; - /** Initialize the graph - * - * @param[in] use_cl_tuner Use the CLTuner if this value is true - */ - void graph_init(const bool use_cl_tuner = false); - /** Executes the graph */ - void run(); + /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ + Graph &operator=(const Graph &) = delete; + /** Allow instances of this class to be moved */ + Graph(Graph &&) = default; + /** Allow instances of this class to be move assigned */ + Graph &operator=(Graph &&) = default; /** Adds a node to the graph * - * @param[in] node Node to add + * @note Models a single output node + * + * @tparam NT Node operation + * @tparam Ts Arguments to operation + * + * @param args Node arguments + * + * @return ID of the node + */ + template + NodeID add_node(Ts &&... args); + /** Remove the node with the given ID + * + * @param[in] nid ID of the node to remove + * + * @return True if the removal took place else false + */ + bool remove_node(NodeID nid); + /** Adds a connection between two nodes + * + * @param[in] source ID of the source node + * @param[in] source_idx Output index of the source node + * @param[in] sink ID of the sink node + * @param[in] sink_idx Input index of the sink node + * + * @return ID of this connection + */ + EdgeID add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx); + /** Removes an edge (connection) + * + * @param[in] eid Connection to remove + * + * @return True if the removal took place else false + */ + bool remove_connection(EdgeID eid); + /** Returns graph name + * + * @return Graph name + */ + std::string name() const; + /** Returns graph id + * + * @return Graph id */ - void add_node(std::unique_ptr node); - /** Adds a tensor to the graph + GraphID id() const; + /** Returns graph input nodes * - * @param[in] tensor Tensor to add + * @return vector containing the graph inputs */ - void add_tensor_object(std::unique_ptr tensor); - /** Check if the OpenCL target is available + const std::vector &inputs(); + /** Returns nodes of graph + * + * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph + * + * @return Nodes of graph */ - static bool opencl_is_available(); - /** Returns the GPU target + std::vector> &nodes(); + /** Returns nodes of graph + * + * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph + * + * @return Nodes of graph */ - static GPUTarget gpu_target(); - /** Manually sets the output of the current node + const std::vector> &nodes() const; + /** Returns edges of graph + * + * @warning Edges can be nullptr if they have been removed during the mutation steps of the graph * - * @param[in] tmp Output info to set + * @return Edges of graph */ - void set_temp(TensorInfo &&tmp); + const std::vector> &edges() const; + /** Returns tensors of graph + * + * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph + * + * @return Tensors of graph + */ + std::vector> &tensors(); + /** Returns tensors of graph + * + * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph + * + * @return Tensors of graph + */ + const std::vector> &tensors() const; + /** Get node object given its id + * + * @warning Can be nullptr if node was removed during the mutation steps of the graph + * + * @param[in] id Node ID + * + * @return The actual node object + */ + const INode *node(NodeID id) const; + /** Get node object given its id + * + * @warning Can be nullptr if node was removed during the mutation steps of the graph + * + * @param[in] id Node ID + * + * @return The actual node object + */ + INode *node(NodeID id); + /** Get edge object given its id + * + * @warning Can be nullptr if node was removed during the mutation steps of the graph + * + * @param[in] id Edge ID + * + * @return The actual edge object + */ + const Edge *edge(EdgeID id) const; + /** Get edge object given its id + * + * @warning Can be nullptr if node was removed during the mutation steps of the graph + * + * @param[in] id Edge ID + * + * @return The actual edge object + */ + Edge *edge(EdgeID id); + /** Get tensor object given its id + * + * @warning Can be nullptr if tensor was removed during the mutation steps of the graph + * + * @param[in] id Tensor ID + * + * @return The actual tensor object + */ + const Tensor *tensor(TensorID id) const; + /** Get tensor object given its id + * + * @warning Can be nullptr if tensor was removed during the mutation steps of the graph + * + * @param[in] id Tensor ID + * + * @return The actual tensor object + */ + Tensor *tensor(TensorID id); - /** Returns the graph hints that are currently used +private: + /** Creates a tensor object * - * @return Graph hints + * @param[in] desc Tensor descriptor + * + * @return Tensor ID */ - GraphHints &hints(); + TensorID create_tensor(TensorDescriptor desc = TensorDescriptor()); private: - class Private; - std::unique_ptr _pimpl; /**< Internal implementation class */ + GraphID _id = GraphID(0); /**< Graph id */ + std::string _name = {}; /**< Graph name */ + std::vector> _nodes = {}; /**< Graph nodes */ + std::vector> _edges = {}; /**< Graph edges */ + std::vector> _tensors = {}; /**< Graph tensors */ + std::map> _tagged_nodes = {}; /**< Graph nodes map with the node type as key */ + arm_compute::Mutex _mtx = {}; /**< Mutex used for graph construction */ }; -/** Overloaded stream operator to add a tensor through its tensor info to the graph - * - * @param[in, out] graph Graph to add the tensor - * @param[in] info Tensor information of the tensor to be added - * - * @return Updated graph - */ -Graph &operator<<(Graph &graph, TensorInfo &&info); -/** Overloaded stream operator to add a tensor to the graph - * - * @param[in, out] graph Graph to add the tensor - * @param[in] tensor Tensor to be added - * - * @return Updated graph - */ -Graph &operator<<(Graph &graph, Tensor &&tensor); -/** Overloaded stream operator to add a sub-tensor to the graph - * - * @param[in, out] graph Graph to add the tensor - * @param[in] sub_tensor Sub-tensor to be added - * - * @return Updated graph - */ -Graph &operator<<(Graph &graph, SubTensor &&sub_tensor); -/** Overloaded stream operator to provide a target hint to the graph - * - * @param[in, out] graph Graph to provide the hint to - * @param[in] target_hint Target hint to be considered - * - * @return Updated graph - */ -Graph &operator<<(Graph &graph, TargetHint target_hint); -/** Overloaded stream operator to provide a convolution method hint to the graph - * - * @param[in, out] graph Graph to provide the hint to - * @param[in] conv_method_hint Convolution method hint to be considered - * - * @return Updated graph - */ -Graph &operator<<(Graph &graph, ConvolutionMethodHint conv_method_hint); -/** Overloaded stream operator to add a node to the graph - * - * @param[in, out] graph Graph to add the tensor - * @param[in] node Node to be added - * - * @return Updated graph - */ -template -Graph &operator<<(Graph &graph, Node node) +template +inline NodeID Graph::add_node(Ts &&... args) { - graph.add_node(arm_compute::support::cpp14::make_unique(std::move(node))); - return graph; + std::lock_guard lock(_mtx); + + // Create node + NodeID nid = _nodes.size(); + auto node = support::cpp14::make_unique(std::forward(args)...); + node->set_graph(this); + node->set_id(nid); + + // Keep track of input nodes + if(node->type() == NodeType::Input) + { + _tagged_nodes[NodeType::Input].push_back(nid); + } + + // Associate a new tensor with each output + for(auto &output : node->_outputs) + { + output = create_tensor(); + } + + // Propagate node shape if possible + node->forward_descriptors(); + + // Add node to the graph nodes + _nodes.push_back(std::move(node)); + + return nid; } } // namespace graph } // namespace arm_compute -- cgit v1.2.1