Skip to content
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

Implementation of TRT wrapping via inference.json #620

Merged
merged 38 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
85efcc3
POC implementation of TRT wrapping via inference.json
borisfom Aug 16, 2024
98a615c
Bumped bundle version
borisfom Aug 16, 2024
d3275e9
Adding pathology_nuclei_classification and swin_unetr_btcv_segmentation
borisfom Aug 20, 2024
18c580f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 20, 2024
936103d
Adjusting arguments
borisfom Aug 22, 2024
58d76de
Merge branch 'trt_wrapper' of github.com:borisfom/model-zoo into trt_…
borisfom Aug 22, 2024
9e4bac4
Merge remote-tracking branch 'origin/dev' into trt_wrapper
borisfom Aug 22, 2024
8461033
Added vista3 inference_trt.json
borisfom Aug 22, 2024
8588674
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 22, 2024
f7da0ec
Renamed trt_wrap -> trt_compile
borisfom Aug 23, 2024
c8d64bf
Merge branch 'trt_wrapper' of github.com:borisfom/model-zoo into trt_…
borisfom Aug 23, 2024
aab37c5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2024
7b64083
Added TRT for pathology_nuclei_segmentation_classification
borisfom Aug 26, 2024
43da1f5
Merge branch 'trt_wrapper' of github.com:borisfom/model-zoo into trt_…
borisfom Aug 26, 2024
3d8a738
Merge remote-tracking branch 'origin/dev' into trt_wrapper
borisfom Aug 26, 2024
4a9f2ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 26, 2024
7496dfb
Merge remote-tracking branch 'origin/dev' into trt_wrapper
borisfom Aug 26, 2024
337fcf3
Added Vista2D TRT config
borisfom Aug 26, 2024
16f720f
Cleaned up using new config syntax, batch size added
borisfom Aug 26, 2024
6cc423d
Merge branch 'trt_wrapper' of github.com:borisfom/model-zoo into trt_…
borisfom Aug 26, 2024
a205bf4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 26, 2024
a38f85d
Stash
borisfom Aug 28, 2024
a099cd5
Merge remote-tracking branch 'origin/dev' into trt_wrapper
borisfom Sep 6, 2024
f6507c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 6, 2024
c6744e9
Fixed models/brats_mri_axial_slices_generative_diffusion
borisfom Sep 6, 2024
1b634d2
Merge branch 'trt_wrapper' of github.com:borisfom/model-zoo into trt_…
borisfom Sep 6, 2024
956d931
Merge remote-tracking branch 'origin/dev' into trt_wrapper
borisfom Sep 6, 2024
affbf12
Fixing TRT configs for brats_mri_xx
borisfom Sep 6, 2024
ee06c91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 6, 2024
f0a9329
update metadata
yiheng-wang-nv Sep 9, 2024
bde5d52
update mgpu test script
yiheng-wang-nv Sep 9, 2024
c7c4b50
add benchmark for non generative models
binliunls Sep 9, 2024
626ba3c
add benchmark results for generative bundles
binliunls Sep 9, 2024
7f47fb4
add version for trt benchmark
binliunls Sep 10, 2024
5d58414
update vista2d version
binliunls Sep 10, 2024
19ba83c
update format
binliunls Sep 10, 2024
3e80c93
use nvidia host large files
yiheng-wang-nv Sep 10, 2024
35e697c
update vista2d
yiheng-wang-nv Sep 10, 2024
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 @@ -77,8 +77,7 @@
"load_autoencoder": "$@autoencoder_def.load_old_state_dict(torch.load(@load_autoencoder_path))",
"autoencoder": "$@autoencoder_def.to(@device)",
"load_diffusion_path": "$@model_dir + '/model.pt'",
"load_diffusion": "$@network_def.load_old_state_dict(torch.load(@load_diffusion_path))",
"diffusion": "$@network_def.to(@device)",
"load_diffusion": "$@network_def.load_state_dict(torch.load(@load_diffusion_path))",
"noise_scheduler": {
"_target_": "monai.networks.schedulers.ddim.DDIMScheduler",
"_requires_": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ The following code generates a synthetic image from a random sampled noise.
python -m monai.bundle run --config_file configs/inference.json
```

#### Execute inference with the TensorRT model:

```
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```

# References
[1] Rombach, Robin, et al. "High-resolution image synthesis with latent diffusion models." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2022. https://openaccess.thecvf.com/content/CVPR2022/papers/Rombach_High-Resolution_Image_Synthesis_With_Latent_Diffusion_Models_CVPR_2022_paper.pdf

Expand Down
5 changes: 2 additions & 3 deletions models/brats_mri_generative_diffusion/configs/inference.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@
"num_res_blocks": 2
},
"load_autoencoder_path": "$@bundle_root + '/models/model_autoencoder.pt'",
"load_autoencoder": "$@autoencoder_def.load_state_dict(torch.load(@load_autoencoder_path))",
"load_autoencoder": "$@autoencoder_def.to(@device).load_state_dict(torch.load(@load_autoencoder_path, weights_only=True))",
"autoencoder": "$@autoencoder_def.to(@device)",
"load_diffusion_path": "$@model_dir + '/model.pt'",
"load_diffusion": "$@network_def.load_state_dict(torch.load(@load_diffusion_path))",
"diffusion": "$@network_def.to(@device)",
"load_diffusion": "$@network_def.load_state_dict(torch.load(@load_diffusion_path, weights_only=True))",
"noise_scheduler": {
"_target_": "generative.networks.schedulers.DDIMScheduler",
"_requires_": [
Expand Down
5 changes: 3 additions & 2 deletions models/brats_mri_generative_diffusion/configs/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_generator_ldm_20230507.json",
"version": "1.0.9",
"version": "1.0.10",
"changelog": {
"1.0.10": "update to use monai 1.3.3rc1 + trt_wrap",
"1.0.9": "update to use monai 1.3.1",
"1.0.8": "update run section",
"1.0.7": "update with EnsureChannelFirstd",
Expand All @@ -13,7 +14,7 @@
"1.0.1": "update dependency, update trained model weights",
"1.0.0": "Initial release"
},
"monai_version": "1.3.1",
"monai_version": "1.3.3rc1",
yiheng-wang-nv marked this conversation as resolved.
Show resolved Hide resolved
"pytorch_version": "2.2.2",
"numpy_version": "1.24.4",
"optional_packages_version": {
Expand Down
7 changes: 3 additions & 4 deletions models/brats_mri_generative_diffusion/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,13 @@ The following code generates a synthetic image from a random sampled noise.
python -m monai.bundle run --config_file configs/inference.json
```

#### Export checkpoint to TorchScript file

The Autoencoder can be exported into a TorchScript file.
#### Execute inference with the TensorRT model:

```
python -m monai.bundle ckpt_export autoencoder_def --filepath models/model_autoencoder.ts --ckpt_file models/model_autoencoder.pt --meta_file configs/metadata.json --config_file configs/inference.json
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```


# References
[1] Rombach, Robin, et al. "High-resolution image synthesis with latent diffusion models." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2022. https://openaccess.thecvf.com/content/CVPR2022/papers/Rombach_High-Resolution_Image_Synthesis_With_Latent_Diffusion_Models_CVPR_2022_paper.pdf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"$import os"
],
"bundle_root": ".",
"checkpoint": "$@bundle_root + '/models/model.pt'",
"output_dir": "$@bundle_root + '/eval'",
"dataset_dir": "/workspace/data/CoNSePNuclei",
"images": "$list(sorted(glob.glob(@dataset_dir + '/Test/Images/*.png')))[:1]",
Expand Down Expand Up @@ -88,7 +89,7 @@
"handlers": [
{
"_target_": "CheckpointLoader",
"load_path": "$@bundle_root + '/models/model.pt'",
"load_path": "$@checkpoint",
"load_dict": {
"model": "@network"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
borisfom marked this conversation as resolved.
Show resolved Hide resolved
"imports": [
"$import glob",
"$import os",
"$import pathlib",
"$import json",
"$import torch_tensorrt"
"+imports": [
"$from monai.networks import trt_compile"
],
"handlers#0#_disabled_": true,
"network_def": "$torch.jit.load(@bundle_root + '/models/model_trt.ts')",
"evaluator#amp": false
"network": "$trt_compile(@network_def.to(@device), @checkpoint)"
}
6 changes: 0 additions & 6 deletions models/pathology_nuclei_classification/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,6 @@ torchrun --standalone --nnodes=1 --nproc_per_node=2 -m monai.bundle run --config
python -m monai.bundle run --config_file configs/inference.json
```

#### Export checkpoint to TensorRT based models with fp32 or fp16 precision:

```
python -m monai.bundle trt_export --net_id network_def --filepath models/model_trt.ts --ckpt_file models/model.pt --meta_file configs/metadata.json --config_file configs/inference.json --precision <fp32/fp16>
```

#### Execute inference with the TensorRT model:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@
"progress": true,
"extra_input_padding": "$((@patch_size - @out_size) // 2,) * 4"
},
"sub_keys": [
"horizontal_vertical",
"nucleus_prediction",
"type_prediction"
],
"postprocessing": {
"_target_": "Compose",
"transforms": [
{
"_target_": "FlattenSubKeysd",
"keys": "pred",
"sub_keys": [
"horizontal_vertical",
"nucleus_prediction",
"type_prediction"
],
"sub_keys": "$@sub_keys",
"delete_keys": true
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"+imports": [
"$from monai.networks import trt_compile"
],
"trt_args": {
"output_names": "$@sub_keys",
"dynamic_batchsize": "$[1, @sw_batch_size, @sw_batch_size]"
},
"network": "$trt_compile(@network_def.to(@device), @bundle_root + '/models/model.pt', args=@trt_args)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ python -m monai.bundle run --config_file "['configs/train.json','configs/evaluat
python -m monai.bundle run --config_file configs/inference.json
```

#### Execute inference with the TensorRT model:

```
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```

# References
[1] Simon Graham, Quoc Dang Vu, Shan E Ahmed Raza, Ayesha Azam, Yee Wah Tsang, Jin Tae Kwak, Nasir Rajpoot, Hover-Net: Simultaneous segmentation and classification of nuclei in multi-tissue histology images, Medical Image Analysis, 2019 https://doi.org/10.1016/j.media.2019.101563

Expand Down
3 changes: 2 additions & 1 deletion models/swin_unetr_btcv_segmentation/configs/inference.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"$import os"
],
"bundle_root": ".",
"checkpoint": "$@bundle_root + '/models/model.pt'",
"image_key": "image",
"output_dir": "$@bundle_root + '/eval'",
"output_ext": ".nii.gz",
Expand Down Expand Up @@ -139,7 +140,7 @@
},
"checkpointloader": {
"_target_": "CheckpointLoader",
"load_path": "$@bundle_root + '/models/model.pt'",
"load_path": "$@checkpoint",
"load_dict": {
"model": "@network"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"+imports": [
"$from monai.networks import trt_compile"
],
"trt_args": {
"dynamic_batchsize": "$[1, @inferer#sw_batch_size, @inferer#sw_batch_size]"
},
"network": "$trt_compile(@network_def.to(@device), @checkpoint, args=@trt_args)"
}
7 changes: 5 additions & 2 deletions models/swin_unetr_btcv_segmentation/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ python -m monai.bundle run --config_file "['configs/train.json','configs/evaluat
python -m monai.bundle run --config_file configs/inference.json
```

#### Export checkpoint to TorchScript file:
#### Execute inference with the TensorRT model:

```
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```

TorchScript conversion is currently not supported.

# References
[1] Hatamizadeh, Ali, et al. "Swin UNETR: Swin Transformers for Semantic Segmentation of Brain Tumors in MRI Images." arXiv preprint arXiv:2201.01266 (2022). https://arxiv.org/abs/2201.01266.
Expand Down
2 changes: 1 addition & 1 deletion models/vista2d/configs/inference.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
256,
256
],
"input_dict": "${'image': '/home/venn/Desktop/data/medical/cellpose_dataset/test/001_img.png'}",
"input_dict": "${'image': '/cellpose_dataset/test/001_img.png'}",
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
"sam_ckpt_path": "$@ckpt_dir + '/sam_vit_b_01ec64.pth'",
"pretrained_ckpt_path": "$@ckpt_dir + '/model.pt'",
Expand Down
10 changes: 10 additions & 0 deletions models/vista2d/configs/inference_trt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"imports": [
"$import numpy",
"$from monai.networks import trt_compile"
],
"trt_args": {
"dynamic_batchsize": "$[1, @inferer#sw_batch_size, @inferer#sw_batch_size]"
},
"network": "$trt_compile(@network_def.to(@device), @pretrained_ckpt_path, args=@trt_args)"
}
8 changes: 7 additions & 1 deletion models/vista2d/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ torchrun --nproc_per_node=gpu -m monai.bundle run_workflow "scripts.workflow.Vis
python -m monai.bundle run --config_file configs/inference.json
```

Please note that the data used in the config file is: "/cellpose_dataset/test/001_img.png", if the dataset path is different or you want to do inference on another file, please modify in `configs/inference.json` accordingly.
Please note that the data used in this config file is: "/cellpose_dataset/test/001_img.png", if the dataset path is different or you want to do inference on another file, please modify in `configs/inference.json` accordingly.

#### Execute inference with the TensorRT model:

```
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```

### Execute multi-GPU inference
```bash
Expand Down
9 changes: 9 additions & 0 deletions models/vista3d/configs/inference_trt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"+imports": [
"$from monai.networks import trt_compile"
],
"trt_args": {
"dynamic_batchsize": "$[1, @inferer#sw_batch_size, @inferer#sw_batch_size]"
},
"network": "$trt_compile(@network_def.to(@device), @bundle_root + '/models/model.pt', args=@trt_args, submodule=['image_encoder.encoder', 'class_head'])"
}
7 changes: 7 additions & 0 deletions models/vista3d/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ This default is overridable by changing the input folder `input_dir`, or the inp

Set `"postprocessing#transforms#0#_disabled_": false` to move the postprocessing to cpu to reduce the GPU memory footprint.

#### Execute inference with the TensorRT model:

```
python -m monai.bundle run --config_file "['configs/inference.json', 'configs/inference_trt.json']"
```


## Automatic segmentation label prompts :
The mapping between organ name and label prompt is in the [json file](labels.json)

Expand Down
Loading