예제 #1
0
    def test_init_zero3(self):
        # test that zero.Init() works correctly under zero3
        ds_config = {
            "train_batch_size": 1,
            "zero_optimization": {
                "stage": 3,
            },
        }

        dschf = HfDeepSpeedConfig(ds_config)

        self.assertTrue(dschf.is_zero3())
        self.assertTrue(is_deepspeed_zero3_enabled())

        with LoggingLevel(logging.INFO):
            with mockenv_context(**self.dist_env_1_gpu):
                logger = logging.get_logger("transformers.modeling_utils")
                with CaptureLogger(logger) as cl:
                    AutoModel.from_pretrained(T5_TINY)
        self.assertIn("Detected DeepSpeed ZeRO-3", cl.out)

        # now remove zero optimization
        del ds_config["zero_optimization"]
        dschf = HfDeepSpeedConfig(ds_config)

        self.assertFalse(dschf.is_zero3())
        self.assertFalse(is_deepspeed_zero3_enabled())

        with LoggingLevel(logging.INFO):
            with mockenv_context(**self.dist_env_1_gpu):
                logger = logging.get_logger("transformers.modeling_utils")
                with CaptureLogger(logger) as cl:
                    AutoModel.from_pretrained(T5_TINY)
        self.assertNotIn("Detected DeepSpeed ZeRO-3", cl.out)
예제 #2
0
    def test_integration(self):
        level_origin = logging.get_verbosity()

        logger = logging.get_logger(
            "transformers.models.bart.tokenization_bart")
        msg = "Testing 1, 2, 3"

        # should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
        if level_origin <= logging.WARNING:
            with CaptureLogger(logger) as cl:
                logger.warning(msg)
            self.assertEqual(cl.out, msg + "\n")

        # this is setting the level for all of `transformers.*` loggers
        logging.set_verbosity_error()

        # should not be able to log warnings
        with CaptureLogger(logger) as cl:
            logger.warning(msg)
        self.assertEqual(cl.out, "")

        # should be able to log warnings again
        logging.set_verbosity_warning()
        with CaptureLogger(logger) as cl:
            logger.warning(msg)
        self.assertEqual(cl.out, msg + "\n")

        # restore to the original level
        logging.set_verbosity(level_origin)
    def test_load_with_mismatched_shapes(self):
        if not self.test_mismatched_shapes:
            return
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common(
        )

        for model_class in self.all_model_classes:
            if model_class not in get_values(
                    FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING):
                continue

            with self.subTest(msg=f"Testing {model_class}"):
                with tempfile.TemporaryDirectory() as tmp_dir:
                    model = model_class(config)
                    model.save_pretrained(tmp_dir)

                    # Fails when we don't set ignore_mismatched_sizes=True
                    with self.assertRaises(ValueError):
                        new_model = FlaxAutoModelForSequenceClassification.from_pretrained(
                            tmp_dir, num_labels=42)
                    with self.assertRaises(ValueError):
                        new_model_without_prefix = FlaxAutoModel.from_pretrained(
                            tmp_dir, vocab_size=10)

                    logger = logging.get_logger(
                        "transformers.modeling_flax_utils")
                    with CaptureLogger(logger) as cl:
                        new_model = FlaxAutoModelForSequenceClassification.from_pretrained(
                            tmp_dir,
                            num_labels=42,
                            ignore_mismatched_sizes=True)
                    self.assertIn("the shapes did not match", cl.out)

                    logits = new_model(**inputs_dict)["logits"]
                    self.assertEqual(logits.shape[1], 42)

                    with CaptureLogger(logger) as cl:
                        new_model_without_prefix = FlaxAutoModel.from_pretrained(
                            tmp_dir,
                            vocab_size=10,
                            ignore_mismatched_sizes=True)
                    self.assertIn("the shapes did not match", cl.out)
                    input_ids = ids_tensor((2, 8), 10)
                    if self.is_encoder_decoder:
                        new_model_without_prefix(input_ids,
                                                 decoder_input_ids=input_ids)
                    else:
                        new_model_without_prefix(input_ids)
예제 #4
0
    def test_hyperparameter_search(self):
        with mockenv_context(**self.dist_env_1_gpu):

            ds_config_zero3_dict = self.get_config_dict(ZERO3)

            # hyperparameter_search requires model_init() to recreate the model for each trial
            def model_init():
                config = RegressionModelConfig(a=0, b=0, double_output=False)
                model = RegressionPreTrainedModel(config)
                return model

            trainer = get_regression_trainer(
                local_rank=0,
                fp16=True,
                model_init=model_init,
                deepspeed=ds_config_zero3_dict,
            )

            n_trials = 3
            with CaptureLogger(deepspeed_logger) as cl:
                with CaptureStd() as cs:
                    trainer.hyperparameter_search(direction="maximize",
                                                  n_trials=n_trials)
            self.assertIn("DeepSpeed info", cl.out,
                          "expected DeepSpeed logger output but got none")
            self.assertIn(f"Trial {n_trials-1} finished with value", cs.err,
                          "expected hyperparameter_search output")
            self.assertIn("Best is trial", cs.err,
                          "expected hyperparameter_search output")
예제 #5
0
 def test_env_invalid_override(self):
     # reset for the env var to take effect, next time some logger call is made
     transformers.utils.logging._reset_library_root_logger()
     logger = logging.logging.getLogger()
     with CaptureLogger(logger) as cl:
         # this action activates the env var
         logging.get_logger("transformers.models.bart.tokenization_bart")
     self.assertIn("Unknown option TRANSFORMERS_VERBOSITY=super-error", cl.out)
예제 #6
0
 def tokenize_function(examples):
     with CaptureLogger(tok_logger) as cl:
         output = tokenizer(examples[text_column_name])
     # clm input could be much much longer than block_size
     if "Token indices sequence length is longer than the" in cl.out:
         tok_logger.warning(
             "^^^^^^^^^^^^^^^^ Please ignore the warning above - this long input will be chunked into smaller bits before being passed to the model."
         )
     return output
예제 #7
0
    def test_advisory_warnings(self):
        # testing `logger.warning_advice()`

        logger = logging.get_logger(
            "transformers.models.bart.tokenization_bart")
        msg = "Testing 1, 2, 3"

        with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS="1"):
            # nothing should be logged as env var disables this method
            with CaptureLogger(logger) as cl:
                logger.warning_advice(msg)
            self.assertEqual(cl.out, "")

        with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=""):
            # should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
            with CaptureLogger(logger) as cl:
                logger.warning_advice(msg)
            self.assertEqual(cl.out, msg + "\n")
    def test_warning_logs(self):
        transformers_logging.set_verbosity_debug()
        logger_ = transformers_logging.get_logger(
            "transformers.pipelines.base")

        alias = "text-classification"
        with CaptureLogger(logger_) as cm:
            PIPELINE_REGISTRY.register_pipeline(alias, {})
        self.assertIn(f"{alias} is already registered", cm.out)
예제 #9
0
 def test_hf_optimizer_with_offload(self, stage):
     # non-DS optimizers can be used with ZERO-offload (as long as they have both CPU and GPU implementation (except LAMB))
     ds_config_dict = self.get_config_dict(stage)
     del ds_config_dict["optimizer"]  # force default HF Trainer optimizer
     # force cpu offload
     ds_config_dict["zero_optimization"]["offload_optimizer"]["device"] = "cpu"
     with mockenv_context(**self.dist_env_1_gpu):
         trainer = get_regression_trainer(local_rank=0, fp16=True, deepspeed=ds_config_dict)
         with CaptureLogger(deepspeed_logger) as cl:
             trainer.train()
         self.assertIn("DeepSpeed info", cl.out, "expected DeepSpeed logger output but got none")
예제 #10
0
    def test_fake_notebook_no_launcher(self, stage):
        # this setup emulates a notebook where a launcher needs to be emulated by hand

        # note that unittest resets sys.stdout each test, so `CaptureStd` will work here to capture
        # DeepSpeed log if this test happens to run first in this pytest worker. But it will fail if
        # it's run not as a first test as `sys.stdout` will no longer be the same. So we either have
        # to reset `deepspeed_logger.handlers[0].setStream(sys.stdout)` or directly capture from the deepspeed_logger.
        with mockenv_context(**self.dist_env_1_gpu):
            trainer = get_regression_trainer(local_rank=0, fp16=True, deepspeed=self.get_config_dict(stage))
            with CaptureLogger(deepspeed_logger) as cl:
                trainer.train()
            self.assertIn("DeepSpeed info", cl.out, "expected DeepSpeed logger output but got none")
예제 #11
0
 def test_stage3_nvme_offload(self):
     with mockenv_context(**self.dist_env_1_gpu):
         # this actually doesn't have to be on NVMe, any storage will do since this test only
         # runs a simple check that we can use some directory as if it were NVMe
         nvme_path = self.get_auto_remove_tmp_dir()
         nvme_config = dict(device="nvme", nvme_path=nvme_path)
         ds_config_zero3_dict = self.get_config_dict(ZERO3)
         ds_config_zero3_dict["zero_optimization"]["offload_optimizer"] = nvme_config
         ds_config_zero3_dict["zero_optimization"]["offload_param"] = nvme_config
         trainer = get_regression_trainer(local_rank=0, fp16=True, deepspeed=ds_config_zero3_dict)
         with CaptureLogger(deepspeed_logger) as cl:
             trainer.train()
         self.assertIn("DeepSpeed info", cl.out, "expected DeepSpeed logger output but got none")
    def test_warning_logs(self):
        transformers_logging.set_verbosity_debug()
        logger_ = transformers_logging.get_logger(
            "transformers.pipelines.base")

        alias = "text-classification"
        # Get the original task, so we can restore it at the end.
        # (otherwise the subsequential tests in `TextClassificationPipelineTests` will fail)
        original_task, original_task_options = PIPELINE_REGISTRY.check_task(
            alias)

        try:
            with CaptureLogger(logger_) as cm:
                PIPELINE_REGISTRY.register_pipeline(alias, {})
            self.assertIn(f"{alias} is already registered", cm.out)
        finally:
            # restore
            PIPELINE_REGISTRY.register_pipeline(alias, original_task)
    def test_run_clm_config_overrides(self):
        # test that config_overrides works, despite the misleading dumps of default un-updated
        # config via tokenizer

        tmp_dir = self.get_auto_remove_tmp_dir()
        testargs = f"""
            run_clm.py
            --model_type gpt2
            --tokenizer_name gpt2
            --train_file ./tests/fixtures/sample_text.txt
            --output_dir {tmp_dir}
            --config_overrides n_embd=10,n_head=2
            """.split()

        if torch_device != "cuda":
            testargs.append("--no_cuda")

        logger = run_clm.logger
        with patch.object(sys, "argv", testargs):
            with CaptureLogger(logger) as cl:
                run_clm.main()

        self.assertIn('"n_embd": 10', cl.out)
        self.assertIn('"n_head": 2', cl.out)