Skip to content

Force to remap to CPU for TorchScript model if GPUdoesn't exist #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
image_tensor = self.transform(img) # (1, 64, 64), torch.float64
image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32

# Comment below line if you want to do CPU inference
image_tensor = image_tensor.cuda()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
image_tensor = image_tensor.to(device)

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

with torch.no_grad():
outputs = model(image_tensor)
Expand Down
4 changes: 3 additions & 1 deletion monai/deploy/core/models/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def predictor(self) -> "torch.nn.Module": # type: ignore
torch.nn.Module: the model's predictor
"""
if self._predictor is None:
self._predictor = torch.jit.load(self.path).eval()
# Use a device to dynamically remap, depending on the GPU availability.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self._predictor = torch.jit.load(self.path, map_location=device).eval()
return self._predictor

@predictor.setter
Expand Down
4 changes: 2 additions & 2 deletions monai/deploy/operators/monai_seg_inference_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
pre_transforms = self._pre_transform if self._pre_transform else self.pre_process(self._reader)
post_transforms = self._post_transforms if self._post_transforms else self.post_process(pre_transforms)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = None
if context.models:
# `context.models.get(model_name)` returns a model instance if exists.
# If model_name is not specified and only one model exists, it returns that model.
model = context.models.get()
else:
print(f"Loading TorchScript model from: {MonaiSegInferenceOperator.MODEL_LOCAL_PATH}")
model = torch.jit.load(MonaiSegInferenceOperator.MODEL_LOCAL_PATH)
model = torch.jit.load(MonaiSegInferenceOperator.MODEL_LOCAL_PATH, map_location=device)

dataset = Dataset(data=[{self._input_dataset_key: img_name}], transform=pre_transforms)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

with torch.no_grad():
for d in dataloader:
Expand Down
12 changes: 4 additions & 8 deletions notebooks/tutorials/02_mednist_app-prebuilt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,10 @@
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
"\n",
" # Comment below line if you want to do CPU inference\n",
" image_tensor = image_tensor.cuda()\n",
" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
" image_tensor = image_tensor.to(device)\n",
"\n",
" model = context.models.get() # get a TorchScriptModel object\n",
" # Uncomment the following line if you want to do CPU inference\n",
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(image_tensor)\n",
Expand Down Expand Up @@ -652,12 +650,10 @@
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
"\n",
" # Comment below line if you want to do CPU inference\n",
" image_tensor = image_tensor.cuda()\n",
" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
" image_tensor = image_tensor.to(device)\n",
"\n",
" model = context.models.get() # get a TorchScriptModel object\n",
" # Uncomment the following line if you want to do CPU inference\n",
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(image_tensor)\n",
Expand Down
12 changes: 4 additions & 8 deletions notebooks/tutorials/02_mednist_app.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,10 @@
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
"\n",
" # Comment below line if you want to do CPU inference\n",
" image_tensor = image_tensor.cuda()\n",
" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
" image_tensor = image_tensor.to(device)\n",
"\n",
" model = context.models.get() # get a TorchScriptModel object\n",
" # Uncomment the following line if you want to do CPU inference\n",
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(image_tensor)\n",
Expand Down Expand Up @@ -748,12 +746,10 @@
" image_tensor = self.transform(img) # (1, 64, 64), torch.float64\n",
" image_tensor = image_tensor[None].float() # (1, 1, 64, 64), torch.float32\n",
"\n",
" # Comment below line if you want to do CPU inference\n",
" image_tensor = image_tensor.cuda()\n",
" device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
" image_tensor = image_tensor.to(device)\n",
"\n",
" model = context.models.get() # get a TorchScriptModel object\n",
" # Uncomment the following line if you want to do CPU inference\n",
" # model.predictor = torch.jit.load(model.path, map_location=\"cpu\").eval()\n",
"\n",
" with torch.no_grad():\n",
" outputs = model(image_tensor)\n",
Expand Down