builder.cpp 6.05 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 18 19
#include "gtest/gtest.h"

#include "ngraph/ngraph.hpp"
#include "util/all_close.hpp"
20
#include "util/test_tools.hpp"
21 22 23 24

using namespace ngraph;
using namespace std;

25
shared_ptr<runtime::Tensor>
26
    make_reduce_result(function<shared_ptr<Node>(const shared_ptr<Node>&, const AxisSet&)> func)
27
{
28
    Shape shape_a{3, 2};
29
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
30
    Shape shape_rt{2};
31
    auto f = make_shared<Function>(func(A, {0}), ParameterVector{A});
32
    auto backend = runtime::Backend::create("INTERPRETER");
33
    // Create some tensors for input/output
34
    auto a = backend->create_tensor(element::f32, shape_a);
35
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
36
    auto result = backend->create_tensor(element::f32, shape_rt);
37
    auto handle = backend->compile(f);
38
    handle->call_with_validate({result}, {a});
39 40 41 42

    return result;
}

43
shared_ptr<runtime::Tensor> make_reduce_result_true(
44
    function<shared_ptr<Node>(const shared_ptr<Node>&, const AxisSet&, bool)> func)
45
{
46
    Shape shape_a{3, 2};
47
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
48
    Shape shape_rt{2};
49
    auto f = make_shared<Function>(func(A, {0}, true), ParameterVector{A});
50
    auto backend = runtime::Backend::create("INTERPRETER");
51
    // Create some tensors for input/output
52
    auto a = backend->create_tensor(element::f32, shape_a);
53
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
54
    auto result = backend->create_tensor(element::f32, shape_rt);
55
    auto handle = backend->compile(f);
56
    handle->call_with_validate({result}, {a});
57 58 59 60

    return result;
}

61
shared_ptr<runtime::Tensor> make_reduce_result_false(
62
    function<shared_ptr<Node>(const shared_ptr<Node>&, const AxisSet&, bool)> func)
63
{
64
    Shape shape_a{3, 2};
65
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
66
    Shape shape_rt{2};
67
    auto f = make_shared<Function>(func(A, {0}, false), ParameterVector{A});
68
    auto backend = runtime::Backend::create("INTERPRETER");
69
    // Create some tensors for input/output
70
    auto a = backend->create_tensor(element::f32, shape_a);
71
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
72
    auto result = backend->create_tensor(element::f32, shape_rt);
73
    auto handle = backend->compile(f);
74
    handle->call_with_validate({result}, {a});
75 76 77 78

    return result;
}

79
TEST(builder, l2_norm)
80 81
{
    auto result = make_reduce_result(builder::l2_norm);
82 83
    ASSERT_TRUE(test::all_close((vector<float>{5.9160797831f, 7.48331477355f}),
                                read_vector<float>(result)));
84 85
}

86
TEST(builder, mean)
87 88
{
    auto result = make_reduce_result(builder::mean);
89
    ASSERT_TRUE(test::all_close((vector<float>{3, 4}), read_vector<float>(result)));
90 91
}

92
TEST(builder, std_dev)
93 94
{
    auto result = make_reduce_result_false(builder::std_dev);
95 96
    ASSERT_TRUE(test::all_close((vector<float>{1.63299316186f, 1.63299316186f}),
                                read_vector<float>(result)));
97
    result = make_reduce_result_true(builder::std_dev);
98
    ASSERT_TRUE(test::all_close((vector<float>{2, 2}), read_vector<float>(result)));
99 100
}

101
TEST(builder, variance)
102 103
{
    auto result = make_reduce_result_false(builder::variance);
104 105
    ASSERT_TRUE(test::all_close((vector<float>{2.66666666666f, 2.66666666666f}),
                                read_vector<float>(result)));
106
    result = make_reduce_result_true(builder::variance);
107
    ASSERT_TRUE(test::all_close((vector<float>{4, 4}), read_vector<float>(result)));
108
}
109 110 111 112 113

TEST(builder, numpy_transpose)
{
    // 2D Transpose
    Shape shape{2, 4};
114 115
    auto param = make_shared<op::Parameter>(element::f32, shape);
    auto transposed = dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param));
116 117 118 119
    EXPECT_EQ(Shape({4, 2}), transposed->get_output_shape());

    // Multidimensional Transpose
    shape = Shape{2, 4, 8};
120 121
    param = make_shared<op::Parameter>(element::f32, shape);
    transposed = dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param));
122 123 124 125
    EXPECT_EQ(Shape({8, 4, 2}), transposed->get_output_shape());

    // Dimshuffle
    shape = Shape{2, 4, 8};
126 127 128
    param = make_shared<op::Parameter>(element::f32, shape);
    transposed =
        dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param, AxisVector{2, 0, 1}));
129 130 131 132
    EXPECT_EQ(Shape({8, 2, 4}), transposed->get_output_shape());

    // Bad Orders
    EXPECT_ANY_THROW(
133 134 135
        dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param, AxisVector{2})));
    EXPECT_ANY_THROW(
        dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param, AxisVector{2, 2, 1})));
136
}
137 138 139 140 141 142 143

TEST(builder, tensor_mask)
{
    Shape max_sequence_length{3};
    auto sequence_lengths = make_shared<op::Parameter>(element::u32, max_sequence_length);

    Shape mask_shape{3, 5};
144 145
    auto f =
        make_shared<Function>(builder::tensor_mask<op::Less>(sequence_lengths, 1, 0, mask_shape, 0),
146
                              ParameterVector{sequence_lengths});
147

148
    auto backend = runtime::Backend::create("INTERPRETER");
149

150
    auto sequence_lengths_data = backend->create_tensor(element::u32, max_sequence_length);
151
    copy_data(sequence_lengths_data, vector<uint32_t>{1, 3, 2});
152
    auto result = backend->create_tensor(element::boolean, mask_shape);
153

154
    auto handle = backend->compile(f);
155
    handle->call_with_validate({result}, {sequence_lengths_data});
156 157 158 159
    vector<char> expected{1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0};

    EXPECT_EQ(expected, read_vector<char>(result));
}