test_tools.hpp 9.25 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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.
//*****************************************************************************
16

17
#pragma once
18

19
#include <exception>
20
#include <fstream>
21 22
#include <iomanip>
#include <iostream>
23
#include <list>
Robert Kimball's avatar
Robert Kimball committed
24
#include <memory>
25
#include <random>
26
#include <vector>
27

28 29
#include "gtest/gtest.h"

Scott Cyphers's avatar
Scott Cyphers committed
30
#include "ngraph/descriptor/layout/tensor_layout.hpp"
31
#include "ngraph/file_util.hpp"
32
#include "ngraph/log.hpp"
33
#include "ngraph/runtime/backend.hpp"
34
#include "ngraph/runtime/tensor.hpp"
35
#include "ngraph/serializer.hpp"
36

37 38 39 40 41 42
#ifdef NGRAPH_MLIR_ENABLE
#define MLIR_DISABLE_TEST(name) DISABLED_##name
#else
#define MLIR_DISABLE_TEST(name) name
#endif

43
namespace ngraph
44
{
45
    class Node;
46
    class Function;
47
}
48

49
bool validate_list(const std::list<std::shared_ptr<ngraph::Node>>& nodes);
50
std::shared_ptr<ngraph::Function> make_test_graph();
51
#ifndef NGRAPH_JSON_DISABLE
52
std::shared_ptr<ngraph::Function> make_function_from_file(const std::string& file_name);
53
#endif
54 55

template <typename T>
56
void copy_data(std::shared_ptr<ngraph::runtime::Tensor> tv, const std::vector<T>& data)
57 58
{
    size_t data_size = data.size() * sizeof(T);
59
    tv->write(data.data(), data_size);
60
}
61 62

template <typename T>
63
std::vector<T> read_vector(std::shared_ptr<ngraph::runtime::Tensor> tv)
64
{
65
    if (ngraph::element::from<T>() != tv->get_element_type())
66
    {
67
        throw std::invalid_argument("read_vector type must match Tensor type");
68 69 70 71
    }
    size_t element_count = ngraph::shape_size(tv->get_shape());
    size_t size = element_count * sizeof(T);
    std::vector<T> rc(element_count);
72
    tv->read(rc.data(), size);
73 74 75
    return rc;
}

76
std::vector<float> read_float_vector(std::shared_ptr<ngraph::runtime::Tensor> tv);
77

78
template <typename T>
79
void write_vector(std::shared_ptr<ngraph::runtime::Tensor> tv, const std::vector<T>& values)
80
{
81
    tv->write(values.data(), values.size() * sizeof(T));
82
}
83

84 85 86 87 88 89
template <typename T>
std::vector<std::shared_ptr<T>> get_ops_of_type(std::shared_ptr<ngraph::Function> f)
{
    std::vector<std::shared_ptr<T>> ops;
    for (auto op : f->get_ops())
    {
90
        if (auto cop = ngraph::as_type_ptr<T>(op))
91 92 93 94 95 96 97 98
        {
            ops.push_back(cop);
        }
    }

    return ops;
}

99 100 101 102 103 104
template <typename T>
size_t count_ops_of_type(std::shared_ptr<ngraph::Function> f)
{
    size_t count = 0;
    for (auto op : f->get_ops())
    {
105
        if (ngraph::as_type_ptr<T>(op))
106 107 108 109 110 111 112
        {
            count++;
        }
    }

    return count;
}
113

114 115 116 117 118 119 120 121 122 123
template <typename T>
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<T> dist(min, max);
    std::vector<T> vec(size);
    for (T& element : vec)
    {
        element = dist(engine);
    }
124
    tv->write(vec.data(), vec.size() * sizeof(T));
125 126 127 128 129 130 131 132 133 134 135 136
}

template <typename T>
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<T> dist(min, max);
    std::vector<T> vec(size);
    for (T& element : vec)
    {
        element = dist(engine);
    }
137
    tv->write(vec.data(), vec.size() * sizeof(T));
138 139 140 141
}

void random_init(ngraph::runtime::Tensor* tv, std::default_random_engine& engine);

142
template <typename T1, typename T2>
tsocha's avatar
tsocha committed
143 144
std::vector<std::shared_ptr<ngraph::runtime::Tensor>>
    prepare_and_run(const std::shared_ptr<ngraph::Function>& function,
145 146
                    std::vector<std::vector<T1>> t1args,
                    std::vector<std::vector<T2>> t2args,
tsocha's avatar
tsocha committed
147
                    const std::string& backend_id)
148
{
149
    auto backend = ngraph::runtime::Backend::create(backend_id);
150

151
    auto parms = function->get_parameters();
152

153
    if (parms.size() != t1args.size() + t2args.size())
154 155 156 157
    {
        throw ngraph::ngraph_error("number of parameters and arguments don't match");
    }

158 159 160 161 162
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> arg_tensors(t1args.size() +
                                                                      t2args.size());

    size_t total_arg_count = 0;
    for (size_t i = 0; i < t1args.size(); i++)
163
    {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        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++;
179 180
    }

181
    auto results = function->get_results();
182
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors(results.size());
183 184 185 186 187 188 189

    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());
    }

190
    auto handle = backend->compile(function);
191
    handle->call_with_validate(result_tensors, arg_tensors);
192

tsocha's avatar
tsocha committed
193 194 195
    return result_tensors;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
template <typename T>
std::vector<std::shared_ptr<ngraph::runtime::Tensor>>
    prepare_and_run(const std::shared_ptr<ngraph::Function>& function,
                    std::vector<std::vector<T>> args,
                    const std::string& backend_id)
{
    std::vector<std::vector<T>> emptyargs;
    return prepare_and_run<T, T>(function, args, emptyargs, backend_id);
}

template <typename TIN1, typename TIN2, typename TOUT>
std::vector<std::vector<TOUT>> execute(const std::shared_ptr<ngraph::Function>& function,
                                       std::vector<std::vector<TIN1>> t1args,
                                       std::vector<std::vector<TIN2>> t2args,
                                       const std::string& backend_id)
tsocha's avatar
tsocha committed
211 212
{
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors =
213
        prepare_and_run(function, t1args, t2args, backend_id);
214

215
    std::vector<std::vector<TOUT>> result_vectors;
216 217
    for (auto rt : result_tensors)
    {
218
        result_vectors.push_back(read_vector<TOUT>(rt));
219 220 221
    }
    return result_vectors;
}
222

223 224 225 226 227 228 229 230 231
template <typename TIN, typename TOUT = TIN>
std::vector<std::vector<TOUT>> execute(const std::shared_ptr<ngraph::Function>& function,
                                       std::vector<std::vector<TIN>> args,
                                       const std::string& backend_id)
{
    std::vector<std::vector<TIN>> emptyargs;
    return execute<TIN, TIN, TOUT>(function, args, emptyargs, backend_id);
}

232
template <typename T>
233 234 235
std::string get_results_str(const std::vector<T>& ref_data,
                            const std::vector<T>& actual_data,
                            size_t max_results = 16)
236
{
237
    std::stringstream ss;
238
    size_t num_results = std::min(static_cast<size_t>(max_results), ref_data.size());
239
    ss << "First " << num_results << " results";
240 241
    for (size_t i = 0; i < num_results; ++i)
    {
242 243 244 245
        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];
246
    }
247
    ss << std::endl;
248 249

    return ss.str();
250 251 252
}

template <>
253 254
std::string get_results_str(const std::vector<char>& ref_data,
                            const std::vector<char>& actual_data,
255
                            size_t max_results);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

/// \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 <typename T>
std::vector<T> read_binary_file(const std::string& path)
{
    std::vector<T> 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<char*>(file_content.data()), size);
    return file_content;
}
288 289 290

testing::AssertionResult test_ordered_ops(std::shared_ptr<ngraph::Function> f,
                                          const ngraph::NodeVector& required_ops);