|
3 | 3 | import torch
|
4 | 4 | import torchvision.models as models
|
5 | 5 |
|
6 |
| -from multi_gpu_test_case import MultiGpuTestCase |
7 |
| - |
8 |
| -gpu_id = 1 |
9 |
| -class TestCompile(MultiGpuTestCase): |
| 6 | +from model_test_case import ModelTestCase |
10 | 7 |
|
| 8 | +class TestMultiGpuSwitching(ModelTestCase): |
11 | 9 | def setUp(self):
|
12 |
| - self.input = torch.randn((1, 3, 224, 224)).to("cuda") |
| 10 | + if torch.cuda.device_count() < 2: |
| 11 | + self.fail("Test is not relevant for this platform since number of available CUDA devices is less than 2") |
| 12 | + |
| 13 | + trtorch.set_device(0) |
| 14 | + self.target_gpu = 1 |
| 15 | + self.input = torch.randn((1, 3, 224, 224)).to("cuda:1") |
| 16 | + self.model = self.model.to("cuda:1") |
13 | 17 | self.traced_model = torch.jit.trace(self.model, [self.input])
|
14 | 18 | self.scripted_model = torch.jit.script(self.model)
|
15 | 19 |
|
16 | 20 | def test_compile_traced(self):
|
| 21 | + trtorch.set_device(0) |
17 | 22 | compile_spec = {
|
18 | 23 | "input_shapes": [self.input.shape],
|
19 | 24 | "device": {
|
20 | 25 | "device_type": trtorch.DeviceType.GPU,
|
21 |
| - "gpu_id": gpu_id, |
| 26 | + "gpu_id": self.target_gpu, |
22 | 27 | "dla_core": 0,
|
23 | 28 | "allow_gpu_fallback": False,
|
24 | 29 | "disable_tf32": False
|
25 | 30 | }
|
26 | 31 | }
|
27 | 32 |
|
28 | 33 | trt_mod = trtorch.compile(self.traced_model, compile_spec)
|
| 34 | + trtorch.set_device(self.target_gpu) |
29 | 35 | same = (trt_mod(self.input) - self.traced_model(self.input)).abs().max()
|
| 36 | + trtorch.set_device(0) |
30 | 37 | self.assertTrue(same < 2e-3)
|
31 | 38 |
|
32 | 39 | def test_compile_script(self):
|
| 40 | + trtorch.set_device(0) |
33 | 41 | compile_spec = {
|
34 | 42 | "input_shapes": [self.input.shape],
|
35 | 43 | "device": {
|
36 | 44 | "device_type": trtorch.DeviceType.GPU,
|
37 |
| - "gpu_id": gpu_id, |
| 45 | + "gpu_id": self.target_gpu, |
38 | 46 | "dla_core": 0,
|
39 | 47 | "allow_gpu_fallback": False,
|
40 | 48 | "disable_tf32": False
|
41 | 49 | }
|
42 | 50 | }
|
43 | 51 |
|
44 | 52 | trt_mod = trtorch.compile(self.scripted_model, compile_spec)
|
| 53 | + trtorch.set_device(self.target_gpu) |
45 | 54 | same = (trt_mod(self.input) - self.scripted_model(self.input)).abs().max()
|
| 55 | + trtorch.set_device(0) |
46 | 56 | self.assertTrue(same < 2e-3)
|
47 | 57 |
|
48 |
| - |
49 |
| - |
50 | 58 | def test_suite():
|
51 | 59 | suite = unittest.TestSuite()
|
52 |
| - suite.addTest(TestCompile.parametrize(TestCompile, model=models.resnet18(pretrained=True))) |
| 60 | + suite.addTest(TestMultiGpuSwitching.parametrize(TestMultiGpuSwitching, model=models.resnet18(pretrained=True))) |
53 | 61 |
|
54 | 62 | return suite
|
55 | 63 |
|
56 |
| -if not torch.cuda.device_count() > 1: |
57 |
| - raise ValueError("This test case is applicable for multi-gpu configurations only") |
58 |
| - |
59 |
| -# Setting it up here so that all CUDA allocations are done on correct device |
60 |
| -trtorch.set_device(gpu_id) |
61 | 64 | suite = test_suite()
|
62 | 65 |
|
63 | 66 | runner = unittest.TextTestRunner()
|
|
0 commit comments