builder.cpp 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2017-2018 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.
*******************************************************************************/
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 26
shared_ptr<runtime::TensorView>
    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}), op::ParameterVector{A});
32
    auto manager = runtime::Manager::get("INTERPRETER");
33 34 35 36
    auto external = manager->compile(f);
    auto backend = manager->allocate_backend();
    auto cf = backend->make_call_frame(external);
    // Create some tensors for input/output
37
    auto a = backend->make_primary_tensor_view(element::f32, shape_a);
38
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
39
    auto result = backend->make_primary_tensor_view(element::f32, shape_rt);
40 41 42 43 44
    cf->call({a}, {result});

    return result;
}

45 46
shared_ptr<runtime::TensorView> make_reduce_result_true(
    function<shared_ptr<Node>(const shared_ptr<Node>&, const AxisSet&, bool)> func)
47
{
48
    Shape shape_a{3, 2};
49
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
50
    Shape shape_rt{2};
51
    auto f = make_shared<Function>(func(A, {0}, true), op::ParameterVector{A});
52
    auto manager = runtime::Manager::get("INTERPRETER");
53 54 55 56
    auto external = manager->compile(f);
    auto backend = manager->allocate_backend();
    auto cf = backend->make_call_frame(external);
    // Create some tensors for input/output
57
    auto a = backend->make_primary_tensor_view(element::f32, shape_a);
58
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
59
    auto result = backend->make_primary_tensor_view(element::f32, shape_rt);
60 61 62 63 64
    cf->call({a}, {result});

    return result;
}

65 66
shared_ptr<runtime::TensorView> make_reduce_result_false(
    function<shared_ptr<Node>(const shared_ptr<Node>&, const AxisSet&, bool)> func)
67
{
68
    Shape shape_a{3, 2};
69
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
70
    Shape shape_rt{2};
71
    auto f = make_shared<Function>(func(A, {0}, false), op::ParameterVector{A});
72
    auto manager = runtime::Manager::get("INTERPRETER");
73 74 75 76
    auto external = manager->compile(f);
    auto backend = manager->allocate_backend();
    auto cf = backend->make_call_frame(external);
    // Create some tensors for input/output
77
    auto a = backend->make_primary_tensor_view(element::f32, shape_a);
78
    copy_data(a, vector<float>{1, 2, 3, 4, 5, 6});
79
    auto result = backend->make_primary_tensor_view(element::f32, shape_rt);
80 81 82 83 84
    cf->call({a}, {result});

    return result;
}

85
TEST(builder, l2_norm)
86 87
{
    auto result = make_reduce_result(builder::l2_norm);
88 89
    ASSERT_TRUE(test::all_close((vector<float>{5.9160797831f, 7.48331477355f}),
                                read_vector<float>(result)));
90 91
}

92
TEST(builder, mean)
93 94
{
    auto result = make_reduce_result(builder::mean);
95
    ASSERT_TRUE(test::all_close((vector<float>{3, 4}), read_vector<float>(result)));
96 97
}

98
TEST(builder, std_dev)
99 100
{
    auto result = make_reduce_result_false(builder::std_dev);
101 102
    ASSERT_TRUE(test::all_close((vector<float>{1.63299316186f, 1.63299316186f}),
                                read_vector<float>(result)));
103
    result = make_reduce_result_true(builder::std_dev);
104
    ASSERT_TRUE(test::all_close((vector<float>{2, 2}), read_vector<float>(result)));
105 106
}

107
TEST(builder, variance)
108 109
{
    auto result = make_reduce_result_false(builder::variance);
110 111
    ASSERT_TRUE(test::all_close((vector<float>{2.66666666666f, 2.66666666666f}),
                                read_vector<float>(result)));
112
    result = make_reduce_result_true(builder::variance);
113
    ASSERT_TRUE(test::all_close((vector<float>{4, 4}), read_vector<float>(result)));
114
}
115 116 117 118 119

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

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

    // Dimshuffle
    shape = Shape{2, 4, 8};
132 133 134
    param = make_shared<op::Parameter>(element::f32, shape);
    transposed =
        dynamic_pointer_cast<op::Reshape>(builder::numpy_transpose(param, AxisVector{2, 0, 1}));
135 136 137 138
    EXPECT_EQ(Shape({8, 2, 4}), transposed->get_output_shape());

    // Bad Orders
    EXPECT_ANY_THROW(
139 140 141
        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})));
142
}