Caffe2 - Python API
A deep learning, cross platform ML framework
mkl_test_util.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package mkl_test_util
17 # Module caffe2.python.mkl_test_util
18 """
19 The MKL test utils is a small addition on top of the hypothesis test utils
20 under caffe2/python, which allows one to more easily test MKL related
21 operators.
22 """
23 
24 from __future__ import absolute_import
25 from __future__ import division
26 from __future__ import print_function
27 from __future__ import unicode_literals
28 
29 import hypothesis.strategies as st
30 
31 from caffe2.proto import caffe2_pb2
32 from caffe2.python import workspace
33 from caffe2.python import hypothesis_test_util as hu
34 
35 cpu_do = hu.cpu_do
36 gpu_do = hu.gpu_do
37 mkl_do = caffe2_pb2.DeviceOption(device_type=caffe2_pb2.MKLDNN)
38 device_options = hu.device_options + (
39  [mkl_do] if workspace.C.has_mkldnn else [])
40 
41 
42 def device_checker_device_options():
43  return st.just(device_options)
44 
45 
46 def gradient_checker_device_option():
47  return st.sampled_from(device_options)
48 
49 
50 gcs = dict(
51  gc=gradient_checker_device_option(),
52  dc=device_checker_device_options()
53 )
54 
55 gcs_cpu_only = dict(gc=st.sampled_from([cpu_do]), dc=st.just([cpu_do]))
56 gcs_gpu_only = dict(gc=st.sampled_from([gpu_do]), dc=st.just([gpu_do]))
57 gcs_mkl_only = dict(gc=st.sampled_from([mkl_do]), dc=st.just([mkl_do]))
58 
59 gcs_cpu_mkl = dict(gc=st.sampled_from([cpu_do, mkl_do]), dc=st.just([cpu_do, mkl_do]))