tensor.cpp 3.76 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 <algorithm>
Robert Kimball's avatar
Robert Kimball committed
18
#include <memory>
19 20 21
#include <sstream>
#include <string>
#include <vector>
Robert Kimball's avatar
Robert Kimball committed
22 23

#include "gtest/gtest.h"
24
#include "ngraph/function.hpp"
25
#include "ngraph/ngraph.hpp"
26
#include "ngraph/pass/liveness.hpp"
27
#include "ngraph/pass/manager.hpp"
28
#include "util/test_tools.hpp"
Robert Kimball's avatar
Robert Kimball committed
29 30

using namespace std;
31
using namespace ngraph;
Robert Kimball's avatar
Robert Kimball committed
32

33
TEST(tensor, size)
Robert Kimball's avatar
Robert Kimball committed
34
{
35
    pass::Manager pass_manager;
36 37

    pass_manager.register_pass<pass::Liveness>();
38 39

    {
40
        auto arg0 = make_shared<op::Parameter>(element::f32, Shape{2, 3});
41
        auto add = make_shared<op::Add>(arg0, arg0);
42
        auto f0 = make_shared<Function>(add, ParameterVector{arg0});
43 44 45

        pass_manager.run_passes(f0);

46 47
        ASSERT_EQ(1, arg0->get_output_size());
        descriptor::Tensor& output = arg0->output(0).get_tensor();
48 49 50
        EXPECT_EQ(2 * 3 * 4, output.size());
    }

Robert Kimball's avatar
Robert Kimball committed
51
    {
52
        auto arg0 = make_shared<op::Parameter>(element::f32, Shape{});
53
        auto add = make_shared<op::Add>(arg0, arg0);
54
        auto f0 = make_shared<Function>(add, ParameterVector{arg0});
55 56

        pass_manager.run_passes(f0);
Robert Kimball's avatar
Robert Kimball committed
57

58 59
        ASSERT_EQ(1, arg0->get_output_size());
        descriptor::Tensor& output = arg0->output(0).get_tensor();
60 61 62 63
        EXPECT_EQ(1 * 4, output.size());
    }

    {
64
        auto arg0 = make_shared<op::Parameter>(element::f32, Shape{1});
65
        auto add = make_shared<op::Add>(arg0, arg0);
66
        auto f0 = make_shared<Function>(add, ParameterVector{arg0});
Robert Kimball's avatar
Robert Kimball committed
67

68
        pass_manager.run_passes(f0);
Robert Kimball's avatar
Robert Kimball committed
69

70 71
        ASSERT_EQ(1, arg0->get_output_size());
        descriptor::Tensor& output = arg0->output(0).get_tensor();
72 73
        EXPECT_EQ(1 * 4, output.size());
    }
Robert Kimball's avatar
Robert Kimball committed
74
}
75

76
template <typename T>
77
void test_read_write(const vector<T>& x)
78
{
79
    auto backend = runtime::Backend::create("INTERPRETER");
80

81
    auto a = backend->create_tensor(element::from<T>(), Shape{2, x.size()});
82

83
    vector<T> result(2 * x.size());
84 85

    a->write(&x[0], 0, x.size() * sizeof(T));
86
    copy(x.begin(), x.end(), result.begin());
87
    a->write(&x[0], x.size() * sizeof(T), x.size() * sizeof(T));
88
    copy(x.begin(), x.end(), result.begin() + x.size());
89

90
    vector<T> af_vector(2 * x.size());
91
    a->read(af_vector.data(), 0, af_vector.size() * sizeof(T));
92 93
    ASSERT_EQ(af_vector, result);

94 95 96
    vector<T> result1(x.size());
    vector<T> result2(x.size());
    copy(result.begin() + 1, result.begin() + 1 + x.size(), result1.begin());
97 98 99 100
    a->read(&result2[0], sizeof(T), sizeof(T) * x.size());
    ASSERT_EQ(result1, result2);
}

101
#if defined(NGRAPH_INTERPRETER_ENABLE)
102 103
TEST(tensor, read_write)
{
104 105
    test_read_write<float>({1.0, 3.0, 5.0});
    test_read_write<int64_t>({-1, 2, 4});
106
}
107
#endif
108 109 110 111 112 113

TEST(tensor, output_flag)
{
    pass::Manager pass_manager;
    pass_manager.register_pass<pass::Liveness>();

114
    auto arg0 = make_shared<op::Parameter>(element::f32, Shape{1});
115
    auto add = make_shared<op::Add>(arg0, arg0);
116
    auto f0 = make_shared<Function>(add, ParameterVector{arg0});
117 118 119

    pass_manager.run_passes(f0);

120
    for (size_t i = 0; i < f0->get_output_size(); ++i)
121
    {
122
        EXPECT_TRUE(f0->get_output_op(i)->is_output());
123 124
    }
}