From 50c1c19fc7cdb5fe94697131d7ee04253596e994 Mon Sep 17 00:00:00 2001 From: NielsRogge <48327001+NielsRogge@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:47:17 +0200 Subject: [PATCH] [UDOP] Fix tests (#29573) * Fix tests * Fix tests * Remove no_split_modules --- src/transformers/models/udop/modeling_udop.py | 1 - tests/models/udop/test_modeling_udop.py | 27 +++++++++++++------ tests/models/udop/test_processor_udop.py | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/transformers/models/udop/modeling_udop.py b/src/transformers/models/udop/modeling_udop.py index 7e242c8b52..0d817a324b 100644 --- a/src/transformers/models/udop/modeling_udop.py +++ b/src/transformers/models/udop/modeling_udop.py @@ -413,7 +413,6 @@ class UdopPreTrainedModel(PreTrainedModel): config_class = UdopConfig base_model_prefix = "transformer" supports_gradient_checkpointing = True - _no_split_modules = ["UdopBlock"] _keep_in_fp32_modules = ["wo"] def _init_weights(self, module): diff --git a/tests/models/udop/test_modeling_udop.py b/tests/models/udop/test_modeling_udop.py index 7041f25f4e..257f6245ee 100644 --- a/tests/models/udop/test_modeling_udop.py +++ b/tests/models/udop/test_modeling_udop.py @@ -226,6 +226,20 @@ class UdopModelTester: ) self.parent.assertTrue(torch.all(output_with_past_cache == output_without_past_cache)) + def create_and_check_model_fp16_forward( + self, + config, + input_ids, + bbox, + decoder_input_ids, + attention_mask, + decoder_attention_mask, + lm_labels, + ): + model = UdopForConditionalGeneration(config=config).to(torch_device).half().eval() + output = model(input_ids, bbox=bbox, attention_mask=attention_mask, decoder_input_ids=decoder_input_ids).logits + self.parent.assertFalse(torch.isnan(output).any().item()) + def prepare_config_and_inputs_for_common(self): config_and_inputs = self.prepare_config_and_inputs() ( @@ -268,6 +282,7 @@ class UdopModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase): test_resize_embeddings = True test_model_parallel = False is_encoder_decoder = True + test_cpu_offload = False # The small UDOP model needs higher percentages for CPU/MP tests model_split_percents = [0.8, 0.9] @@ -491,10 +506,11 @@ class UdopEncoderOnlyModelTester: self, config, input_ids, + bbox, attention_mask, ): model = UdopEncoderModel(config=config).to(torch_device).half().eval() - output = model(input_ids, attention_mask=attention_mask)["last_hidden_state"] + output = model(input_ids, bbox=bbox, attention_mask=attention_mask)["last_hidden_state"] self.parent.assertFalse(torch.isnan(output).any().item()) @@ -504,7 +520,7 @@ class UdopEncoderOnlyModelTest(ModelTesterMixin, unittest.TestCase): test_torchscript = False test_head_masking = False test_resize_embeddings = False - test_model_parallel = True + test_model_parallel = False all_parallelizable_model_classes = (UdopEncoderModel,) if is_torch_available() else () def setUp(self): @@ -518,11 +534,6 @@ class UdopEncoderOnlyModelTest(ModelTesterMixin, unittest.TestCase): config_and_inputs = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*config_and_inputs) - @unittest.skipIf(torch_device == "cpu", "Cant do half precision") - def test_model_fp16_forward(self): - config_and_inputs = self.model_tester.prepare_config_and_inputs() - self.model_tester.create_and_check_model_fp16_forward(*config_and_inputs) - @unittest.skip( "Not currently compatible. Fails with - NotImplementedError: Cannot copy out of meta tensor; no data!" ) @@ -558,7 +569,7 @@ class UdopModelIntegrationTests(unittest.TestCase): model = self.model prompt = "Question answering. In which year is the report made?" - encoding = processor(images=self.image, text=prompt, return_tensors="pt") + encoding = processor(images=self.image, text=prompt, return_tensors="pt").to(torch_device) predicted_ids = model.generate(**encoding) diff --git a/tests/models/udop/test_processor_udop.py b/tests/models/udop/test_processor_udop.py index 53a50b9a11..ceb5f1e331 100644 --- a/tests/models/udop/test_processor_udop.py +++ b/tests/models/udop/test_processor_udop.py @@ -286,7 +286,7 @@ class UdopProcessorIntegrationTests(unittest.TestCase): # verify input_ids # this was obtained with Tesseract 4.1.1 # fmt: off - expected_decoding = "7 ITC Limited REPORT AND ACCOUNTS 2013 ITC’s Brands: An Asset for the Nation The consumer needs and aspirations they fulfil, the benefit they generate for millions across ITC’s value chains, the future-ready capabilities that support them, and the value that they create for the country, have made ITC’s brands national assets, adding to India’s competitiveness. It is ITC’s aspiration to be the No 1 FMCG player in the country, driven by its new FMCG businesses. A recent Nielsen report has highlighted that ITC's new FMCG businesses are the fastest growing among the top consumer goods companies operating in India. ITC takes justifiable pride that, along with generating economic value, these celebrated Indian brands also drive the creation of larger societal capital through the virtuous cycle of sustainable and inclusive growth. DI WILLS * ; LOVE DELIGHTFULLY SOFT SKIN? aia Ans Source: https://www.industrydocuments.ucsf.edu/docs/snbx0223" # noqa: E231 + expected_decoding = "7 ITC Limited REPORT AND ACCOUNTS 2013 ITC’s Brands: An Asset for the Nation The consumer needs and aspirations they fulfil, the benefit they generate for millions across ITC’s value chains, the future-ready capabilities that support them, and the value that they create for the country, have made ITC’s brands national assets, adding to India’s competitiveness. It is ITC’s aspiration to be the No 1 FMCG player in the country, driven by its new FMCG businesses. A recent Nielsen report has highlighted that ITC's new FMCG businesses are the fastest growing among the top consumer goods companies operating in India. ITC takes justifiable pride that, along with generating economic value, these celebrated Indian brands also drive the creation of larger societal capital through the virtuous cycle of sustainable and inclusive growth. DI WILLS * ; LOVE DELIGHTFULLY SOFT SKIN? aia Ans Source: https://www.industrydocuments.ucsf.edu/docs/snbx0223" # noqa: E231 # fmt: on decoding = processor.decode(input_processor.input_ids[1].tolist()) self.assertSequenceEqual(decoding, expected_decoding)