From 0c29cd3aac40e512d648506b1b3b8332bb45f063 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 18 Oct 2017 17:29:27 +0100 Subject: COMPMID-630 : Rework nodes for selective target compilation. Reworked nodes: -ActivationLayer Change-Id: Iaa394531ef208db48caa2c18a41ad9a845471f94 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/92281 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- SConscript | 3 + arm_compute/graph/IOperation.h | 56 ++++++++ arm_compute/graph/NodeContext.h | 143 +++++++++++++++++++++ arm_compute/graph/NodeParameter.h | 74 +++++++++++ arm_compute/graph/OperationRegistrar.h | 59 +++++++++ arm_compute/graph/OperationRegistry.h | 86 +++++++++++++ .../operations/CL/CLActivationLayerOperation.h | 49 +++++++ .../operations/NEON/NEActivationLayerOperation.h | 49 +++++++ src/graph/NodeContext.cpp | 70 ++++++++++ src/graph/OperationRegistry.cpp | 61 +++++++++ src/graph/nodes/ActivationLayer.cpp | 63 ++------- src/graph/nodes/ConvolutionLayer.cpp | 6 +- .../operations/CL/CLActivationLayerOperation.cpp | 73 +++++++++++ .../operations/NEON/NEActivationLayerOperation.cpp | 73 +++++++++++ utils/GraphTypePrinter.h | 25 ++++ 15 files changed, 835 insertions(+), 55 deletions(-) create mode 100644 arm_compute/graph/IOperation.h create mode 100644 arm_compute/graph/NodeContext.h create mode 100644 arm_compute/graph/NodeParameter.h create mode 100644 arm_compute/graph/OperationRegistrar.h create mode 100644 arm_compute/graph/OperationRegistry.h create mode 100644 arm_compute/graph/operations/CL/CLActivationLayerOperation.h create mode 100644 arm_compute/graph/operations/NEON/NEActivationLayerOperation.h create mode 100644 src/graph/NodeContext.cpp create mode 100644 src/graph/OperationRegistry.cpp create mode 100644 src/graph/operations/CL/CLActivationLayerOperation.cpp create mode 100644 src/graph/operations/NEON/NEActivationLayerOperation.cpp diff --git a/SConscript b/SConscript index 195eff18bd..4a351fff56 100644 --- a/SConscript +++ b/SConscript @@ -204,6 +204,9 @@ if env['neon'] and env['opencl']: graph_files = Glob('src/graph/*.cpp') graph_files += Glob('src/graph/nodes/*.cpp') + graph_files += Glob('src/graph/operations/CL/*.cpp') + graph_files += Glob('src/graph/operations/NEON/*.cpp') + graph_files += Glob('src/graph/CL/*.cpp') graph_files += Glob('src/graph/NEON/*.cpp') diff --git a/arm_compute/graph/IOperation.h b/arm_compute/graph/IOperation.h new file mode 100644 index 0000000000..c2c56a349d --- /dev/null +++ b/arm_compute/graph/IOperation.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_IOPERATION_H__ +#define __ARM_COMPUTE_GRAPH_IOPERATION_H__ + +#include "arm_compute/graph/NodeContext.h" +#include "arm_compute/graph/Types.h" +#include "arm_compute/runtime/IFunction.h" + +#include + +namespace arm_compute +{ +namespace graph +{ +/** Operation functor interface */ +class IOperation +{ +public: + /** Virtual Destructor */ + virtual ~IOperation() = default; + /** Interface to be implemented that configures an operation + * + * @param[in] ctx Node parameters to be used by the operation + */ + virtual std::unique_ptr configure(NodeContext &ctx) = 0; + /** Interface to be implemented that returns the target of the operation + * + * @return Target of the operation + */ + virtual TargetHint target() const = 0; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_IOPERATION_H__ */ diff --git a/arm_compute/graph/NodeContext.h b/arm_compute/graph/NodeContext.h new file mode 100644 index 0000000000..8e8a761d22 --- /dev/null +++ b/arm_compute/graph/NodeContext.h @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__ +#define __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__ + +#include "arm_compute/core/Error.h" +#include "arm_compute/graph/NodeParameter.h" +#include "arm_compute/graph/Types.h" +#include "support/ToolchainSupport.h" + +#include +#include +#include + +namespace arm_compute +{ +namespace graph +{ +/** Node Context class + * + * Node context class is used to hold all the parameters required by a node to execute + */ +class NodeContext +{ +public: + /** Default Constructor + * (TODO(geopin01): Should we have an enum with all the supported ops instead?) + * + * @param[in] operation Name of the operation + */ + NodeContext(std::string operation) + : _operation(operation), _target(TargetHint::DONT_CARE), _inputs(), _outputs(), _parameters() {}; + /** Sets the execution target of the node + * + * @param[in] target Execution target of the node + */ + void set_target(TargetHint target); + /** Adds an input tensor to the context + * + * @param[in] input Input to add + */ + void add_input(arm_compute::ITensor *input); + /** Adds and output to the context + * + * @param[in] output Output to add + */ + void add_output(arm_compute::ITensor *output); + /** Adds a parameter to the context + * + * @param[in] name Parameter name + * @param[in] parameter Parameter to add + */ + template + void add_parameter(std::string name, T parameter); + /** Returns the operation of this node. + * + * @return The operation name + */ + std::string operation() const; + /** Returns the execution target of this node + * + * @return The execution target + */ + TargetHint target() const; + /** Returns input tensor of a given index + * + * @param[in] idx Index of the input tensor + * + * @return A pointer the requested input tensor else nullptr + */ + arm_compute::ITensor *input(size_t idx) const; + /** Returns output tensor of a given index + * + * @param[in] idx Index of the output tensor + * + * @return A pointer the requested output tensor else nullptr + */ + arm_compute::ITensor *output(size_t idx) const; + /** Returns the parameter with the given name + * + * @param[in] name Parameter name + * + * @return The requested parameter else an empty object + */ + template + T parameter(std::string name) const; + /** Returns number of inputs + * + * @return Number of inputs + */ + size_t num_inputs() const; + /** Returns number of output + * + * @return Number of outputs + */ + size_t num_outputs() const; + +private: + std::string _operation; + TargetHint _target; + std::vector _inputs; + std::vector _outputs; + std::map> _parameters; +}; + +template +inline void NodeContext::add_parameter(std::string name, T parameter) +{ + ARM_COMPUTE_ERROR_ON_MSG(_parameters.find(name) != _parameters.end(), "Parameter already exists!"); + _parameters[name] = support::cpp14::make_unique>(name, parameter); +} + +template +inline T NodeContext::parameter(std::string name) const +{ + auto it = _parameters.find(name); + ARM_COMPUTE_ERROR_ON(it == _parameters.end()); + return static_cast *>(it->second.get())->value(); +} +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__ */ diff --git a/arm_compute/graph/NodeParameter.h b/arm_compute/graph/NodeParameter.h new file mode 100644 index 0000000000..9d3823d543 --- /dev/null +++ b/arm_compute/graph/NodeParameter.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_NODE_PARAMETER_H__ +#define __ARM_COMPUTE_GRAPH_NODE_PARAMETER_H__ + +#include +#include + +namespace arm_compute +{ +namespace graph +{ +/**Node Parameter Empty base class */ +class NodeParameterBase +{ +}; + +/** Template parameter implementation */ +template +class NodeParameter : public NodeParameterBase +{ +public: + /** Default Constructor + * + * @param[in] name Paremeter name + * @param[in] val Parameter value + */ + NodeParameter(std::string name, T val) + : _name(name), _val(val) {}; + /** Returns parameter's name + * + * @return the name of the parameter + */ + std::string name() const + { + return _name; + } + /** Returns parameter's value + * + * @return the value of the parameter + */ + T value() + { + return _val; + } + +private: + std::string _name; + T _val; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_NODE_PARAMETER_H__ */ diff --git a/arm_compute/graph/OperationRegistrar.h b/arm_compute/graph/OperationRegistrar.h new file mode 100644 index 0000000000..ff31963dc3 --- /dev/null +++ b/arm_compute/graph/OperationRegistrar.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_GRAPH_OPERATION_REGISTRAR +#define ARM_COMPUTE_GRAPH_OPERATION_REGISTRAR + +#include "arm_compute/graph/OperationRegistry.h" +#include "arm_compute/graph/Types.h" + +#include +#include + +namespace arm_compute +{ +namespace graph +{ +namespace detail +{ +/** Helper class to statically register an operation */ +template +class OperationRegistrar final +{ +public: + /** Add a new test case with the given name to the framework. + * + * @param[in] operation_name Operation name + */ + OperationRegistrar(std::string operation_name); +}; + +template +inline OperationRegistrar::OperationRegistrar(std::string operation_name) +{ + OperationRegistry::get().add_operation(std::move(operation_name)); +} +} // namespace detail +} // namespace graph +} // namespace arm_compute +#endif /* ARM_COMPUTE_GRAPH_OPERATION_REGISTRAR */ \ No newline at end of file diff --git a/arm_compute/graph/OperationRegistry.h b/arm_compute/graph/OperationRegistry.h new file mode 100644 index 0000000000..905e8ee66f --- /dev/null +++ b/arm_compute/graph/OperationRegistry.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_OPERATION_REGISTRY_H__ +#define __ARM_COMPUTE_GRAPH_OPERATION_REGISTRY_H__ + +#include "arm_compute/graph/IOperation.h" +#include "arm_compute/graph/Types.h" +#include "support/ToolchainSupport.h" + +#include +#include +#include + +namespace arm_compute +{ +namespace graph +{ +/** Registry holding all the supported operations */ +class OperationRegistry +{ +public: + /** Gets operation registry instance + * + * @return Operation registry instance + */ + static OperationRegistry &get(); + /** Finds an operation in the registry + * + * @param[in] operation Name of the operation to find + * @param[in] target Target of the operation + * + * @return Pointer to the operation functor if found, else nullptr + */ + IOperation *find_operation(const std::string &operation, TargetHint target); + /** Checks if an operation for a given target exists + * + * @param[in] operation Operation name + * @param[in] target Execution target + * + * @return True if exists else false + */ + bool contains(const std::string &operation, TargetHint target) const; + /** Registers and operation to the registry + * + * @param operation_name Name of the operation to register + */ + template + void add_operation(const std::string &operation_name); + +private: + /** Default Constructor */ + OperationRegistry(); + +private: + std::map>> _registered_ops; +}; + +template +inline void OperationRegistry::add_operation(const std::string &operation_name) +{ + _registered_ops[operation_name].emplace_back(support::cpp14::make_unique()); +} +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_OPERATION_REGISTRY_H__ */ diff --git a/arm_compute/graph/operations/CL/CLActivationLayerOperation.h b/arm_compute/graph/operations/CL/CLActivationLayerOperation.h new file mode 100644 index 0000000000..2053fb674d --- /dev/null +++ b/arm_compute/graph/operations/CL/CLActivationLayerOperation.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_CL_ACTIVATIONLAYER_OPERATION_H__ +#define __ARM_COMPUTE_GRAPH_CL_ACTIVATIONLAYER_OPERATION_H__ + +#include "arm_compute/graph/IOperation.h" + +#include "arm_compute/graph/NodeContext.h" +#include "arm_compute/graph/Types.h" +#include "arm_compute/runtime/IFunction.h" + +#include + +namespace arm_compute +{ +namespace graph +{ +/** Operation functor interface */ +class CLActivationLayerOperation : public IOperation +{ +public: + // Inherited methods overriden: + std::unique_ptr configure(NodeContext &ctx) final; + TargetHint target() const final; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_CL_ACTIVATIONLAYER_OPERATION_H__ */ diff --git a/arm_compute/graph/operations/NEON/NEActivationLayerOperation.h b/arm_compute/graph/operations/NEON/NEActivationLayerOperation.h new file mode 100644 index 0000000000..f6e1bd7d39 --- /dev/null +++ b/arm_compute/graph/operations/NEON/NEActivationLayerOperation.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_GRAPH_NE_ACTIVATIONLAYER_OPERATION_H__ +#define __ARM_COMPUTE_GRAPH_NE_ACTIVATIONLAYER_OPERATION_H__ + +#include "arm_compute/graph/IOperation.h" + +#include "arm_compute/graph/NodeContext.h" +#include "arm_compute/graph/Types.h" +#include "arm_compute/runtime/IFunction.h" + +#include + +namespace arm_compute +{ +namespace graph +{ +/** Operation functor interface */ +class NEActivationLayerOperation : public IOperation +{ +public: + // Inherited methods overriden: + std::unique_ptr configure(NodeContext &ctx) final; + TargetHint target() const final; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_NE_ACTIVATIONLAYER_OPERATION_H__ */ diff --git a/src/graph/NodeContext.cpp b/src/graph/NodeContext.cpp new file mode 100644 index 0000000000..5b0dc6c5d8 --- /dev/null +++ b/src/graph/NodeContext.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/NodeContext.h" + +using namespace arm_compute::graph; + +void NodeContext::add_input(arm_compute::ITensor *input) +{ + ARM_COMPUTE_ERROR_ON(input == nullptr); + _inputs.emplace_back(input); +} + +void NodeContext::add_output(arm_compute::ITensor *output) +{ + ARM_COMPUTE_ERROR_ON(output == nullptr); + _outputs.emplace_back(output); +} + +std::string NodeContext::operation() const +{ + return _operation; +} + +TargetHint NodeContext::target() const +{ + return _target; +} + +arm_compute::ITensor *NodeContext::input(size_t idx) const +{ + ARM_COMPUTE_ERROR_ON(idx >= _inputs.size()); + return _inputs[idx]; +} + +arm_compute::ITensor *NodeContext::output(size_t idx) const +{ + ARM_COMPUTE_ERROR_ON(idx >= _outputs.size()); + return _outputs[idx]; +} + +size_t NodeContext::num_inputs() const +{ + return _inputs.size(); +} + +size_t NodeContext::num_outputs() const +{ + return _outputs.size(); +} \ No newline at end of file diff --git a/src/graph/OperationRegistry.cpp b/src/graph/OperationRegistry.cpp new file mode 100644 index 0000000000..7de714b214 --- /dev/null +++ b/src/graph/OperationRegistry.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/OperationRegistry.h" + +using namespace arm_compute::graph; + +OperationRegistry::OperationRegistry() + : _registered_ops() +{ +} + +OperationRegistry &OperationRegistry::get() +{ + static OperationRegistry instance; + return instance; +} + +IOperation *OperationRegistry::find_operation(const std::string &operation, TargetHint target) +{ + ARM_COMPUTE_ERROR_ON(!contains(operation, target)); + auto it = std::find_if(_registered_ops[operation].begin(), _registered_ops[operation].end(), [&](const std::unique_ptr &op) + { + return (op->target() == target); + }); + ARM_COMPUTE_ERROR_ON(it == _registered_ops[operation].end()); + return (*it).get(); +} + +bool OperationRegistry::contains(const std::string &operation, TargetHint target) const +{ + auto it = _registered_ops.find(operation); + if(it != _registered_ops.end()) + { + return std::any_of(it->second.begin(), it->second.end(), [&](const std::unique_ptr &op) + { + return (op->target() == target); + }); + } + return false; +} diff --git a/src/graph/nodes/ActivationLayer.cpp b/src/graph/nodes/ActivationLayer.cpp index df73ba7078..ea87fd9592 100644 --- a/src/graph/nodes/ActivationLayer.cpp +++ b/src/graph/nodes/ActivationLayer.cpp @@ -23,45 +23,11 @@ */ #include "arm_compute/graph/nodes/ActivationLayer.h" -#include "arm_compute/runtime/CL/CLTensor.h" -#include "arm_compute/runtime/CL/functions/CLActivationLayer.h" -#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" -#include "arm_compute/runtime/Tensor.h" -#include "support/ToolchainSupport.h" -#include "utils/TypePrinter.h" +#include "arm_compute/graph/NodeContext.h" +#include "arm_compute/graph/OperationRegistry.h" using namespace arm_compute::graph; -namespace -{ -template -std::unique_ptr instantiate_function(arm_compute::ITensor *input, arm_compute::ITensor *output, const ActivationLayerInfo &activation_info) -{ - auto activation = arm_compute::support::cpp14::make_unique(); - activation->configure( - dynamic_cast(input), - dynamic_cast(output), - activation_info); - - return std::move(activation); -} - -template -std::unique_ptr instantiate(arm_compute::ITensor *input, arm_compute::ITensor *output, const ActivationLayerInfo &activation_info); - -template <> -std::unique_ptr instantiate(arm_compute::ITensor *input, arm_compute::ITensor *output, const ActivationLayerInfo &activation_info) -{ - return instantiate_function(input, output, activation_info); -} - -template <> -std::unique_ptr instantiate(arm_compute::ITensor *input, arm_compute::ITensor *output, const ActivationLayerInfo &activation_info) -{ - return instantiate_function(input, output, activation_info); -} -} // namespace - ActivationLayer::ActivationLayer(const ActivationLayerInfo activation_info) : _activation_info(activation_info) { @@ -78,23 +44,14 @@ std::unique_ptr ActivationLayer::instantiate_node(GraphC arm_compute::ITensor *in = input->tensor(); arm_compute::ITensor *out = output->tensor(); - if(_target_hint == TargetHint::OPENCL) - { - func = instantiate(in, out, _activation_info); - ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLActivationLayer"); - } - else - { - func = instantiate(in, out, _activation_info); - ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEActivationLayer"); - } + // Create node context + NodeContext node_ctx("ActivationLayer"); + node_ctx.add_input(in); + node_ctx.add_output(out); + node_ctx.add_parameter("ActivationLayerInfo", _activation_info); + + // Get function + func = OperationRegistry::get().find_operation("ActivationLayer", _target_hint)->configure(node_ctx); - ARM_COMPUTE_LOG_GRAPH_INFO(" Data Type: " << in->info()->data_type() - << " Input shape: " << in->info()->tensor_shape() - << " Output shape: " << out->info()->tensor_shape() - << " Activation function: " << _activation_info.activation() - << " a: " << _activation_info.a() - << " b: " << _activation_info.b() - << std::endl); return func; } diff --git a/src/graph/nodes/ConvolutionLayer.cpp b/src/graph/nodes/ConvolutionLayer.cpp index 07d42617e2..d3ab97fb2d 100644 --- a/src/graph/nodes/ConvolutionLayer.cpp +++ b/src/graph/nodes/ConvolutionLayer.cpp @@ -216,12 +216,10 @@ std::unique_ptr ConvolutionLayer::instantiate_node(Graph if(_num_groups == 1) { func = instantiate_convolution(in, out, conv_method_hint); - ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer"); } else { func = instantiate_grouped_convolution(in, out, conv_method_hint); - ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer"); } // Fill weights @@ -253,10 +251,12 @@ std::unique_ptr ConvolutionLayer::instantiate_convolutio std::unique_ptr func; if(_target_hint == TargetHint::OPENCL) { + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer"); func = instantiate(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint); } else { + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer"); func = instantiate(input, _weights.tensor(), _biases.tensor(), output, _conv_info, _weights_info, conv_method_hint); } return func; @@ -318,10 +318,12 @@ std::unique_ptr ConvolutionLayer::instantiate_grouped_co // Instantiate convolution function if(_target_hint == TargetHint::OPENCL) { + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLConvolutionLayer"); func = instantiate(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint); } else { + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEConvolutionLayer"); func = instantiate(_is[i].tensor(), _ws[i].tensor(), _bs[i].tensor(), _os[i].tensor(), _conv_info, _weights_info, conv_method_hint); } diff --git a/src/graph/operations/CL/CLActivationLayerOperation.cpp b/src/graph/operations/CL/CLActivationLayerOperation.cpp new file mode 100644 index 0000000000..d0045e2500 --- /dev/null +++ b/src/graph/operations/CL/CLActivationLayerOperation.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/operations/CL/CLActivationLayerOperation.h" + +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/graph/OperationRegistrar.h" +#include "arm_compute/graph/Types.h" +#include "arm_compute/runtime/CL/functions/CLActivationLayer.h" +#include "support/ToolchainSupport.h" +#include "utils/GraphTypePrinter.h" +#include "utils/TypePrinter.h" + +#include + +using namespace arm_compute::graph; + +std::unique_ptr CLActivationLayerOperation::configure(NodeContext &ctx) +{ + ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1); + ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1); + ARM_COMPUTE_ERROR_ON(dynamic_cast(ctx.input(0)) == nullptr); + ARM_COMPUTE_ERROR_ON(dynamic_cast(ctx.output(0)) == nullptr); + + // Extract IO and info + auto *in = dynamic_cast(ctx.input(0)); + auto *out = dynamic_cast(ctx.output(0)); + const auto act_info = ctx.parameter("ActivationLayerInfo"); + + // Create and configure function + auto activation = arm_compute::support::cpp14::make_unique(); + activation->configure(in, out, act_info); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating CLActivationLayer" + << " Data Type: " << in->info()->data_type() + << " Input shape: " << in->info()->tensor_shape() + << " Output shape: " << out->info()->tensor_shape() + << " Activation function: " << act_info.activation() + << " a: " << act_info.a() + << " b: " << act_info.b() + << std::endl); + + return std::move(activation); +} + +TargetHint CLActivationLayerOperation::target() const +{ + return TargetHint::OPENCL; +} + +static detail::OperationRegistrar registrar("ActivationLayer"); \ No newline at end of file diff --git a/src/graph/operations/NEON/NEActivationLayerOperation.cpp b/src/graph/operations/NEON/NEActivationLayerOperation.cpp new file mode 100644 index 0000000000..355fd38f67 --- /dev/null +++ b/src/graph/operations/NEON/NEActivationLayerOperation.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017 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 OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/operations/NEON/NEActivationLayerOperation.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/graph/OperationRegistrar.h" +#include "arm_compute/graph/Types.h" +#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" +#include "support/ToolchainSupport.h" +#include "utils/GraphTypePrinter.h" +#include "utils/TypePrinter.h" + +#include + +using namespace arm_compute::graph; + +std::unique_ptr NEActivationLayerOperation::configure(NodeContext &ctx) +{ + ARM_COMPUTE_ERROR_ON(ctx.num_inputs() != 1); + ARM_COMPUTE_ERROR_ON(ctx.num_outputs() != 1); + ARM_COMPUTE_ERROR_ON(dynamic_cast(ctx.input(0)) == nullptr); + ARM_COMPUTE_ERROR_ON(dynamic_cast(ctx.output(0)) == nullptr); + + // Extract IO and info + auto *in = dynamic_cast(ctx.input(0)); + auto *out = dynamic_cast(ctx.output(0)); + const auto act_info = ctx.parameter("ActivationLayerInfo"); + + // Create and configure function + auto activation = arm_compute::support::cpp14::make_unique(); + activation->configure(in, out, act_info); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiating NEActivationLayer" + << " Data Type: " << in->info()->data_type() + << " Input shape: " << in->info()->tensor_shape() + << " Output shape: " << out->info()->tensor_shape() + << " Activation function: " << act_info.activation() + << " a: " << act_info.a() + << " b: " << act_info.b() + << std::endl); + + return std::move(activation); +} + +TargetHint NEActivationLayerOperation::target() const +{ + return TargetHint::NEON; +} + +static detail::OperationRegistrar registrar("ActivationLayer"); \ No newline at end of file diff --git a/utils/GraphTypePrinter.h b/utils/GraphTypePrinter.h index be56d59853..4ff1019cce 100644 --- a/utils/GraphTypePrinter.h +++ b/utils/GraphTypePrinter.h @@ -58,6 +58,31 @@ inline std::string to_string(const ConvolutionMethodHint &conv_method) str << conv_method; return str.str(); } + +/** Formatted output of the @ref TargetHint type. */ +inline ::std::ostream &operator<<(::std::ostream &os, const TargetHint &target_hint) +{ + switch(target_hint) + { + case TargetHint::NEON: + os << "NEON"; + break; + case TargetHint::OPENCL: + os << "OPENCL"; + break; + default: + ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); + } + + return os; +} + +inline std::string to_string(const TargetHint &target_hint) +{ + std::stringstream str; + str << target_hint; + return str.str(); +} } // namespace graph } // namespace arm_compute #endif /* __ARM_COMPUTE_TEST_GRAPH_TYPE_PRINTER_H__ */ -- cgit v1.2.1