@@ -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
0 commit comments