Skip to content

enable AMD GPU #406

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 5 commits into from
Apr 6, 2023
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
8 changes: 8 additions & 0 deletions docs/source/getting_started/tutorials/mednist_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ monai-deploy package examples/apps/mednist_classifier_monaideploy/mednist_classi
--model classifier.zip \
-l DEBUG

# For AMD GPUs, nvidia-docker is not required. Use --base [base image] option to override the docker base image.
# Please see https://hub.docker.com/r/rocm/pytorch for rocm/pytorch docker images.
monai-deploy package -b rocm/pytorch:rocm5.4.1_ubuntu20.04_py3.7_pytorch_1.12.1 \
examples/apps/mednist_classifier_monaideploy/mednist_classifier_monaideploy.py \
--tag mednist_app:latest \
--model classifier.zip \
-l DEBUG

# Run the app with docker image and input file locally
monai-deploy run mednist_app:latest input output
cat output/output.json
Expand Down
4 changes: 4 additions & 0 deletions docs/source/getting_started/tutorials/monai_bundle_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ monai-deploy exec ../examples/apps/ai_spleen_seg_app/app.py -i dcm/ -o output -m
# Please see https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker to install nvidia-docker2.
monai-deploy package examples/apps/ai_spleen_seg_app --tag seg_app:latest --model model.ts -l DEBUG

# For AMD GPUs, nvidia-docker is not required. Use --base [base image] option to override the docker base image.
# Please see https://hub.docker.com/r/rocm/pytorch for rocm/pytorch docker images.
monai-deploy package -b rocm/pytorch:rocm5.4.1_ubuntu20.04_py3.7_pytorch_1.12.1 examples/apps/ai_spleen_seg_app --tag seg_app:latest --model model.ts -l DEBUG

# Run the app with docker image and input file locally
monai-deploy run seg_app:latest dcm/ output
```
4 changes: 4 additions & 0 deletions docs/source/getting_started/tutorials/multi_model_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ monai-deploy exec ../examples/apps/examples/apps/ai_multi_ai_app/app.py -i dcm/
# Please see https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker to install nvidia-docker2.
monai-deploy package -b nvcr.io/nvidia/pytorch:22.08-py3 examples/apps/ai_multi_ai_app --tag multi_model_app:latest --model multi_models -l DEBUG

# For AMD GPUs, nvidia-docker is not required. Use --base [base image] option to override the docker base image.
# Please see https://hub.docker.com/r/rocm/pytorch for rocm/pytorch docker images.
monai-deploy package -b rocm/pytorch:rocm5.4.1_ubuntu20.04_py3.7_pytorch_1.12.1 examples/apps/ai_multi_ai_app --tag multi_model_app:latest --model multi_models -l DEBUG

# Run the app with docker image and input file locally
monai-deploy run multi_model_app:latest dcm/ output
```
4 changes: 4 additions & 0 deletions docs/source/getting_started/tutorials/segmentation_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ python examples/apps/ai_spleen_seg_app/app.py -i dcm/ -o output -m model.ts
# Please see https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker to install nvidia-docker2.
monai-deploy package examples/apps/ai_spleen_seg_app --tag seg_app:latest --model model.ts -l DEBUG

# For AMD GPUs, nvidia-docker is not required. Use --base [base image] option to override the docker base image.
# Please see https://hub.docker.com/r/rocm/pytorch for rocm/pytorch docker images.
monai-deploy package -b rocm/pytorch:rocm5.4.1_ubuntu20.04_py3.7_pytorch_1.12.1 examples/apps/ai_spleen_seg_app --tag seg_app:latest --model model.ts -l DEBUG

# Run the app with docker image and input file locally
monai-deploy run seg_app:latest dcm/ output
```
9 changes: 7 additions & 2 deletions monai/deploy/packager/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def verify_base_image(base_image: str) -> str:
str: returns string identifier of the dockerfile template to build MAP
if valid base image provided, returns empty string otherwise
"""
valid_prefixes = {"nvcr.io/nvidia/cuda": "ubuntu", "nvcr.io/nvidia/pytorch": "pytorch"}
if "rocm" in base_image:
valid_prefixes = {"rocm/pytorch": "ubuntu"}
else:
valid_prefixes = {"nvcr.io/nvidia/cuda": "ubuntu", "nvcr.io/nvidia/pytorch": "pytorch"}

for prefix, template in valid_prefixes.items():
if prefix in base_image:
Expand Down Expand Up @@ -91,10 +94,12 @@ def initialize_args(args: Namespace) -> Dict:
if not dockerfile_type:
logger.error(
"Provided base image '{}' is not supported \n \
Please provide a Cuda or Pytorch image from https://ngc.nvidia.com/ (nvcr.io/nvidia)".format(
Please provide a ROCm or Cuda based Pytorch image from \n \
https://hub.docker.com/r/rocm/pytorch or https://ngc.nvidia.com/ (nvcr.io/nvidia)".format(
args.base
)
)

sys.exit(1)

processed_args["dockerfile_type"] = dockerfile_type if args.base else DefaultValues.DOCKERFILE_TYPE
Expand Down
17 changes: 10 additions & 7 deletions monai/deploy/runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Tuple

from monai.deploy.runner.utils import get_requested_gpus, run_cmd, verify_image
from monai.deploy.utils.deviceutil import has_rocm

logger = logging.getLogger("app_runner")

Expand Down Expand Up @@ -87,7 +88,8 @@ def run_app(map_name: str, input_path: Path, output_path: Path, app_info: dict,
# Use nvidia-docker if GPU resources are requested
requested_gpus = get_requested_gpus(pkg_info)
if requested_gpus > 0:
cmd = "nvidia-docker run --rm -a STDERR"
if not has_rocm():
cmd = "nvidia-docker run --rm -a STDERR"

if not quiet:
cmd += " -a STDOUT"
Expand Down Expand Up @@ -160,12 +162,13 @@ def pkg_specific_dependency_verification(pkg_info: dict) -> bool:
"""
requested_gpus = get_requested_gpus(pkg_info)
if requested_gpus > 0:
# check for nvidia-docker
prog = "nvidia-docker"
logger.info('--> Verifying if "%s" is installed...\n', prog)
if not shutil.which(prog):
logger.error('ERROR: "%s" not installed, please install nvidia-docker.', prog)
return False
if not has_rocm():
# check for nvidia-docker
prog = "nvidia-docker"
logger.info('--> Verifying if "%s" is installed...\n', prog)
if not shutil.which(prog):
logger.error('ERROR: "%s" not installed, please install nvidia-docker.', prog)
return False

return True

Expand Down
36 changes: 36 additions & 0 deletions monai/deploy/utils/deviceutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import subprocess


def has_rocm():
"""Return True if ROCm is installed and GPU device is detected.

Args:

Returns:
True if ROCm is installed and GPU device is detected, otherwise False.
"""
cmd = "rocminfo"
try:
process = subprocess.run([cmd], stdout=subprocess.PIPE)
for line_in in process.stdout.decode().splitlines():
if "Device Type" in line_in and "GPU" in line_in:
return True
except Exception:
pass

return False


if __name__ == "__main__":
print(has_rocm())