@@ -834,3 +834,100 @@ def test_returns_linen_train_state_and_annotations(self):
834834
835835if __name__ == "__main__" :
836836 unittest .main ()
837+
838+
839+ class TestFromPretrainedAuth (unittest .TestCase ):
840+ """Tests for Hugging Face authentication in from_pretrained."""
841+
842+ def _make_nnx_metadata_mock (self ):
843+ meta = MagicMock ()
844+ meta .item_metadata .tree .keys .return_value = ["decoder" ]
845+ meta .item_metadata .tree .get .return_value = {}
846+ return meta
847+
848+ @patch ("maxtext.utils.model_creation_utils.ocp" )
849+ @patch ("maxtext.utils.model_creation_utils.subprocess.run" )
850+ @patch ("maxtext.utils.model_creation_utils.get_token" )
851+ @patch ("maxtext.utils.model_creation_utils.epath.Path" )
852+ def test_auth_success_with_config_token (self , mock_path , mock_get_token , mock_run , mock_ocp ):
853+ config = _make_config (
854+ convert_checkpoint_if_possible = True ,
855+ base_output_directory = "gs://fake_bucket/fake_run" ,
856+ hf_access_token = "config_token" ,
857+ )
858+ mesh = _make_mesh (config )
859+
860+ mock_ckpt_path = MagicMock ()
861+ mock_ckpt_path .exists .return_value = False
862+ mock_path .return_value .__truediv__ .return_value .__truediv__ .return_value = mock_ckpt_path
863+
864+ mock_ckptr = MagicMock ()
865+ mock_ckptr .metadata .return_value = self ._make_nnx_metadata_mock ()
866+ mock_ckptr .restore .side_effect = lambda path , item = None , ** kw : item
867+ mock_ocp .Checkpointer .return_value = mock_ckptr
868+ mock_ocp .checkpoint_utils .construct_restore_args .return_value = {}
869+ mock_ocp .ArrayRestoreArgs = ocp .ArrayRestoreArgs
870+
871+ model = model_creation_utils .from_pretrained (config , mesh )
872+ self .assertIsInstance (model , models .Transformer )
873+
874+ mock_run .assert_called_once ()
875+ called_env = mock_run .call_args [1 ].get ("env" , {})
876+ self .assertEqual (called_env .get ("HF_TOKEN" ), "config_token" )
877+ mock_get_token .assert_not_called ()
878+
879+ @patch ("maxtext.utils.model_creation_utils.ocp" )
880+ @patch ("maxtext.utils.model_creation_utils.subprocess.run" )
881+ @patch ("maxtext.utils.model_creation_utils.get_token" )
882+ @patch ("maxtext.utils.model_creation_utils.epath.Path" )
883+ def test_auth_success_with_cached_token (self , mock_path , mock_get_token , mock_run , mock_ocp ):
884+ config = _make_config (
885+ convert_checkpoint_if_possible = True ,
886+ base_output_directory = "gs://fake_bucket/fake_run" ,
887+ hf_access_token = "" ,
888+ )
889+ mesh = _make_mesh (config )
890+
891+ mock_ckpt_path = MagicMock ()
892+ mock_ckpt_path .exists .return_value = False
893+ mock_path .return_value .__truediv__ .return_value .__truediv__ .return_value = mock_ckpt_path
894+
895+ mock_get_token .return_value = "cached_token"
896+
897+ mock_ckptr = MagicMock ()
898+ mock_ckptr .metadata .return_value = self ._make_nnx_metadata_mock ()
899+ mock_ckptr .restore .side_effect = lambda path , item = None , ** kw : item
900+ mock_ocp .Checkpointer .return_value = mock_ckptr
901+ mock_ocp .checkpoint_utils .construct_restore_args .return_value = {}
902+ mock_ocp .ArrayRestoreArgs = ocp .ArrayRestoreArgs
903+
904+ model = model_creation_utils .from_pretrained (config , mesh )
905+ self .assertIsInstance (model , models .Transformer )
906+
907+ mock_get_token .assert_called_once ()
908+ mock_run .assert_called_once ()
909+ called_env = mock_run .call_args [1 ].get ("env" , {})
910+ self .assertEqual (called_env .get ("HF_TOKEN" ), "cached_token" )
911+
912+ @patch ("maxtext.utils.model_creation_utils.ocp" )
913+ @patch ("maxtext.utils.model_creation_utils.subprocess.run" )
914+ @patch ("maxtext.utils.model_creation_utils.get_token" )
915+ @patch ("maxtext.utils.model_creation_utils.epath.Path" )
916+ def test_auth_failure_no_token (self , mock_path , mock_get_token , mock_run , mock_ocp ):
917+ config = _make_config (
918+ convert_checkpoint_if_possible = True ,
919+ base_output_directory = "gs://fake_bucket/fake_run" ,
920+ hf_access_token = "" ,
921+ )
922+ mesh = _make_mesh (config )
923+
924+ mock_ckpt_path = MagicMock ()
925+ mock_ckpt_path .exists .return_value = False
926+ mock_path .return_value .__truediv__ .return_value .__truediv__ .return_value = mock_ckpt_path
927+
928+ mock_get_token .return_value = None
929+
930+ with self .assertRaisesRegex (ValueError , "hf_access_token must be provided" ):
931+ model_creation_utils .from_pretrained (config , mesh )
932+
933+ mock_run .assert_not_called ()
0 commit comments