From 7b607dc5058d2f6a398cfac4d2eab65d415c3733 Mon Sep 17 00:00:00 2001 From: Anthony Barbier Date: Fri, 13 Jul 2018 15:55:24 +0100 Subject: COMPMID-1400: Add command line option to specify the tuner's config file Change-Id: Ib597e0dff4c8c01f7e6bd46d03824beef4bc1e9a Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/139923 Tested-by: Jenkins Reviewed-by: Georgios Pinitas --- arm_compute/graph/Types.h | 9 +++++---- arm_compute/graph/backends/CL/CLDeviceBackend.h | 1 + examples/graph_alexnet.cpp | 2 ++ examples/graph_googlenet.cpp | 2 ++ examples/graph_inception_v3.cpp | 2 ++ examples/graph_inception_v4.cpp | 2 ++ examples/graph_lenet.cpp | 2 ++ examples/graph_mobilenet.cpp | 2 ++ examples/graph_resnext50.cpp | 2 ++ examples/graph_squeezenet.cpp | 2 ++ examples/graph_squeezenet_v1_1.cpp | 2 ++ examples/graph_vgg16.cpp | 2 ++ examples/graph_vgg19.cpp | 2 ++ src/graph/backends/CL/CLDeviceBackend.cpp | 24 ++++++++++------------ tests/main.cpp | 27 +++++++++++++++++++++++++ utils/CommonGraphOptions.cpp | 6 +++++- utils/CommonGraphOptions.h | 2 ++ 17 files changed, 73 insertions(+), 18 deletions(-) diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index 9cfede9cf3..c5b7fb1c51 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -77,10 +77,11 @@ class TensorDescriptor; /** Graph configuration structure */ struct GraphConfig { - bool use_function_memory_manager{ true }; /**< Use a memory manager to manage per-funcion auxilary memory */ - 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 */ - 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. */ + bool use_function_memory_manager{ true }; /**< Use a memory manager to manage per-funcion auxilary memory */ + 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 */ + 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 */ }; /**< Device target types */ diff --git a/arm_compute/graph/backends/CL/CLDeviceBackend.h b/arm_compute/graph/backends/CL/CLDeviceBackend.h index 0b45dfe479..c1a6a28e6c 100644 --- a/arm_compute/graph/backends/CL/CLDeviceBackend.h +++ b/arm_compute/graph/backends/CL/CLDeviceBackend.h @@ -66,6 +66,7 @@ private: bool _initialized; /**< Flag that specifies if the backend has been default initialized */ CLTuner _tuner; /**< CL kernel tuner */ std::unique_ptr _allocator; /**< CL buffer affinity allocator */ + std::string _tuner_file; /** Filename to load/store the tuner's values from */ }; } // namespace backends } // namespace graph diff --git a/examples/graph_alexnet.cpp b/examples/graph_alexnet.cpp index bf4d131ac5..e4bc2da571 100644 --- a/examples/graph_alexnet.cpp +++ b/examples/graph_alexnet.cpp @@ -149,6 +149,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_googlenet.cpp b/examples/graph_googlenet.cpp index ff7992c164..f9cb9787f1 100644 --- a/examples/graph_googlenet.cpp +++ b/examples/graph_googlenet.cpp @@ -121,6 +121,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_inception_v3.cpp b/examples/graph_inception_v3.cpp index 1b7b0fd6c2..6262193b02 100644 --- a/examples/graph_inception_v3.cpp +++ b/examples/graph_inception_v3.cpp @@ -199,6 +199,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_inception_v4.cpp b/examples/graph_inception_v4.cpp index 93166a41c2..def73dff7a 100644 --- a/examples/graph_inception_v4.cpp +++ b/examples/graph_inception_v4.cpp @@ -143,6 +143,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_lenet.cpp b/examples/graph_lenet.cpp index 9be5bed6a1..c02e2f9ec0 100644 --- a/examples/graph_lenet.cpp +++ b/examples/graph_lenet.cpp @@ -105,6 +105,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_mobilenet.cpp b/examples/graph_mobilenet.cpp index 2b662d9055..395936b8f7 100644 --- a/examples/graph_mobilenet.cpp +++ b/examples/graph_mobilenet.cpp @@ -109,6 +109,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_resnext50.cpp b/examples/graph_resnext50.cpp index 0fce3a1b04..a02e406acb 100644 --- a/examples/graph_resnext50.cpp +++ b/examples/graph_resnext50.cpp @@ -97,6 +97,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_squeezenet.cpp b/examples/graph_squeezenet.cpp index 09fb1c5960..9751539af6 100644 --- a/examples/graph_squeezenet.cpp +++ b/examples/graph_squeezenet.cpp @@ -156,6 +156,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_squeezenet_v1_1.cpp b/examples/graph_squeezenet_v1_1.cpp index 75ea6b258b..be0c326f2a 100644 --- a/examples/graph_squeezenet_v1_1.cpp +++ b/examples/graph_squeezenet_v1_1.cpp @@ -158,6 +158,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_vgg16.cpp b/examples/graph_vgg16.cpp index c2a9b82f68..8ce1221ead 100644 --- a/examples/graph_vgg16.cpp +++ b/examples/graph_vgg16.cpp @@ -213,6 +213,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/examples/graph_vgg19.cpp b/examples/graph_vgg19.cpp index 8de0223a48..dcde4a2a65 100644 --- a/examples/graph_vgg19.cpp +++ b/examples/graph_vgg19.cpp @@ -225,6 +225,8 @@ public: GraphConfig config; config.num_threads = common_params.threads; config.use_tuner = common_params.enable_tuner; + config.tuner_file = common_params.tuner_file; + graph.finalize(common_params.target, config); return true; diff --git a/src/graph/backends/CL/CLDeviceBackend.cpp b/src/graph/backends/CL/CLDeviceBackend.cpp index b235c3aa48..6717f9f8e3 100644 --- a/src/graph/backends/CL/CLDeviceBackend.cpp +++ b/src/graph/backends/CL/CLDeviceBackend.cpp @@ -62,20 +62,17 @@ bool file_exists(const std::string &filename) /** Register CL backend */ static detail::BackendRegistrar CLDeviceBackend_registrar(Target::CL); -/** Tuner export file */ -static const std::string tuner_data_filename = "acl_tuner.csv"; - CLDeviceBackend::CLDeviceBackend() - : _initialized(false), _tuner(), _allocator(nullptr) + : _initialized(false), _tuner(), _allocator(nullptr), _tuner_file() { } CLDeviceBackend::~CLDeviceBackend() { // TODO (geopin01) : Shouldn't call non exception safe stuff here - if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty()) + if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty()) { - _tuner.save_to_file(tuner_data_filename); + _tuner.save_to_file(_tuner_file); } } @@ -86,12 +83,6 @@ void CLDeviceBackend::set_kernel_tuning(bool enable_tuning) void CLDeviceBackend::initialize_backend() { - // Load tuner data if available - if(_tuner.lws_table().empty() && file_exists(tuner_data_filename)) - { - _tuner.load_from_file(tuner_data_filename); - } - // Setup Scheduler CLScheduler::get().default_init(&_tuner); @@ -109,6 +100,13 @@ void CLDeviceBackend::setup_backend_context(GraphContext &ctx) } // Setup tuner + _tuner_file = ctx.config().tuner_file; + // Load tuner data if available + if(file_exists(_tuner_file)) + { + _tuner.load_from_file(_tuner_file); + } + set_kernel_tuning(ctx.config().use_tuner); // Setup a management backend @@ -193,4 +191,4 @@ std::shared_ptr CLDeviceBackend::create_memory_mana } } // namespace backends } // namespace graph -} // namespace arm_compute \ No newline at end of file +} // namespace arm_compute diff --git a/tests/main.cpp b/tests/main.cpp index 2da6804c60..aaae99157a 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -61,6 +61,17 @@ std::unique_ptr library; } // namespace test } // namespace arm_compute +namespace +{ +#ifdef ARM_COMPUTE_CL +bool file_exists(const std::string &filename) +{ + std::ifstream file(filename); + return file.good(); +} +#endif /* ARM_COMPUTE_CL */ +} //namespace + int main(int argc, char **argv) { #ifdef ARM_COMPUTE_CL @@ -106,6 +117,8 @@ int main(int argc, char **argv) #ifdef ARM_COMPUTE_CL auto enable_tuner = parser.add_option("enable-tuner"); enable_tuner->set_help("Enable OpenCL dynamic tuner"); + auto tuner_file = parser.add_option>("tuner-file", ""); + tuner_file->set_help("File to load/save CLTuner values"); #endif /* ARM_COMPUTE_CL */ auto threads = parser.add_option>("threads", 1); threads->set_help("Number of threads to use"); @@ -127,6 +140,16 @@ int main(int argc, char **argv) if(enable_tuner->is_set()) { cl_tuner.set_tune_new_kernels(enable_tuner->value()); + // If that's the first run then the file won't exist yet + if(file_exists(tuner_file->value())) + { + cl_tuner.load_from_file(tuner_file->value()); + } + } + else if(!tuner_file->value().empty()) + { + //If we're not tuning and the file doesn't exist then we should raise an error: + cl_tuner.load_from_file(tuner_file->value()); } #endif /* ARM_COMPUTE_CL */ if(options.log_level->value() > framework::LogLevel::NONE) @@ -216,6 +239,10 @@ int main(int argc, char **argv) #ifdef ARM_COMPUTE_CL CLScheduler::get().sync(); + if(enable_tuner->is_set() && enable_tuner->value() && tuner_file->is_set()) + { + cl_tuner.save_to_file(tuner_file->value()); + } #endif /* ARM_COMPUTE_CL */ return (success ? 0 : 1); diff --git a/utils/CommonGraphOptions.cpp b/utils/CommonGraphOptions.cpp index bf882f5164..7ac7bbce2b 100644 --- a/utils/CommonGraphOptions.cpp +++ b/utils/CommonGraphOptions.cpp @@ -83,6 +83,7 @@ namespace utils os << "Data type : " << common_params.data_type << std::endl; os << "Data layout : " << common_params.data_layout << std::endl; os << "Tuner enabled? : " << (common_params.enable_tuner ? true_str : false_str) << std::endl; + os << "Tuner file : " << common_params.tuner_file << std::endl; os << "Fast math enabled? : " << (common_params.fast_math_hint == FastMathHint::ENABLED ? true_str : false_str) << std::endl; if(!common_params.data_path.empty()) { @@ -122,7 +123,8 @@ CommonGraphOptions::CommonGraphOptions(CommandLineParser &parser) labels(parser.add_option>("labels")), validation_file(parser.add_option>("validation-file")), validation_path(parser.add_option>("validation-path")), - validation_range(parser.add_option>("validation-range")) + validation_range(parser.add_option>("validation-range")), + tuner_file(parser.add_option>("tuner-file")) { std::set supported_targets { @@ -161,6 +163,7 @@ CommonGraphOptions::CommonGraphOptions(CommandLineParser &parser) validation_file->set_help("File used to validate the graph"); validation_path->set_help("Path to the validation data"); validation_range->set_help("Range of the images to validate for (Format : start,end)"); + tuner_file->set_help("File to load/save CLTuner values"); } CommonGraphParams consume_common_graph_parameters(CommonGraphOptions &options) @@ -183,6 +186,7 @@ CommonGraphParams consume_common_graph_parameters(CommonGraphOptions &options) common_params.validation_path = options.validation_path->value(); common_params.validation_range_start = validation_range.first; common_params.validation_range_end = validation_range.second; + common_params.tuner_file = options.tuner_file->value(); return common_params; } diff --git a/utils/CommonGraphOptions.h b/utils/CommonGraphOptions.h index b429a18069..24c8dbb67c 100644 --- a/utils/CommonGraphOptions.h +++ b/utils/CommonGraphOptions.h @@ -49,6 +49,7 @@ struct CommonGraphParams std::string labels{}; std::string validation_file{}; std::string validation_path{}; + std::string tuner_file{}; unsigned int validation_range_start{ 0 }; unsigned int validation_range_end{ std::numeric_limits::max() }; }; @@ -103,6 +104,7 @@ public: SimpleOption *validation_file; /**< Validation file */ SimpleOption *validation_path; /**< Validation data path */ SimpleOption *validation_range; /**< Validation range */ + SimpleOption *tuner_file; /**< File to load/store the tuner's values from */ }; /** Consumes the common graph options and creates a structure containing any information -- cgit v1.2.1