Skip to content

Commit cf638be

Browse files
authored
only override forward if using cuda-graph (deepspeedai#2291)
1 parent 95d1151 commit cf638be

3 files changed

Lines changed: 16 additions & 50 deletions

File tree

.github/workflows/nv-inference.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ jobs:
4040
run: |
4141
git clone https://github.com/huggingface/transformers
4242
cd transformers
43-
# if needed switch to the last known good SHA until transformers@master is fixed
44-
git checkout v4.21.2
4543
git rev-parse --short HEAD
4644
pip uninstall --yes transformers
4745
pip install .

deepspeed/inference/engine.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ def __init__(self,
162162
torch.cuda.set_rng_state(_rng_state.cpu())
163163

164164
if self.mp_world_size > 1:
165-
self.model_orig_fwd = self.module.forward
166-
self.module.forward = self.forward
167-
else:
168-
self.module.register_forward_pre_hook(self._pre_forward_hook)
165+
assert not self.enable_cuda_graph, "Cuda graph is not supported for model parallelism"
169166

170167
def _get_model_config_generate(self, config):
171168
self.config = getattr(self.module, 'config', None) if config is None else config
@@ -475,14 +472,6 @@ def _convert_to_dtype(self):
475472
elif self.dtype == torch.float:
476473
self.module.float()
477474

478-
def _pre_forward_hook(self, module, *inputs, **kwargs):
479-
for input in inputs:
480-
if torch.is_tensor(input):
481-
input = input.to(torch.cuda.current_device())
482-
for k in kwargs:
483-
if torch.is_tensor(kwargs[k]):
484-
kwargs[k] = kwargs[k].to(torch.cuda.current_device())
485-
486475
def _create_cuda_graph(self, *inputs, **kwargs):
487476
# warmup to create the workspace and cublas handle
488477
cuda_stream = torch.cuda.Stream()
@@ -519,30 +508,13 @@ def forward(self, *inputs, **kwargs):
519508
*inputs: Variable length input list
520509
**kwargs: variable length keyword arguments
521510
"""
522-
523-
if self.mp_world_size > 1:
524-
if self.mpu is None:
525-
for input in inputs:
526-
if torch.is_tensor(input):
527-
input = input.to(torch.cuda.current_device())
528-
if not input.is_contiguous():
529-
input = input.contiguous()
530-
dist.broadcast(input, 0)
531-
for k in kwargs:
532-
if torch.is_tensor(kwargs[k]):
533-
kwargs[k] = kwargs[k].to(torch.cuda.current_device())
534-
if not kwargs[k].is_contiguous():
535-
kwargs[k] = kwargs[k].contiguous()
536-
dist.broadcast(kwargs[k], 0)
537-
outputs = self.model_orig_fwd(*inputs, **kwargs)
538-
else:
539-
if self.enable_cuda_graph:
540-
if self.cuda_graph_created:
541-
outputs = self._graph_replay(*inputs, **kwargs)
542-
else:
543-
self._create_cuda_graph(*inputs, **kwargs)
544-
outputs = self._graph_replay(*inputs, **kwargs)
511+
if self.enable_cuda_graph:
512+
if self.cuda_graph_created:
513+
outputs = self._graph_replay(*inputs, **kwargs)
545514
else:
546-
outputs = self.module(*inputs, **kwargs)
547-
#outputs = self.module(*inputs, **kwargs)
515+
self._create_cuda_graph(*inputs, **kwargs)
516+
outputs = self._graph_replay(*inputs, **kwargs)
517+
else:
518+
outputs = self.module(*inputs, **kwargs)
519+
548520
return outputs

tests/unit/inference/test_inference.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ def test(
292292

293293
@pytest.mark.seq_inference
294294
@pytest.mark.parametrize("model_w_task",
295-
[("gpt2",
295+
[("EleutherAI/gpt-neo-1.3B",
296296
"text-generation"),
297297
("EleutherAI/gpt-neox-20b",
298298
"text-generation"),
299299
("bigscience/bloom-3b",
300300
"text-generation")],
301-
ids=["gpt2",
301+
ids=["gpt-neo",
302302
"gpt-neox",
303303
"bloom"])
304304
class TestMPSize(DistributedTest):
@@ -308,7 +308,6 @@ def test(
308308
self,
309309
model_w_task,
310310
dtype,
311-
enable_cuda_graph,
312311
query,
313312
inf_kwargs,
314313
assert_fn,
@@ -325,14 +324,11 @@ def test(
325324
pipe = pipeline(task, model=model, device=-1, framework="pt")
326325
bs_output = pipe(query, **inf_kwargs)
327326

328-
pipe.model = deepspeed.init_inference(
329-
pipe.model,
330-
mp_size=self.world_size,
331-
dtype=dtype,
332-
replace_method="auto",
333-
replace_with_kernel_inject=True,
334-
enable_cuda_graph=enable_cuda_graph,
335-
)
327+
pipe.model = deepspeed.init_inference(pipe.model,
328+
mp_size=self.world_size,
329+
dtype=dtype,
330+
replace_method="auto",
331+
replace_with_kernel_inject=True)
336332
# Switch device to GPU so that input tensors are not on CPU
337333
pipe.device = torch.device(f"cuda:{local_rank}")
338334
ds_output = pipe(query, **inf_kwargs)

0 commit comments

Comments
 (0)