|
15 | 15 |
|
16 | 16 | import unittest, os
|
17 | 17 | import numpy as np
|
| 18 | +import hypothesis.strategies as st |
| 19 | +import caffe2.python.hypothesis_test_util as hu |
18 | 20 |
|
19 |
| -from caffe2.proto import caffe2_pb2 |
20 |
| -from caffe2.python import core, workspace, dyndep |
| 21 | +from hypothesis import given |
| 22 | +from caffe2.python import core, dyndep |
21 | 23 |
|
22 |
| -tc_c2_lib = os.path.join(os.environ.get("CONDA_PREFIX"), "lib/libtc_c2.so") |
23 |
| -dyndep.InitOpsLibrary(tc_c2_lib) |
24 | 24 |
|
| 25 | +CONDA_PREFIX = os.environ.get("CONDA_PREFIX") |
| 26 | +if CONDA_PREFIX: |
| 27 | + tc_c2_lib = os.path.join(CONDA_PREFIX, "lib/libtc_c2.so") |
| 28 | +else: |
| 29 | + dyndep.InitOpsLibrary("@/tc/tc:tc_c2") |
25 | 30 |
|
26 |
| -class TestCaffe2(unittest.TestCase): |
27 | 31 |
|
28 |
| - def test_matmul_caffe2(self): |
29 |
| - lang = """ |
| 32 | +class TestCaffe2(hu.HypothesisTestCase): |
| 33 | + @given(n=st.integers(1, 128), |
| 34 | + m=st.integers(1, 128), |
| 35 | + k=st.integers(1, 128), |
| 36 | + seed=st.integers(min_value=0, max_value=2**32 - 1), |
| 37 | + **hu.gcs_gpu_only) |
| 38 | + def test_matmul(self, n, m, k, seed, gc, dc): |
| 39 | + np.random.seed(seed) |
| 40 | + |
| 41 | + tc_forward = """ |
30 | 42 | def matmul(float(M,N) A, float(N,K) B) -> (output) {
|
31 | 43 | output(i, j) +=! A(i, kk) * B(kk, j)
|
32 | 44 | }
|
33 | 45 | """
|
| 46 | + |
34 | 47 | # TODO: (prigoyal) serialize the options
|
35 | 48 | # options = Options("mlp")
|
36 |
| - mat1, mat2 = np.random.rand(100, 400), np.random.rand(400, 500) |
37 |
| - with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)): |
38 |
| - workspace.FeedBlob('mat1', mat1.astype(np.float32)) |
39 |
| - workspace.FeedBlob('mat2', mat2.astype(np.float32)) |
40 |
| - matmul = core.CreateOperator( |
41 |
| - "TcOp", ["mat1", "mat2"], ["out"], lang=lang, tcName="matmul" |
42 |
| - ) |
43 |
| - workspace.RunOperatorOnce(matmul) |
44 |
| - out = workspace.FetchBlob("out") |
| 49 | + X = np.random.rand(m, k).astype(np.float32) |
| 50 | + W = np.random.rand(k, n).astype(np.float32) |
| 51 | + |
| 52 | + def ref(X, W): |
| 53 | + return [np.dot(X, W)] |
| 54 | + |
| 55 | + op = core.CreateOperator( |
| 56 | + "TcOp", ["X", "Y"], "out", |
| 57 | + tcDef=tc_forward, |
| 58 | + tcName="matmul", |
| 59 | + ) |
| 60 | + |
| 61 | + self.assertReferenceChecks( |
| 62 | + device_option=gc, |
| 63 | + op=op, |
| 64 | + inputs=[X, W], |
| 65 | + reference=ref, |
| 66 | + ) |
45 | 67 |
|
46 | 68 |
|
47 | 69 | if __name__ == '__main__':
|
|
0 commit comments