@@ -606,11 +606,16 @@ def _parse_devices(self):
606606 if devices is None :
607607 if torch .cuda .is_available ():
608608 self .device = torch .device ('cuda:0' )
609+ elif hasattr (torch .backends , 'mps' ) and torch .backends .mps .is_available ():
610+ self .device = torch .device ('mps' )
609611 else :
610612 self .device = torch .device ('cpu' )
611613 else :
612614 devices = devices if isinstance (devices , list ) else [devices ]
613- available_devices = [f'cuda:{ i } ' for i in range (torch .cuda .device_count ())] + ['cpu' ]
615+ available_devices = [f'cuda:{ i } ' for i in range (torch .cuda .device_count ())]
616+ if hasattr (torch .backends , 'mps' ) and torch .backends .mps .is_available ():
617+ available_devices .append ('mps' )
618+ available_devices .append ('cpu' )
614619 for dev in devices :
615620 if isinstance (dev , torch .device ):
616621 self .devices .append (dev )
@@ -628,10 +633,11 @@ def _parse_devices(self):
628633 if device not in self .devices [:i ]]
629634 self .device = self .devices [0 ]
630635
631- if self .device .type == 'cpu ' :
636+ if self .device .type != 'cuda ' :
632637 #TODO: maybe, we should add warning
633638 self .amp = False
634- torch .backends .cudnn .benchmark = config .get ('benchmark' , 'cuda' in self .device .type )
639+ if torch .cuda .is_available ():
640+ torch .backends .cudnn .benchmark = config .get ('benchmark' , self .device .type == 'cuda' )
635641
636642 def _parse_placeholder_shapes (self ):
637643 """ Extract `inputs_shapes`, `targets_shapes`, `classes` from config. """
@@ -685,7 +691,7 @@ def make_infrastructure(self):
685691 self .make_loss ()
686692 self .make_optimizer ()
687693 self .make_decay ()
688- self .scaler = torch .GradScaler ("cuda" )
694+ self .scaler = torch .GradScaler (self . device . type )
689695
690696 self .setup_gradient_clipping ()
691697 self .setup_weights_averaging ()
@@ -884,7 +890,7 @@ def finalize_wa(self):
884890 self .model_to_device ()
885891
886892 self .make_optimizer ()
887- self .scaler = torch .cuda . amp . GradScaler ()
893+ self .scaler = torch .GradScaler (self . device . type )
888894
889895 self .wa_finalized = True
890896
@@ -1215,15 +1221,15 @@ def _train(self, inputs, targets, outputs_dict, sync_frequency, sam_rho, sam_ind
12151221 targets = self .transfer_to_device (targets , non_blocking = True )
12161222
12171223 # Compute predictions; store shapes for introspection
1218- with torch .amp .autocast ('cuda' , enabled = self .amp ):
1224+ with torch .amp .autocast (self . device . type , enabled = self .amp ):
12191225 predictions = self .model (inputs )
12201226
12211227 # SAM: store grads from previous microbatches
12221228 if self .iteration >= 1 and bool (sam_rho ):
12231229 self ._train_sam_store_gradients ()
12241230
12251231 # Compute loss and gradients; store loss value for every microbatch
1226- with torch .amp .autocast ('cuda' , enabled = self .amp ):
1232+ with torch .amp .autocast (self . device . type , enabled = self .amp ):
12271233 loss = self .loss (predictions , targets )
12281234 loss_ = loss / sync_frequency
12291235
@@ -1314,7 +1320,7 @@ def _train_sam_update_gradients(self, inputs, targets, sync_frequency, sam_rho,
13141320 params_with_grads = [p + eps for p , eps in zip (params_with_grads , epsilons )]
13151321
13161322 # Compute new gradients: direction to move to minimize the local maxima
1317- with torch .amp .autocast ('cuda' , enabled = self .amp ):
1323+ with torch .amp .autocast (self . device . type , enabled = self .amp ):
13181324 predictions_inner = self .model (inputs )
13191325 loss_inner = self .loss (predictions_inner , targets ) / sync_frequency
13201326 (self .scaler .scale (loss_inner ) if self .amp else loss_inner ).backward ()
@@ -1470,7 +1476,7 @@ def _predict(self, inputs, targets, outputs_dict, amp, no_grad, transfer_from_de
14701476 inputs = inputs [0 ] if len (inputs ) == 1 and isinstance (inputs , list ) else inputs
14711477 targets = targets [0 ] if len (targets ) == 1 and isinstance (targets , list ) else targets
14721478
1473- with (torch .no_grad () if no_grad else nullcontext ()), torch .amp .autocast ('cuda' , enabled = amp ):
1479+ with (torch .no_grad () if no_grad else nullcontext ()), torch .amp .autocast (self . device . type , enabled = amp ):
14741480 inputs = self .transfer_to_device (inputs )
14751481 predictions = self .model (inputs )
14761482
0 commit comments