one_hot.in.cpp 8.24 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2020 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
//
// 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.
//*****************************************************************************

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdlib>
#include <random>
#include <string>

#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "util/all_close.hpp"
#include "util/all_close_f.hpp"
#include "util/ndarray.hpp"
#include "util/test_control.hpp"
#include "util/test_tools.hpp"

using namespace std;
using namespace ngraph;

static string s_manifest = "${MANIFEST}";

NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_2_in_3)
{
    Shape shape_a{};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{3};
    auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
43
    auto f = make_shared<Function>(r, ParameterVector{A});
44 45 46 47 48 49 50 51

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{2});
    auto result = backend->create_tensor(element::i32, shape_r);

52
    auto handle = backend->compile(f);
53
    handle->call_with_validate({result}, {a});
54 55 56 57 58 59 60 61 62
    EXPECT_EQ((vector<int32_t>{0, 0, 1}), read_vector<int32_t>(result));
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_1_in_3)
{
    Shape shape_a{};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{3};
    auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
63
    auto f = make_shared<Function>(r, ParameterVector{A});
64 65 66 67 68 69 70 71

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{1});
    auto result = backend->create_tensor(element::i32, shape_r);

72
    auto handle = backend->compile(f);
73
    handle->call_with_validate({result}, {a});
74 75 76 77 78 79 80 81 82
    EXPECT_EQ((vector<int32_t>{0, 1, 0}), read_vector<int32_t>(result));
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_0_in_3)
{
    Shape shape_a{};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{3};
    auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
83
    auto f = make_shared<Function>(r, ParameterVector{A});
84 85 86 87 88 89 90 91

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{0});
    auto result = backend->create_tensor(element::i32, shape_r);

92
    auto handle = backend->compile(f);
93
    handle->call_with_validate({result}, {a});
94 95 96 97 98 99 100 101 102
    EXPECT_EQ((vector<int32_t>{1, 0, 0}), read_vector<int32_t>(result));
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_0)
{
    Shape shape_a{8};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{3, 8};
    auto r = make_shared<op::OneHot>(A, Shape{3, 8}, 0);
103
    auto f = make_shared<Function>(r, ParameterVector{A});
104 105 106 107 108 109 110 111

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{2, 1, 0, 0, 2, 2, 1, 0});
    auto result = backend->create_tensor(element::i32, shape_r);

112
    auto handle = backend->compile(f);
113
    handle->call_with_validate({result}, {a});
114 115 116 117 118 119 120 121 122 123 124
    EXPECT_EQ(
        (vector<int32_t>{0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}),
        read_vector<int32_t>(result));
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1)
{
    Shape shape_a{8};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{8, 3};
    auto r = make_shared<op::OneHot>(A, Shape{8, 3}, 1);
125
    auto f = make_shared<Function>(r, ParameterVector{A});
126 127 128 129 130 131 132 133

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{2, 1, 0, 0, 2, 2, 1, 0});
    auto result = backend->create_tensor(element::i32, shape_r);

134
    auto handle = backend->compile(f);
135
    handle->call_with_validate({result}, {a});
136 137 138 139 140 141 142 143 144 145 146
    EXPECT_EQ(
        (vector<int32_t>{0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0}),
        read_vector<int32_t>(result));
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1_barely_oob)
{
    Shape shape_a{8};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{8, 3};
    auto r = make_shared<op::OneHot>(A, Shape{8, 3}, 1);
147
    auto f = make_shared<Function>(r, ParameterVector{A});
148 149 150 151 152 153 154 155

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a, vector<int32_t>{2, 1, 0, 0, 3, 2, 1, 0});
    auto result = backend->create_tensor(element::i32, shape_r);

156 157 158
    auto handle = backend->compile(f);
    handle->call_with_validate({result}, {a});
    vector<int32_t> rv = read_vector<int32_t>(result);
159

160 161 162
    EXPECT_EQ(rv[0], 0);
    EXPECT_EQ(rv[1], 0);
    EXPECT_EQ(rv[2], 1);
163

164 165 166
    EXPECT_EQ(rv[3], 0);
    EXPECT_EQ(rv[4], 1);
    EXPECT_EQ(rv[5], 0);
167

168 169 170
    EXPECT_EQ(rv[6], 1);
    EXPECT_EQ(rv[7], 0);
    EXPECT_EQ(rv[8], 0);
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    EXPECT_EQ(rv[9], 1);
    EXPECT_EQ(rv[10], 0);
    EXPECT_EQ(rv[11], 0);

    // These are undefined since value is out of bounds
    // EXPECT_EQ(rv[12], 0);
    // EXPECT_EQ(rv[13], 0);
    // EXPECT_EQ(rv[14], 0);

    EXPECT_EQ(rv[15], 0);
    EXPECT_EQ(rv[16], 0);
    EXPECT_EQ(rv[17], 1);

    EXPECT_EQ(rv[18], 0);
    EXPECT_EQ(rv[19], 1);
    EXPECT_EQ(rv[20], 0);

    EXPECT_EQ(rv[21], 1);
    EXPECT_EQ(rv[22], 0);
    EXPECT_EQ(rv[23], 0);
192 193 194 195 196 197 198 199
}

NGRAPH_TEST(${BACKEND_NAME}, one_hot_matrix_0)
{
    Shape shape_a{3, 3};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{3, 3, 3};
    auto r = make_shared<op::OneHot>(A, Shape{3, 3, 3}, 0);
200
    auto f = make_shared<Function>(r, ParameterVector{A});
201 202 203 204 205 206 207 208 209 210 211

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    copy_data(a,
              vector<int32_t>{
                  0, 1, 1, 2, 1, 0, 0, 2, 1,
              });
    auto result = backend->create_tensor(element::i32, shape_r);

212
    auto handle = backend->compile(f);
213
    handle->call_with_validate({result}, {a});
214 215 216 217 218 219 220 221
    EXPECT_EQ((vector<int32_t>{1, 0, 0, 0, 0, 1, 1, 0, 0,

                               0, 1, 1, 0, 1, 0, 0, 0, 1,

                               0, 0, 0, 1, 0, 0, 0, 1, 0}),
              read_vector<int32_t>(result));
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_many_categories)
{
    // Imagenet has roughly 20,000 categories
    uint32_t category_count = 20000;
    Shape shape_a{6};
    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    Shape shape_r{6, category_count};
    auto r = make_shared<op::OneHot>(A, Shape{6, category_count}, 1);
    auto f = make_shared<Function>(r, ParameterVector{A});

    auto backend = runtime::Backend::create("${BACKEND_NAME}");

    // Create some tensors for input/output
    auto a = backend->create_tensor(element::i32, shape_a);
    vector<int32_t> input_data{0, 11, 101, 1001, 10001, static_cast<int32_t>(category_count - 1)};
    copy_data(a, input_data);
    auto result = backend->create_tensor(element::i32, shape_r);

    auto handle = backend->compile(f);
    handle->call_with_validate({result}, {a});
    vector<int32_t> data = read_vector<int32_t>(result);

    vector<int32_t> bit_positions;
    for (size_t i = 0; i < shape_size(shape_r); ++i)
    {
        if (data[i] == 1)
        {
            bit_positions.push_back(i % category_count);
        }
    }
    EXPECT_EQ(bit_positions, input_data);
}