test_ops.py 43 KB
Newer Older
1
# ******************************************************************************
2
# Copyright 2017-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
from ngraph.impl import util
from ngraph.impl import Shape, Strides, CoordinateDiff, AxisSet, AxisVector, Coordinate
24
from ngraph.impl import Type, Function
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
import test
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]
119
    function = Function([binary_op(op_str, A, B)], parameter_list, 'test')
120
    backend = Backend.create(test.BACKEND_NAME)
121

122 123 124
    a = backend.create_tensor(element_type, shape)
    b = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
125

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

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

    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]
149
    function = Function([binary_op(op_str, A, B)], parameter_list, 'test')
150
    backend = Backend.create(test.BACKEND_NAME)
151

152 153 154
    a = backend.create_tensor(element_type, shape)
    b = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(Type.boolean, shape)
155

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

    result_arr = np.array([[False, False], [False, False]], dtype=np.bool)
160
    result.write(util.numpy_to_c(result_arr), 4)
161 162
    handle = backend.compile(function)
    handle.call([result], [a, b])
163
    result.read(util.numpy_to_c(result_arr), 4)
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

    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]
248
    function = Function([Multiply(Add(A, B), C)], parameter_list, 'test')
249
    backend = Backend.create(test.BACKEND_NAME)
250

251 252 253 254
    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)
255

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

    result_arr = np.array([0, 0, 0, 0], dtype=np.float32)
261
    result.write(util.numpy_to_c(result_arr), 16)
262 263
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
264
    result.read(util.numpy_to_c(result_arr), 16)
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 356

    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
357
    shape = Shape(np.array(input_list).shape)
358 359 360
    shape_np = np.array(input_list).shape
    A = Parameter(element_type, shape)
    parameter_list = [A]
361
    function = Function([unary_op(op_str, A)], parameter_list, 'test')
362
    backend = Backend.create(test.BACKEND_NAME)
363

364 365
    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, shape)
366

367
    a.write(util.numpy_to_c(np.array(input_list, dtype=np.float32)), 16)
368 369

    result_arr = np.zeros(shape_np, dtype=np.float32)
370
    result.write(util.numpy_to_c(result_arr), 16)
371 372
    handle = backend.compile(function)
    handle.call([result], [a])
373
    result.read(util.numpy_to_c(result_arr), 16)
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

    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)


483
@pytest.mark.skip_on_gpu
484 485 486 487 488 489 490 491 492 493 494
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]
495
    function = Function([Not(A)], parameter_list, 'test')
496
    backend = Backend.create(test.BACKEND_NAME)
497

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

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

    result_arr = np.array([False, False], dtype=np.bool)
504
    result.write(util.numpy_to_c(result_arr), 2)
505 506
    handle = backend.compile(function)
    handle.call([result], [a])
507
    result.read(util.numpy_to_c(result_arr), 2)
508 509 510 511 512 513 514 515 516 517 518 519 520

    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]
521
    function = Function([Sum(A, AxisSet({1}))], parameter_list, 'test')
522
    backend = Backend.create(test.BACKEND_NAME)
523

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

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

    result_arr = np.array([0], dtype=np.float32)
530
    result.write(util.numpy_to_c(result_arr), 4)
531 532
    handle = backend.compile(function)
    handle.call([result], [a])
533
    result.read(util.numpy_to_c(result_arr), 4)
534 535 536 537 538 539 540 541 542 543 544 545 546

    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]
547
    function = Function([Reshape(A, AxisVector([0, 1]), Shape([3, 2]))], parameter_list, 'test')
548
    backend = Backend.create(test.BACKEND_NAME)
549

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

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

    result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32)
556
    result.write(util.numpy_to_c(result_arr), 24)
557 558
    handle = backend.compile(function)
    handle.call([result], [a])
559
    result.read(util.numpy_to_c(result_arr), 24)
560 561 562 563 564 565 566 567 568 569 570 571 572 573

    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
574
    function = Function([Convert(A, Type.boolean)], parameter_list, 'test')
575
    backend = Backend.create(test.BACKEND_NAME)
576

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

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

    result_arr = np.array([False, False, False], dtype=np.bool)
583
    result.write(util.numpy_to_c(result_arr), 3)
584 585
    handle = backend.compile(function)
    handle.call([result], [a])
586
    result.read(util.numpy_to_c(result_arr), 3)
587 588 589 590 591 592

    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
593
    function = Function([Convert(A, Type.i32)], parameter_list, 'test')
594
    backend = Backend.create(test.BACKEND_NAME)
595

596
    result = backend.create_tensor(Type.i32, shape)
597

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

    result_arr = np.array([0, 0, 0], dtype=np.int32)
601
    result.write(util.numpy_to_c(result_arr), 12)
602 603
    handle = backend.compile(function)
    handle.call([result], [a])
604
    result.read(util.numpy_to_c(result_arr), 12)
605 606 607 608 609 610 611 612 613 614 615 616

    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]
617
    function = Function([Broadcast(A, Shape([3, 3]), AxisSet({0}))], parameter_list, 'test')
618
    backend = Backend.create(test.BACKEND_NAME)
619

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

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

    result_arr = np.zeros((3, 3), dtype=np.float32)
626
    result.write(util.numpy_to_c(result_arr), 36)
627 628
    handle = backend.compile(function)
    handle.call([result], [a])
629
    result.read(util.numpy_to_c(result_arr), 36)
630 631 632 633 634 635 636 637 638 639 640 641

    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 = []
642
    function = Function([Constant(element_type, Shape([3, 3]), list(range(9)))],
643
                        parameter_list, 'test')
644
    backend = Backend.create(test.BACKEND_NAME)
645

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

    result_arr = np.zeros((3, 3), dtype=np.float32)
649
    result.write(util.numpy_to_c(result_arr), 36)
650 651
    handle = backend.compile(function)
    handle.call([result], [])
652
    result.read(util.numpy_to_c(result_arr), 36)
653 654 655 656 657 658 659 660

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

    assert np.allclose(result_arr, result_arr_ref)


def test_onehot():

661
    element_type = Type.i32
662 663
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
664
    function = Function([OneHot(A, Shape([3, 3]), 0)], parameter_list, 'test')
665
    backend = Backend.create(test.BACKEND_NAME)
666

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

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

672
    result_arr = np.zeros((3, 3), dtype=np.int32)
673
    result.write(util.numpy_to_c(result_arr), 36)
674 675
    handle = backend.compile(function)
    handle.call([result], [a])
676
    result.read(util.numpy_to_c(result_arr), 36)
677 678 679 680 681 682 683

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

    assert np.allclose(result_arr, result_arr_ref)


684
@pytest.mark.skip_on_gpu
685 686 687 688 689 690 691 692
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
693
    function = Function([Concat([A, B, C], axis)], parameter_list, 'test')
694
    backend = Backend.create(test.BACKEND_NAME)
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
    a.write(util.numpy_to_c(np.array([1, 2], dtype=np.float32)), 8)
    b.write(util.numpy_to_c(np.array([5, 6], dtype=np.float32)), 8)
    c.write(util.numpy_to_c(np.array([7, 8], dtype=np.float32)), 8)
704 705

    result_arr = np.zeros(6, dtype=np.float32).reshape(3, 2)
706
    result.write(util.numpy_to_c(result_arr), 24)
707 708
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
709
    result.read(util.numpy_to_c(result_arr), 24)
710 711 712 713 714 715 716 717 718

    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
@pytest.mark.skip_on_gpu
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
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
@pytest.mark.skip_on_gpu
737 738 739 740 741 742 743 744
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]

745
    function = Function([Select(A, B, C)], parameter_list, 'test')
746
    backend = Backend.create(test.BACKEND_NAME)
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
    a.write(util.numpy_to_c(np.array([[True, False]], dtype=np.bool)), 2)
    b.write(util.numpy_to_c(np.array([[5, 6]], dtype=np.float32)), 8)
    c.write(util.numpy_to_c(np.array([[7, 8]], dtype=np.float32)), 8)
756 757

    result_arr = np.array([[0, 0]], dtype=np.float32)
758
    result.write(util.numpy_to_c(result_arr), 8)
759 760
    handle = backend.compile(function)
    handle.call([result], [a, b, c])
761
    result.read(util.numpy_to_c(result_arr), 8)
762 763 764 765 766 767

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

    assert np.allclose(result_arr, result_arr_ref)


768
@pytest.mark.skip_on_gpu
769 770 771 772 773 774 775 776 777 778 779
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 781
    function = Function([Slice(A, Coordinate(lower_bounds),
                                   Coordinate(upper_bounds))], parameter_list, 'test')
782
    backend = Backend.create(test.BACKEND_NAME)
783

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

787
    a.write(util.numpy_to_c(input_arr), 36*4)
788 789

    result_arr = np.zeros(16, dtype=np.float32).reshape(4, 4)
790
    result.write(util.numpy_to_c(result_arr), 16*4)
791 792
    handle = backend.compile(function)
    handle.call([result], [a])
793
    result.read(util.numpy_to_c(result_arr), 64)
794 795 796 797 798 799 800 801 802

    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]

803 804
    function = Function([Slice(A, Coordinate(lower_bounds), Coordinate(upper_bounds),
                        Strides(strides))], parameter_list, 'test')
805
    backend = Backend.create(test.BACKEND_NAME)
806

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

810
    result.write(util.numpy_to_c(result_arr), 8*4)
811 812
    handle = backend.compile(function)
    handle.call([result], [a])
813
    result.read(util.numpy_to_c(result_arr), 32)
814 815 816 817 818 819

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

    assert np.allclose(result_arr, result_arr_ref)


820
@pytest.mark.skip_on_gpu
821
@pytest.mark.skip_on_intelgpu
822 823 824 825 826 827 828 829 830 831 832 833
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]

834 835
    function = Function([ReplaceSlice(A, B, Coordinate(lower_bounds),
                        Coordinate(upper_bounds))], parameter_list, 'test')
836
    backend = Backend.create(test.BACKEND_NAME)
837

838 839 840
    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]))
841

842 843
    a.write(util.numpy_to_c(input_arr_a), 24*4)
    b.write(util.numpy_to_c(input_arr_b), 6*4)
844 845

    result_arr = np.zeros(24, dtype=np.float32).reshape(6, 4)
846
    result.write(util.numpy_to_c(result_arr), 24*4)
847 848
    handle = backend.compile(function)
    handle.call([result], [a, b])
849
    result.read(util.numpy_to_c(result_arr), 24*4)
850 851 852 853 854 855 856 857 858 859 860

    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]

861 862
    function = Function([ReplaceSlice(A, B, Coordinate(lower_bounds),
                        Coordinate(upper_bounds), Strides(strides))],
863
                        parameter_list, 'test')
864
    backend = Backend.create(test.BACKEND_NAME)
865

866 867
    handle = backend.compile(function)
    handle.call([result], [a, b])
868
    result.read(util.numpy_to_c(result_arr), 24*4)
869 870 871 872 873 874 875

    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)


876
@pytest.mark.skip_on_gpu
877 878 879 880 881 882 883 884 885 886 887
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]

888
    function = Function([MaxPool(A, Shape(window_shape))], parameter_list, 'test')
889
    backend = Backend.create(test.BACKEND_NAME)
890

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

894
    a.write(util.numpy_to_c(input_arr), 10*4)
895 896

    result_arr = np.zeros(8, dtype=np.float32).reshape(1, 1, 8)
897
    result.write(util.numpy_to_c(result_arr), 8*4)
898 899
    handle = backend.compile(function)
    handle.call([result], [a])
900
    result.read(util.numpy_to_c(result_arr), 32)
901 902 903 904 905 906 907

    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]

908
    function = Function([MaxPool(A, Shape(window_shape), Strides(strides))], parameter_list, 'test')
909
    backend = Backend.create(test.BACKEND_NAME)
910 911

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

915
    result.write(util.numpy_to_c(result_arr), size*4)
916 917
    handle = backend.compile(function)
    handle.call([result], [a])
918
    result.read(util.numpy_to_c(result_arr), size*4)
919 920 921 922 923 924 925 926 927 928 929 930 931

    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]

932
    function = Function([MaxPool(A, Shape(window_shape))], parameter_list, 'test')
933
    backend = Backend.create(test.BACKEND_NAME)
934

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

938
    a.write(util.numpy_to_c(input_arr), 10*10*4)
939 940

    result_arr = np.zeros(64, dtype=np.float32).reshape(1, 1, 8, 8)
941
    result.write(util.numpy_to_c(result_arr), 8*8*4)
942 943
    handle = backend.compile(function)
    handle.call([result], [a])
944
    result.read(util.numpy_to_c(result_arr), 8*8*4)
945 946 947 948 949 950 951

    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]

952
    function = Function([MaxPool(A, Shape(window_shape), Strides(strides))], parameter_list, 'test')
953
    backend = Backend.create(test.BACKEND_NAME)
954 955

    size = 4
956
    result = backend.create_tensor(element_type, Shape([1, 1, size, size]))
957 958
    result_arr = np.zeros(size*size, dtype=np.float32).reshape(1, 1, size, size)

959
    result.write(util.numpy_to_c(result_arr), size*size*4)
960 961
    handle = backend.compile(function)
    handle.call([result], [a])
962
    result.read(util.numpy_to_c(result_arr), size*size*4)
963 964 965 966 967

    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)


968
@pytest.mark.skip_on_gpu
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
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


1011
@pytest.mark.skip_on_gpu
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
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)

1030
    function = Function([Convolution(A, B)], parameter_list, 'test')
1031
    backend = Backend.create(test.BACKEND_NAME)
1032

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

1036 1037
    a.write(util.numpy_to_c(image_arr), 16*16*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1038

1039
    result = backend.create_tensor(element_type, Shape([1, 1, 14, 14]))
1040
    result.write(util.numpy_to_c(result_arr), 14*14*4)
1041 1042
    handle = backend.compile(function)
    handle.call([result], [a, b])
1043
    result.read(util.numpy_to_c(result_arr), 14*14*4)
1044 1045 1046 1047 1048

    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)


1049
@pytest.mark.skip_on_gpu
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
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]

1064
    function = Function([Convolution(A, B, Strides(strides))], parameter_list, 'test')
1065
    backend = Backend.create(test.BACKEND_NAME)
1066

1067 1068
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1069

1070 1071
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1072 1073

    result_arr = np.zeros(16, dtype=np.float32).reshape(1, 1, 4, 4)
1074
    result = backend.create_tensor(element_type, Shape([1, 1, 4, 4]))
1075
    result.write(util.numpy_to_c(result_arr), 4*4*4)
1076 1077
    handle = backend.compile(function)
    handle.call([result], [a, b])
1078

1079
    result.read(util.numpy_to_c(result_arr), 4*4*4)
1080 1081 1082 1083
    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)


1084
@pytest.mark.skip_on_gpu
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
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]

1099
    function = Function([Convolution(A, B, Strides(strides), Strides(dilation))], parameter_list, 'test')
1100
    backend = Backend.create(test.BACKEND_NAME)
1101

1102 1103
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1104

1105 1106
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1107 1108

    result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6)
1109
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1110
    result.write(util.numpy_to_c(result_arr), 6*6*4)
1111 1112
    handle = backend.compile(function)
    handle.call([result], [a, b])
1113

1114
    result.read(util.numpy_to_c(result_arr), 6*6*4)
1115 1116 1117 1118 1119
    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)


1120
@pytest.mark.skip_on_gpu
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
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]

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

1143 1144
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1145

1146 1147
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1148 1149

    result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6)
1150
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
1151
    result.write(util.numpy_to_c(result_arr), 6*6*4)
1152 1153
    handle = backend.compile(function)
    handle.call([result], [a, b])
1154

1155
    result.read(util.numpy_to_c(result_arr), 6*6*4)
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
    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]

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

1182 1183
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1184

1185 1186
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1187 1188

    result_arr = np.zeros(81, dtype=np.float32).reshape(1, 1, 9, 9)
1189
    result = backend.create_tensor(element_type, Shape([1, 1, 9, 9]))
1190
    result.write(util.numpy_to_c(result_arr), 9*9*4)
1191 1192
    handle = backend.compile(function)
    handle.call([result], [a, b])
1193

1194
    result.read(util.numpy_to_c(result_arr), 9*9*4)
1195 1196 1197 1198 1199 1200
    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)


1201
@pytest.mark.skip_on_gpu
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
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]

1219
    function = Function([Convolution(A, B, Strides(strides), Strides(dilation),
1220
                                    CoordinateDiff(padding_below), CoordinateDiff(padding_above),
1221
                                    Strides(data_dilation))], parameter_list, 'test')
1222
    backend = Backend.create(test.BACKEND_NAME)
1223

1224 1225
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)
1226

1227 1228
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(filter_arr), 3*3*4)
1229 1230

    result_arr = np.zeros(17*17, dtype=np.float32).reshape(1, 1, 17, 17)
1231
    result = backend.create_tensor(element_type, Shape([1, 1, 17, 17]))
1232
    result.write(util.numpy_to_c(result_arr), 17*17*4)
1233 1234
    handle = backend.compile(function)
    handle.call([result], [a, b])
1235

1236
    result.read(util.numpy_to_c(result_arr), 17*17*4)
1237 1238 1239 1240 1241 1242
    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)


1243
@pytest.mark.skip_on_gpu
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
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]

1267
    function = Function([ConvolutionBackpropData(image_shape, A, B, Strides(window_strides), Strides(window_dilation),
1268
                                     CoordinateDiff(padding_below), CoordinateDiff(padding_above),
1269
                                     Strides(data_dilation))], parameter_list, 'test')
1270
    backend = Backend.create(test.BACKEND_NAME)
1271

1272 1273
    a = backend.create_tensor(element_type, filter_shape)
    b = backend.create_tensor(element_type, output_shape)
1274

1275 1276
    a.write(util.numpy_to_c(filter_arr), 3*3*4)
    b.write(util.numpy_to_c(output_arr), 17*17*4)
1277 1278

    result_arr = np.zeros(10*10, dtype=np.float32).reshape(1, 1, 10, 10)
1279
    result = backend.create_tensor(element_type, Shape([1, 1, 10, 10]))
1280
    result.write(util.numpy_to_c(result_arr), 10*10*4)
1281 1282
    handle = backend.compile(function)
    handle.call([result], [a, b])
1283

1284
    result.read(util.numpy_to_c(result_arr), 10*10*4)
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    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)


1299
@pytest.mark.skip_on_gpu
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
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]

1323
    function = Function([ConvolutionBackpropFilters(A, filter_shape, B, Strides(window_strides), Strides(window_dilation),
1324
                                     CoordinateDiff(padding_below),CoordinateDiff(padding_above),
1325
                                     Strides(data_dilation))], parameter_list, 'test')
1326
    backend = Backend.create(test.BACKEND_NAME)
1327

1328 1329
    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, output_shape)
1330

1331 1332
    a.write(util.numpy_to_c(image_arr), 10*10*4)
    b.write(util.numpy_to_c(output_arr), 17*17*4)
1333 1334

    result_arr = np.zeros(3*3, dtype=np.float32).reshape(1, 1, 3, 3)
1335
    result = backend.create_tensor(element_type, Shape([1, 1, 3, 3]))
1336
    result.write(util.numpy_to_c(result_arr), 3*3*4)
1337 1338
    handle = backend.compile(function)
    handle.call([result], [a, b])
1339

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