test_ops.py 44.6 KB
Newer Older
1
# ******************************************************************************
2
# Copyright 2018-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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, Executable
26 27 28 29 30 31 32
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
33
from ngraph.impl.op import OneHot, Broadcast, Reshape, Convert
34 35 36
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 131
    handle = backend.compile(function)
    handle.call([result], [a, b])
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    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')
149
    backend = Backend.create(pytest.config.getoption('backend'))
150

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

    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)
160 161
    handle = backend.compile(function)
    handle.call([result], [a, b])
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 246 247
    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')
248
    backend = Backend.create(pytest.config.getoption('backend'))
249

250 251 252 253
    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)
254 255 256 257 258 259 260

    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)
261 262
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
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 353 354 355
    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
356
    shape = Shape(np.array(input_list).shape)
357 358 359 360
    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')
361
    backend = Backend.create(pytest.config.getoption('backend'))
362

363 364
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
365 366 367 368 369

    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)
370 371
    handle = backend.compile(function)
    handle.call([result], [a])
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 491 492 493 494
    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')
495
    backend = Backend.create(pytest.config.getoption('backend'))
496

497 498
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
499 500 501 502 503

    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)
504 505
    handle = backend.compile(function)
    handle.call([result], [a])
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    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')
521
    backend = Backend.create(pytest.config.getoption('backend'))
522

523 524
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1]))
525 526 527 528 529

    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)
530 531 532
    handle = backend.compile(function)
    handle.get_performance_data()
    handle.call([result], [a])
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    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')
548
    backend = Backend.create(pytest.config.getoption('backend'))
549

550 551
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([3, 2]))
552 553 554 555 556

    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)
557 558
    handle = backend.compile(function)
    handle.call([result], [a])
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    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')
575
    backend = Backend.create(pytest.config.getoption('backend'))
576

577 578
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
579 580 581 582 583

    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)
584 585
    handle = backend.compile(function)
    handle.call([result], [a])
586 587 588 589 590 591 592 593
    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')
594
    backend = Backend.create(pytest.config.getoption('backend'))
595

596
    result = backend.create_tensor(Type.i32, shape)
597 598 599 600 601

    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)
602 603
    handle = backend.compile(function)
    handle.call([result], [a])
604 605 606 607 608 609 610 611 612 613 614 615 616 617
    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')
618
    backend = Backend.create(pytest.config.getoption('backend'))
619

620 621
    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))
622 623 624 625 626

    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)
627 628
    handle = backend.compile(function)
    handle.call([result], [a])
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    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')
644
    backend = Backend.create(pytest.config.getoption('backend'))
645

646
    result = backend.create_tensor(element_type, Shape([3, 3]))
647 648 649

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
650 651
    handle = backend.compile(function)
    handle.call([result], [])
652 653 654 655 656 657 658 659 660 661 662 663 664
    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)


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')
665
    backend = Backend.create(pytest.config.getoption('backend'))
666

667 668
    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))
669 670 671 672 673

    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)
674 675
    handle = backend.compile(function)
    handle.call([result], [a])
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    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')
694
    backend = Backend.create(pytest.config.getoption('backend'))
695

696 697 698 699
    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]))
700 701 702 703 704 705 706

    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)
707 708
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
709 710 711 712 713 714 715 716 717 718
    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)


719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
@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)


736 737 738 739 740 741 742 743 744 745
@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')
746
    backend = Backend.create(pytest.config.getoption('backend'))
747

748 749 750 751
    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]))
752 753 754 755 756 757 758

    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)
759 760
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    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]

780
    function = Function(NodeVector([Slice(A, Coordinate(lower_bounds),
781
                                   Coordinate(upper_bounds))]), parameter_list, 'test')
782
    backend = Backend.create(pytest.config.getoption('backend'))
783

784 785
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([4, 4]))
786 787 788 789 790

    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)
791 792
    handle = backend.compile(function)
    handle.call([result], [a])
793 794 795 796 797 798 799 800 801 802 803 804
    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')
805
    backend = Backend.create(pytest.config.getoption('backend'))
806

807
    result = backend.create_tensor(element_type, Shape([4, 2]))
808 809 810
    result_arr = np.zeros(8, dtype=np.float32).reshape(4, 2)

    result.write(util.numpy_to_c(result_arr), 0, 8*4)
811 812
    handle = backend.compile(function)
    handle.call([result], [a])
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    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')
835
    backend = Backend.create(pytest.config.getoption('backend'))
836

837 838 839
    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]))
840 841 842 843 844 845

    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)
846 847
    handle = backend.compile(function)
    handle.call([result], [a, b])
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    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')
863
    backend = Backend.create(pytest.config.getoption('backend'))
864

865 866
    handle = backend.compile(function)
    handle.call([result], [a, b])
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    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')
888
    backend = Backend.create(pytest.config.getoption('backend'))
889

890 891
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8]))
892 893 894 895 896

    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)
897 898
    handle = backend.compile(function)
    handle.call([result], [a])
899 900 901 902 903 904 905 906 907
    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')
908
    backend = Backend.create(pytest.config.getoption('backend'))
909 910

    size = 4
911
    result = backend.create_tensor(element_type, Shape([1, 1, size]))
912 913 914
    result_arr = np.zeros(size, dtype=np.float32).reshape(1, 1, size)

    result.write(util.numpy_to_c(result_arr), 0, size*4)
915 916
    handle = backend.compile(function)
    handle.call([result], [a])
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
    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')
932
    backend = Backend.create(pytest.config.getoption('backend'))
933

934 935
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8, 8]))
936 937 938 939 940

    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)
941 942
    handle = backend.compile(function)
    handle.call([result], [a])
943 944 945 946 947 948 949 950 951
    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')
952
    backend = Backend.create(pytest.config.getoption('backend'))
953 954

    size = 4
955
    result = backend.create_tensor(element_type, Shape([1, 1, size, size]))
956 957 958
    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)
959 960
    handle = backend.compile(function)
    handle.call([result], [a])
961 962 963 964 965 966 967 968 969 970 971 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
    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')
1030
    backend = Backend.create(pytest.config.getoption('backend'))
1031

1032 1033
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1034 1035 1036 1037

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

1038
    result = backend.create_tensor(element_type, Shape([1, 1, 14, 14]))
1039
    result.write(util.numpy_to_c(result_arr), 0, 14*14*4)
1040 1041
    handle = backend.compile(function)
    handle.call([result], [a, b])
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    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')
1064
    backend = Backend.create(pytest.config.getoption('backend'))
1065

1066 1067
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1068 1069 1070 1071 1072

    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)
1073
    result = backend.create_tensor(element_type, Shape([1, 1, 4, 4]))
1074
    result.write(util.numpy_to_c(result_arr), 0, 4*4*4)
1075 1076
    handle = backend.compile(function)
    handle.call([result], [a, b])
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

    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')
1099
    backend = Backend.create(pytest.config.getoption('backend'))
1100

1101 1102
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1103 1104 1105 1106 1107

    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)
1108
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1109
    result.write(util.numpy_to_c(result_arr), 0, 6*6*4)
1110 1111
    handle = backend.compile(function)
    handle.call([result], [a, b])
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

    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]

1137
    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation),
1138 1139
                        CoordinateDiff(padding_below), CoordinateDiff(padding_above))]),
                        parameter_list, 'test')
1140
    backend = Backend.create(pytest.config.getoption('backend'))
1141

1142 1143
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1144 1145 1146 1147 1148

    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)
1149
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1150
    result.write(util.numpy_to_c(result_arr), 0, 6*6*4)
1151 1152
    handle = backend.compile(function)
    handle.call([result], [a, b])
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

    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]

1176
    function = Function(NodeVector([Convolution(A, B, Strides(strides), Strides(dilation),
1177 1178
                        CoordinateDiff(padding_below), CoordinateDiff(padding_above))]),
                        parameter_list, 'test')
1179
    backend = Backend.create(pytest.config.getoption('backend'))
1180

1181 1182
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1183 1184 1185 1186 1187

    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)
1188
    result = backend.create_tensor(element_type, Shape([1, 1, 9, 9]))
1189
    result.write(util.numpy_to_c(result_arr), 0, 9*9*4)
1190 1191
    handle = backend.compile(function)
    handle.call([result], [a, b])
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

    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')
1221
    backend = Backend.create(pytest.config.getoption('backend'))
1222

1223 1224
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1225 1226 1227 1228 1229

    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)
1230
    result = backend.create_tensor(element_type, Shape([1, 1, 17, 17]))
1231
    result.write(util.numpy_to_c(result_arr), 0, 17*17*4)
1232 1233
    handle = backend.compile(function)
    handle.call([result], [a, b])
1234 1235 1236 1237 1238 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

    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')
1269
    backend = Backend.create(pytest.config.getoption('backend'))
1270

1271 1272
    a = backend.create_tensor(element_type, filter_shape)
    b = backend.create_tensor(element_type, output_shape)
1273 1274 1275 1276 1277

    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)
1278
    result = backend.create_tensor(element_type, Shape([1, 1, 10, 10]))
1279
    result.write(util.numpy_to_c(result_arr), 0, 10*10*4)
1280 1281
    handle = backend.compile(function)
    handle.call([result], [a, b])
1282 1283 1284 1285 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

    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')
1325
    backend = Backend.create(pytest.config.getoption('backend'))
1326

1327 1328
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, output_shape)
1329 1330 1331 1332 1333

    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)
1334
    result = backend.create_tensor(element_type, Shape([1, 1, 3, 3]))
1335
    result.write(util.numpy_to_c(result_arr), 0, 3*3*4)
1336 1337
    handle = backend.compile(function)
    handle.call([result], [a, b])
1338 1339 1340 1341 1342 1343 1344

    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)