//***************************************************************************** // Copyright 2017-2019 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //***************************************************************************** #pragma once #include #include #include #include #include #include #include #include #include "gtest/gtest.h" #include "ngraph/descriptor/layout/tensor_layout.hpp" #include "ngraph/file_util.hpp" #include "ngraph/log.hpp" #include "ngraph/runtime/backend.hpp" #include "ngraph/runtime/tensor.hpp" #include "ngraph/serializer.hpp" #ifdef NGRAPH_MLIR_ENABLE #define MLIR_DISABLE_TEST(name) DISABLED_##name #else #define MLIR_DISABLE_TEST(name) name #endif namespace ngraph { class Node; class Function; } bool validate_list(const std::list>& nodes); std::shared_ptr make_test_graph(); #ifndef NGRAPH_JSON_DISABLE std::shared_ptr make_function_from_file(const std::string& file_name); #endif template void copy_data(std::shared_ptr tv, const std::vector& data) { size_t data_size = data.size() * sizeof(T); tv->write(data.data(), data_size); } template std::vector read_vector(std::shared_ptr tv) { if (ngraph::element::from() != tv->get_element_type()) { throw std::invalid_argument("read_vector type must match Tensor type"); } size_t element_count = ngraph::shape_size(tv->get_shape()); size_t size = element_count * sizeof(T); std::vector rc(element_count); tv->read(rc.data(), size); return rc; } std::vector read_float_vector(std::shared_ptr tv); template void write_vector(std::shared_ptr tv, const std::vector& values) { tv->write(values.data(), values.size() * sizeof(T)); } template std::vector> get_ops_of_type(std::shared_ptr f) { std::vector> ops; for (auto op : f->get_ops()) { if (auto cop = ngraph::as_type_ptr(op)) { ops.push_back(cop); } } return ops; } template size_t count_ops_of_type(std::shared_ptr f) { size_t count = 0; for (auto op : f->get_ops()) { if (ngraph::as_type_ptr(op)) { count++; } } return count; } template void init_int_tv(ngraph::runtime::Tensor* tv, std::default_random_engine& engine, T min, T max) { size_t size = tv->get_element_count(); std::uniform_int_distribution dist(min, max); std::vector vec(size); for (T& element : vec) { element = dist(engine); } tv->write(vec.data(), vec.size() * sizeof(T)); } template void init_real_tv(ngraph::runtime::Tensor* tv, std::default_random_engine& engine, T min, T max) { size_t size = tv->get_element_count(); std::uniform_real_distribution dist(min, max); std::vector vec(size); for (T& element : vec) { element = dist(engine); } tv->write(vec.data(), vec.size() * sizeof(T)); } void random_init(ngraph::runtime::Tensor* tv, std::default_random_engine& engine); template std::vector> prepare_and_run(const std::shared_ptr& function, std::vector> t1args, std::vector> t2args, const std::string& backend_id) { auto backend = ngraph::runtime::Backend::create(backend_id); auto parms = function->get_parameters(); if (parms.size() != t1args.size() + t2args.size()) { throw ngraph::ngraph_error("number of parameters and arguments don't match"); } std::vector> arg_tensors(t1args.size() + t2args.size()); size_t total_arg_count = 0; for (size_t i = 0; i < t1args.size(); i++) { auto t = backend->create_tensor(parms.at(total_arg_count)->get_element_type(), parms.at(total_arg_count)->get_shape()); auto x = t1args.at(i); copy_data(t, x); arg_tensors.at(total_arg_count) = t; total_arg_count++; } for (size_t i = 0; i < t2args.size(); i++) { auto t = backend->create_tensor(parms.at(total_arg_count)->get_element_type(), parms.at(total_arg_count)->get_shape()); copy_data(t, t2args.at(i)); arg_tensors.at(total_arg_count) = t; total_arg_count++; } auto results = function->get_results(); std::vector> result_tensors(results.size()); for (size_t i = 0; i < results.size(); i++) { result_tensors.at(i) = backend->create_tensor(results.at(i)->get_element_type(), results.at(i)->get_shape()); } auto handle = backend->compile(function); handle->call_with_validate(result_tensors, arg_tensors); return result_tensors; } template std::vector> prepare_and_run(const std::shared_ptr& function, std::vector> args, const std::string& backend_id) { std::vector> emptyargs; return prepare_and_run(function, args, emptyargs, backend_id); } template std::vector> execute(const std::shared_ptr& function, std::vector> t1args, std::vector> t2args, const std::string& backend_id) { std::vector> result_tensors = prepare_and_run(function, t1args, t2args, backend_id); std::vector> result_vectors; for (auto rt : result_tensors) { result_vectors.push_back(read_vector(rt)); } return result_vectors; } template std::vector> execute(const std::shared_ptr& function, std::vector> args, const std::string& backend_id) { std::vector> emptyargs; return execute(function, args, emptyargs, backend_id); } template std::string get_results_str(const std::vector& ref_data, const std::vector& actual_data, size_t max_results = 16) { std::stringstream ss; size_t num_results = std::min(static_cast(max_results), ref_data.size()); ss << "First " << num_results << " results"; for (size_t i = 0; i < num_results; ++i) { ss << std::endl // use unary + operator to force integral values to be displayed as numbers << std::setw(4) << i << " ref: " << std::setw(16) << std::left << +ref_data[i] << " actual: " << std::setw(16) << std::left << +actual_data[i]; } ss << std::endl; return ss.str(); } template <> std::string get_results_str(const std::vector& ref_data, const std::vector& actual_data, size_t max_results); /// \brief Reads a binary file to a vector. /// /// \param[in] path The path where the file is located. /// /// \tparam T The type we want to interpret as the elements in binary file. /// /// \return Return vector of data read from input binary file. /// template std::vector read_binary_file(const std::string& path) { std::vector file_content; std::ifstream inputs_fs{path, std::ios::in | std::ios::binary}; if (!inputs_fs) { throw std::runtime_error("Failed to open the file: " + path); } inputs_fs.seekg(0, std::ios::end); auto size = inputs_fs.tellg(); inputs_fs.seekg(0, std::ios::beg); if (size % sizeof(T) != 0) { throw std::runtime_error( "Error reading binary file content: Input file size (in bytes) " "is not a multiple of requested data type size."); } file_content.resize(size / sizeof(T)); inputs_fs.read(reinterpret_cast(file_content.data()), size); return file_content; } testing::AssertionResult test_ordered_ops(std::shared_ptr f, const ngraph::NodeVector& required_ops);