Skip to content

Commit d81de7a

Browse files
committed
Force to remap to CPU for TorchScript model if GPUdoesn't exist
Signed-off-by: Gigon Bae <gbae@nvidia.com>
1 parent 90d15ca commit d81de7a

File tree

5 files changed

+15
-22
lines changed

5 files changed

+15
-22
lines changed

examples/apps/mednist_classifier_monaideploy/mednist_classifier_monaideploy.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
6666
image_tensor = self.transform(img) # (1, 64, 64), torch.float64
6767
image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32
6868

69-
# Comment below line if you want to do CPU inference
70-
image_tensor = image_tensor.cuda()
69+
if torch.cuda.is_available():
70+
image_tensor = image_tensor.cuda()
7171

7272
model = context.models.get() # get a TorchScriptModel object
73-
# Uncomment the following line if you want to do CPU inference
74-
# model.predictor = torch.jit.load(model.path, map_location="cpu").eval()
7573

7674
with torch.no_grad():
7775
outputs = model(image_tensor)

monai/deploy/core/models/torch_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def predictor(self) -> "torch.nn.Module": # type: ignore
4747
torch.nn.Module: the model's predictor
4848
"""
4949
if self._predictor is None:
50-
self._predictor = torch.jit.load(self.path).eval()
50+
# If GPU is not available, explicitly use "cpu" to dynamically remap storages to CPU.
51+
map_location = None if torch.cuda.is_available() else "cpu"
52+
self._predictor = torch.jit.load(self.path, map_location=map_location).eval()
5153
return self._predictor
5254

5355
@predictor.setter

monai/deploy/operators/monai_seg_inference_operator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
165165
model = context.models.get()
166166
else:
167167
print(f"Loading TorchScript model from: {MonaiSegInferenceOperator.MODEL_LOCAL_PATH}")
168-
model = torch.jit.load(MonaiSegInferenceOperator.MODEL_LOCAL_PATH)
168+
map_location = None if torch.cuda.is_available() else "cpu"
169+
model = torch.jit.load(MonaiSegInferenceOperator.MODEL_LOCAL_PATH, map_location=map_location)
169170

170171
dataset = Dataset(data=[{self._input_dataset_key: img_name}], transform=pre_transforms)
171172
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1)

notebooks/tutorials/02_mednist_app-prebuilt.ipynb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,10 @@
432432
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
433433
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
434434
"\n",
435-
" # Comment below line if you want to do CPU inference\n",
436-
" image_tensor = image_tensor.cuda()\n",
435+
" if torch.cuda.is_available():\n",
436+
" image_tensor = image_tensor.cuda()\n",
437437
"\n",
438438
" model = context.models.get() # get a TorchScriptModel object\n",
439-
" # Uncomment the following line if you want to do CPU inference\n",
440-
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
441439
"\n",
442440
" with torch.no_grad():\n",
443441
" outputs = model(image_tensor)\n",
@@ -652,12 +650,10 @@
652650
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
653651
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
654652
"\n",
655-
" # Comment below line if you want to do CPU inference\n",
656-
" image_tensor = image_tensor.cuda()\n",
653+
" if torch.cuda.is_available():\n",
654+
" image_tensor = image_tensor.cuda()\n",
657655
"\n",
658656
" model = context.models.get() # get a TorchScriptModel object\n",
659-
" # Uncomment the following line if you want to do CPU inference\n",
660-
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
661657
"\n",
662658
" with torch.no_grad():\n",
663659
" outputs = model(image_tensor)\n",

notebooks/tutorials/02_mednist_app.ipynb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,10 @@
510510
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
511511
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
512512
"\n",
513-
" # Comment below line if you want to do CPU inference\n",
514-
" image_tensor = image_tensor.cuda()\n",
513+
" if torch.cuda.is_available():\n",
514+
" image_tensor = image_tensor.cuda()\n",
515515
"\n",
516516
" model = context.models.get() # get a TorchScriptModel object\n",
517-
" # Uncomment the following line if you want to do CPU inference\n",
518-
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
519517
"\n",
520518
" with torch.no_grad():\n",
521519
" outputs = model(image_tensor)\n",
@@ -748,12 +746,10 @@
748746
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
749747
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
750748
"\n",
751-
" # Comment below line if you want to do CPU inference\n",
752-
" image_tensor = image_tensor.cuda()\n",
749+
" if torch.cuda.is_available():\n",
750+
" image_tensor = image_tensor.cuda()\n",
753751
"\n",
754752
" model = context.models.get() # get a TorchScriptModel object\n",
755-
" # Uncomment the following line if you want to do CPU inference\n",
756-
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
757753
"\n",
758754
" with torch.no_grad():\n",
759755
" outputs = model(image_tensor)\n",

0 commit comments

Comments
 (0)