forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmul_op_test.py
More file actions
388 lines (336 loc) · 14 KB
/
Copy pathmatmul_op_test.py
File metadata and controls
388 lines (336 loc) · 14 KB
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for tensorflow.ops.math_ops.matmul."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
class MatMulTest(tf.test.TestCase):
def assertAllCloseAccordingToType(self, a, b, rtol=1e-6, atol=1e-6):
"""Like test_util.assertAllCloseToType, but with looser fp16 limits.
With matrix multiplication, many values are summed, compounding
accuracy issues. Thus, we set fp16 tolerance to 1e-2 instead of 1e-6.
(This primarily affects the CPU versions, which accumulate in fp16;
the CUDA versions currently use fp32 math internally.)
Args:
a: a numpy ndarray or anything can be converted to one.
b: a numpy ndarray or anything can be converted to one.
rtol: relative tolerance
atol: absolute tolerance
"""
a = self._GetNdArray(a)
b = self._GetNdArray(b)
if a.dtype == np.float16 or b.dtype == np.float16:
rtol = max(rtol, 1e-2)
atol = max(atol, 1e-2)
self.assertAllClose(a, b, rtol=rtol, atol=atol)
def _testCpuMatmul(self, x, y, transpose_x=False, transpose_y=False):
x_mat = np.matrix(x).T if transpose_x else np.matrix(x)
y_mat = np.matrix(y).T if transpose_y else np.matrix(y)
np_ans = x_mat * y_mat
with self.test_session(use_gpu=False):
tf_ans = tf.matmul(x, y, transpose_x, transpose_y).eval()
self.assertAllCloseAccordingToType(np_ans, tf_ans)
self.assertAllEqual(np_ans.shape, tf_ans.shape)
def _testGpuMatmul(self, x, y, transpose_x=False, transpose_y=False):
x_mat = np.matrix(x).T if transpose_x else np.matrix(x)
y_mat = np.matrix(y).T if transpose_y else np.matrix(y)
np_ans = x_mat * y_mat
with self.test_session(use_gpu=True):
tf_ans = tf.matmul(x, y, transpose_x, transpose_y).eval()
self.assertAllCloseAccordingToType(np_ans, tf_ans)
self.assertAllEqual(np_ans.shape, tf_ans.shape)
def _randMatrix(self, rows, cols, dtype):
if dtype in (np.complex64, np.complex128):
if dtype == np.complex64:
float_dtype = np.float32
else:
float_dtype = np.float64
real = self._randMatrix(rows, cols, float_dtype)
imag = self._randMatrix(rows, cols, float_dtype)
return real + 1j * imag
else:
return np.random.uniform(low=1.0, high=100.0, size=rows * cols).reshape(
[rows, cols]).astype(dtype)
# Basic test:
# [ [1],
# [2],
# [3], * [1, 2]
# [4] ]
def testFloatBasic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.float32)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.float32)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testDoubleBasic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.float64)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.float64)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testHalfBasic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.float16)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.float16)
self._testCpuMatmul(x, y)
if test_util.CudaSupportsHalfMatMulAndConv():
self._testGpuMatmul(x, y)
else:
print("Built without fp16 matmul support, skipping GPU test.")
def testInt32Basic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.int32)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.int32)
self._testCpuMatmul(x, y)
def testComplex64Basic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.complex64)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.complex64)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testComplex128Basic(self):
x = np.arange(1., 5.).reshape([4, 1]).astype(np.complex128)
y = np.arange(1., 3.).reshape([1, 2]).astype(np.complex128)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
# Vector optimized tests
# x * y
# [1, 2, 3, 4] * [[1, 2, 3, 4],
# [5, 6, 7, 8]]
#
# and y^T * x^T
def _vectorTest(self, dtype, gpu):
x = np.arange(1., 5.).reshape([1, 4]).astype(np.float32)
y = np.arange(1., 9.).reshape([4, 2]).astype(np.float32)
x_t = x.transpose()
y_t = y.transpose()
if gpu:
self._testGpuMatmul(x, y)
self._testGpuMatmul(x_t, y, transpose_x=True)
self._testGpuMatmul(x, y_t, transpose_y=True)
self._testGpuMatmul(y_t, x_t)
self._testGpuMatmul(y, x_t, transpose_x=True)
self._testGpuMatmul(y_t, x, transpose_y=True)
else:
self._testCpuMatmul(x, y)
self._testCpuMatmul(x_t, y, transpose_x=True)
self._testCpuMatmul(x, y_t, transpose_y=True)
self._testCpuMatmul(y_t, x_t)
self._testCpuMatmul(y, x_t, transpose_x=True)
self._testCpuMatmul(y_t, x, transpose_y=True)
def testFloatVector(self):
self._vectorTest(np.float32, gpu=False)
self._vectorTest(np.float32, gpu=True)
def testDoubleVector(self):
self._vectorTest(np.float64, gpu=False)
self._vectorTest(np.float64, gpu=True)
def testHalfVector(self):
self._vectorTest(np.float16, gpu=False)
if test_util.CudaSupportsHalfMatMulAndConv():
self._vectorTest(np.float16, gpu=True)
else:
print("Built without fp16 matmul support, skipping GPU test.")
def testInt32Vector(self):
self._vectorTest(np.int32, gpu=False)
self._vectorTest(np.int32, gpu=True)
def testComplex64Vector(self):
self._vectorTest(np.complex64, gpu=False)
self._vectorTest(np.complex64, gpu=True)
def testComplex128Vector(self):
self._vectorTest(np.complex128, gpu=False)
self._vectorTest(np.complex128, gpu=True)
# Tests testing random sized matrices.
def testFloatRandom(self):
for _ in range(10):
n, k, m = np.random.randint(1, 100, size=3)
x = self._randMatrix(n, k, np.float32)
y = self._randMatrix(k, m, np.float32)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testDoubleRandom(self):
for _ in range(10):
n, k, m = np.random.randint(1, 100, size=3)
x = self._randMatrix(n, k, np.float64)
y = self._randMatrix(k, m, np.float64)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testHalfRandom(self):
for _ in range(10):
n, k, m = np.random.randint(1, 10, size=3) # Smaller range than float.
x = self._randMatrix(n, k, np.float16)
y = self._randMatrix(k, m, np.float16)
self._testCpuMatmul(x, y)
if test_util.CudaSupportsHalfMatMulAndConv():
self._testGpuMatmul(x, y)
else:
print("Built without fp16 matmul support, skipping GPU test.")
def testInt32Random(self):
for _ in range(10):
n, k, m = np.random.randint(1, 100, size=3)
x = self._randMatrix(n, k, np.int32)
y = self._randMatrix(k, m, np.int32)
self._testCpuMatmul(x, y)
def testComplex64Random(self):
for _ in range(10):
n, k, m = np.random.randint(1, 10, size=3) # Smaller range than float
x = self._randMatrix(n, k, np.complex64)
y = self._randMatrix(k, m, np.complex64)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testComplex128Random(self):
for _ in range(10):
n, k, m = np.random.randint(1, 10, size=3) # Smaller range than float
x = self._randMatrix(n, k, np.complex128)
y = self._randMatrix(k, m, np.complex128)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
# Test the cases that transpose the matrices before multiplying.
# NOTE(keveman): The cases where only one of the inputs is
# transposed are covered by tf.matmul's gradient function.
def testFloatRandomTransposeBoth(self):
for _ in range(10):
n, k, m = np.random.randint(1, 100, size=3)
x = self._randMatrix(k, n, np.float32)
y = self._randMatrix(m, k, np.float32)
self._testCpuMatmul(x, y, True, True)
self._testGpuMatmul(x, y, True, True)
def testDoubleRandomTransposeBoth(self):
for _ in range(10):
n, k, m = np.random.randint(1, 100, size=3)
x = self._randMatrix(k, n, np.float64)
y = self._randMatrix(m, k, np.float64)
self._testCpuMatmul(x, y, True, True)
self._testGpuMatmul(x, y, True, True)
def testHalfRandomTransposeBoth(self):
for _ in range(10):
n, k, m = np.random.randint(1, 10, size=3) # Smaller range than float.
x = self._randMatrix(k, n, np.float16)
y = self._randMatrix(m, k, np.float16)
self._testCpuMatmul(x, y, True, True)
if test_util.CudaSupportsHalfMatMulAndConv():
self._testGpuMatmul(x, y, True, True)
else:
print("Built without fp16 matmul support, skipping GPU test.")
def testMatMul_OutEmpty_A(self):
n, k, m = 0, 8, 3
x = self._randMatrix(n, k, np.float32)
y = self._randMatrix(k, m, np.float32)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testMatMul_OutEmpty_B(self):
n, k, m = 3, 8, 0
x = self._randMatrix(n, k, np.float32)
y = self._randMatrix(k, m, np.float32)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testMatMul_Inputs_Empty(self):
n, k, m = 3, 0, 4
x = self._randMatrix(n, k, np.float32)
y = self._randMatrix(k, m, np.float32)
self._testCpuMatmul(x, y)
self._testGpuMatmul(x, y)
def testShapeErrors(self):
a = tf.placeholder(tf.float32, [32, 37])
b = tf.placeholder(tf.float32, [36, 2])
c = tf.placeholder(tf.float32, [37])
with self.assertRaisesRegexp(
ValueError, "Dimensions 37 and 36 are not compatible"):
tf.matmul(a, b)
with self.assertRaisesRegexp(ValueError, "must have rank 2"):
tf.matmul(a, c)
# TODO(zhifengc): Figures out how to test matmul gradients on GPU.
class MatMulGradientTest(tf.test.TestCase):
def testGradientInput0(self):
with self.test_session(use_gpu=False):
x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2],
dtype=tf.float64, name="x")
y = tf.constant([1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7],
shape=[2, 4], dtype=tf.float64, name="y")
m = tf.matmul(x, y, name="matmul")
err = tf.test.compute_gradient_error(x, [3, 2], m, [3, 4])
print("matmul input0 gradient err = ", err)
self.assertLess(err, 1e-10)
def testGradientInput1(self):
with self.test_session(use_gpu=False):
x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2],
dtype=tf.float64, name="x")
y = tf.constant([1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7],
shape=[2, 4], dtype=tf.float64, name="y")
m = tf.matmul(x, y, name="matmul")
err = tf.test.compute_gradient_error(y, [2, 4], m, [3, 4])
print("matmul input1 gradient err = ", err)
self.assertLess(err, 1e-10)
def _VerifyInput0(self, transpose_a, transpose_b):
shape_x = [3, 2]
shape_y = [2, 4]
if transpose_a:
shape_x = list(reversed(shape_x))
if transpose_b:
shape_y = list(reversed(shape_y))
with self.test_session(use_gpu=False):
x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=shape_x,
dtype=tf.float64, name="x")
y = tf.constant([1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7],
shape=shape_y, dtype=tf.float64, name="y")
m = tf.matmul(x, y, transpose_a, transpose_b, name="matmul")
err = tf.test.compute_gradient_error(x, shape_x, m, [3, 4])
print("matmul input0 gradient err = ", err)
self.assertLess(err, 1e-10)
def testGradientInput0WithTranspose(self):
self._VerifyInput0(transpose_a=True, transpose_b=False)
self._VerifyInput0(transpose_a=False, transpose_b=True)
self._VerifyInput0(transpose_a=True, transpose_b=True)
def _VerifyInput1(self, transpose_a, transpose_b):
shape_x = [3, 2]
shape_y = [2, 4]
if transpose_a:
shape_x = list(reversed(shape_x))
if transpose_b:
shape_y = list(reversed(shape_y))
with self.test_session(use_gpu=False):
x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=shape_x,
dtype=tf.float64, name="x")
y = tf.constant([1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7],
shape=shape_y, dtype=tf.float64, name="y")
m = tf.matmul(x, y, transpose_a, transpose_b, name="matmul")
err = tf.test.compute_gradient_error(y, shape_y, m, [3, 4])
print("matmul input1 gradient err = ", err)
self.assertLess(err, 1e-10)
def testGradientInput1WithTranspose(self):
self._VerifyInput1(transpose_a=True, transpose_b=False)
self._VerifyInput1(transpose_a=False, transpose_b=True)
self._VerifyInput1(transpose_a=True, transpose_b=True)
class MatMulStatsTest(tf.test.TestCase):
def testSimpleStatistics(self):
g = tf.Graph()
with g.as_default():
a = tf.Variable(tf.random_normal([25, 16]))
b = tf.Variable(tf.random_normal([16, 9]))
tf.matmul(a, b)
for op in g.get_operations():
flops = ops.get_stats_for_node_def(g, op.node_def, "flops").value
if op.name == "MatMul":
self.assertEqual(7200, flops)
def testTransposedStatistics(self):
g = tf.Graph()
with g.as_default():
a = tf.Variable(tf.random_normal([16, 25]))
b = tf.Variable(tf.random_normal([16, 9]))
tf.matmul(a, b, transpose_a=True)
for op in g.get_operations():
flops = ops.get_stats_for_node_def(g, op.node_def, "flops").value
if op.name == "MatMul":
self.assertEqual(7200, flops)
if __name__ == "__main__":
tf.test.main()