@@ -453,6 +453,71 @@ def test_init_backend_config_uses_dataset_train_batch_size(self):
453453 self .assertEqual (result ["config" ]["train_batch_size" ], 2 )
454454 self .assertEqual (result ["bucket_report" ].constraints ["train_batch_size" ], 2 )
455455
456+ def test_init_backend_config_rejects_unsafe_dataset_train_batch_size_on_mps (self ):
457+ from simpletuner .helpers .data_backend .factory import init_backend_config
458+
459+ self .args .train_batch_size = 1
460+ training_dataset_types = (
461+ DatasetType .IMAGE ,
462+ DatasetType .VIDEO ,
463+ DatasetType .AUDIO ,
464+ DatasetType .CONDITIONING ,
465+ DatasetType .CAPTION ,
466+ DatasetType .GROUNDING ,
467+ )
468+
469+ with patch (
470+ "simpletuner.helpers.configuration.platform_validation.torch.backends.mps.is_available" , return_value = True
471+ ):
472+ for dataset_type in training_dataset_types :
473+ with self .subTest (dataset_type = dataset_type .value ):
474+ backend = {
475+ "id" : f"{ dataset_type .value } -unsafe-batch" ,
476+ "type" : "local" ,
477+ "dataset_type" : dataset_type .value ,
478+ "train_batch_size" : 17 ,
479+ "instance_data_dir" : self .temp_dir ,
480+ }
481+
482+ with self .assertRaisesRegex (ValueError , "Please reduce the batch size to 12 or lower" ):
483+ init_backend_config (backend , self .args , self .accelerator )
484+
485+ def test_init_backend_config_allows_train_batch_size_at_mps_limit (self ):
486+ from simpletuner .helpers .data_backend .factory import init_backend_config
487+
488+ backend = {
489+ "id" : "image-safe-mps-batch" ,
490+ "type" : "local" ,
491+ "dataset_type" : "image" ,
492+ "train_batch_size" : 16 ,
493+ "instance_data_dir" : self .temp_dir ,
494+ }
495+
496+ with patch (
497+ "simpletuner.helpers.configuration.platform_validation.torch.backends.mps.is_available" , return_value = True
498+ ):
499+ result = init_backend_config (backend , self .args , self .accelerator )
500+
501+ self .assertEqual (result ["config" ]["train_batch_size" ], 16 )
502+
503+ def test_init_backend_config_allows_large_train_batch_size_without_mps (self ):
504+ from simpletuner .helpers .data_backend .factory import init_backend_config
505+
506+ backend = {
507+ "id" : "image-non-mps-batch" ,
508+ "type" : "local" ,
509+ "dataset_type" : "image" ,
510+ "train_batch_size" : 17 ,
511+ "instance_data_dir" : self .temp_dir ,
512+ }
513+
514+ with patch (
515+ "simpletuner.helpers.configuration.platform_validation.torch.backends.mps.is_available" , return_value = False
516+ ):
517+ result = init_backend_config (backend , self .args , self .accelerator )
518+
519+ self .assertEqual (result ["config" ]["train_batch_size" ], 17 )
520+
456521 def test_eval_backend_config_forces_train_batch_size_one (self ):
457522 from simpletuner .helpers .data_backend .factory import init_backend_config
458523
@@ -461,11 +526,14 @@ def test_eval_backend_config_forces_train_batch_size_one(self):
461526 "id" : "eval-custom-batch" ,
462527 "type" : "local" ,
463528 "dataset_type" : "eval" ,
464- "train_batch_size" : 3 ,
529+ "train_batch_size" : 17 ,
465530 "instance_data_dir" : self .temp_dir ,
466531 }
467532
468- result = init_backend_config (backend , self .args , self .accelerator )
533+ with patch (
534+ "simpletuner.helpers.configuration.platform_validation.torch.backends.mps.is_available" , return_value = True
535+ ):
536+ result = init_backend_config (backend , self .args , self .accelerator )
469537
470538 self .assertEqual (result ["config" ]["train_batch_size" ], 1 )
471539
0 commit comments