copy.cpp 11.7 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 20 21 22

#include <memory>
#include <string>

#include "gtest/gtest.h"

#include "ngraph/ngraph.hpp"
23
#include "util/ndarray.hpp"
24
#include "util/test_tools.hpp"
25 26 27 28 29 30 31 32

using namespace std;
using namespace ngraph;

template <typename OP>
bool check_unary()
{
    Shape shape{1};
33
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
34
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape)};
35 36 37 38

    auto node = make_shared<OP>(arg0);
    auto new_node = node->copy_with_new_args(new_args);

39
    return (nullptr != new_node) && (new_args == new_node->get_arguments());
40 41 42 43 44 45
}

template <typename OP>
bool check_binary()
{
    Shape shape{1};
46 47
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
    auto arg1 = make_shared<op::Parameter>(element::f32, shape);
48 49
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, shape)};
50 51 52 53

    auto node = make_shared<OP>(arg0, arg1);
    auto new_node = node->copy_with_new_args(new_args);

54
    return (nullptr != new_node) && (new_args == new_node->get_arguments());
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

TEST(copy, abs)
{
    ASSERT_TRUE(check_unary<op::Abs>());
}

TEST(copy, acos)
{
    ASSERT_TRUE(check_unary<op::Acos>());
}

TEST(copy, add)
{
    ASSERT_TRUE(check_binary<op::Add>());
}

TEST(copy, asin)
{
    ASSERT_TRUE(check_unary<op::Asin>());
}

TEST(copy, atan)
{
    ASSERT_TRUE(check_unary<op::Atan>());
}

TEST(copy, broadcast)
{
    Shape shape1{1};
85
    auto arg0 = make_shared<op::Parameter>(element::f32, shape1);
86
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape1)};
87 88 89 90 91 92 93

    Shape shape{4, 1, 3};
    AxisSet axes{0, 2};

    auto node = make_shared<op::Broadcast>(arg0, shape, axes);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Broadcast>(new_node);
94
    ASSERT_NE(node_cast, nullptr);
95 96

    ASSERT_TRUE(nullptr != new_node);
97
    ASSERT_TRUE(new_args == new_node->get_arguments());
98 99 100 101 102 103 104 105 106 107 108 109
    ASSERT_TRUE(shape == node_cast->get_broadcast_shape());
    ASSERT_TRUE(axes == node_cast->get_broadcast_axes());
}

TEST(copy, ceiling)
{
    ASSERT_TRUE(check_unary<op::Ceiling>());
}

TEST(copy, concat)
{
    Shape shape{1};
110 111
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
    auto arg1 = make_shared<op::Parameter>(element::f32, shape);
112 113
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, shape)};
114
    size_t axis = 0;
115
    auto node = make_shared<op::Concat>(NodeVector{arg0, arg1}, axis);
116 117
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Concat>(new_node);
118
    ASSERT_NE(node_cast, nullptr);
119 120

    ASSERT_TRUE(nullptr != new_node);
121
    ASSERT_TRUE(new_args == new_node->get_arguments());
122 123 124 125 126 127
    ASSERT_TRUE(node_cast->get_concatenation_axis() == axis);
}

TEST(copy, constant)
{
    Shape shape{};
128
    vector<float> c{2.4f};
129
    auto& et = element::f32;
130
    auto node = op::Constant::create(et, shape, c);
131
    auto new_node = node->copy_with_new_args(NodeVector{});
132
    auto node_cast = dynamic_pointer_cast<op::Constant>(new_node);
133
    ASSERT_NE(node_cast, nullptr);
134
    ASSERT_TRUE(nullptr != new_node);
135
    ASSERT_TRUE(NodeVector{} == new_node->get_arguments());
136
    ASSERT_TRUE(node_cast->get_vector<float>() == c);
137 138 139 140 141 142 143
    ASSERT_TRUE(node_cast->get_shape() == shape);
    ASSERT_TRUE(node_cast->get_element_type() == et);
}

TEST(copy, convert)
{
    Shape shape;
144 145
    auto& et = element::f64;
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
146
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape)};
147 148 149 150

    auto node = make_shared<op::Convert>(arg0, et);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Convert>(new_node);
151
    ASSERT_NE(node_cast, nullptr);
152 153

    ASSERT_TRUE(nullptr != new_node);
154
    ASSERT_TRUE(new_args == new_node->get_arguments());
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    ASSERT_TRUE(et == node_cast->get_convert_element_type());
}

TEST(copy, cos)
{
    ASSERT_TRUE(check_unary<op::Cos>());
}

TEST(copy, cosh)
{
    ASSERT_TRUE(check_unary<op::Cosh>());
}

TEST(copy, divide)
{
    ASSERT_TRUE(check_binary<op::Divide>());
}

TEST(copy, dot)
{
    ASSERT_TRUE(check_binary<op::Dot>());
}

TEST(copy, equal)
{
    ASSERT_TRUE(check_binary<op::Equal>());
}

TEST(copy, exp)
{
    ASSERT_TRUE(check_unary<op::Exp>());
}

TEST(copy, floor)
{
    ASSERT_TRUE(check_unary<op::Floor>());
}

TEST(copy, FunctionCall)
{
    Shape shape{1};
196 197 198
    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);
199
    auto f = make_shared<Function>((A + B) * C, op::ParameterVector{A, B, C});
200

201 202 203
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
    auto arg1 = make_shared<op::Parameter>(element::f32, shape);
    auto arg2 = make_shared<op::Parameter>(element::f32, shape);
204
    auto node = make_shared<op::FunctionCall>(f, NodeVector{arg0, arg1, arg2});
205

206 207 208
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, shape)};
209 210
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::FunctionCall>(new_node);
211
    ASSERT_NE(node_cast, nullptr);
212 213

    ASSERT_TRUE(nullptr != new_node);
214
    EXPECT_TRUE(new_args == new_node->get_arguments());
215 216
    ASSERT_EQ(node_cast->get_functions().size(), 1);
    EXPECT_NE(f, node_cast->get_functions()[0]);
217 218 219 220 221 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

TEST(copy, greater_eq)
{
    ASSERT_TRUE(check_binary<op::GreaterEq>());
}

TEST(copy, greater)
{
    ASSERT_TRUE(check_binary<op::Greater>());
}

TEST(copy, less_eq)
{
    ASSERT_TRUE(check_binary<op::LessEq>());
}

TEST(copy, less)
{
    ASSERT_TRUE(check_binary<op::Less>());
}

TEST(copy, log)
{
    ASSERT_TRUE(check_unary<op::Log>());
}

TEST(copy, maximum)
{
    ASSERT_TRUE(check_binary<op::Maximum>());
}

TEST(copy, minimum)
{
    ASSERT_TRUE(check_binary<op::Minimum>());
}

TEST(copy, multiply)
{
    ASSERT_TRUE(check_binary<op::Multiply>());
}

TEST(copy, negative)
{
    ASSERT_TRUE(check_unary<op::Negative>());
}

TEST(copy, not_equal)
{
    ASSERT_TRUE(check_binary<op::NotEqual>());
}

TEST(copy, parameter)
{
    Shape shape{1};
272
    auto node = make_shared<op::Parameter>(element::f32, shape);
273 274
    auto new_node = node->copy_with_new_args({});
    auto node_cast = dynamic_pointer_cast<op::Parameter>(new_node);
275
    ASSERT_NE(node_cast, nullptr);
276 277

    ASSERT_TRUE(nullptr != new_node);
278
    ASSERT_TRUE(new_node->get_arguments().size() == 0);
279
    ASSERT_TRUE(node->has_same_type(new_node));
280 281 282 283 284 285 286 287 288 289
}

TEST(copy, power)
{
    ASSERT_TRUE(check_binary<op::Power>());
}

TEST(copy, reduce)
{
    Shape scalar_shape{};
290 291
    auto A = make_shared<op::Parameter>(element::f32, scalar_shape);
    auto B = make_shared<op::Parameter>(element::f32, scalar_shape);
292
    auto f = make_shared<Function>(A + B, op::ParameterVector{A, B});
293 294 295

    Shape shape{4, 3};
    AxisSet axes{1};
296 297
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
    auto arg_init = make_shared<op::Parameter>(element::f32, scalar_shape);
298 299
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, scalar_shape)};
300 301 302 303

    auto node = make_shared<op::Reduce>(arg0, arg_init, f, axes);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Reduce>(new_node);
304
    ASSERT_NE(node_cast, nullptr);
305 306

    ASSERT_TRUE(nullptr != new_node);
307
    EXPECT_TRUE(new_args == new_node->get_arguments());
308 309 310
    ASSERT_EQ(node_cast->get_functions().size(), 1);
    EXPECT_NE(f, node_cast->get_functions()[0]);
    EXPECT_TRUE(axes == node_cast->get_reduction_axes());
311 312 313 314 315 316 317 318 319 320 321 322 323
}

TEST(copy, remainder)
{
    ASSERT_TRUE(check_binary<op::Remainder>());
}

TEST(copy, reshape)
{
    Shape shape_in{2, 3, 4};
    AxisVector axes{0, 1, 2};
    Shape shape_out{6, 4};

324
    auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
325
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
326 327 328 329

    auto node = make_shared<op::Reshape>(arg0, axes, shape_out);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Reshape>(new_node);
330
    ASSERT_NE(node_cast, nullptr);
331 332

    ASSERT_TRUE(nullptr != new_node);
333
    ASSERT_TRUE(new_args == new_node->get_arguments());
334 335 336 337 338 339 340
    ASSERT_TRUE(axes == node_cast->get_input_order());
    ASSERT_TRUE(shape_out == node_cast->get_output_shape());
}

TEST(copy, select)
{
    Shape shape{1};
341 342 343
    auto arg0 = make_shared<op::Parameter>(element::boolean, shape);
    auto arg1 = make_shared<op::Parameter>(element::f32, shape);
    auto arg2 = make_shared<op::Parameter>(element::f32, shape);
344 345 346
    NodeVector new_args{make_shared<op::Parameter>(element::boolean, shape),
                        make_shared<op::Parameter>(element::f32, shape),
                        make_shared<op::Parameter>(element::f32, shape)};
347 348 349 350

    auto node = make_shared<op::Select>(arg0, arg1, arg2);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Select>(new_node);
351
    ASSERT_NE(node_cast, nullptr);
352 353

    ASSERT_TRUE(nullptr != new_node);
354
    ASSERT_TRUE(new_args == new_node->get_arguments());
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
}

TEST(copy, sign)
{
    ASSERT_TRUE(check_unary<op::Sign>());
}

TEST(copy, sin)
{
    ASSERT_TRUE(check_unary<op::Sin>());
}

TEST(copy, sinh)
{
    ASSERT_TRUE(check_unary<op::Sinh>());
}

TEST(copy, slice)
{
    Shape shape_in{2, 3, 4};
    Coordinate lower{0, 0, 0};
    Coordinate upper{2, 3, 4};
377
    Strides strides{1, 1, 1};
378

379
    auto arg0 = make_shared<op::Parameter>(element::f32, shape_in);
380
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape_in)};
381

382
    auto node = make_shared<op::Slice>(arg0, lower, upper, strides);
383 384
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Slice>(new_node);
385
    ASSERT_NE(node_cast, nullptr);
386 387

    ASSERT_TRUE(nullptr != new_node);
388
    ASSERT_TRUE(new_args == new_node->get_arguments());
389 390
    ASSERT_TRUE(lower == node_cast->get_lower_bounds());
    ASSERT_TRUE(upper == node_cast->get_upper_bounds());
391
    ASSERT_TRUE(strides == node_cast->get_strides());
392 393 394 395 396 397 398 399 400 401 402
}

TEST(copy, subtract)
{
    ASSERT_TRUE(check_binary<op::Subtract>());
}

TEST(copy, sum)
{
    Shape shape{4, 3};
    AxisSet axes{1};
403
    auto arg0 = make_shared<op::Parameter>(element::f32, shape);
404
    NodeVector new_args{make_shared<op::Parameter>(element::f32, shape)};
405 406 407 408

    auto node = make_shared<op::Sum>(arg0, axes);
    auto new_node = node->copy_with_new_args(new_args);
    auto node_cast = dynamic_pointer_cast<op::Sum>(new_node);
409
    ASSERT_NE(node_cast, nullptr);
410 411

    ASSERT_TRUE(nullptr != new_node);
412
    ASSERT_TRUE(new_args == new_node->get_arguments());
413 414 415 416 417 418 419 420 421 422 423 424
    ASSERT_TRUE(axes == node_cast->get_reduction_axes());
}

TEST(copy, tan)
{
    ASSERT_TRUE(check_unary<op::Tan>());
}

TEST(copy, tanh)
{
    ASSERT_TRUE(check_unary<op::Tanh>());
}