forked from NervanaSystems/neon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_backend_tensor.py
173 lines (131 loc) · 5.37 KB
/
test_backend_tensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# ----------------------------------------------------------------------------
# Copyright 2015-2016 Nervana Systems Inc.
# 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.
# ----------------------------------------------------------------------------
# pylint: skip-file
"""
Test of basic math operations on the Tensors and compare with numpy results
The Tensor types includes GPU and CPU Tensors
"""
import numpy as np
import itertools as itt
import pytest
from utils import tensors_allclose
def init_helper(lib, inA, inB, dtype):
A = lib.array(inA, dtype=dtype)
B = lib.array(inB, dtype=dtype)
C = lib.empty(inB.shape, dtype=dtype)
return A, B, C
def math_helper(lib, op, inA, inB, dtype):
A, B, C = init_helper(lib, inA, inB, dtype)
if op == '+':
C[:] = A + B
elif op == '-':
C[:] = A - B
elif op == '*':
C[:] = A * B
elif op == '/':
C[:] = A / B
elif op == '>':
C[:] = A > B
elif op == '>=':
C[:] = A >= B
elif op == '<':
C[:] = A < B
elif op == '<=':
C[:] = A <= B
return C
def compare_helper(op, inA, inB, ng, nc, dtype):
numpy_result = math_helper(np, op, inA, inB, dtype=np.float32).astype(dtype)
nervanaGPU_result = math_helper(ng, op, inA, inB, dtype=dtype).get()
np.allclose(numpy_result, nervanaGPU_result, rtol=0, atol=1e-5)
nervanaCPU_result = math_helper(nc, op, inA, inB, dtype=dtype).get()
np.allclose(numpy_result, nervanaCPU_result, rtol=0, atol=1e-5)
def rand_unif(dtype, dims):
if np.dtype(dtype).kind == 'f':
return np.random.uniform(-1, 1, dims).astype(dtype)
else:
iinfo = np.iinfo(dtype)
return np.around(np.random.uniform(iinfo.min, iinfo.max, dims)).clip(iinfo.min, iinfo.max)
def pytest_generate_tests(metafunc):
"""
Build a list of test arguments.
"""
dims = [(64, 327),
(64, 1),
(1, 1023),
(4, 3),
]
if 'fargs_tests' in metafunc.fixturenames:
fargs = itt.product(dims)
metafunc.parametrize("fargs_tests", fargs)
@pytest.mark.hasgpu
def test_math(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]
ng, nc = backend_pair_dtype
dtype = ng.default_dtype
randA = rand_unif(dtype, dims)
randB = rand_unif(dtype, dims)
compare_helper('+', randA, randB, ng, nc, dtype)
compare_helper('-', randA, randB, ng, nc, dtype)
compare_helper('*', randA, randB, ng, nc, dtype)
compare_helper('>', randA, randB, ng, nc, dtype)
compare_helper('>=', randA, randB, ng, nc, dtype)
compare_helper('<', randA, randB, ng, nc, dtype)
compare_helper('<=', randA, randB, ng, nc, dtype)
@pytest.mark.hasgpu
def test_slicing(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]
gpu, cpu = backend_pair_dtype
dtype = gpu.default_dtype
array_np = np.random.uniform(-1, 1, dims).astype(dtype)
array_ng = gpu.array(array_np, dtype=dtype)
array_nc = cpu.array(array_np, dtype=dtype)
assert tensors_allclose(array_ng[0], array_nc[0], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[-1], array_nc[-1], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[0, :], array_nc[0, :], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[0:], array_nc[0:], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[:-1], array_nc[:-1], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[:, 0], array_nc[:, 0], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[:, 0:1], array_nc[:, 0:1], rtol=0, atol=1e-3)
assert tensors_allclose(array_ng[-1, 0:], array_nc[-1:, 0:], rtol=0, atol=1e-3)
array_ng[0] = 0
array_nc[0] = 0
assert tensors_allclose(array_ng, array_nc, rtol=0, atol=1e-3)
@pytest.mark.hasgpu
def test_reshape_separate(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]
gpu, cpu = backend_pair_dtype
dtype = gpu.default_dtype
array_np = np.random.uniform(-1, 1, dims).astype(dtype)
array_ng = gpu.array(array_np, dtype=dtype)
array_nc = cpu.array(array_np, dtype=dtype)
assert array_ng.is_contiguous
if (dims[0] % 2) == 0:
reshaped_ng = array_ng.reshape((2, dims[0] // 2, dims[1]))
reshaped_nc = array_nc.reshape((2, dims[0] // 2, dims[1]))
assert tensors_allclose(reshaped_ng, reshaped_nc, rtol=0, atol=1e-6)
@pytest.mark.hasgpu
def test_reshape_combine(fargs_tests, backend_pair_dtype):
dims = fargs_tests[0]
gpu, cpu = backend_pair_dtype
dtype = gpu.default_dtype
if (dims[0] % 2) == 0:
orig_shape = (2, dims[0] // 2, dims[1])
array_np = np.random.uniform(-1, 1, orig_shape).astype(dtype)
array_ng = gpu.array(array_np, dtype=dtype)
array_nc = cpu.array(array_np, dtype=dtype)
assert array_ng.is_contiguous
reshaped_ng = array_ng.reshape(dims)
reshaped_nc = array_nc.reshape(dims)
assert tensors_allclose(reshaped_ng, reshaped_nc, rtol=0, atol=1e-6)