test_ops.py 44.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# ******************************************************************************
# Copyright 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.
# ******************************************************************************
# flake8: noqa
17 18
from __future__ import absolute_import

19 20 21
import pytest
import numpy as np

22 23 24
from ngraph.impl import util
from ngraph.impl import Shape, Strides, CoordinateDiff, AxisSet, AxisVector, Coordinate
from ngraph.impl import Type, Function, NodeVector
25
from ngraph.impl.runtime import Backend
26 27 28 29 30 31 32 33 34 35 36
from ngraph.impl.op import Acos, Asin, Atan, Cos, Sin, Tan
from ngraph.impl.op import Cosh, Sinh, Tanh, Sqrt, Sign
from ngraph.impl.op import Power, Negative, Ceiling, Floor
from ngraph.impl.op import Parameter, Maximum, Minimum
from ngraph.impl.op import Add, Subtract, Multiply, Divide, Dot
from ngraph.impl.op import Constant, Abs, Exp, Log, Sum
from ngraph.impl.op import Greater, Less, Equal, NotEqual, GreaterEq, LessEq, Not
from ngraph.impl.op import OneHot, Broadcast, Reshape, Convert, Reduce
from ngraph.impl.op import Concat, Select
from ngraph.impl.op import Reverse, MaxPool, ReplaceSlice, Slice
from ngraph.impl.op import Convolution, ConvolutionBackpropData, ConvolutionBackpropFilters
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118


def binary_op(op_str, a, b):

    if op_str == '+':
        return a + b
    elif op_str == 'Add':
        return Add(a, b)
    elif op_str == '-':
        return a - b
    elif op_str == 'Sub':
        return Subtract(a, b)
    elif op_str == '*':
        return a * b
    elif op_str == 'Mul':
        return Multiply(a, b)
    elif op_str == '/':
        return a / b
    elif op_str == 'Div':
        return Divide(a, b)
    elif op_str == 'Dot':
        return Dot(a, b)
    elif op_str == 'Equal':
        return Equal(a, b)
    elif op_str == 'Greater':
        return Greater(a, b)
    elif op_str == 'GreaterEq':
        return GreaterEq(a, b)
    elif op_str == 'Less':
        return Less(a, b)
    elif op_str == 'LessEq':
        return LessEq(a, b)
    elif op_str == 'Maximum':
        return Maximum(a, b)
    elif op_str == 'Minimum':
        return Minimum(a, b)
    elif op_str == 'NotEqual':
        return NotEqual(a, b)
    elif op_str == 'Power':
        return Power(a, b)


def binary_op_ref(op_str, a, b):

    if op_str == '+' or op_str == 'Add':
        return a + b
    elif op_str == '-' or op_str == 'Sub':
        return a - b
    elif op_str == '*' or op_str == 'Mul':
        return a * b
    elif op_str == '/' or op_str == 'Div':
        return a / b
    elif op_str == 'Dot':
        return np.dot(a, b)
    elif op_str == 'Equal':
        return np.equal(a, b)
    elif op_str == 'Greater':
        return np.greater(a, b)
    elif op_str == 'GreaterEq':
        return np.greater_equal(a, b)
    elif op_str == 'Less':
        return np.less(a, b)
    elif op_str == 'LessEq':
        return np.less_equal(a, b)
    elif op_str == 'Maximum':
        return np.maximum(a, b)
    elif op_str == 'Minimum':
        return np.minimum(a, b)
    elif op_str == 'NotEqual':
        return np.not_equal(a, b)
    elif op_str == 'Power':
        return np.power(a, b)


def binary_op_exec(op_str):

    element_type = Type.f32
    shape = Shape([2, 2])
    A = Parameter(element_type, shape)
    B = Parameter(element_type, shape)
    parameter_list = [A, B]
    function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test')
119
    backend = Backend.create(pytest.config.getoption('backend'))
120

121 122 123
    a = backend.create_tensor(element_type, shape)
    b = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
124 125 126 127 128 129

    a.write(util.numpy_to_c(np.array([[1, 6], [7, 4]], dtype=np.float32)), 0, 16)
    b.write(util.numpy_to_c(np.array([[5, 2], [3, 8]], dtype=np.float32)), 0, 16)

    result_arr = np.array([[0, 0], [0, 0]], dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 16)
130
    backend.call(function, [result], [a, b])
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    result.read(util.numpy_to_c(result_arr), 0, 16)

    a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32)
    b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32)
    result_arr_ref = binary_op_ref(op_str, a_arr, b_arr)

    assert np.allclose(result_arr, result_arr_ref)


def binary_op_comparison(op_str):

    element_type = Type.f32
    shape = Shape([2, 2])
    A = Parameter(element_type, shape)
    B = Parameter(element_type, shape)
    parameter_list = [A, B]
    function = Function(NodeVector([binary_op(op_str, A, B)]), parameter_list, 'test')
148
    backend = Backend.create(pytest.config.getoption('backend'))
149

150 151 152
    a = backend.create_tensor(element_type, shape)
    b = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
153 154 155 156 157 158

    a.write(util.numpy_to_c(np.array([[1, 5], [3, 2]], dtype=np.float32)), 0, 16)
    b.write(util.numpy_to_c(np.array([[2, 4], [3, 1]], dtype=np.float32)), 0, 16)

    result_arr = np.array([[False, False], [False, False]], dtype=np.bool)
    result.write(util.numpy_to_c(result_arr), 0, 4)
159
    backend.call(function, [result], [a, b])
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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
    result.read(util.numpy_to_c(result_arr), 0, 4)

    a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32)
    b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32)
    result_arr_ref = binary_op_ref(op_str, a_arr, b_arr)

    assert np.allclose(result_arr, result_arr_ref)


def test_add():
    binary_op_exec('+')


def test_add_op():
    binary_op_exec('Add')


def test_sub():
    binary_op_exec('-')


def test_sub_op():
    binary_op_exec('Sub')


def test_mul():
    binary_op_exec('*')


def test_mul_op():
    binary_op_exec('Mul')


def test_div():
    binary_op_exec('/')


def test_div_op():
    binary_op_exec('Div')


def test_dot():
    binary_op_exec('Dot')


def test_maximum():
    binary_op_exec('Maximum')


def test_minimum():
    binary_op_exec('Minimum')


def test_power():
    binary_op_exec('Power')


def test_greater():
    binary_op_comparison('Greater')


def test_greater_eq():
    binary_op_comparison('GreaterEq')


def test_less():
    binary_op_comparison('Less')


def test_less_eq():
    binary_op_comparison('LessEq')


def test_not_equal():
    binary_op_comparison('NotEqual')


def test_add_with_mul():

    element_type = Type.f32
    shape = Shape([2, 2])
    A = Parameter(element_type, shape)
    B = Parameter(element_type, shape)
    C = Parameter(element_type, shape)
    parameter_list = [A, B, C]
    function = Function(NodeVector([Multiply(Add(A, B), C)]), parameter_list, 'test')
246
    backend = Backend.create(pytest.config.getoption('backend'))
247

248 249 250 251
    a = backend.create_tensor(element_type, shape)
    b = backend.create_tensor(element_type, shape)
    c = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
252 253 254 255 256 257 258

    a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 0, 16)
    b.write(util.numpy_to_c(np.array([5, 6, 7, 8], dtype=np.float32)), 0, 16)
    c.write(util.numpy_to_c(np.array([9, 10, 11, 12], dtype=np.float32)), 0, 16)

    result_arr = np.array([0, 0, 0, 0], dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 16)
259
    backend.call(function, [result], [a, b, c])
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    result.read(util.numpy_to_c(result_arr), 0, 16)

    a_arr = np.array([1, 2, 3, 4], dtype=np.float32)
    b_arr = np.array([5, 6, 7, 8], dtype=np.float32)
    c_arr = np.array([9, 10, 11, 12], dtype=np.float32)
    result_arr_ref = (a_arr + b_arr) * c_arr

    assert np.allclose(result_arr, result_arr_ref)


def unary_op(op_str, a):
    if op_str == 'Abs':
        return Abs(a)
    elif op_str == 'Acos':
        return Acos(a)
    elif op_str == 'Asin':
        return Asin(a)
    elif op_str == 'Atan':
        return Atan(a)
    elif op_str == 'Ceiling':
        return Ceiling(a)
    elif op_str == 'Cos':
        return Cos(a)
    elif op_str == 'Cosh':
        return Cosh(a)
    elif op_str == 'Floor':
        return Floor(a)
    elif op_str == 'log':
        return Log(a)
    elif op_str == 'exp':
        return Exp(a)
    elif op_str == 'negative':
        return Negative(a)
    elif op_str == 'Reverse':
        return Reverse(a, AxisSet({1}))
    elif op_str == 'Sign':
        return Sign(a)
    elif op_str == 'Sin':
        return Sin(a)
    elif op_str == 'Sinh':
        return Sinh(a)
    elif op_str == 'Sqrt':
        return Sqrt(a)
    elif op_str == 'Tan':
        return Tan(a)
    elif op_str == 'Tanh':
        return Tanh(a)


def unary_op_ref(op_str, a):
    if op_str == 'Abs':
        return np.abs(a)
    elif op_str == 'Acos':
        return np.arccos(a)
    elif op_str == 'Asin':
        return np.arcsin(a)
    elif op_str == 'Atan':
        return np.arctan(a)
    elif op_str == 'Ceiling':
        return np.ceil(a)
    elif op_str == 'Cos':
        return np.cos(a)
    elif op_str == 'Cosh':
        return np.cosh(a)
    elif op_str == 'Floor':
        return np.floor(a)
    elif op_str == 'log':
        return np.log(a)
    elif op_str == 'exp':
        return np.exp(a)
    elif op_str == 'negative':
        return np.negative(a)
    elif op_str == 'Reverse':
        return np.fliplr(a)
    elif op_str == 'Sign':
        return np.sign(a)
    elif op_str == 'Sin':
        return np.sin(a)
    elif op_str == 'Sinh':
        return np.sinh(a)
    elif op_str == 'Sqrt':
        return np.sqrt(a)
    elif op_str == 'Tan':
        return np.tan(a)
    elif op_str == 'Tanh':
        return np.tanh(a)


def unary_op_exec(op_str, input_list):
    """
    input_list needs to have deep length of 4
    """
    element_type = Type.f32
353
    shape = Shape(np.array(input_list).shape)
354 355 356 357
    shape_np = np.array(input_list).shape
    A = Parameter(element_type, shape)
    parameter_list = [A]
    function = Function(NodeVector([unary_op(op_str, A)]), parameter_list, 'test')
358
    backend = Backend.create(pytest.config.getoption('backend'))
359

360 361
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
362 363 364 365 366

    a.write(util.numpy_to_c(np.array(input_list, dtype=np.float32)), 0, 16)

    result_arr = np.zeros(shape_np, dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 16)
367
    backend.call(function, [result], [a])
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    result.read(util.numpy_to_c(result_arr), 0, 16)

    a_arr = np.array(input_list, dtype=np.float32)
    result_arr_ref = unary_op_ref(op_str, a_arr)

    assert np.allclose(result_arr, result_arr_ref)


def test_abs():
    input_list = [-1, 0, 1, 2]
    op_str = 'Abs'
    unary_op_exec(op_str, input_list)


def test_acos():
    input_list = [-1, 0, 0.5, 1]
    op_str = 'Acos'
    unary_op_exec(op_str, input_list)


def test_asin():
    input_list = [-1, 0, 0.5, 1]
    op_str = 'Asin'
    unary_op_exec(op_str, input_list)


def test_atan():
    input_list = [-1, 0, 0.5, 1]
    op_str = 'Atan'
    unary_op_exec(op_str, input_list)


def test_ceiling():
    input_list = [0.5, 0, 0.4, 0.5]
    op_str = 'Ceiling'
    unary_op_exec(op_str, input_list)


def test_cos():
    input_list = [0, 0.7, 1.7, 3.4]
    op_str = 'Cos'
    unary_op_exec(op_str, input_list)


def test_cosh():
    input_list = [-1, 0., 0.5, 1]
    op_str = 'Cosh'
    unary_op_exec(op_str, input_list)


def test_floor():
    input_list = [-0.5, 0, 0.4, 0.5]
    op_str = 'Floor'
    unary_op_exec(op_str, input_list)


def test_log():
    input_list = [1, 2, 3, 4]
    op_str = 'log'
    unary_op_exec(op_str, input_list)


def test_exp():
    input_list = [-1, 0, 1, 2]
    op_str = 'exp'
    unary_op_exec(op_str, input_list)


def test_negative():
    input_list = [-1, 0, 1, 2]
    op_str = 'negative'
    unary_op_exec(op_str, input_list)


def test_sign():
    input_list = [-1, 0, 0.5, 1]
    op_str = 'Sign'
    unary_op_exec(op_str, input_list)


def test_sin():
    input_list = [0, 0.7, 1.7, 3.4]
    op_str = 'Sin'
    unary_op_exec(op_str, input_list)


def test_sinh():
    input_list = [-1, 0., 0.5, 1]
    op_str = 'Sinh'
    unary_op_exec(op_str, input_list)


def test_sqrt():
    input_list = [0., 0.5, 1, 2]
    op_str = 'Sqrt'
    unary_op_exec(op_str, input_list)


def test_tan():
    input_list = [-np.pi / 4, 0, np.pi / 8, np.pi / 8]
    op_str = 'Tan'
    unary_op_exec(op_str, input_list)


def test_tanh():
    input_list = [-1, 0, 0.5, 1]
    op_str = 'Tanh'
    unary_op_exec(op_str, input_list)


@pytest.config.gpu_skip(reason="Not implemented")
def test_reverse():
    input_list = [[-1, 0], [0.5, 1]]
    op_str = 'Reverse'
    unary_op_exec(op_str, input_list)


def test_not():
    element_type = Type.boolean
    shape = Shape([2])
    A = Parameter(element_type, shape)
    parameter_list = [A]
    function = Function(NodeVector([Not(A)]), parameter_list, 'test')
491
    backend = Backend.create(pytest.config.getoption('backend'))
492

493 494
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
495 496 497 498 499

    a.write(util.numpy_to_c(np.array([True, False], dtype=np.bool)), 0, 2)

    result_arr = np.array([False, False], dtype=np.bool)
    result.write(util.numpy_to_c(result_arr), 0, 2)
500
    backend.call(function, [result], [a])
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    result.read(util.numpy_to_c(result_arr), 0, 2)

    a_arr = np.array([True, False], dtype=np.bool)
    result_arr_ref = np.logical_not(a_arr)

    assert np.allclose(result_arr, result_arr_ref)


def test_sum():

    element_type = Type.f32
    shape = Shape([1, 4])
    A = Parameter(element_type, shape)
    parameter_list = [A]
    function = Function(NodeVector([Sum(A, AxisSet({1}))]), parameter_list, 'test')
516
    backend = Backend.create(pytest.config.getoption('backend'))
517

518 519
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1]))
520 521 522 523 524

    a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 0, 16)

    result_arr = np.array([0], dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 4)
525
    backend.call(function, [result], [a])
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    result.read(util.numpy_to_c(result_arr), 0, 4)

    a_arr = np.array([1, 2, 3, 4], dtype=np.float32)
    result_arr_ref = np.sum(a_arr)

    assert np.allclose(result_arr[0], result_arr_ref)


def test_reshape():

    element_type = Type.f32
    shape = Shape([2, 3])
    A = Parameter(element_type, shape)
    parameter_list = [A]
    function = Function(NodeVector([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))]), parameter_list, 'test')
541
    backend = Backend.create(pytest.config.getoption('backend'))
542

543 544
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([3, 2]))
545 546 547 548 549

    a.write(util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 0, 24)

    result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 24)
550
    backend.call(function, [result], [a])
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    result.read(util.numpy_to_c(result_arr), 0, 24)

    a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
    result_arr_ref = np.reshape(a_arr, (3, 2))

    assert np.allclose(result_arr, result_arr_ref)


def test_convert():

    element_type = Type.f32
    shape = Shape([1, 3])
    A = Parameter(element_type, shape)
    parameter_list = [A]
    # f32 to boolean
    function = Function(NodeVector([Convert(A, Type.boolean)]), parameter_list, 'test')
567
    backend = Backend.create(pytest.config.getoption('backend'))
568

569 570
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
571 572 573 574 575

    a.write(util.numpy_to_c(np.array([1, 5, 3], dtype=np.float32)), 0, 12)

    result_arr = np.array([False, False, False], dtype=np.bool)
    result.write(util.numpy_to_c(result_arr), 0, 3)
576
    backend.call(function, [result], [a])
577 578 579 580 581 582 583 584
    result.read(util.numpy_to_c(result_arr), 0, 3)

    a_arr = np.array([1, 5, 3], dtype=np.float32)
    result_arr_ref = a_arr.astype(bool)
    assert np.allclose(result_arr, result_arr_ref)

    # f32 to i32
    function = Function(NodeVector([Convert(A, Type.i32)]), parameter_list, 'test')
585
    backend = Backend.create(pytest.config.getoption('backend'))
586

587
    result = backend.create_tensor(Type.i32, shape)
588 589 590 591 592

    a.write(util.numpy_to_c(np.array([1.4, 5.5, 3.9], dtype=np.float32)), 0, 12)

    result_arr = np.array([0, 0, 0], dtype=np.int32)
    result.write(util.numpy_to_c(result_arr), 0, 12)
593
    backend.call(function, [result], [a])
594 595 596 597 598 599 600 601 602 603 604 605 606 607
    result.read(util.numpy_to_c(result_arr), 0, 12)

    a_arr = np.array([1.4, 5.4, 3.9], dtype=np.float32)
    result_arr_ref = a_arr.astype(int)

    assert np.allclose(result_arr, result_arr_ref)


def test_broadcast():

    element_type = Type.f32
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
    function = Function(NodeVector([Broadcast(A, Shape([3, 3]), AxisSet({0}))]), parameter_list, 'test')
608
    backend = Backend.create(pytest.config.getoption('backend'))
609

610 611
    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))
612 613 614 615 616

    a.write(util.numpy_to_c(np.array([1, 2, 3], dtype=np.float32)), 0, 12)

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
617
    backend.call(function, [result], [a])
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    result.read(util.numpy_to_c(result_arr), 0, 36)

    a_arr = np.array([[0], [0], [0]], dtype=np.float32)
    b_arr = np.array([[1, 2, 3]], dtype=np.float32)
    result_arr_ref = np.add(a_arr, b_arr)

    assert np.allclose(result_arr, result_arr_ref)


def test_constant():

    element_type = Type.f32
    parameter_list = []
    function = Function(NodeVector([Constant(element_type, Shape([3, 3]), list(range(9)))]),
                        parameter_list, 'test')
633
    backend = Backend.create(pytest.config.getoption('backend'))
634

635
    result = backend.create_tensor(element_type, Shape([3, 3]))
636 637 638

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
639
    backend.call(function, [result], [])
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    result.read(util.numpy_to_c(result_arr), 0, 36)

    result_arr_ref = np.arange(9).reshape(3, 3)

    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_reduce():

    float_element_type = Type.f32

    AddParam1 = Parameter(float_element_type, Shape([]))
    AddParam2 = Parameter(float_element_type, Shape([]))
    constant_op = Constant(float_element_type, Shape([]), [0.])
    reduce_function = Function(NodeVector([Add(AddParam1, AddParam2)]),
                               [AddParam1, AddParam2], 'add')

    A = Parameter(float_element_type, Shape([2, 2, 2]))
    parameter_list = [A]

    function = Function(NodeVector([Reduce(A, constant_op, reduce_function, AxisSet({0}))]),
                        parameter_list, 'test')
663
    backend = Backend.create(pytest.config.getoption('backend'))
664

665 666
    a = backend.create_tensor(float_element_type, Shape([2, 2, 2]))
    result = backend.create_tensor(float_element_type, Shape([2, 2]))
667 668 669 670 671

    a.write(util.numpy_to_c(np.arange(8, dtype=np.float32).reshape(2, 2, 2)), 0, 32)

    result_arr = np.zeros((2, 2), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 16)
672
    backend.call(function, [result], [a])
673 674 675 676 677 678 679 680 681 682 683 684 685 686
    result.read(util.numpy_to_c(result_arr), 0, 16)

    a_arr = np.arange(8).reshape(2, 2, 2)
    result_arr_ref = np.add.reduce(a_arr)

    assert np.allclose(result_arr, result_arr_ref)


def test_onehot():

    element_type = Type.f32
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
    function = Function(NodeVector([OneHot(A, Shape([3, 3]), 0)]), parameter_list, 'test')
687
    backend = Backend.create(pytest.config.getoption('backend'))
688

689 690
    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))
691 692 693 694 695

    a.write(util.numpy_to_c(np.array([1, 0, 2], dtype=np.float32)), 0, 12)

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
696
    backend.call(function, [result], [a])
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    result.read(util.numpy_to_c(result_arr), 0, 36)

    a_arr = np.array([1, 0, 2])
    result_arr_ref = np.eye(3)[a_arr]

    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_concat():

    element_type = Type.f32
    A = Parameter(element_type, Shape([1, 2]))
    B = Parameter(element_type, Shape([1, 2]))
    C = Parameter(element_type, Shape([1, 2]))
    parameter_list = [A, B, C]
    axis = 0
    function = Function(NodeVector([Concat(NodeVector([A, B, C]), axis)]), parameter_list, 'test')
715
    backend = Backend.create(pytest.config.getoption('backend'))
716

717 718 719 720
    a = backend.create_tensor(element_type, Shape([1, 2]))
    b = backend.create_tensor(element_type, Shape([1, 2]))
    c = backend.create_tensor(element_type, Shape([1, 2]))
    result = backend.create_tensor(element_type, Shape([3, 2]))
721 722 723 724 725 726 727

    a.write(util.numpy_to_c(np.array([1, 2], dtype=np.float32)), 0, 8)
    b.write(util.numpy_to_c(np.array([5, 6], dtype=np.float32)), 0, 8)
    c.write(util.numpy_to_c(np.array([7, 8], dtype=np.float32)), 0, 8)

    result_arr = np.zeros(6, dtype=np.float32).reshape(3, 2)
    result.write(util.numpy_to_c(result_arr), 0, 24)
728
    backend.call(function, [result], [a, b, c])
729 730 731 732 733 734 735 736 737 738
    result.read(util.numpy_to_c(result_arr), 0, 24)

    a_arr = np.array([[1, 2]], dtype=np.float32)
    b_arr = np.array([[5, 6]], dtype=np.float32)
    c_arr = np.array([[7, 8]], dtype=np.float32)
    result_arr_ref = np.concatenate((a_arr, b_arr, c_arr), axis)

    assert np.allclose(result_arr, result_arr_ref)


739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
@pytest.config.gpu_skip(reason="Not implemented")
def test_axisset():

    set_axisset = AxisSet({1, 2, 3})
    list_axisset = AxisSet([1, 2, 3])
    tuple_axisset = AxisSet((1, 2, 3))

    assert len(set_axisset) == 3
    assert set(set_axisset) == {1, 2, 3}

    assert len(list_axisset) == 3
    assert set(list_axisset) == set(set_axisset)

    assert len(tuple_axisset) == 3
    assert set(tuple_axisset) == set(set_axisset)


756 757 758 759 760 761 762 763 764 765
@pytest.config.gpu_skip(reason="Not implemented")
def test_select():

    element_type = Type.f32
    A = Parameter(Type.boolean, Shape([1, 2]))
    B = Parameter(element_type, Shape([1, 2]))
    C = Parameter(element_type, Shape([1, 2]))
    parameter_list = [A, B, C]

    function = Function(NodeVector([Select(A, B, C)]), parameter_list, 'test')
766
    backend = Backend.create(pytest.config.getoption('backend'))
767

768 769 770 771
    a = backend.create_tensor(Type.boolean, Shape([1, 2]))
    b = backend.create_tensor(element_type, Shape([1, 2]))
    c = backend.create_tensor(element_type, Shape([1, 2]))
    result = backend.create_tensor(element_type, Shape([1, 2]))
772 773 774 775 776 777 778

    a.write(util.numpy_to_c(np.array([[True, False]], dtype=np.bool)), 0, 2)
    b.write(util.numpy_to_c(np.array([[5, 6]], dtype=np.float32)), 0, 8)
    c.write(util.numpy_to_c(np.array([[7, 8]], dtype=np.float32)), 0, 8)

    result_arr = np.array([[0, 0]], dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 8)
779
    backend.call(function, [result], [a, b, c])
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    result.read(util.numpy_to_c(result_arr), 0, 8)

    result_arr_ref = np.array([[5, 8]])

    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_slice():

    element_type = Type.f32
    shape = Shape([6, 6])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(36, dtype=np.float32).reshape(6, 6)
    lower_bounds = [1, 1]
    upper_bounds = [5, 5]

799
    function = Function(NodeVector([Slice(A, Coordinate(lower_bounds),
800
                                   Coordinate(upper_bounds))]), parameter_list, 'test')
801
    backend = Backend.create(pytest.config.getoption('backend'))
802

803 804
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([4, 4]))
805 806 807 808 809

    a.write(util.numpy_to_c(input_arr), 0, 36*4)

    result_arr = np.zeros(16, dtype=np.float32).reshape(4, 4)
    result.write(util.numpy_to_c(result_arr), 0, 16*4)
810
    backend.call(function, [result], [a])
811 812 813 814 815 816 817 818 819 820 821 822
    result.read(util.numpy_to_c(result_arr), 0, 64)

    result_arr_ref = input_arr[lower_bounds[0]:upper_bounds[0], lower_bounds[1]:upper_bounds[1]]

    assert np.allclose(result_arr, result_arr_ref)


    #test with strides
    strides = [1, 2]

    function = Function(NodeVector([Slice(A, Coordinate(lower_bounds), Coordinate(upper_bounds),
                        Strides(strides))]), parameter_list, 'test')
823
    backend = Backend.create(pytest.config.getoption('backend'))
824

825
    result = backend.create_tensor(element_type, Shape([4, 2]))
826 827 828
    result_arr = np.zeros(8, dtype=np.float32).reshape(4, 2)

    result.write(util.numpy_to_c(result_arr), 0, 8*4)
829
    backend.call(function, [result], [a])
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    result.read(util.numpy_to_c(result_arr), 0, 32)

    result_arr_ref = result_arr_ref[::strides[0], ::strides[1]]

    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_replace_slice():

    element_type = Type.f32
    A = Parameter(element_type, Shape([6, 4]))
    B = Parameter(element_type, Shape([3, 2]))
    parameter_list = [A, B]

    input_arr_a = np.zeros(24, dtype=np.float32).reshape(6, 4)
    input_arr_b = np.ones(6, dtype=np.float32).reshape(3, 2)
    lower_bounds = [0, 1]
    upper_bounds = [3, 3]

    function = Function(NodeVector([ReplaceSlice(A, B, Coordinate(lower_bounds),
                        Coordinate(upper_bounds))]), parameter_list, 'test')
852
    backend = Backend.create(pytest.config.getoption('backend'))
853

854 855 856
    a = backend.create_tensor(element_type, Shape([6, 4]))
    b = backend.create_tensor(element_type, Shape([3, 2]))
    result = backend.create_tensor(element_type, Shape([6, 4]))
857 858 859 860 861 862

    a.write(util.numpy_to_c(input_arr_a), 0, 24*4)
    b.write(util.numpy_to_c(input_arr_b), 0, 6*4)

    result_arr = np.zeros(24, dtype=np.float32).reshape(6, 4)
    result.write(util.numpy_to_c(result_arr), 0, 24*4)
863
    backend.call(function, [result], [a, b])
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    result.read(util.numpy_to_c(result_arr), 0, 24*4)

    result_arr_ref = np.copy(input_arr_a)
    result_arr_ref[lower_bounds[0]:upper_bounds[0], lower_bounds[1]:upper_bounds[1]] = input_arr_b

    assert np.allclose(result_arr, result_arr_ref)

    #test with strides
    lower_bounds = [0, 0]
    upper_bounds = [5, 3]
    strides = [2, 2]

    function = Function(NodeVector([ReplaceSlice(A, B, Coordinate(lower_bounds),
                        Coordinate(upper_bounds), Strides(strides))]),
                        parameter_list, 'test')
879
    backend = Backend.create(pytest.config.getoption('backend'))
880

881
    backend.call(function, [result], [a, b])
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    result.read(util.numpy_to_c(result_arr), 0, 24*4)

    result_arr_ref = np.copy(input_arr_a)
    result_arr_ref[::strides[0], ::strides[1]] = input_arr_b

    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_max_pool():

    #test 1d
    element_type = Type.f32
    shape = Shape([1, 1, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(10, dtype=np.float32).reshape(1, 1, 10)
    window_shape = [3]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape))]), parameter_list, 'test')
903
    backend = Backend.create(pytest.config.getoption('backend'))
904

905 906
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8]))
907 908 909 910 911

    a.write(util.numpy_to_c(input_arr), 0, 10*4)

    result_arr = np.zeros(8, dtype=np.float32).reshape(1, 1, 8)
    result.write(util.numpy_to_c(result_arr), 0, 8*4)
912
    backend.call(function, [result], [a])
913 914 915 916 917 918 919 920 921
    result.read(util.numpy_to_c(result_arr), 0, 32)

    result_arr_ref = (np.arange(8) + 2).reshape(1, 1, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 1d with strides
    strides = [2]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape), Strides(strides))]), parameter_list, 'test')
922
    backend = Backend.create(pytest.config.getoption('backend'))
923 924

    size = 4
925
    result = backend.create_tensor(element_type, Shape([1, 1, size]))
926 927 928
    result_arr = np.zeros(size, dtype=np.float32).reshape(1, 1, size)

    result.write(util.numpy_to_c(result_arr), 0, size*4)
929
    backend.call(function, [result], [a])
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    result.read(util.numpy_to_c(result_arr), 0, size*4)

    result_arr_ref = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d
    element_type = Type.f32
    shape = Shape([1, 1, 10, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    window_shape = [3, 3]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape))]), parameter_list, 'test')
945
    backend = Backend.create(pytest.config.getoption('backend'))
946

947 948
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8, 8]))
949 950 951 952 953

    a.write(util.numpy_to_c(input_arr), 0, 10*10*4)

    result_arr = np.zeros(64, dtype=np.float32).reshape(1, 1, 8, 8)
    result.write(util.numpy_to_c(result_arr), 0, 8*8*4)
954
    backend.call(function, [result], [a])
955 956 957 958 959 960 961 962 963
    result.read(util.numpy_to_c(result_arr), 0, 8*8*4)

    result_arr_ref = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d with strides
    strides = [2, 2]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape), Strides(strides))]), parameter_list, 'test')
964
    backend = Backend.create(pytest.config.getoption('backend'))
965 966

    size = 4
967
    result = backend.create_tensor(element_type, Shape([1, 1, size, size]))
968 969 970
    result_arr = np.zeros(size*size, dtype=np.float32).reshape(1, 1, size, size)

    result.write(util.numpy_to_c(result_arr), 0, size*size*4)
971
    backend.call(function, [result], [a])
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    result.read(util.numpy_to_c(result_arr), 0, size*size*4)

    result_arr_ref = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def convolution2d(image, filterit, strides=(1, 1), dilation=(1, 1), padding_below=(0, 0),
                  padding_above=(0, 0), data_dilation=(1, 1)):

    def dilate(arr, dil=(1, 1)):
        m, n = arr.shape
        new_m, new_n = (m - 1) * dil[0] + 1, (n - 1) * dil[1] + 1
        new_arr = np.zeros(new_m * new_n, dtype=np.float32).reshape(new_m, new_n)
        for i in range(m):
            for j in range(n):
                new_arr[dil[0] * i][dil[1] * j] = arr[i][j]
        return new_arr

    i_m, i_n = image.shape
    new_image = np.zeros((i_m + padding_below[0] + padding_above[0]) * \
                         (i_n + padding_below[1] + padding_above[1]),
                         dtype=np.float32).reshape(i_m + padding_below[0] + padding_above[0],
                                                   i_n + padding_below[1] + padding_above[1])
    new_image[padding_below[0] : padding_below[0] + i_m,
              padding_below[1] : padding_below[1] + i_n] = image
    image = new_image
    image = image if data_dilation[0] == data_dilation[1] == 1 else dilate(image, data_dilation)
    i_m, i_n = image.shape

    filterit = filterit if dilation[0] == dilation[1] == 1 else dilate(filterit, dilation)
    f_m, f_n = filterit.shape

    #result_shape
    r_m = i_m - f_m + 1
    r_n = i_n - f_n + 1
    r_m //= strides[0]
    r_n //= strides[1]

    result = np.zeros(r_m * r_n, dtype=np.float32).reshape(r_m, r_n)

    for i in range(r_m):
        for j in range(r_n):
            sub_m = image[i * strides[0] : i * strides[0] + f_m,
                          j * strides[1] : j * strides[1] + f_n]
            result[i][j] = np.sum(sub_m * filterit)
    return result


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolution():

    element_type = Type.f32
    image_shape = Shape([1, 1, 16, 16])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filter_arr[0][0][0][0] = -1
    filter_arr[0][0][1][1] = -1
    filter_arr[0][0][2][2] = -1
    filter_arr[0][0][0][2] = -1
    filter_arr[0][0][2][0] = -1
    result_arr = np.zeros(196, dtype=np.float32).reshape(1, 1, 14, 14)

    function = Function(NodeVector([Convolution(A, B)]), parameter_list, 'test')
1041
    backend = Backend.create(pytest.config.getoption('backend'))
1042

1043 1044
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1045 1046 1047 1048

    a.write(util.numpy_to_c(image_arr), 0, 16*16*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

1049
    result = backend.create_tensor(element_type, Shape([1, 1, 14, 14]))
1050
    result.write(util.numpy_to_c(result_arr), 0, 14*14*4)
1051
    backend.call(function, [result], [a, b])
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    result.read(util.numpy_to_c(result_arr), 0, 14*14*4)

    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolution_with_strides():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filter_arr[0][0][1][1] = 1
    strides = [2, 2]

    function = Function(NodeVector([Convolution(A, B, Strides(strides))]), parameter_list, 'test')
1074
    backend = Backend.create(pytest.config.getoption('backend'))
1075

1076 1077
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1078 1079 1080 1081 1082

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

    result_arr = np.zeros(16, dtype=np.float32).reshape(1, 1, 4, 4)
1083
    result = backend.create_tensor(element_type, Shape([1, 1, 4, 4]))
1084
    result.write(util.numpy_to_c(result_arr), 0, 4*4*4)
1085
    backend.call(function, [result], [a, b])
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

    result.read(util.numpy_to_c(result_arr), 0, 4*4*4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolution_with_filter_dilation():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    strides = [1, 1]
    dilation = [2, 2]

    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation))]), parameter_list, 'test')
1108
    backend = Backend.create(pytest.config.getoption('backend'))
1109

1110 1111
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1112 1113 1114 1115 1116

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

    result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6)
1117
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1118
    result.write(util.numpy_to_c(result_arr), 0, 6*6*4)
1119
    backend.call(function, [result], [a, b])
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

    result.read(util.numpy_to_c(result_arr), 0, 6*6*4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation).reshape(1, 1, 6, 6)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolution_with_padding():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filter_arr[0][0][1][1] = 1
    strides = [1, 1]
    dilation = [2, 2]
    padding_below = [0, 0]
    padding_above = [0, 0]

1145
    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation),
1146 1147
                        CoordinateDiff(padding_below), CoordinateDiff(padding_above))]),
                        parameter_list, 'test')
1148
    backend = Backend.create(pytest.config.getoption('backend'))
1149

1150 1151
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1152 1153 1154 1155 1156

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

    result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6)
1157
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1158
    result.write(util.numpy_to_c(result_arr), 0, 6*6*4)
1159
    backend.call(function, [result], [a, b])
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

    result.read(util.numpy_to_c(result_arr), 0, 6*6*4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below,
                                   padding_above).reshape(1, 1, 6, 6)
    assert np.allclose(result_arr, result_arr_ref)

    # test with non-zero padding
    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1
    filter_arr[0][0][1][1] = 1
    strides = [1, 1]
    dilation = [2, 2]
    padding_below = [2, 1]
    padding_above = [1, 2]

1183
    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation),
1184 1185
                        CoordinateDiff(padding_below), CoordinateDiff(padding_above))]),
                        parameter_list, 'test')
1186
    backend = Backend.create(pytest.config.getoption('backend'))
1187

1188 1189
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1190 1191 1192 1193 1194

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

    result_arr = np.zeros(81, dtype=np.float32).reshape(1, 1, 9, 9)
1195
    result = backend.create_tensor(element_type, Shape([1, 1, 9, 9]))
1196
    result.write(util.numpy_to_c(result_arr), 0, 9*9*4)
1197
    backend.call(function, [result], [a, b])
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

    result.read(util.numpy_to_c(result_arr), 0, 9*9*4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below,
                                   padding_above).reshape(1, 1, 9, 9)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolution_with_data_dilation():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    strides = [1, 1]
    dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation),
                                    CoordinateDiff(padding_below), CoordinateDiff(padding_above),
                                    Strides(data_dilation))]), parameter_list, 'test')
1227
    backend = Backend.create(pytest.config.getoption('backend'))
1228

1229 1230
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1231 1232 1233 1234 1235

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 0, 3*3*4)

    result_arr = np.zeros(17*17, dtype=np.float32).reshape(1, 1, 17, 17)
1236
    result = backend.create_tensor(element_type, Shape([1, 1, 17, 17]))
1237
    result.write(util.numpy_to_c(result_arr), 0, 17*17*4)
1238
    backend.call(function, [result], [a, b])
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

    result.read(util.numpy_to_c(result_arr), 0, 17*17*4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below, padding_above,
                                   data_dilation).reshape(1, 1, 17, 17)
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolutionBackpropData():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    output_shape = Shape([1, 1, 17, 17])

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    window_strides = [1, 1]
    window_dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    output_arr = convolution2d(image_arr[0][0], filter_arr[0][0], window_strides,
                               window_dilation, padding_below, padding_above,
                               data_dilation).reshape(1, 1, 17, 17)

    A = Parameter(element_type, filter_shape)
    B = Parameter(element_type, output_shape)
    parameter_list = [A, B]

    function = Function(NodeVector([ConvolutionBackpropData(image_shape, A, B, Strides(window_strides), Strides(window_dilation),
                                     CoordinateDiff(padding_below), CoordinateDiff(padding_above),
                                     Strides(data_dilation))]), parameter_list, 'test')
1274
    backend = Backend.create(pytest.config.getoption('backend'))
1275

1276 1277
    a = backend.create_tensor(element_type, filter_shape)
    b = backend.create_tensor(element_type, output_shape)
1278 1279 1280 1281 1282

    a.write(util.numpy_to_c(filter_arr), 0, 3*3*4)
    b.write(util.numpy_to_c(output_arr), 0, 17*17*4)

    result_arr = np.zeros(10*10, dtype=np.float32).reshape(1, 1, 10, 10)
1283
    result = backend.create_tensor(element_type, Shape([1, 1, 10, 10]))
1284
    result.write(util.numpy_to_c(result_arr), 0, 10*10*4)
1285
    backend.call(function, [result], [a, b])
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

    result.read(util.numpy_to_c(result_arr), 0, 10*10*4)
    result_arr_ref = np.array(
        [[[[  22,   60,   70,   80,   90,  100,  110,  120,  130,   54.],
           [ 105,  275,  300,  325,  350,  375,  400,  425,  450,  185.],
           [ 205,  525,  550,  575,  600,  625,  650,  675,  700,  285.],
           [ 305,  775,  800,  825,  850,  875,  900,  925,  950,  385.],
           [ 405, 1025, 1050, 1075, 1100, 1125, 1150, 1175, 1200,  485.],
           [ 505, 1275, 1300, 1325, 1350, 1375, 1400, 1425, 1450,  585.],
           [ 605, 1525, 1550, 1575, 1600, 1625, 1650, 1675, 1700,  685.],
           [ 705, 1775, 1800, 1825, 1850, 1875, 1900, 1925, 1950,  785.],
           [ 805, 2025, 2050, 2075, 2100, 2125, 2150, 2175, 2200,  885.],
           [ 342,  860,  870,  880,  890,  900,  910,  920,  930,  374.]]]])
    assert np.allclose(result_arr, result_arr_ref)


@pytest.config.gpu_skip(reason="Not implemented")
def test_convolutionBackpropFilters():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    output_shape = Shape([1, 1, 17, 17])

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    window_strides = [1, 1]
    window_dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    output_arr = convolution2d(image_arr[0][0], filter_arr[0][0], window_strides,
                               window_dilation, padding_below, padding_above,
                               data_dilation).reshape(1, 1, 17, 17)

    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, output_shape)
    parameter_list = [A, B]

    function = Function(NodeVector([ConvolutionBackpropFilters(A, filter_shape, B, Strides(window_strides), Strides(window_dilation),
                                     CoordinateDiff(padding_below),CoordinateDiff(padding_above),
                                     Strides(data_dilation))]), parameter_list, 'test')
1329
    backend = Backend.create(pytest.config.getoption('backend'))
1330

1331 1332
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, output_shape)
1333 1334 1335 1336 1337

    a.write(util.numpy_to_c(image_arr), 0, 10*10*4)
    b.write(util.numpy_to_c(output_arr), 0, 17*17*4)

    result_arr = np.zeros(3*3, dtype=np.float32).reshape(1, 1, 3, 3)
1338
    result = backend.create_tensor(element_type, Shape([1, 1, 3, 3]))
1339
    result.write(util.numpy_to_c(result_arr), 0, 3*3*4)
1340
    backend.call(function, [result], [a, b])
1341 1342 1343 1344 1345 1346 1347

    result.read(util.numpy_to_c(result_arr), 0, 3*3*4)
    result_arr_ref = np.array(
        [[[[ 923832,  413952,  939870.],
           [ 425832,  190752,  432960.],
           [1084212,  485232, 1100250.]]]])
    assert np.allclose(result_arr, result_arr_ref)