util.cpp 21.6 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2020 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.
//*****************************************************************************
Robert Kimball's avatar
Robert Kimball committed
16

17
#include <fstream>
Robert Kimball's avatar
Robert Kimball committed
18
#include <sstream>
19 20
#include <string>
#include <vector>
Robert Kimball's avatar
Robert Kimball committed
21 22 23

#include "gtest/gtest.h"

24
#include "ngraph/file_util.hpp"
Robert Kimball's avatar
Robert Kimball committed
25
#include "ngraph/function.hpp"
varun-intel's avatar
varun-intel committed
26
#include "ngraph/graph_util.hpp"
27
#include "ngraph/ngraph.hpp"
28
#include "ngraph/op/util/op_annotations.hpp"
29 30
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/visualize_tree.hpp"
31
#include "ngraph/serializer.hpp"
32
#include "util/all_close.hpp"
33
#include "util/autodiff/backprop_function.hpp"
34
#include "util/ndarray.hpp"
Robert Kimball's avatar
Robert Kimball committed
35 36

using namespace std;
37
using namespace ngraph;
Robert Kimball's avatar
Robert Kimball committed
38 39 40 41 42

TEST(util, split)
{
    {
        string s1 = "this,is,a,test";
43
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
44 45 46 47 48 49 50 51 52
        ASSERT_EQ(4, r1.size());
        EXPECT_STRCASEEQ("this", r1[0].c_str());
        EXPECT_STRCASEEQ("is", r1[1].c_str());
        EXPECT_STRCASEEQ("a", r1[2].c_str());
        EXPECT_STRCASEEQ("test", r1[3].c_str());
    }

    {
        string s1 = "this,is,a,test,";
53
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
54 55 56 57 58 59 60 61 62 63
        ASSERT_EQ(5, r1.size());
        EXPECT_STRCASEEQ("this", r1[0].c_str());
        EXPECT_STRCASEEQ("is", r1[1].c_str());
        EXPECT_STRCASEEQ("a", r1[2].c_str());
        EXPECT_STRCASEEQ("test", r1[3].c_str());
        EXPECT_STRCASEEQ("", r1[4].c_str());
    }

    {
        string s1 = ",this,is,a,test";
64
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
65 66 67 68 69 70 71 72 73 74
        ASSERT_EQ(5, r1.size());
        EXPECT_STRCASEEQ("", r1[0].c_str());
        EXPECT_STRCASEEQ("this", r1[1].c_str());
        EXPECT_STRCASEEQ("is", r1[2].c_str());
        EXPECT_STRCASEEQ("a", r1[3].c_str());
        EXPECT_STRCASEEQ("test", r1[4].c_str());
    }

    {
        string s1 = "this,,is,a,test";
75
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
76 77 78 79 80 81 82 83 84 85
        ASSERT_EQ(5, r1.size());
        EXPECT_STRCASEEQ("this", r1[0].c_str());
        EXPECT_STRCASEEQ("", r1[1].c_str());
        EXPECT_STRCASEEQ("is", r1[2].c_str());
        EXPECT_STRCASEEQ("a", r1[3].c_str());
        EXPECT_STRCASEEQ("test", r1[4].c_str());
    }

    {
        string s1 = "this";
86
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
87 88 89 90 91 92
        ASSERT_EQ(1, r1.size());
        EXPECT_STRCASEEQ("this", r1[0].c_str());
    }

    {
        string s1 = "";
93
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
94 95 96 97 98 99 100 101 102 103 104 105
        ASSERT_EQ(1, r1.size());
        EXPECT_STRCASEEQ("", r1[0].c_str());
    }
}

TEST(DISABLED_util, dump)
{
    string text = "this is a text string used to test the dump function.";

    dump(cout, text.data(), text.size());
}

106 107 108 109
#ifdef _WIN32
#include "windows.h"
#define usleep(a) Sleep(a / 1000)
#endif
Robert Kimball's avatar
Robert Kimball committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
TEST(util, stopwatch)
{
    stopwatch t1;

    t1.start();
    usleep(1000);
    t1.stop();

    t1.start();
    usleep(1000);
    t1.stop();

    t1.start();
    usleep(1000);
    t1.stop();

    EXPECT_EQ(3, t1.get_call_count());

    EXPECT_GT(t1.get_total_microseconds(), t1.get_microseconds());
}

TEST(util, trim)
{
    EXPECT_STREQ("test", trim("test").c_str());
    EXPECT_STREQ("test", trim(" test").c_str());
    EXPECT_STREQ("test", trim("test ").c_str());
    EXPECT_STREQ("test", trim(" test ").c_str());
    EXPECT_STREQ("test", trim("           test            ").c_str());
    EXPECT_STREQ("test", trim("\ttest").c_str());
    EXPECT_STREQ("test", trim("test\t").c_str());
    EXPECT_STREQ("test", trim("\ttest\t").c_str());
    EXPECT_STREQ("test", trim(" \t test \t ").c_str());
}

144
#if defined(NGRAPH_INTERPRETER_ENABLE)
145 146
TEST(util, all_close)
{
147
    auto backend = runtime::Backend::create("INTERPRETER");
148 149

    // Create some tensors for input/output
150 151
    auto a = backend->create_tensor(element::f32, Shape{2, 3});
    auto b = backend->create_tensor(element::f32, Shape{2, 3});
Robert Kimball's avatar
Robert Kimball committed
152

153 154
    copy_data(a, test::NDArray<float, 2>({{1, 2, 3}, {3, 4, 5}}).get_vector());
    copy_data(b, test::NDArray<float, 2>({{1, 2, 3}, {3, 4, 5}}).get_vector());
155

156
    EXPECT_TRUE(ngraph::test::all_close<float>(a, b));
157

158
    auto c = backend->create_tensor(element::f32, Shape{2, 3});
159
    copy_data(c, test::NDArray<float, 2>({{1.1f, 2, 3}, {3, 4, 5}}).get_vector());
Robert Kimball's avatar
Robert Kimball committed
160

161 162
    EXPECT_FALSE(ngraph::test::all_close<float>(c, a, 0, .05f));
    EXPECT_TRUE(ngraph::test::all_close<float>(c, a, 0, .11f));
163

164 165
    EXPECT_FALSE(ngraph::test::all_close<float>(c, a, .05f, 0));
    EXPECT_TRUE(ngraph::test::all_close<float>(c, a, .11f, 0));
166
}
167
#endif
Robert Kimball's avatar
Robert Kimball committed
168 169 170 171

TEST(util, traverse_functions)
{
    // First create "f(A,B,C) = (A+B)*C".
172
    Shape shape{2, 2};
173 174 175
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
176
    auto f = make_shared<Function>((A + B) * C, ParameterVector{A, B, C}, "f");
Robert Kimball's avatar
Robert Kimball committed
177 178

    vector<Function*> functions;
179 180
    traverse_functions(f, [&](shared_ptr<Function> fp) { functions.push_back(fp.get()); });
    ASSERT_EQ(1, functions.size());
Robert Kimball's avatar
Robert Kimball committed
181
}
182 183 184 185 186 187

class CloneTest : public ::testing::Test
{
public:
    // (A + B) * C
    Shape shape = Shape{2, 2};
188 189 190
    std::shared_ptr<op::Parameter> A = make_shared<op::Parameter>(element::f32, shape);
    std::shared_ptr<op::Parameter> B = make_shared<op::Parameter>(element::f32, shape);
    std::shared_ptr<op::Parameter> C = make_shared<op::Parameter>(element::f32, shape);
191 192 193 194 195 196
    std::shared_ptr<Node> AplusB = A + B;
    std::shared_ptr<Node> AplusBtimesC = AplusB * C;

    NodeMap node_map;
    std::list<std::shared_ptr<ngraph::Node>> nodes;
    std::shared_ptr<Function> func =
197
        make_shared<Function>(AplusBtimesC, ParameterVector{A, B, C}, "f");
198 199 200 201 202 203 204 205 206 207

    void SetUp()
    {
        nodes.push_back(AplusBtimesC);
        nodes.push_back(AplusB);
        nodes.push_back(A);
        nodes.push_back(B);
        nodes.push_back(C);
    }

208 209 210
    bool CompareNodeVector(const std::list<std::shared_ptr<ngraph::Node>>& orig,
                           const std::list<std::shared_ptr<ngraph::Node>>& clone,
                           const NodeMap& nm)
211 212 213 214 215 216 217 218 219
    {
        if (orig.size() != clone.size())
        {
            return false;
        }
        auto origit = orig.begin();
        auto cloneit = clone.begin();
        while (origit != orig.end() && cloneit != clone.end())
        {
220
            if (*cloneit != nm.at((*origit).get()))
221 222 223 224 225 226 227 228 229 230 231 232 233
            {
                return false;
            }
            ++origit;
            ++cloneit;
        }
        return true;
    }
};

TEST_F(CloneTest, clone_nodes_full)
{
    auto cloned_nodes = clone_nodes(nodes, node_map);
234
    ASSERT_TRUE(CompareNodeVector(nodes, cloned_nodes, node_map));
235

236 237 238 239 240
    ASSERT_NE(nullptr, as_type_ptr<op::Parameter>(node_map.at(A.get())));
    ASSERT_NE(nullptr, as_type_ptr<op::Parameter>(node_map.at(B.get())));
    ASSERT_NE(nullptr, as_type_ptr<op::Parameter>(node_map.at(C.get())));
    ASSERT_NE(nullptr, as_type_ptr<op::Add>(node_map.at(AplusB.get())));
    ASSERT_NE(nullptr, as_type_ptr<op::Multiply>(node_map.at(AplusBtimesC.get())));
241 242 243

    auto sorted_nodes = topological_sort(nodes);
    auto sorted_cloned_nodes = topological_sort(cloned_nodes);
244
    ASSERT_TRUE(CompareNodeVector(sorted_nodes, sorted_cloned_nodes, node_map));
245 246 247 248 249
}

TEST_F(CloneTest, clone_nodes_partial)
{
    // map A -> A' prior to clone
250
    auto Aprime = make_shared<op::Parameter>(element::f32, shape);
251
    node_map[A.get()] = Aprime;
252 253

    auto cloned_nodes = clone_nodes(nodes, node_map);
254
    ASSERT_TRUE(CompareNodeVector(nodes, cloned_nodes, node_map));
255 256

    // ensure A -> A' after clone
257
    ASSERT_EQ(Aprime, node_map.at(A.get()));
258 259 260 261
}

TEST_F(CloneTest, clone_function_full)
{
262
    auto cloned_func = clone_function(*func, node_map);
263
    ASSERT_TRUE(CompareNodeVector(func->get_ops(), cloned_func->get_ops(), node_map));
264
}
265

266 267
TEST(graph_util, clone_multiple_results)
{
268
    Shape shape{2, 2};
269 270 271 272 273 274
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
    auto A_add_B = make_shared<op::Add>(A, B);
    auto A_add_B_mul_C = make_shared<op::Multiply>(A_add_B, C);

275
    auto f = make_shared<Function>(NodeVector{A_add_B, A_add_B_mul_C}, ParameterVector{A, B, C});
276

277
    auto copy = clone_function(*f);
278 279
}

280 281 282 283 284 285 286 287 288
TEST(util, round_up)
{
    EXPECT_EQ(0, round_up(0, 4));
    EXPECT_EQ(4, round_up(1, 4));
    EXPECT_EQ(4, round_up(2, 4));
    EXPECT_EQ(4, round_up(3, 4));
    EXPECT_EQ(4, round_up(4, 4));
    EXPECT_EQ(8, round_up(5, 4));
}
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

TEST(util, parse_string)
{
    EXPECT_FLOAT_EQ(2, parse_string<float>("2"));
    EXPECT_FLOAT_EQ(2.125, parse_string<float>("2.125"));
    EXPECT_FLOAT_EQ(numeric_limits<float>::infinity(), parse_string<float>("INFINITY"));
    EXPECT_FLOAT_EQ(numeric_limits<float>::infinity(), parse_string<float>("infinity"));
    EXPECT_FLOAT_EQ(-numeric_limits<float>::infinity(), parse_string<float>("-INFINITY"));
    EXPECT_TRUE(isnan(parse_string<float>("NaN")));

    EXPECT_FLOAT_EQ(2, parse_string<double>("2"));
    EXPECT_FLOAT_EQ(2.125, parse_string<double>("2.125"));
    EXPECT_FLOAT_EQ(numeric_limits<double>::infinity(), parse_string<double>("INFINITY"));
    EXPECT_FLOAT_EQ(numeric_limits<double>::infinity(), parse_string<double>("infinity"));
    EXPECT_FLOAT_EQ(-numeric_limits<double>::infinity(), parse_string<double>("-INFINITY"));
304
    EXPECT_TRUE(std::isnan(parse_string<double>("NaN")));
305
}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

TEST(graph_util, get_subgraph_outputs_trivial_tests)
{
    auto outputs = ngraph::get_subgraph_outputs(NodeVector{}, NodeVector{});
    ASSERT_EQ(outputs.size(), 0);

    Shape shape{};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto absn = make_shared<op::Abs>(A);
    auto neg_absn = make_shared<op::Negative>(absn);
    outputs = ngraph::get_subgraph_outputs(NodeVector{A}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{A}));

    outputs = ngraph::get_subgraph_outputs(NodeVector{A}, NodeVector{A});
    ASSERT_EQ(outputs, (NodeVector{}));

    outputs = ngraph::get_subgraph_outputs(NodeVector{A, absn}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{absn}));

    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto abs_b = make_shared<op::Abs>(B);
    auto neg_b = make_shared<op::Negative>(B);
    auto abs_b_neg = make_shared<op::Negative>(abs_b);
    outputs = ngraph::get_subgraph_outputs(NodeVector{B, abs_b}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{B, abs_b}));

    outputs = ngraph::get_subgraph_outputs(NodeVector{B, abs_b}, NodeVector{B});
    ASSERT_EQ(outputs, (NodeVector{abs_b}));

    outputs = ngraph::get_subgraph_outputs(NodeVector{B, abs_b, abs_b_neg}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{B}));

    auto add_b = make_shared<op::Add>(neg_b, abs_b_neg);
    outputs =
        ngraph::get_subgraph_outputs(NodeVector{B, abs_b, neg_b, abs_b_neg, add_b}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{}));

343
    // now add_b uses abs_b_neg
344 345 346
    outputs = ngraph::get_subgraph_outputs(NodeVector{B, abs_b, abs_b_neg}, NodeVector{});
    ASSERT_EQ(outputs, (NodeVector{B, abs_b_neg}));
}
347 348 349 350 351 352 353 354 355

TEST(util, test_fprop_cache)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
    auto output = (A + B) * C + A;

356
    auto f = make_shared<Function>(NodeVector{output}, ParameterVector{A, B, C});
357 358 359 360 361 362 363 364

    auto bf = autodiff::backprop_function(f);

    auto fprop_cache = cache_fprop(f, bf);

    EXPECT_EQ(fprop_cache.fprop->get_results().size(), 2);
    EXPECT_EQ(fprop_cache.bprop->get_parameters().size(), 5);
}
365 366 367 368 369 370 371 372 373

TEST(graph_util, test_subgraph_topological_sort)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
    auto add = A + B;
    auto mul = C * add;
374
    auto result = make_shared<op::Result>(mul);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    auto sorted = ngraph::subgraph_topological_sort(NodeVector{mul, add, A});
    std::list<std::shared_ptr<Node>> expected{A, add, mul};
    ASSERT_EQ(expected, sorted);
}

TEST(graph_util, test_subgraph_topological_sort_control_dependencies)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
    auto D = make_shared<op::Abs>(A);
    auto E = make_shared<op::Abs>(B);
    auto add = A + B;
    add->add_control_dependency(D);
    add->add_control_dependency(E);
    auto mul = C * add;
392
    auto result = make_shared<op::Result>(mul);
393 394 395 396
    auto sorted = ngraph::subgraph_topological_sort(NodeVector{mul, add, A, D}, true);
    std::list<std::shared_ptr<Node>> expected{A, D, add, mul};
    ASSERT_EQ(expected, sorted);
}
397

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
TEST(util, enum_mask_construction)
{
    enum class Type : uint32_t
    {
        a = 0x1,
        b = 1 << 1,
        c = 1 << 2,
        d = 1 << 3
    };
    {
        EnumMask<Type> m;
        EXPECT_EQ(0, m.value());
    }
    {
        EnumMask<Type> m(Type::c);
        EXPECT_EQ(static_cast<uint32_t>(Type::c), m.value());
    }
    {
        EnumMask<Type> a(Type::c);
        EnumMask<Type> b{a};
        EXPECT_EQ(a.value(), b.value());
    }
    {
        EnumMask<Type> a{Type::a, Type::c, Type::d};
        EXPECT_EQ((static_cast<uint32_t>(Type::a) | static_cast<uint32_t>(Type::c) |
                   static_cast<uint32_t>(Type::d)),
                  a.value());
    }
}

TEST(util, enum_mask_set_clear)
{
    enum class Type : uint32_t
    {
        a = 0x1,
        b = 1 << 1,
        c = 1 << 2,
        d = 1 << 3
    };
    EnumMask<Type> m;
    m.set(Type::b);
    EXPECT_EQ(static_cast<uint32_t>(Type::b), m.value());
    m.set(Type::c);
    EXPECT_EQ(static_cast<uint32_t>(Type::b) | static_cast<uint32_t>(Type::c), m.value());
    m.clear(Type::b);
    EXPECT_EQ(static_cast<uint32_t>(Type::c), m.value());
    m.clear_all();
    EXPECT_EQ(0, m.value());
    m.set(Type::d);
    m.set(Type::b);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    EXPECT_TRUE(m.is_set(Type::d));
    EXPECT_FALSE(m.is_set(Type::a));
    EXPECT_TRUE(m.is_set(Type::b));
    EXPECT_FALSE(m.is_set(Type::c));
    EXPECT_FALSE(m.is_set({Type::a, Type::b}));
    EXPECT_FALSE(m.is_set({Type::c, Type::d}));
    EXPECT_FALSE(m.is_set({Type::a, Type::c}));
    EXPECT_TRUE(m.is_set({Type::b, Type::d}));
    EXPECT_FALSE(m.is_clear(Type::d));
    EXPECT_TRUE(m.is_clear(Type::a));
    EXPECT_FALSE(m.is_clear(Type::b));
    EXPECT_TRUE(m.is_clear(Type::c));
    EXPECT_FALSE(m.is_clear({Type::c, Type::d}));
    EXPECT_FALSE(m.is_clear({Type::a, Type::b}));
    EXPECT_TRUE(m.is_clear({Type::a, Type::c}));
    EXPECT_FALSE(m.is_clear({Type::b, Type::d}));

    EXPECT_TRUE(m.is_any_set({Type::a, Type::b}));
    EXPECT_TRUE(m.is_any_set({Type::a, Type::d}));
    EXPECT_TRUE(m.is_any_set({Type::b, Type::c}));
    EXPECT_TRUE(m.is_any_set({Type::c, Type::d}));
    EXPECT_FALSE(m.is_any_set({Type::a, Type::c}));
    EXPECT_TRUE(m.is_any_clear({Type::c, Type::d}));
    EXPECT_TRUE(m.is_any_clear({Type::a, Type::b}));
    EXPECT_TRUE(m.is_any_clear({Type::a, Type::c}));
    EXPECT_TRUE(m.is_any_clear({Type::b, Type::c}));
    EXPECT_FALSE(m.is_any_clear({Type::b, Type::d}));
475 476

    m.set(Type::a);
477 478 479 480
    EXPECT_FALSE(m.is_clear(Type::a));
    EXPECT_FALSE(m.is_clear(Type::b));
    EXPECT_TRUE(m.is_clear(Type::c));
    EXPECT_FALSE(m.is_clear(Type::d));
481 482 483 484 485 486 487 488 489 490 491 492 493 494
}

TEST(util, enum_mask_operators)
{
    enum class Type : uint32_t
    {
        a = 0x1,
        b = 1 << 1,
        c = 1 << 2,
        d = 1 << 3
    };
    EnumMask<Type> m;
    m = Type::b;
    EXPECT_EQ(static_cast<uint32_t>(Type::b), m.value());
495 496 497
    EXPECT_TRUE(m[Type::b]);
    EXPECT_FALSE(m[Type::a]);
    EXPECT_FALSE(m[Type::c]);
498 499 500 501 502 503 504
    m |= Type::c;
    EXPECT_EQ(static_cast<uint32_t>(Type::b) | static_cast<uint32_t>(Type::c), m.value());
    m &= Type::d;
    EXPECT_EQ(0, m.value());

    m |= Type::a;
    m |= Type::c;
505 506 507 508 509 510 511 512 513 514
    EXPECT_TRUE(m.is_set(Type::a));
    EXPECT_FALSE(m.is_set(Type::b));
    EXPECT_TRUE(m.is_set(Type::c));
    EXPECT_FALSE(m.is_set(Type::d));
    EXPECT_TRUE(m.is_any_set(Type::a));
    EXPECT_FALSE(m.is_any_set(Type::b));
    EXPECT_TRUE(m.is_any_set(Type::c));
    EXPECT_FALSE(m.is_any_set(Type::d));
    EXPECT_TRUE(m.is_any_set({Type::a, Type::c}));
    EXPECT_FALSE(m.is_any_set({Type::b, Type::d}));
515 516 517 518 519 520 521

    EnumMask<Type> n;
    n = m | n;
    EXPECT_EQ(m, n);
    n = m & n;
    EXPECT_EQ(m, n);
    bool r = (n == m);
522
    EXPECT_TRUE(r);
523
    r = (n != m);
524
    EXPECT_FALSE(r);
525 526 527
    n.clear_all();
    n = {Type::a, Type::b};
    r = (n == m);
528
    EXPECT_FALSE(r);
529
    r = (n != m);
530
    EXPECT_TRUE(r);
531 532 533
    n = m & n;
    EXPECT_EQ(static_cast<uint32_t>(Type::a), n.value());
    n = m | Type::b;
534 535 536 537 538 539
    EXPECT_TRUE(n.is_set(Type::a));
    EXPECT_TRUE(n.is_set(Type::b));
    EXPECT_TRUE(n.is_set(Type::c));
    EXPECT_FALSE(n.is_set(Type::d));
    EXPECT_FALSE(n[Type::d]);
    EXPECT_TRUE(n[Type::b]);
540
}
541

Scott Cyphers's avatar
Scott Cyphers committed
542 543 544 545 546 547 548 549 550 551
TEST(graph, huge)
{
    std::vector<std::weak_ptr<Node>> weak_nodes;
    {
        auto param = make_shared<op::Parameter>(element::f32, Shape{3, 3});
        std::shared_ptr<Node> n = param;
        for (size_t i = 0; i < 1000000; i++)
        {
            n = make_shared<op::Negative>(n);
        }
552 553 554
        auto f = make_shared<Function>(NodeVector{n}, ParameterVector{param});
        f->map_unordered_ops(
            [&weak_nodes](Node* node) { weak_nodes.push_back(node->shared_from_this()); });
Scott Cyphers's avatar
Scott Cyphers committed
555 556
    }

557
    for (auto& weak_node : weak_nodes)
Scott Cyphers's avatar
Scott Cyphers committed
558 559 560 561 562
    {
        EXPECT_TRUE(weak_node.expired());
    }
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
TEST(util, apply_permutation)
{
    ASSERT_EQ(apply_permutation(Shape{0, 1, 2, 3}, AxisVector{2, 1, 0, 3}), (Shape{2, 1, 0, 3}));
}

TEST(util, apply_permutation_too_short_fails)
{
    ASSERT_THROW(apply_permutation(Shape{0, 1, 2, 3}, AxisVector{0, 1, 2}), CheckFailure);
}

TEST(util, apply_permutation_too_long_fails)
{
    ASSERT_THROW(apply_permutation(Shape{0, 1, 2, 3}, AxisVector{0, 1, 2, 3, 3}), CheckFailure);
}

TEST(util, apply_permutation_oob_axis_fails)
{
    ASSERT_THROW(apply_permutation(Shape{0, 1, 2, 3}, AxisVector{0, 1, 2, 4}), CheckFailure);
}

TEST(util, apply_permutation_repeated_axis_fails)
{
    ASSERT_THROW(apply_permutation(Shape{0, 1, 2, 3}, AxisVector{0, 1, 2, 2}), CheckFailure);
}

TEST(util, apply_permutation_pshape)
{
    ASSERT_TRUE(
        apply_permutation(PartialShape{0, Dimension::dynamic(), 2, 3}, AxisVector{2, 1, 0, 3})
            .same_scheme(PartialShape{2, Dimension::dynamic(), 0, 3}));
}

TEST(util, apply_permutation_pshape_rank_dynamic)
{
    ASSERT_TRUE(apply_permutation(PartialShape::dynamic(), AxisVector{2, 1, 0, 3})
                    .same_scheme(PartialShape::dynamic()));
}

TEST(util, apply_permutation_pshape_too_short_fails)
{
    ASSERT_THROW(
        apply_permutation(PartialShape{0, Dimension::dynamic(), 2, 3}, AxisVector{0, 1, 2}),
        CheckFailure);
}

TEST(util, apply_permutation_pshape_too_long_fails)
{
    ASSERT_THROW(
        apply_permutation(PartialShape{0, Dimension::dynamic(), 2, 3}, AxisVector{0, 1, 2, 3, 3}),
        CheckFailure);
}

TEST(util, apply_permutation_pshape_oob_axis_fails)
{
    ASSERT_THROW(
        apply_permutation(PartialShape{0, Dimension::dynamic(), 2, 3}, AxisVector{0, 1, 2, 4}),
        CheckFailure);
}

TEST(util, apply_permutation_pshape_repeated_axis_fails)
{
    ASSERT_THROW(
        apply_permutation(PartialShape{0, Dimension::dynamic(), 2, 3}, AxisVector{0, 1, 2, 2}),
        CheckFailure);
}

TEST(util, apply_permutation_pshape_rank_dynamic_inviable_permutation_fails)
{
    ASSERT_THROW(apply_permutation(PartialShape::dynamic(), AxisVector{0, 1, 2, 2}), CheckFailure);
}
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

TEST(util, clone_function_friendly_name)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto f = make_shared<Function>(make_shared<op::Add>(A, B), ParameterVector{A, B});

    A->set_friendly_name("A");
    B->set_friendly_name("B");

    auto g = clone_function(*f);

    bool found_A = false;
    bool found_B = false;
    for (auto parameter : g->get_parameters())
    {
        found_A |= parameter->get_friendly_name() == "A";
        found_B |= parameter->get_friendly_name() == "B";
    }
    EXPECT_TRUE(found_A);
    EXPECT_TRUE(found_B);
}
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

TEST(util, clone_function_op_annotations)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
    auto f = make_shared<Function>(A + B + C, ParameterVector{A, B, C});

    auto cacheable_op_annotation = std::make_shared<op::util::OpAnnotations>();
    cacheable_op_annotation->set_cacheable(true);
    A->set_op_annotations(cacheable_op_annotation);

    auto uncacheable_op_annotation = std::make_shared<op::util::OpAnnotations>();
    uncacheable_op_annotation->set_cacheable(false);
    B->set_op_annotations(uncacheable_op_annotation);

    auto g = clone_function(*f);

    bool found_A = false;
    bool found_B = false;
    for (auto parameter : g->get_parameters())
    {
        if (auto op_annotation = parameter->get_op_annotations())
        {
681 682 683 684 685 686 687 688
            if (op_annotation->is_cacheable())
            {
                found_A = true;
            }
            else
            {
                found_B = true;
            }
689 690 691 692 693
        }
    }
    EXPECT_TRUE(found_A);
    EXPECT_TRUE(found_B);
}