diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2019-12-02 11:58:19 +0000 |
---|---|---|
committer | Michele Di Giorgio <michele.digiorgio@arm.com> | 2019-12-06 14:54:19 +0000 |
commit | f4261adf78bdb9f8b2d6f2970636125096c173cb (patch) | |
tree | a2cf7abd84787720bdc286f09422336691d6fa95 | |
parent | 6f58b37a18cfade5dbec38638926f7bd368756d9 (diff) | |
download | ComputeLibrary-f4261adf78bdb9f8b2d6f2970636125096c173cb.tar.gz |
COMPMID-2779: Add support for generating synthetic int8 graphs.
Adds SyntheticDataTypeMutator, which is responsible for mutating graphs
to int8 and thus enable performance analysis on a wider range of models.
Change-Id: I9a00f0ae59421ab11952660f5115b5dcd9314aaf
Signed-off-by: Georgios Pinitas <georgios.pinitas@arm.com>
Reviewed-on: https://review.mlplatform.org/c/2418
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com>
34 files changed, 448 insertions, 79 deletions
diff --git a/arm_compute/graph/IGraphMutator.h b/arm_compute/graph/IGraphMutator.h index b80d37d274..94a28f63b1 100644 --- a/arm_compute/graph/IGraphMutator.h +++ b/arm_compute/graph/IGraphMutator.h @@ -35,6 +35,14 @@ class Graph; class IGraphMutator { public: + /** Mutation type */ + enum class MutationType + { + IR, /** IR specific mutation */ + Backend /** Backend specific mutation */ + }; + +public: /** Virtual Destructor */ virtual ~IGraphMutator() = default; /** Walk the graph and perform a specific mutation @@ -42,6 +50,11 @@ public: * @param[in, out] g Graph to walk and mutate */ virtual void mutate(Graph &g) = 0; + /** Returns mutation type + * + * @return Mutation type enumeration + */ + virtual MutationType type() const = 0; /** Returns mutator name * * @return Mutator name diff --git a/arm_compute/graph/PassManager.h b/arm_compute/graph/PassManager.h index c2b2319bcb..c8920ba296 100644 --- a/arm_compute/graph/PassManager.h +++ b/arm_compute/graph/PassManager.h @@ -78,12 +78,18 @@ public: * @param[in, out] g Graph to run the mutations on */ void run_all(Graph &g); + /** Runs a mutation passes of a specific type on a given graph + * + * @param[in, out] g Graph to run the mutation on + * @param[in] type Type of the mutations to execute + */ + void run_type(Graph &g, IGraphMutator::MutationType type); /** Runs a specific mutation pass on a given graph * * @param[in, out] g Graph to run the mutation on * @param[in] index Index of the mutation to execute */ - void run(Graph &g, size_t index); + void run_index(Graph &g, size_t index); private: std::vector<std::unique_ptr<IGraphMutator>> _passes; /**< Vector of graph passes */ diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index c337126c8d..cd4da694d2 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -81,6 +81,7 @@ struct GraphConfig bool use_function_weights_manager{ true }; /**< Use a weights manager to manage transformed weights */ bool use_transition_memory_manager{ true }; /**< Use a memory manager to manager transition buffer memory */ bool use_tuner{ false }; /**< Use a tuner in tunable backends */ + bool convert_to_uint8{ false }; /**< Convert graph to a synthetic uint8 graph */ CLTunerMode tuner_mode{ CLTunerMode::EXHAUSTIVE }; /**< Tuner mode to be used by the CL tuner */ int num_threads{ -1 }; /**< Number of threads to use (thread capable backends), if 0 the backend will auto-initialize, if -1 the backend will stay as it is. */ std::string tuner_file{ "acl_tuner.csv" }; /**< File to load/store tuning values from */ diff --git a/arm_compute/graph/Utils.h b/arm_compute/graph/Utils.h index 1760bc6dee..57a6f14bae 100644 --- a/arm_compute/graph/Utils.h +++ b/arm_compute/graph/Utils.h @@ -85,10 +85,11 @@ void force_target_to_graph(Graph &g, Target target); /** Creates a default @ref PassManager * * @param[in] target Target to create the pass manager for + * @param[in] cfg Graph configuration meta-data * * @return A PassManager with default mutating passes */ -PassManager create_default_pass_manager(Target target); +PassManager create_default_pass_manager(Target target, const GraphConfig &cfg); /** Setups requested backend context if it exists, is supported and hasn't been initialized already. * * @param[in,out] ctx Graph Context. diff --git a/arm_compute/graph/mutators/DepthConcatSubTensorMutator.h b/arm_compute/graph/mutators/DepthConcatSubTensorMutator.h index 11fc82ffa1..14a427ba7c 100644 --- a/arm_compute/graph/mutators/DepthConcatSubTensorMutator.h +++ b/arm_compute/graph/mutators/DepthConcatSubTensorMutator.h @@ -39,6 +39,7 @@ class DepthConcatSubTensorMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/GraphMutators.h b/arm_compute/graph/mutators/GraphMutators.h index f6e390eaff..6ae06990a4 100644 --- a/arm_compute/graph/mutators/GraphMutators.h +++ b/arm_compute/graph/mutators/GraphMutators.h @@ -30,5 +30,6 @@ #include "arm_compute/graph/mutators/NodeExecutionMethodMutator.h" #include "arm_compute/graph/mutators/NodeFusionMutator.h" #include "arm_compute/graph/mutators/SplitLayerSubTensorMutator.h" +#include "arm_compute/graph/mutators/SyntheticDataTypeMutator.h" #endif /* ARM_COMPUTE_GRAPH_GRAPH_MUTATORS_H */ diff --git a/arm_compute/graph/mutators/GroupedConvolutionMutator.h b/arm_compute/graph/mutators/GroupedConvolutionMutator.h index b6dac71182..01c9d0ee2c 100644 --- a/arm_compute/graph/mutators/GroupedConvolutionMutator.h +++ b/arm_compute/graph/mutators/GroupedConvolutionMutator.h @@ -39,6 +39,7 @@ class GroupedConvolutionMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/InPlaceOperationMutator.h b/arm_compute/graph/mutators/InPlaceOperationMutator.h index 43e88383b9..7932b6245c 100644 --- a/arm_compute/graph/mutators/InPlaceOperationMutator.h +++ b/arm_compute/graph/mutators/InPlaceOperationMutator.h @@ -36,6 +36,7 @@ class InPlaceOperationMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/NodeExecutionMethodMutator.h b/arm_compute/graph/mutators/NodeExecutionMethodMutator.h index 59df8181c5..3de940654b 100644 --- a/arm_compute/graph/mutators/NodeExecutionMethodMutator.h +++ b/arm_compute/graph/mutators/NodeExecutionMethodMutator.h @@ -41,6 +41,7 @@ class NodeExecutionMethodMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/NodeFusionMutator.h b/arm_compute/graph/mutators/NodeFusionMutator.h index a4dd5c925d..b99ee79f94 100644 --- a/arm_compute/graph/mutators/NodeFusionMutator.h +++ b/arm_compute/graph/mutators/NodeFusionMutator.h @@ -37,6 +37,7 @@ class NodeFusionMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/SplitLayerSubTensorMutator.h b/arm_compute/graph/mutators/SplitLayerSubTensorMutator.h index 29d061febb..c9747fd889 100644 --- a/arm_compute/graph/mutators/SplitLayerSubTensorMutator.h +++ b/arm_compute/graph/mutators/SplitLayerSubTensorMutator.h @@ -39,6 +39,7 @@ class SplitLayerSubTensorMutator final : public IGraphMutator public: // Inherited methods overridden virtual void mutate(Graph &g) override; + MutationType type() const override; const char *name() override; }; } // namespace graph diff --git a/arm_compute/graph/mutators/SyntheticDataTypeMutator.h b/arm_compute/graph/mutators/SyntheticDataTypeMutator.h new file mode 100644 index 0000000000..74f4b56acd --- /dev/null +++ b/arm_compute/graph/mutators/SyntheticDataTypeMutator.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 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_SYNTHETIC_DATA_TYPE_MUTATOR_H +#define ARM_COMPUTE_GRAPH_SYNTHETIC_DATA_TYPE_MUTATOR_H + +#include "arm_compute/graph/Graph.h" +#include "arm_compute/graph/IGraphMutator.h" + +namespace arm_compute +{ +namespace graph +{ +/** Mutation pass to create synthetic graphs of a given data type */ +class SyntheticDataTypeMutator final : public IGraphMutator +{ +public: + // Inherited methods overridden + virtual void mutate(Graph &g) override; + MutationType type() const override; + const char *name() override; +}; +} // namespace graph +} // namespace arm_compute +#endif /* ARM_COMPUTE_GRAPH_SYNTHETIC_DATA_TYPE_MUTATOR_H */ diff --git a/arm_compute/graph/nodes/SoftmaxLayerNode.h b/arm_compute/graph/nodes/SoftmaxLayerNode.h index c6990b161d..cbcd06a477 100644 --- a/arm_compute/graph/nodes/SoftmaxLayerNode.h +++ b/arm_compute/graph/nodes/SoftmaxLayerNode.h @@ -51,6 +51,9 @@ public: TensorDescriptor configure_output(size_t idx) const override; void accept(INodeVisitor &v) override; +public: + static constexpr NodeType node_type = NodeType::SoftmaxLayer; + private: float _beta; }; diff --git a/examples/graph_inception_v3.cpp b/examples/graph_inception_v3.cpp index 1de6a5fad7..d49561ae38 100644 --- a/examples/graph_inception_v3.cpp +++ b/examples/graph_inception_v3.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -199,10 +196,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_inception_v4.cpp b/examples/graph_inception_v4.cpp index bac85eef15..a322b2268d 100644 --- a/examples/graph_inception_v4.cpp +++ b/examples/graph_inception_v4.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -150,10 +147,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); // Load the precompiled kernels from a file into the kernel library, in this way the next time they are needed // compilation won't be required. diff --git a/examples/graph_resnet50.cpp b/examples/graph_resnet50.cpp index 17506dcf71..84355131d0 100644 --- a/examples/graph_resnet50.cpp +++ b/examples/graph_resnet50.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -113,10 +110,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_resnet_v2_50.cpp b/examples/graph_resnet_v2_50.cpp index 785ae9c27a..e2325151bc 100644 --- a/examples/graph_resnet_v2_50.cpp +++ b/examples/graph_resnet_v2_50.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -116,10 +113,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_squeezenet.cpp b/examples/graph_squeezenet.cpp index 9721775495..234def150d 100644 --- a/examples/graph_squeezenet.cpp +++ b/examples/graph_squeezenet.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -166,10 +163,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_squeezenet_v1_1.cpp b/examples/graph_squeezenet_v1_1.cpp index 0fd52b9e19..b43c8ffdad 100644 --- a/examples/graph_squeezenet_v1_1.cpp +++ b/examples/graph_squeezenet_v1_1.cpp @@ -55,9 +55,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -166,10 +163,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_srcnn955.cpp b/examples/graph_srcnn955.cpp index b69305889b..29faf72ac2 100644 --- a/examples/graph_srcnn955.cpp +++ b/examples/graph_srcnn955.cpp @@ -75,9 +75,6 @@ public: std::cout << "Image width: " << image_width << std::endl; std::cout << "Image height: " << image_height << std::endl; - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Get trainable parameters data path const std::string data_path = common_params.data_path; const std::string model_path = "/cnn_data/srcnn955_model/"; @@ -120,10 +117,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_vgg16.cpp b/examples/graph_vgg16.cpp index d58bf6cbf5..2c7f614f64 100644 --- a/examples/graph_vgg16.cpp +++ b/examples/graph_vgg16.cpp @@ -65,9 +65,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -224,10 +221,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_vgg19.cpp b/examples/graph_vgg19.cpp index 82895bb516..e3733d859c 100644 --- a/examples/graph_vgg19.cpp +++ b/examples/graph_vgg19.cpp @@ -64,9 +64,6 @@ public: return false; } - // Checks - ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); - // Print parameter values std::cout << common_params << std::endl; @@ -235,10 +232,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/examples/graph_vgg_vdsr.cpp b/examples/graph_vgg_vdsr.cpp index f82ae4c20e..870d3cb391 100644 --- a/examples/graph_vgg_vdsr.cpp +++ b/examples/graph_vgg_vdsr.cpp @@ -138,10 +138,11 @@ public: // Finalize graph GraphConfig config; - config.num_threads = common_params.threads; - config.use_tuner = common_params.enable_tuner; - config.tuner_mode = common_params.tuner_mode; - config.tuner_file = common_params.tuner_file; + config.num_threads = common_params.threads; + config.use_tuner = common_params.enable_tuner; + config.tuner_mode = common_params.tuner_mode; + config.tuner_file = common_params.tuner_file; + config.convert_to_uint8 = (common_params.data_type == DataType::QASYMM8); graph.finalize(common_params.target, config); diff --git a/src/graph/GraphManager.cpp b/src/graph/GraphManager.cpp index 4f942b99e4..996e50bbbc 100644 --- a/src/graph/GraphManager.cpp +++ b/src/graph/GraphManager.cpp @@ -51,6 +51,9 @@ void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager & ARM_COMPUTE_ERROR("Graph is already registered!"); } + // Apply IR mutating passes + pm.run_type(graph, IGraphMutator::MutationType::IR); + // Force target to all graph construct // TODO (COMPMID-2014) : Support heterogeneous execution Target forced_target = target; @@ -68,8 +71,8 @@ void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager & // Configure all tensors detail::configure_all_tensors(graph); - // Apply all mutating passes - pm.run_all(graph); + // Apply backend mutating passes + pm.run_type(graph, IGraphMutator::MutationType::Backend); // Perform topological sort std::vector<NodeID> topological_sorted_nodes = dfs(graph); diff --git a/src/graph/PassManager.cpp b/src/graph/PassManager.cpp index 92860e2987..99d979842a 100644 --- a/src/graph/PassManager.cpp +++ b/src/graph/PassManager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -70,7 +70,19 @@ void PassManager::run_all(Graph &g) } } -void PassManager::run(Graph &g, size_t index) +void PassManager::run_type(Graph &g, IGraphMutator::MutationType type) +{ + for(auto &pass : _passes) + { + if(pass && (pass->type() == type)) + { + ARM_COMPUTE_LOG_GRAPH_INFO("Running mutating pass : " << pass->name() << std::endl); + pass->mutate(g); + } + } +} + +void PassManager::run_index(Graph &g, size_t index) { if(index >= _passes.size()) { @@ -78,9 +90,9 @@ void PassManager::run(Graph &g, size_t index) } auto &pass = _passes.at(index); - if(pass != nullptr) { + ARM_COMPUTE_LOG_GRAPH_INFO("Running mutating pass : " << pass->name() << std::endl); pass->mutate(g); } } diff --git a/src/graph/Utils.cpp b/src/graph/Utils.cpp index 4c34dd85a5..3bf0caca7e 100644 --- a/src/graph/Utils.cpp +++ b/src/graph/Utils.cpp @@ -74,13 +74,17 @@ void force_target_to_graph(Graph &g, Target target) } } -PassManager create_default_pass_manager(Target target) +PassManager create_default_pass_manager(Target target, const GraphConfig &cfg) { PassManager pm; const bool is_target_gc = target == Target::GC; // Passes that mutate graph IR + if(cfg.convert_to_uint8) + { + pm.append(support::cpp14::make_unique<SyntheticDataTypeMutator>(), !is_target_gc); + } pm.append(support::cpp14::make_unique<NodeFusionMutator>(), !is_target_gc); pm.append(support::cpp14::make_unique<GroupedConvolutionMutator>()); pm.append(support::cpp14::make_unique<InPlaceOperationMutator>(), !is_target_gc); diff --git a/src/graph/frontend/Stream.cpp b/src/graph/frontend/Stream.cpp index 878d688995..c04a426eef 100644 --- a/src/graph/frontend/Stream.cpp +++ b/src/graph/frontend/Stream.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -39,7 +39,7 @@ Stream::Stream(size_t id, std::string name) void Stream::finalize(Target target, const GraphConfig &config) { - PassManager pm = create_default_pass_manager(target); + PassManager pm = create_default_pass_manager(target, config); _ctx.set_config(config); _manager.finalize_graph(_g, _ctx, pm, target); } diff --git a/src/graph/mutators/DepthConcatSubTensorMutator.cpp b/src/graph/mutators/DepthConcatSubTensorMutator.cpp index 7994541b78..30d6700446 100644 --- a/src/graph/mutators/DepthConcatSubTensorMutator.cpp +++ b/src/graph/mutators/DepthConcatSubTensorMutator.cpp @@ -42,6 +42,11 @@ const char *DepthConcatSubTensorMutator::name() return "DepthConcatSubTensorMutator"; } +IGraphMutator::MutationType DepthConcatSubTensorMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void DepthConcatSubTensorMutator::mutate(Graph &g) { // Early exit if no Concatenation layers exist in graph diff --git a/src/graph/mutators/GroupedConvolutionMutator.cpp b/src/graph/mutators/GroupedConvolutionMutator.cpp index 3d53f49218..bb452f9b94 100644 --- a/src/graph/mutators/GroupedConvolutionMutator.cpp +++ b/src/graph/mutators/GroupedConvolutionMutator.cpp @@ -103,6 +103,11 @@ const char *GroupedConvolutionMutator::name() return "GroupedConvolutionMutator"; } +IGraphMutator::MutationType GroupedConvolutionMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void GroupedConvolutionMutator::mutate(Graph &g) { // Early exit if no Convolution layers exist in graph diff --git a/src/graph/mutators/InPlaceOperationMutator.cpp b/src/graph/mutators/InPlaceOperationMutator.cpp index 07e3ecf2c7..ef4ca47e48 100644 --- a/src/graph/mutators/InPlaceOperationMutator.cpp +++ b/src/graph/mutators/InPlaceOperationMutator.cpp @@ -35,6 +35,11 @@ const char *InPlaceOperationMutator::name() return "InPlaceOperationMutator"; } +IGraphMutator::MutationType InPlaceOperationMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void InPlaceOperationMutator::mutate(Graph &g) { std::set<NodeType> in_place_nodes = { NodeType::BatchNormalizationLayer, NodeType::ActivationLayer }; diff --git a/src/graph/mutators/NodeExecutionMethodMutator.cpp b/src/graph/mutators/NodeExecutionMethodMutator.cpp index b420121c42..72e2645dd2 100644 --- a/src/graph/mutators/NodeExecutionMethodMutator.cpp +++ b/src/graph/mutators/NodeExecutionMethodMutator.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -73,6 +73,11 @@ const char *NodeExecutionMethodMutator::name() return "NodeExecutionMethodMutator"; } +IGraphMutator::MutationType NodeExecutionMethodMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void NodeExecutionMethodMutator::mutate(Graph &g) { // Convolution Layer diff --git a/src/graph/mutators/NodeFusionMutator.cpp b/src/graph/mutators/NodeFusionMutator.cpp index abd6436d74..b7f081dc42 100644 --- a/src/graph/mutators/NodeFusionMutator.cpp +++ b/src/graph/mutators/NodeFusionMutator.cpp @@ -286,6 +286,11 @@ const char *NodeFusionMutator::name() return "NodeFusionMutator"; } +IGraphMutator::MutationType NodeFusionMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void NodeFusionMutator::mutate(Graph &g) { // Supported activations when fusing diff --git a/src/graph/mutators/SplitLayerSubTensorMutator.cpp b/src/graph/mutators/SplitLayerSubTensorMutator.cpp index e21252a9ed..3ba73071ed 100644 --- a/src/graph/mutators/SplitLayerSubTensorMutator.cpp +++ b/src/graph/mutators/SplitLayerSubTensorMutator.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -42,6 +42,11 @@ const char *SplitLayerSubTensorMutator::name() return "SplitLayerSubTensorMutator"; } +IGraphMutator::MutationType SplitLayerSubTensorMutator::type() const +{ + return IGraphMutator::MutationType::Backend; +} + void SplitLayerSubTensorMutator::mutate(Graph &g) { // Early exit if no Split layers exist in graph diff --git a/src/graph/mutators/SyntheticDataTypeMutator.cpp b/src/graph/mutators/SyntheticDataTypeMutator.cpp new file mode 100644 index 0000000000..b318df956e --- /dev/null +++ b/src/graph/mutators/SyntheticDataTypeMutator.cpp @@ -0,0 +1,261 @@ +/* + * Copyright (c) 2019 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/mutators/SyntheticDataTypeMutator.h" + +#include "arm_compute/graph/GraphBuilder.h" +#include "arm_compute/graph/ITensorAccessor.h" +#include "arm_compute/graph/Logger.h" +#include "arm_compute/graph/Utils.h" +#include "arm_compute/graph/nodes/Nodes.h" + +#include "arm_compute/core/utils/misc/Cast.h" + +#include <set> + +namespace arm_compute +{ +namespace graph +{ +namespace +{ +/** Empty accessor class */ +class EmptyAccessor final : public graph::ITensorAccessor +{ +public: + /** Default Constructor */ + EmptyAccessor() = default; + + // Inherited methods overriden: + bool access_tensor(ITensor &tensor) override + { + ARM_COMPUTE_UNUSED(tensor); + return true; + } +}; + +/** Check if the mutation pass can be applied + * + * @param[in] g Graph the mutation pass need to be applied on + * + * @return True if the pass can be applied else false + */ +bool is_mutation_supported(Graph &g) +{ + const std::set<NodeType> unsupported_node_types = { NodeType::DetectionOutputLayer, + NodeType::NormalizationLayer, + NodeType::PriorBoxLayer + }; + + for(const auto &utype : unsupported_node_types) + { + if(!g.nodes(utype).empty()) + { + return false; + } + } + return true; +} + +/** Remove nodes that get optimized out during conversion + * + * @param[in, out] g Graph to remove the nodes from. + */ +void remove_optimized_nodes(Graph &g) +{ + const std::set<NodeType> optimized_node_types = { NodeType::BatchNormalizationLayer }; + + for(const auto &opt_type : optimized_node_types) + { + const std::vector<NodeID> opt_nodes_ids = g.nodes(opt_type); + for(const auto &node_id : opt_nodes_ids) + { + INode *node = g.node(node_id); + + // Get input edge + Edge *input_edge = node->input_edge(0); + ARM_COMPUTE_ERROR_ON(input_edge == nullptr); + + // Get producer node + INode *producer = input_edge->producer(); + const EdgeID producer_edge_id = input_edge->producer_idx(); + ARM_COMPUTE_ERROR_ON(producer == nullptr); + + // Get driving nodes + std::vector<NodeIdxPair> driving_nodes = get_driving_nodes(*node); + + // Remove node + g.remove_node(node->id()); + + // Update connections + for(auto &driving_node : driving_nodes) + { + g.add_connection(producer->id(), producer_edge_id, driving_node.node_id, driving_node.index); + } + } + } +} + +/** Convert tensor meta-data + * + * @param[in,out] g Graph to convert tensors of. + */ +void convert_tensors(Graph &g) +{ + auto &tensors = g.tensors(); + for(auto &tensor : tensors) + { + if(tensor != nullptr) + { + tensor->desc().data_type = DataType::QASYMM8; + tensor->desc().quant_info = QuantizationInfo(0.125f, -10); + } + } +} + +/** Convert special node + * + * @param[in,out] g Graph to convert tensors of. + * @param[in] fnc Conversion function. + * @param[in] optional_arguments Conversion function arguments. + */ +template <typename NT> +void convert_special_node(Graph &g, std::function<bool(INode *, Tensor *)> const &f) +{ + const std::vector<NodeID> nodes_ids = g.nodes(NT::node_type); + for(const auto &nodes_id : nodes_ids) + { + INode *node = arm_compute::utils::cast::polymorphic_downcast<NT *>(g.node(nodes_id)); + ARM_COMPUTE_ERROR_ON(node == nullptr); + + Tensor *output_tensor = node->output(0); + ARM_COMPUTE_ERROR_ON(output_tensor == nullptr); + + f(node, output_tensor); + } +} + +/** Converts special tensors + * + * @param[in,out] g Graph to convert tensors of. + */ +void convert_special_tensors(Graph &g) +{ + auto softmax_func = [](INode * node, Tensor * tensor) + { + ARM_COMPUTE_UNUSED(node); + tensor->desc().quant_info = QuantizationInfo(1.f / 256.f, 0); + return true; + }; + + auto act_func = [](INode * node, Tensor * tensor) + { + auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(node); + if(act_node->activation_info().activation() == ActivationLayerInfo::ActivationFunction::TANH) + { + tensor->desc().quant_info = QuantizationInfo(1.f / 128.f, 128); + } + return true; + }; + + convert_special_node<ActivationLayerNode>(g, act_func); + convert_special_node<SoftmaxLayerNode>(g, softmax_func); +} + +/** Handle nodes with bias + * + * @note Special tensors are for now biases that the data type differ + * + * @param[in,out] g Graph to convert tensors of. + */ +void handle_nodes_with_bias(Graph &g) +{ + const std::set<NodeType> special_node_types = { NodeType::ConvolutionLayer, + NodeType::DeconvolutionLayer, + NodeType::DepthwiseConvolutionLayer, + NodeType::FullyConnectedLayer + }; + + for(const auto &spc_type : special_node_types) + { + const std::vector<NodeID> scp_nodes_ids = g.nodes(spc_type); + for(const auto &node_id : scp_nodes_ids) + { + INode *node = g.node(node_id); + if(node != nullptr) + { + Tensor *tensor = node->input(2); + if(tensor != nullptr) + { + tensor->desc().data_type = DataType::S32; + } + else + { + auto params = node->common_node_params(); + params.name = params.name.empty() ? "" : params.name + "Bias"; + + TensorDescriptor b_desc = node->input(1)->desc(); + auto depth = b_desc.shape[get_dimension_idx(b_desc.layout, DataLayoutDimension::BATCHES)]; + b_desc.shape = TensorShape(depth); + + auto accessor = support::cpp14::make_unique<EmptyAccessor>(); + auto b_nid = GraphBuilder::add_const_node(g, params, b_desc, std::move(accessor)); + g.add_connection(b_nid, 0, node_id, 2); + } + } + } + } +} +} // namespace + +const char *SyntheticDataTypeMutator::name() +{ + return "SyntheticDataTypeMutator"; +} + +IGraphMutator::MutationType SyntheticDataTypeMutator::type() const +{ + return IGraphMutator::MutationType::IR; +} + +void SyntheticDataTypeMutator::mutate(Graph &g) +{ + if(is_mutation_supported(g)) + { + // Remove nodes that get optimized out (e.g. BatchNorm) + remove_optimized_nodes(g); + + // Convert tensor + convert_tensors(g); + convert_special_tensors(g); + + // Handle special nodes + handle_nodes_with_bias(g); + } + else + { + ARM_COMPUTE_LOG_GRAPH_VERBOSE("Synthetic data type mutator couldn't be applied" << std::endl); + } +} +} // namespace graph +} // namespace arm_compute |