3434 get_launch_prefix ,
3535 is_deepspeed_available ,
3636 is_sagemaker_available ,
37+ patch_environment ,
3738)
3839from accelerate .utils .constants import DEEPSPEED_MULTINODE_LAUNCHERS
3940from accelerate .utils .dataclasses import SageMakerDistributedType
@@ -217,6 +218,11 @@ def launch_command_parser(subparsers=None):
217218 default = None ,
218219 help = "The name of the main function to be executed in your script (only for TPU training)." ,
219220 )
221+ parser .add_argument (
222+ "--downcast_bf16" ,
223+ action = "store_true" ,
224+ help = "Whether when using bf16 precision on TPUs if both float and double tensors are cast to bfloat16 or if double tensors remain as float32" ,
225+ )
220226 parser .add_argument (
221227 "-m" ,
222228 "--module" ,
@@ -455,9 +461,19 @@ def deepspeed_launcher(args):
455461def tpu_launcher (args ):
456462 import torch_xla .distributed .xla_multiprocessing as xmp
457463
464+ current_env = {}
465+
458466 if args .no_python :
459467 raise ValueError ("--no_python cannot be used with TPU launcher" )
460468
469+ if args .mixed_precision == "bf16" :
470+ if args .downcast_bf16 :
471+ current_env ["XLA_USE_BF16" ] = "0"
472+ current_env ["XLA_DOWNCAST_BF16" ] = "1"
473+ else :
474+ current_env ["XLA_USE_BF16" ] = "1"
475+ current_env ["XLA_DOWNCAST_BF16" ] = "0"
476+
461477 if args .module :
462478 mod_name = args .training_script
463479 else :
@@ -477,7 +493,8 @@ def tpu_launcher(args):
477493 sys .argv = [mod .__file__ ] + args .training_script_args
478494
479495 main_function = getattr (mod , args .main_training_function )
480- xmp .spawn (PrepareForLaunch (main_function ), args = (), nprocs = args .num_processes )
496+ with patch_environment (** current_env ):
497+ xmp .spawn (PrepareForLaunch (main_function ), args = (), nprocs = args .num_processes )
481498
482499
483500def _convert_nargs_to_dict (nargs : List [str ]) -> Dict [str , str ]:
0 commit comments