pytorch
103 строки · 3.6 Кб
1
2
3
4
5
6from caffe2.proto import caffe2_pb2
7from caffe2.python import core
8from hypothesis import assume, given, settings, HealthCheck
9import caffe2.python.hypothesis_test_util as hu
10import caffe2.python.serialized_test.serialized_test_util as serial
11import hypothesis.strategies as st
12import numpy as np
13import unittest
14
15
16class TestFcOperator(serial.SerializedTestCase):
17def _run_test(self, n, m, k, transposed, multi_dim, dtype, engine, gc, dc):
18if dtype == np.float16:
19# fp16 only supported with CUDA/HIP
20assume(core.IsGPUDeviceType(gc.device_type))
21dc = [d for d in dc if core.IsGPUDeviceType(d.device_type)]
22
23if engine == 'TENSORCORE':
24# TensorCore only makes sense with CUDA
25assume(gc.device_type == caffe2_pb2.CUDA)
26# ensures TensorCore kernels can be called
27m *= 8
28k *= 8
29n *= 8
30
31X = np.random.rand(m, k).astype(dtype) - 0.5
32if multi_dim:
33if transposed:
34W = np.random.rand(k, n, 1, 1).astype(dtype) - 0.5
35else:
36W = np.random.rand(n, k, 1, 1).astype(dtype) - 0.5
37else:
38if transposed:
39W = np.random.rand(k, n).astype(dtype) - 0.5
40else:
41W = np.random.rand(n, k).astype(dtype) - 0.5
42b = np.random.rand(n).astype(dtype) - 0.5
43
44def fc_op(X, W, b):
45return [np.dot(X, W.reshape(n, k).transpose()) + b.reshape(n)]
46
47def fc_transposed_op(X, W, b):
48return [np.dot(X, W.reshape(k, n)) + b.reshape(n)]
49
50op = core.CreateOperator(
51'FCTransposed' if transposed else 'FC',
52['X', 'W', 'b'],
53'out',
54engine=engine,
55)
56
57if dtype == np.float16 and core.IsGPUDeviceType(gc.device_type):
58a = caffe2_pb2.Argument()
59a.i = 1
60a.name = "float16_compute"
61op.arg.extend([a])
62
63# Check against numpy reference
64# ReferenceChecks is flaky, Relaxing to 1e-3.
65threshold = 1e-3
66self.assertReferenceChecks(
67device_option=gc,
68op=op,
69inputs=[X, W, b],
70reference=fc_transposed_op if transposed else fc_op,
71threshold=threshold
72)
73# Check over multiple devices
74self.assertDeviceChecks(dc, op, [X, W, b], [0])
75
76# Gradient checks
77threshold = 0.5 if dtype == np.float16 else 0.005
78stepsize = 0.5 if dtype == np.float16 else 0.05
79for i in range(3):
80self.assertGradientChecks(gc, op, [X, W, b], i, [0],
81threshold=threshold, stepsize=stepsize)
82
83@settings(max_examples=50, suppress_health_check=[HealthCheck.filter_too_much])
84@serial.given(n=st.integers(1, 5),
85m=st.integers(0, 5),
86k=st.integers(1, 5),
87multi_dim=st.sampled_from([True, False]),
88dtype=st.sampled_from([np.float32, np.float16]),
89engine=st.sampled_from(['', 'TENSORCORE']),
90**hu.gcs)
91def test_fc(self, **kwargs):
92self._run_test(transposed=False, **kwargs)
93
94@settings(max_examples=50, suppress_health_check=[HealthCheck.filter_too_much])
95@given(n=st.integers(1, 5),
96m=st.integers(0, 5),
97k=st.integers(1, 5),
98multi_dim=st.sampled_from([True, False]),
99dtype=st.sampled_from([np.float32, np.float16]),
100engine=st.sampled_from(['', 'TENSORCORE']),
101**hu.gcs)
102def test_fc_transposed(self, **kwargs):
103self._run_test(transposed=True, **kwargs)
104
105
106if __name__ == "__main__":
107import unittest
108unittest.main()
109