util.cpp 20.7 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.
//*****************************************************************************
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 29
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/visualize_tree.hpp"
30
#include "ngraph/serializer.hpp"
31
#include "util/all_close.hpp"
32
#include "util/autodiff/backprop_function.hpp"
33
#include "util/ndarray.hpp"
Robert Kimball's avatar
Robert Kimball committed
34 35

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

TEST(util, split)
{
    {
        string s1 = "this,is,a,test";
42
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
43 44 45 46 47 48 49 50 51
        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,";
52
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
53 54 55 56 57 58 59 60 61 62
        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";
63
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
64 65 66 67 68 69 70 71 72 73
        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";
74
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
75 76 77 78 79 80 81 82 83 84
        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";
85
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
86 87 88 89 90 91
        ASSERT_EQ(1, r1.size());
        EXPECT_STRCASEEQ("this", r1[0].c_str());
    }

    {
        string s1 = "";
92
        auto r1 = split(s1, ',');
Robert Kimball's avatar
Robert Kimball committed
93 94 95 96 97 98 99 100 101 102 103 104
        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());
}

105 106 107 108
#ifdef _WIN32
#include "windows.h"
#define usleep(a) Sleep(a / 1000)
#endif
Robert Kimball's avatar
Robert Kimball committed
109 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
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());
}

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

    // Create some tensors for input/output
149 150
    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
151

152 153
    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());
154

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

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

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

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

TEST(util, traverse_functions)
{
    // First create "f(A,B,C) = (A+B)*C".
171
    Shape shape{2, 2};
172 173 174
    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);
175
    auto f = make_shared<Function>((A + B) * C, ParameterVector{A, B, C}, "f");
Robert Kimball's avatar
Robert Kimball committed
176 177

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

class CloneTest : public ::testing::Test
{
public:
    // (A + B) * C
    Shape shape = Shape{2, 2};
187 188 189
    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);
190 191 192 193 194 195
    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 =
196
        make_shared<Function>(AplusBtimesC, ParameterVector{A, B, C}, "f");
197 198 199 200 201 202 203 204 205 206

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

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

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

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

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

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

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

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

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

265 266
TEST(graph_util, clone_multiple_results)
{
267
    Shape shape{2, 2};
268 269 270 271 272 273
    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);

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

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

279 280 281 282 283 284 285 286 287
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));
}
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

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"));
303
    EXPECT_TRUE(std::isnan(parse_string<double>("NaN")));
304
}
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

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

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

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;

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

    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);
}
364 365 366 367 368 369 370 371 372

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;
373
    auto result = make_shared<op::Result>(mul);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    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;
391
    auto result = make_shared<op::Result>(mul);
392 393 394 395
    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);
}
396

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 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
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);
    EXPECT_EQ(true, m.is_set(Type::d));
    EXPECT_EQ(false, m.is_set(Type::a));
    EXPECT_EQ(true, m.is_set(Type::b));
    EXPECT_EQ(false, m.is_set(Type::c));
    EXPECT_EQ(false, m.is_set({Type::a, Type::b}));
    EXPECT_EQ(false, m.is_set({Type::c, Type::d}));
    EXPECT_EQ(false, m.is_set({Type::a, Type::c}));
    EXPECT_EQ(true, m.is_set({Type::b, Type::d}));
    EXPECT_EQ(false, m.is_clear(Type::d));
    EXPECT_EQ(true, m.is_clear(Type::a));
    EXPECT_EQ(false, m.is_clear(Type::b));
    EXPECT_EQ(true, m.is_clear(Type::c));
    EXPECT_EQ(false, m.is_clear({Type::c, Type::d}));
    EXPECT_EQ(false, m.is_clear({Type::a, Type::b}));
    EXPECT_EQ(true, m.is_clear({Type::a, Type::c}));
    EXPECT_EQ(false, m.is_clear({Type::b, Type::d}));

    EXPECT_EQ(true, m.is_any_set({Type::a, Type::b}));
    EXPECT_EQ(true, m.is_any_set({Type::a, Type::d}));
    EXPECT_EQ(true, m.is_any_set({Type::b, Type::c}));
    EXPECT_EQ(true, m.is_any_set({Type::c, Type::d}));
    EXPECT_EQ(false, m.is_any_set({Type::a, Type::c}));
    EXPECT_EQ(true, m.is_any_clear({Type::c, Type::d}));
    EXPECT_EQ(true, m.is_any_clear({Type::a, Type::b}));
    EXPECT_EQ(true, m.is_any_clear({Type::a, Type::c}));
    EXPECT_EQ(true, m.is_any_clear({Type::b, Type::c}));
    EXPECT_EQ(false, m.is_any_clear({Type::b, Type::d}));

    m.set(Type::a);
    EXPECT_EQ(false, m.is_clear(Type::a));
    EXPECT_EQ(false, m.is_clear(Type::b));
    EXPECT_EQ(true, m.is_clear(Type::c));
    EXPECT_EQ(false, m.is_clear(Type::d));
}

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());
    EXPECT_EQ(true, m[Type::b]);
    EXPECT_EQ(false, m[Type::a]);
    EXPECT_EQ(false, m[Type::c]);
    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;
    EXPECT_EQ(true, m.is_set(Type::a));
    EXPECT_EQ(false, m.is_set(Type::b));
    EXPECT_EQ(true, m.is_set(Type::c));
    EXPECT_EQ(false, m.is_set(Type::d));
    EXPECT_EQ(true, m.is_any_set(Type::a));
    EXPECT_EQ(false, m.is_any_set(Type::b));
    EXPECT_EQ(true, m.is_any_set(Type::c));
    EXPECT_EQ(false, m.is_any_set(Type::d));
    EXPECT_EQ(true, m.is_any_set({Type::a, Type::c}));
    EXPECT_EQ(false, m.is_any_set({Type::b, Type::d}));

    EnumMask<Type> n;
    n = m | n;
    EXPECT_EQ(m, n);
    n = m & n;
    EXPECT_EQ(m, n);
    bool r = (n == m);
    EXPECT_EQ(true, r);
    r = (n != m);
    EXPECT_EQ(false, r);
    n.clear_all();
    n = {Type::a, Type::b};
    r = (n == m);
    EXPECT_EQ(false, r);
    r = (n != m);
    EXPECT_EQ(true, r);
    n = m & n;
    EXPECT_EQ(static_cast<uint32_t>(Type::a), n.value());
    n = m | Type::b;
    EXPECT_EQ(true, n.is_set(Type::a));
    EXPECT_EQ(true, n.is_set(Type::b));
    EXPECT_EQ(true, n.is_set(Type::c));
    EXPECT_EQ(false, n.is_set(Type::d));
    EXPECT_EQ(false, n[Type::d]);
    EXPECT_EQ(true, n[Type::b]);
}
540

Scott Cyphers's avatar
Scott Cyphers committed
541 542 543 544 545 546 547 548 549 550
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);
        }
551 552 553
        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
554 555
    }

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

562 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
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);
}
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

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