[BUG: Tensor Parallel Not Effect]
Describe the bug I use deepspeed to inference resnet50 model, I use tp_size=4 to get tensor parallel. however, I found gpu memory don't become smaller than tp_size=1.
To Reproduce run the code below, need 4 gpu:
import torch
import deepspeed
import torch
import torchvision
import pytest
import os
from transformers import ResNetModel
from transformers.models.resnet.modeling_resnet import ResNetBasicLayer,ResNetBottleNeckLayer,ResNetConvLayer
def get_torch_model():
model = ResNetModel.from_pretrained("microsoft/resnet-50")
return model
def get_deepspeed_model():
model = get_torch_model()
world_size = int(os.getenv('WORLD_SIZE', '1'))
ds_model = deepspeed.init_inference(
model = model,
dtype=torch.float32,
tensor_parallel = {
"enabled":True,
"tp_size":world_size,
},
injection_policy={
ResNetBasicLayer:('output.dense'),
}
)
return ds_model
@pytest.mark.parametrize("batch",[256,128,100,64,32,16,8,4,2])
def test_deepspeed_torch_benchmark(benchmark,batch):
model = get_deepspeed_model()
def run_model(model,x):
y = model(x)
return y
x = torch.rand(batch,3,224,224,dtype=torch.float32)
x = x.to('cuda')
benchmark(run_model,model,x)
if __name__ == "__main__":
pytest.main([__file__,"--benchmark-warmup=on","--benchmark-warmup-iterations=300","--benchmark-min-rounds=200","--benchmark-sort=name","--benchmark-disable-gc"])
Expected behavior I expect the model's parametes can be cutted into 4 part, and each in a single gpu.
ds_report output
[2023-08-17 13:12:54,769] [INFO] [real_accelerator.py:133:get_accelerator] Setting ds_accelerator to cuda (auto detect)
--------------------------------------------------
DeepSpeed C++/CUDA extension op report
--------------------------------------------------
NOTE: Ops not installed will be just-in-time (JIT) compiled at
runtime if needed. Op compatibility means that your system
meet the required dependencies to JIT install the op.
--------------------------------------------------
JIT compiled ops requires ninja
ninja .................. [OKAY]
--------------------------------------------------
op name ................ installed .. compatible
--------------------------------------------------
[WARNING] async_io requires the dev libaio .so object and headers but these were not found.
[WARNING] async_io: please install the libaio-dev package with apt
[WARNING] If libaio is already installed (perhaps from source), try setting the CFLAGS and LDFLAGS environment variables to where it can be found.
async_io ............... [NO] ....... [NO]
cpu_adagrad ............ [NO] ....... [OKAY]
cpu_adam ............... [NO] ....... [OKAY]
fused_adam ............. [NO] ....... [OKAY]
fused_lamb ............. [NO] ....... [OKAY]
quantizer .............. [NO] ....... [OKAY]
random_ltd ............. [NO] ....... [OKAY]
[WARNING] please install triton==1.0.0 if you want to use sparse attention
sparse_attn ............ [NO] ....... [NO]
spatial_inference ...... [NO] ....... [OKAY]
transformer ............ [NO] ....... [OKAY]
stochastic_transformer . [NO] ....... [OKAY]
transformer_inference .. [NO] ....... [OKAY]
--------------------------------------------------
DeepSpeed general environment info:
torch install path ............... ['/home/max.qin/dep_install/miniconda/miniconda/envs/deepspeed/lib/python3.9/site-packages/torch']
torch version .................... 1.9.0+cu111
deepspeed install path ........... ['/home/max.qin/dep_install/miniconda/miniconda/envs/deepspeed/lib/python3.9/site-packages/deepspeed']
deepspeed info ................... 0.10.0, unknown, unknown
torch cuda version ............... 11.1
torch hip version ................ None
nvcc version ..................... 11.4
deepspeed wheel compiled w. ...... torch 1.9, cuda 11.1
Screenshots No Screenshots
System info (please complete the following information):
- OS: Ubuntu 20.04 (docker)
- GPU count and types: one machine with 4xA100
- Interconnects: Unknown
- Python version: python3.9
- Any other relevant info about your setup: None
Launcher context deepspeed
Docker context Sorry, I can't share
Additional context I can't find an example about resnet50 inference with deepspeed, can you help me ?
Hi @onedayherocoming I believe you are seeing this because of the line where you load the model:
model = ResNetModel.from_pretrained("microsoft/resnet-50")
Assuming you are running the code on 4 GPUs, this will load the model 4 times (once on each GPU). The model will not be sharded across GPUs until after you call deepspeed.init_inference.
Try updating your get_deepspeed_model() function to the following to see this in action:
def get_deepspeed_model():
from deepspeed.runtime.utils import see_memory_usage
model = get_torch_model()
world_size = int(os.getenv('WORLD_SIZE', '1'))
see_memory_usage("Before DS-inference", force=True)
ds_model = deepspeed.init_inference(
model = model,
dtype=torch.float32,
tensor_parallel = {
"enabled":True,
"tp_size":world_size,
},
injection_policy={
ResNetBasicLayer:('output.dense'),
}
)
see_memory_usage("After DS-inference", force=True)
return ds_model
@mrwyattii Thanks your reply. I notice that model = ResNetModel.from_pretrained("microsoft/resnet-50") didn't cost gpu memory.
Now I hava some question:
- Does DeepSpeed support Resnet50 to Inference with tensor parallelism? If support, how should I pass
injection_policycorrectly? - How data parallelism happed in Inference? Can you show me an detail example? It's very important to me.
- Does DeepSpeed support pipeline parallelism in Inference?
If you could answer my question, I would greatly appreciate it!
- DeepSpeed does not support Resnet50 by default - you will need to pass an
injection_policy, but I'm not certain what the correct one would be. The one you provided looks correct. I do wonder - why do you want to run Resnet50 across several GPUs? The model is very small and the communication overhead will likely make it slower than running on a single GPU. - DeepSpeed-Inference does not do data parallelism. We do tensor parallelism (i.e., tensors are sharded across GPUs). Huggingface Docs has a great description of the different types of parallelism: https://huggingface.co/docs/transformers/v4.31.0/en/perf_train_gpu_many#tensor-parallelism. If you want to do data parallelism, you would need to spin up several processes (one for each GPU) and have a model loaded onto each process. Then split your dataset across those processes and run inference!
- We don't support pipeline parallelism with Inference. Only tensor parallelism.
Got it! Thank you so much!