From 979dc4f336d31d9ddacd4f2b594596c882e868eb Mon Sep 17 00:00:00 2001 From: Anthony Barbier Date: Wed, 7 Mar 2018 09:27:48 +0000 Subject: COMPMID-971: Created validate_examples for MPG They wanted some SGEMM test with validation and benchmarking capabilities which can be configured at runtime, so I created a mix of example + validation framework. This is really hacky and therefore won't be released as part of the library, but it seems to work. Change-Id: I7e7728e1f5c6619c0f0d7c83106c85676d2ffc62 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/123706 Reviewed-by: Gian Marco Iodice Tested-by: Jenkins --- tests/validate_examples/RunExample.cpp | 200 ++++++++++++++++++++++++++++++ tests/validate_examples/ValidateExample.h | 72 +++++++++++ tests/validate_examples/cl_sgemm.cpp | 168 +++++++++++++++++++++++++ 3 files changed, 440 insertions(+) create mode 100644 tests/validate_examples/RunExample.cpp create mode 100644 tests/validate_examples/ValidateExample.h create mode 100644 tests/validate_examples/cl_sgemm.cpp (limited to 'tests/validate_examples') diff --git a/tests/validate_examples/RunExample.cpp b/tests/validate_examples/RunExample.cpp new file mode 100644 index 0000000000..a7b18ce555 --- /dev/null +++ b/tests/validate_examples/RunExample.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "utils/Utils.h" +//FIXME / INTERNAL_ONLY: This file should not be released! + +#define BENCHMARK_EXAMPLES +#include "utils/Utils.cpp" + +#include "ValidateExample.h" +#include "arm_compute/runtime/Scheduler.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/framework/Framework.h" +#include "tests/framework/Macros.h" +#include "tests/framework/command_line/CommandLineParser.h" +#include "tests/framework/command_line/CommonOptions.h" +#include "tests/framework/instruments/Instruments.h" + +#ifdef ARM_COMPUTE_CL +#include "arm_compute/runtime/CL/CLScheduler.h" +#endif /* ARM_COMPUTE_CL */ +#ifdef ARM_COMPUTE_GC +#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" +#endif /* ARM_COMPUTE_GC */ + +#include + +using namespace arm_compute; +using namespace arm_compute::test; + +namespace arm_compute +{ +namespace test +{ +std::unique_ptr library; +} // namespace test +namespace utils +{ +static ValidateExample *g_example = nullptr; +template +class ExampleTest : public arm_compute::test::framework::TestCase +{ +public: + ExampleTest() = default; + void do_run() override + { + g_example->do_run(); + } + void do_teardown() override + { + if(validate) + { + g_example->do_validate(); + } + g_example->do_teardown(); + } +}; + +int run_example(int argc, char **argv, ValidateExample &example) +{ + framework::CommandLineParser parser; + framework::CommonOptions options(parser); + auto example_args = parser.add_option>("example_args"); + example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)"); + auto seed = parser.add_option>("seed", std::random_device()()); + seed->set_help("Global seed for random number generation"); + auto validate = parser.add_option>("validate", 1); + validate->set_help("Enable / disable output validation (0/1)"); + + framework::Framework &framework = framework::Framework::get(); + + parser.parse(argc, argv); + + if(options.help->is_set() && options.help->value()) + { + parser.print_help(argv[0]); + return 0; + } + + std::vector> printers = options.create_printers(); + g_example = &example; + std::vector example_argv = {}; + example_argv.clear(); + example_argv.emplace_back(argv[0]); + for(auto &arg : example_args->value()) + { + example_argv.emplace_back(const_cast(arg.c_str())); // NOLINT + } + + // Set number of threads in Scheduler + Scheduler::get().set_num_threads(options.threads->value()); + library = support::cpp14::make_unique("." /* Only using random values */, seed->value()); + + // We need to do the setup here because framework.init() will need CL / GLES to be initialised + try + { + example.do_setup(example_argv.size(), &example_argv[0]); + } +#ifdef ARM_COMPUTE_CL + catch(cl::Error &err) + { + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + std::cerr << std::endl + << "ERROR " << err.what() << "(" << err.err() << ")" << std::endl; + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + return 1; + } +#endif /* ARM_COMPUTE_CL */ + catch(std::runtime_error &err) + { + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + std::cerr << std::endl + << "ERROR " << err.what() << " " << (errno ? strerror(errno) : "") << std::endl; + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + return 1; + } + + if(options.log_level->value() > framework::LogLevel::NONE) + { + for(auto &p : printers) + { + p->print_global_header(); + } + } + + if(options.log_level->value() >= framework::LogLevel::CONFIG) + { + for(auto &p : printers) + { + p->print_entry("Version", build_information()); + p->print_entry("Seed", support::cpp11::to_string(seed->value())); +#ifdef ARM_COMPUTE_CL + if(opencl_is_available()) + { + p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version()); + } + else + { + p->print_entry("CL_DEVICE_VERSION", "Unavailable"); + } +#endif /* ARM_COMPUTE_CL */ + p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value())); + p->print_entry("Threads", support::cpp11::to_string(options.threads->value())); + example.print_parameters(*p); + } + } + + framework.init(options.instruments->value(), options.iterations->value(), framework::DatasetMode::ALL, "", "", options.log_level->value()); + for(auto &p : printers) + { + framework.add_printer(p.get()); + } + + framework.set_throw_errors(options.throw_errors->value()); + arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" }; + if(validate->value() != 0) + { + framework.add_test_case>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE); + } + else + { + framework.add_test_case>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE); + } + + //func(argc, argv); + bool success = framework.run(); + if(options.log_level->value() > framework::LogLevel::NONE) + { + for(auto &p : printers) + { + p->print_global_footer(); + } + } + + return (success ? 0 : 1); +} + +} // namespace utils +} // namespace arm_compute diff --git a/tests/validate_examples/ValidateExample.h b/tests/validate_examples/ValidateExample.h new file mode 100644 index 0000000000..cedbe3d0d9 --- /dev/null +++ b/tests/validate_examples/ValidateExample.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016-2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __VALIDATE_EXAMPLE_H__ +#define __VALIDATE_EXAMPLE_H__ + +#include "utils/Utils.h" +namespace arm_compute +{ +namespace test +{ +namespace framework +{ +class Printer; +} // namespace framework +} // namespace test +namespace utils +{ +/** Abstract ValidateExample class. + * + * All examples with a validation stage have to inherit from this class. + */ +class ValidateExample : public Example +{ +public: + /** Run reference implementation and validate against the target output + */ + virtual void do_validate() + { + } + /** Print the example parameters + * + * @param[in,out] printer Printer to use to print the parameters + */ + virtual void print_parameters(test::framework::Printer &printer) + { + } + + /** Default destructor */ + virtual ~ValidateExample() = default; +}; +/** Run an example and handle the potential exceptions it throws + * + * @param[in] argc Number of command line arguments + * @param[in] argv Command line arguments + * @param[in] example Example to run + */ +int run_example(int argc, char **argv, ValidateExample &example); + +} // namespace utils +} // namespace arm_compute +#endif /* __VALIDATE_EXAMPLE_H__ */ diff --git a/tests/validate_examples/cl_sgemm.cpp b/tests/validate_examples/cl_sgemm.cpp new file mode 100644 index 0000000000..8b4b98af53 --- /dev/null +++ b/tests/validate_examples/cl_sgemm.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2017-2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */ +#error "This example needs to be built with -DARM_COMPUTE_CL" +#endif /* ARM_COMPUTE_CL */ + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLFunctions.h" +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "arm_compute/runtime/CL/CLTuner.h" + +#include "tests/AssetsLibrary.h" +#include "tests/CL/CLAccessor.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/SimpleTensor.h" +#include "tests/validation/Validation.h" +#include "tests/validation/reference/GEMM.h" + +#include "ValidateExample.h" + +#include "utils/Utils.h" + +#include + +using namespace arm_compute; +using namespace utils; +using namespace arm_compute::test; +using namespace arm_compute::test::validation; + +RelativeTolerance tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr float tolerance_num = 0.02f; /**< Tolerance number */ + +class CLSGEMMValidateExample : public ValidateExample +{ +public: + void do_setup(int argc, char **argv) override + { + alpha = 1.0f; + beta = 0.0f; + + CLScheduler::get().default_init(&tuner); + if(argc < 3) + { + // Print help + std::cout << "Usage: " << argv[0] << " M N K [alpha = 1.0f] [beta = 0.0f]\n\n"; + std::cout << "Too few or no input_matrices provided. Using M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n\n"; + } + else + { + M = strtol(argv[1], nullptr, 10); + N = strtol(argv[2], nullptr, 10); + K = strtol(argv[3], nullptr, 10); + } + + src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, DataType::F32)); + src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32)); + src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32)); + + if(argc > 4) + { + alpha = strtof(argv[4], nullptr); + + if(argc > 5) + { + beta = strtof(argv[5], nullptr); + } + } + + init_sgemm_output(dst, src0, src1, DataType::F32); + + // Configure function + sgemm.configure(&src0, &src1, (src2.info()->total_size() > 0) ? &src2 : nullptr, &dst, alpha, beta); + + // Allocate all the images + src0.allocator()->allocate(); + src1.allocator()->allocate(); + dst.allocator()->allocate(); + + // Fill the input images with either the data provided or random data + src2.allocator()->allocate(); + + fill(CLAccessor(src0), 0); + fill(CLAccessor(src1), 1); + fill(CLAccessor(src2), 2); + } + void print_parameters(framework::Printer &printer) override + { + printer.print_entry("M", support::cpp11::to_string(M)); + printer.print_entry("N", support::cpp11::to_string(N)); + printer.print_entry("K", support::cpp11::to_string(K)); + } + void do_validate() override + { + SimpleTensor ref_src0 = { TensorShape(K, M), DataType::F32, 1 }; + SimpleTensor ref_src1 = { TensorShape(N, K), DataType::F32, 1 }; + SimpleTensor ref_src2 = { TensorShape(N, M), DataType::F32, 1 }; + + fill(ref_src0, 0); + fill(ref_src1, 1); + fill(ref_src2, 2); + + SimpleTensor ref_dst = reference::gemm(ref_src0, ref_src1, ref_src2, alpha, beta); + validate(CLAccessor(dst), ref_dst, tolerance_f32, tolerance_num); + } + void do_run() override + { + // Execute the function + sgemm.run(); + + // Make sure all the OpenCL jobs are done executing: + CLScheduler::get().sync(); + } + +private: + template + void fill(U &&tensor, int i) + { + switch(tensor.data_type()) + { + case DataType::F16: + case DataType::F32: + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + break; + } + default: + library->fill_tensor_uniform(tensor, i); + } + } + size_t M{ 7 }, N{ 3 }, K{ 5 }; + CLTensor src0{}, src1{}, src2{}, dst{}; + CLGEMM sgemm{}; + CLTuner tuner{}; + float alpha{}, beta{}; +}; + +/** Main program for sgemm test + * + * @param[in] argc Number of arguments + * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta ) + */ +int main(int argc, char **argv) +{ + return utils::run_example(argc, argv); +} -- cgit v1.2.1