Skip to content

Commit aa88c2c

Browse files
rohithkrnfacebook-github-bot
authored andcommitted
Unify gpu_support variable in python tests (pytorch#16748)
Summary: Assign `has_gpu_support = has_cuda_support or has_hip_support` and make according changes in python tests. Pull Request resolved: pytorch#16748 Differential Revision: D13983132 Pulled By: bddppq fbshipit-source-id: ca496fd8c6ae3549b736bebd3ace7fa20a6dad7f
1 parent 85ac272 commit aa88c2c

23 files changed

+36
-40
lines changed

caffe2/contrib/nccl/nccl_ops_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def benchmark(ws, net, warmups=5, iters=100):
3838
return after - before
3939

4040

41-
@unittest.skipIf(not workspace.has_gpu_support, "NCCL only on GPU")
41+
@unittest.skipIf(not workspace.has_cuda_support, "NCCL only on CUDA GPU")
4242
class NCCLOpsTest(hu.HypothesisTestCase):
4343
@given(n=st.integers(min_value=2, max_value=workspace.NumCudaDevices()),
4444
m=st.integers(min_value=1, max_value=1000),

caffe2/python/_import_c_extension.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
# if that still fails, we will exit loud.
1111
with extension_loader.DlopenGuard():
1212
has_hip_support = False
13+
has_cuda_support = False
1314
has_gpu_support = False
1415

1516
try:
1617
from caffe2.python.caffe2_pybind11_state_gpu import * # noqa
1718
if num_cuda_devices(): # noqa
18-
has_gpu_support = True
19+
has_gpu_support = has_cuda_support = True
1920
except ImportError as gpu_e:
2021
logging.info('Failed to import cuda module: {}'.format(gpu_e))
2122
try:
2223
from caffe2.python.caffe2_pybind11_state_hip import * # noqa
2324
if num_hip_devices():
24-
has_hip_support = True
25+
has_gpu_support = has_hip_support = True
2526
logging.info('This caffe2 python run has AMD GPU support!')
2627
except ImportError as hip_e:
2728
logging.info('Failed to import AMD hip module: {}'.format(hip_e))

caffe2/python/core_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,7 @@ def test_inject_copy(self):
643643
self.assertEqual(op.input[2], "fc_b")
644644

645645

646-
@unittest.skipIf(not workspace.has_gpu_support
647-
and not workspace.has_hip_support, 'No GPU support')
646+
@unittest.skipIf(not workspace.has_gpu_support, 'No GPU support')
648647
class TestInferDevice(test_util.TestCase):
649648

650649
def setUp(self):

caffe2/python/gradient_check_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import unittest
2424

2525

26-
if (workspace.has_gpu_support or workspace.has_hip_support) and workspace.NumGpuDevices() > 0:
26+
if workspace.has_gpu_support and workspace.NumGpuDevices() > 0:
2727
gpu_device_option = caffe2_pb2.DeviceOption()
2828
gpu_device_option.device_type = workspace.GpuDeviceType
2929
cpu_device_option = caffe2_pb2.DeviceOption()

caffe2/python/hypothesis_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def prod(xs):
320320
for param, _ in enumerate(inputs):
321321
self.assertGradientChecks(gc, op, inputs, param, [0])
322322

323-
@unittest.skipIf(not workspace.has_gpu_support and not workspace.has_hip_support,
323+
@unittest.skipIf(not workspace.has_gpu_support,
324324
"Skipping test due to no gpu present.")
325325
@given(hidden_size=st.integers(min_value=1, max_value=3),
326326
num_layers=st.integers(min_value=1, max_value=3),

caffe2/python/hypothesis_test_util.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,12 @@ def tensors1d(n, min_len=1, max_len=64, dtype=np.float32, elements=None):
277277

278278

279279
cpu_do = caffe2_pb2.DeviceOption()
280-
gpu_do = caffe2_pb2.DeviceOption(device_type=caffe2_pb2.CUDA)
280+
cuda_do = caffe2_pb2.DeviceOption(device_type=caffe2_pb2.CUDA)
281281
hip_do = caffe2_pb2.DeviceOption(device_type=caffe2_pb2.HIP)
282+
gpu_do = caffe2_pb2.DeviceOption(device_type=workspace.GpuDeviceType) # CUDA or ROCm
282283
# (bddppq) Do not rely on this no_hip option! It's just used to
283284
# temporarily skip some flaky tests on ROCM before it's getting more mature.
284-
_device_options_no_hip = [cpu_do] + ([gpu_do] if workspace.has_gpu_support else [])
285+
_device_options_no_hip = [cpu_do] + ([cuda_do] if workspace.has_cuda_support else [])
285286
device_options = _device_options_no_hip + ([hip_do] if workspace.has_hip_support else [])
286287

287288
# Include device option for each GPU
@@ -304,7 +305,8 @@ def gradient_checker_device_option():
304305
)
305306

306307
gcs_cpu_only = dict(gc=st.sampled_from([cpu_do]), dc=st.just([cpu_do]))
307-
gcs_gpu_only = dict(gc=st.sampled_from([gpu_do]), dc=st.just([gpu_do]))
308+
gcs_cuda_only = dict(gc=st.sampled_from([cuda_do]), dc=st.just([cuda_do]))
309+
gcs_gpu_only = dict(gc=st.sampled_from([gpu_do]), dc=st.just([gpu_do])) # CUDA or ROCm
308310
gcs_no_hip = dict(gc=st.sampled_from(_device_options_no_hip), dc=st.just(_device_options_no_hip))
309311

310312

caffe2/python/memonger_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ def test_gradient_optim(self, input_dim, output_dim, batch_size):
223223
np.testing.assert_almost_equal(loss, optimized_loss)
224224
np.testing.assert_almost_equal(grad, optimized_grad)
225225

226-
@unittest.skipIf(not workspace.has_gpu_support
227-
and not workspace.has_hip_support, "No gpu support.")
226+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support.")
228227
def test_memonger_mix_cpu_gpu(self):
229228
'''
230229
Check that memonger does not make blobs cross CPU/GPU boundary

caffe2/python/model_device_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ def _testMiniAlexNet(self, order):
136136
)
137137
self.assertEqual(ret, True)
138138

139-
@unittest.skipIf(not workspace.has_gpu_support
140-
and not workspace.has_hip_support,
139+
@unittest.skipIf(not workspace.has_gpu_support,
141140
"No GPU support. Skipping test.")
142141
def testMiniAlexNetNCHW(self):
143142
self._testMiniAlexNet("NCHW")

caffe2/python/onnx/backend.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ def supports_device(cls, device_str):
919919
if device.type == DeviceType.CPU:
920920
return True
921921
elif core.IsGPUDeviceType(device.type):
922-
return workspace.has_gpu_support or workspace.has_hip_support
922+
return workspace.has_gpu_support
923923
return False
924924

925925
@classmethod

caffe2/python/operator_test/activation_ops_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def relu_ref(X):
4141
self.assertDeviceChecks(dc, op, [X], [0])
4242
self.assertGradientChecks(gc, op, [X], 0, [0])
4343

44-
@unittest.skipIf(not workspace.has_gpu_support and
45-
not workspace.has_hip_support,
44+
@unittest.skipIf(not workspace.has_gpu_support,
4645
"Relu for float16 can only run on GPU now.")
4746
@given(X=hu.tensor(dtype=np.float16), in_place=st.booleans(),
4847
engine=st.sampled_from(["", "CUDNN"]), **hu.gcs)

caffe2/python/operator_test/conv_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ def canonical(o):
748748
["simple", "dag"]
749749
+ (
750750
["async_dag"]
751-
if workspace.has_gpu_support or workspace.has_hip_support
751+
if workspace.has_gpu_support
752752
else []
753753
)
754754
),

caffe2/python/operator_test/cudnn_recurrent_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import unittest
1212

1313

14-
@unittest.skipIf((not workspace.has_gpu_support) and (not workspace.has_hip_support), "No gpu support.")
14+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support.")
1515
class TestLSTMs(unittest.TestCase):
1616

1717
def testEqualToCudnn(self):

caffe2/python/operator_test/elementwise_op_broadcast_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def test_sum_reduce(self, gc, dc):
327327
dc_cpu_only = [d for d in dc if d.device_type != caffe2_pb2.CUDA]
328328
self.assertDeviceChecks(dc_cpu_only, op, [X, Y], [0])
329329

330-
@unittest.skipIf(not workspace.has_gpu_support and not workspace.has_hip_support, "No gpu support")
330+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support")
331331
@given(**hu.gcs)
332332
def test_sum_reduce_fp16(self, gc, dc):
333333
assume(core.IsGPUDeviceType(gc.device_type))

caffe2/python/operator_test/momentum_sgd_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def sparse(grad, m, lr, param, i):
139139
[grad, m, lr, w, indices],
140140
sparse)
141141

142-
@unittest.skipIf(not workspace.has_gpu_support and not workspace.has_hip_support, "No gpu support.")
142+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support.")
143143
@given(n=st.integers(4, 8), nesterov=st.booleans(), **hu.gcs)
144144
def test_fp16momentum_sgd(self, n, nesterov, gc, dc):
145145
assume(core.IsGPUDeviceType(gc.device_type))

caffe2/python/operator_test/pooling_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_pooling_separate_stride_pad(self, stride_h, stride_w,
6363
@unittest.skipIf(not os.getenv('CAFFE2_DEBUG'),
6464
"This is a test that reproduces a cudnn error. If you "
6565
"want to run it, set env variable CAFFE2_DEBUG=1.")
66-
@given(**hu.gcs_gpu_only)
66+
@given(**hu.gcs_cuda_only)
6767
def test_pooling_big_batch(self, gc, dc):
6868
op = core.CreateOperator(
6969
"AveragePool",

caffe2/python/operator_test/softmax_ops_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def test_compare_cpugpu(self):
626626
"SpatialSoftmaxWithLoss",
627627
["X_gpu", "label_gpu"],
628628
["probs_gpu", "avgloss_gpu"],
629-
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
629+
device_option=core.DeviceOption(workspace.GpuDeviceType, 0)
630630
)
631631

632632
cpuop = core.CreateOperator(
@@ -649,7 +649,7 @@ def test_compare_cpugpu(self):
649649
# Initialize label. Some of the labels are (-1), i.e "DONT CARE"
650650
label = (np.random.rand(n, H, W) * (D + 1)).astype(np.int32) - 1
651651

652-
gpu0 = core.DeviceOption(caffe2_pb2.CUDA, 0)
652+
gpu0 = core.DeviceOption(workspace.GpuDeviceType, 0)
653653
workspace.FeedBlob("X_cpu", X)
654654
workspace.FeedBlob("label_cpu", label)
655655
workspace.FeedBlob("X_gpu", X, device_option=gpu0)

caffe2/python/operator_test/spatial_bn_op_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def reference_spatialbn_test(X, scale, bias, mean, var):
6767
reference_spatialbn_test)
6868
self.assertDeviceChecks(dc, op, [X, scale, bias, mean, var], [0])
6969

70-
@unittest.skipIf((not workspace.has_gpu_support) and (
71-
not workspace.has_hip_support), "No gpu support")
70+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support")
7271
@given(size=st.integers(7, 10),
7372
input_channels=st.integers(1, 10),
7473
batch_size=st.integers(0, 3),

caffe2/python/operator_test/transpose_op_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def transpose_ref(X):
3838
self.assertDeviceChecks(dc, op, [X], [0])
3939
self.assertGradientChecks(gc, op, [X], 0, [0])
4040

41-
@unittest.skipIf(not workspace.has_gpu_support, "no gpu support")
41+
@unittest.skipIf(not workspace.has_cuda_support, "no cuda support")
4242
@given(X=hu.tensor(dtype=np.float32), use_axes=st.booleans(),
43-
**hu.gcs_gpu_only)
43+
**hu.gcs_cuda_only)
4444
def test_transpose_cudnn(self, X, use_axes, gc, dc):
4545
ndim = len(X.shape)
4646
axes = np.arange(ndim)
@@ -49,11 +49,11 @@ def test_transpose_cudnn(self, X, use_axes, gc, dc):
4949
if (use_axes):
5050
op = core.CreateOperator(
5151
"Transpose", ["X"], ["Y"], axes=axes, engine="CUDNN",
52-
device_option=hu.gpu_do)
52+
device_option=hu.cuda_do)
5353
else:
5454
op = core.CreateOperator(
5555
"Transpose", ["X"], ["Y"], engine="CUDNN",
56-
device_option=hu.gpu_do)
56+
device_option=hu.cuda_do)
5757

5858
def transpose_ref(X):
5959
if use_axes:

caffe2/python/optimizer_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ def test_caffe2_cpu_vs_numpy(self):
454454
)
455455

456456
@unittest.skip("Results might vary too much. Only for individual use.")
457-
@unittest.skipIf(not workspace.has_gpu_support
458-
and not workspace.has_hip_support, "No gpu support")
457+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support")
459458
def test_caffe2_gpu_vs_numpy(self):
460459
n_dim = 1000000
461460
n_iter = 50

caffe2/python/parallelize_bmuf_distributed_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def bmuf_process(filestore_dir, process_id, shared_results,
2525
dyndep.InitOpsLibrary("@/caffe2/caffe2/distributed:file_store_handler_ops")
2626

2727
if not cpu_device:
28-
if not workspace.has_gpu_support and not workspace.has_hip_support:
28+
if not workspace.has_gpu_support:
2929
log.info('No GPU support test is Ignored.')
3030
return
3131
if workspace.NumGpuDevices() < 4:

caffe2/python/test/executor_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def run_model():
4747
)
4848

4949

50-
@unittest.skipIf(not workspace.has_gpu_support
51-
and not workspace.has_hip_support, "no gpu")
50+
@unittest.skipIf(not workspace.has_gpu_support, "no gpu")
5251
class ExecutorGPUResNetTest(ExecutorTestBase):
5352
@given(executor=st.sampled_from(EXECUTORS),
5453
num_workers=st.sampled_from([8]))

caffe2/python/workspace.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
operator_tracebacks = defaultdict(dict)
4242

4343
is_asan = C.is_asan
44-
has_gpu_support = C.has_gpu_support
44+
has_cuda_support = C.has_cuda_support
4545
has_hip_support = C.has_hip_support
46-
if has_gpu_support:
46+
has_gpu_support = C.has_gpu_support
47+
if has_cuda_support:
4748
GpuDeviceType = caffe2_pb2.CUDA
4849
NumCudaDevices = C.num_cuda_devices
4950
# This is a duplicate of NumCudaDevices. Remove
@@ -69,7 +70,7 @@ def GetGpuPeerAccessPattern():
6970
return np.asarray(C.get_hip_peer_access_pattern())
7071
GetDeviceProperties = C.get_device_properties
7172

72-
if not has_gpu_support and not has_hip_support:
73+
if not has_gpu_support:
7374
# setting cuda as the default GpuDeviceType as some tests
7475
# like core, scope tests use GpuDeviceType even without gpu support
7576
GpuDeviceType = caffe2_pb2.CUDA

caffe2/python/workspace_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ def testCreateWorkspace(self):
320320
self.assertTrue("test" in workspaces)
321321

322322

323-
@unittest.skipIf(not workspace.has_gpu_support
324-
and not workspace.has_hip_support, "No gpu support.")
323+
@unittest.skipIf(not workspace.has_gpu_support, "No gpu support.")
325324
class TestWorkspaceGPU(test_util.TestCase):
326325

327326
def setUp(self):

0 commit comments

Comments
 (0)