transformers
594 строки · 24.5 Кб
1# coding=utf-8
2# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15""" Testing suite for the PyTorch Falcon model. """
16
17
18import tempfile
19import unittest
20
21from parameterized import parameterized
22
23from transformers import (
24AutoModelForCausalLM,
25AutoTokenizer,
26FalconConfig,
27is_torch_available,
28set_seed,
29)
30from transformers.testing_utils import require_bitsandbytes, require_torch, require_torch_sdpa, slow, torch_device
31
32from ...generation.test_utils import GenerationTesterMixin
33from ...test_configuration_common import ConfigTester
34from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
35from ...test_pipeline_mixin import PipelineTesterMixin
36
37
38if is_torch_available():
39import torch
40
41from transformers import (
42FalconForCausalLM,
43FalconForQuestionAnswering,
44FalconForSequenceClassification,
45FalconForTokenClassification,
46FalconModel,
47)
48
49
50class FalconModelTester:
51def __init__(
52self,
53parent,
54batch_size=3,
55seq_length=7,
56is_training=True,
57use_input_mask=True,
58use_token_type_ids=False,
59use_labels=True,
60vocab_size=99,
61hidden_size=32,
62num_hidden_layers=2,
63num_attention_heads=4,
64intermediate_size=37,
65hidden_act="gelu",
66hidden_dropout_prob=0.1,
67attention_probs_dropout_prob=0.1,
68max_position_embeddings=512,
69type_vocab_size=16,
70type_sequence_label_size=2,
71initializer_range=0.02,
72num_labels=3,
73num_choices=4,
74scope=None,
75):
76self.parent = parent
77self.batch_size = batch_size
78self.seq_length = seq_length
79self.is_training = is_training
80self.use_input_mask = use_input_mask
81self.use_token_type_ids = use_token_type_ids
82self.use_labels = use_labels
83self.vocab_size = vocab_size
84self.hidden_size = hidden_size
85self.num_hidden_layers = num_hidden_layers
86self.num_attention_heads = num_attention_heads
87self.intermediate_size = intermediate_size
88self.hidden_act = hidden_act
89self.hidden_dropout_prob = hidden_dropout_prob
90self.attention_probs_dropout_prob = attention_probs_dropout_prob
91self.max_position_embeddings = max_position_embeddings
92self.type_vocab_size = type_vocab_size
93self.type_sequence_label_size = type_sequence_label_size
94self.initializer_range = initializer_range
95self.num_labels = num_labels
96self.num_choices = num_choices
97self.scope = scope
98
99def prepare_config_and_inputs(self):
100input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
101
102input_mask = None
103if self.use_input_mask:
104input_mask = random_attention_mask([self.batch_size, self.seq_length])
105
106token_type_ids = None
107
108sequence_labels = None
109token_labels = None
110choice_labels = None
111if self.use_labels:
112sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
113token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels)
114choice_labels = ids_tensor([self.batch_size], self.num_choices)
115
116config = self.get_config()
117
118return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
119
120def get_config(self):
121return FalconConfig(
122vocab_size=self.vocab_size,
123hidden_size=self.hidden_size,
124num_hidden_layers=self.num_hidden_layers,
125num_attention_heads=self.num_attention_heads,
126intermediate_size=self.intermediate_size,
127hidden_act=self.hidden_act,
128hidden_dropout_prob=self.hidden_dropout_prob,
129attention_probs_dropout_prob=self.attention_probs_dropout_prob,
130max_position_embeddings=self.max_position_embeddings,
131type_vocab_size=self.type_vocab_size,
132is_decoder=False,
133initializer_range=self.initializer_range,
134pad_token_id=1,
135new_decoder_architecture=True,
136)
137
138def create_and_check_model(
139self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
140):
141model = FalconModel(config=config)
142model.to(torch_device)
143model.eval()
144result = model(input_ids, attention_mask=input_mask)
145result = model(input_ids)
146self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
147
148def create_and_check_model_as_decoder(
149self,
150config,
151input_ids,
152token_type_ids,
153input_mask,
154sequence_labels,
155token_labels,
156choice_labels,
157encoder_hidden_states,
158encoder_attention_mask,
159):
160config.add_cross_attention = True
161model = FalconModel(config)
162model.to(torch_device)
163model.eval()
164result = model(
165input_ids,
166attention_mask=input_mask,
167encoder_hidden_states=encoder_hidden_states,
168encoder_attention_mask=encoder_attention_mask,
169)
170result = model(
171input_ids,
172attention_mask=input_mask,
173encoder_hidden_states=encoder_hidden_states,
174)
175result = model(input_ids, attention_mask=input_mask)
176self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
177
178def create_and_check_for_causal_lm(
179self,
180config,
181input_ids,
182token_type_ids,
183input_mask,
184sequence_labels,
185token_labels,
186choice_labels,
187encoder_hidden_states,
188encoder_attention_mask,
189):
190model = FalconForCausalLM(config=config)
191model.to(torch_device)
192model.eval()
193result = model(input_ids, attention_mask=input_mask, labels=token_labels)
194self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
195
196def create_and_check_decoder_model_past_large_inputs(
197self,
198config,
199input_ids,
200token_type_ids,
201input_mask,
202sequence_labels,
203token_labels,
204choice_labels,
205encoder_hidden_states,
206encoder_attention_mask,
207):
208config.is_decoder = True
209config.add_cross_attention = True
210model = FalconForCausalLM(config=config)
211model.to(torch_device)
212model.eval()
213
214# first forward pass
215outputs = model(
216input_ids,
217attention_mask=input_mask,
218encoder_hidden_states=encoder_hidden_states,
219encoder_attention_mask=encoder_attention_mask,
220use_cache=True,
221)
222past_key_values = outputs.past_key_values
223
224# create hypothetical multiple next token and extent to next_input_ids
225next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size)
226next_mask = ids_tensor((self.batch_size, 3), vocab_size=2)
227
228# append to next input_ids and
229next_input_ids = torch.cat([input_ids, next_tokens], dim=-1)
230next_attention_mask = torch.cat([input_mask, next_mask], dim=-1)
231
232output_from_no_past = model(
233next_input_ids,
234attention_mask=next_attention_mask,
235encoder_hidden_states=encoder_hidden_states,
236encoder_attention_mask=encoder_attention_mask,
237output_hidden_states=True,
238)["hidden_states"][0]
239output_from_past = model(
240next_tokens,
241attention_mask=next_attention_mask,
242encoder_hidden_states=encoder_hidden_states,
243encoder_attention_mask=encoder_attention_mask,
244past_key_values=past_key_values,
245output_hidden_states=True,
246)["hidden_states"][0]
247
248# select random slice
249random_slice_idx = ids_tensor((1,), output_from_past.shape[-1]).item()
250output_from_no_past_slice = output_from_no_past[:, -3:, random_slice_idx].detach()
251output_from_past_slice = output_from_past[:, :, random_slice_idx].detach()
252
253self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])
254
255# test that outputs are equal for slice
256self.parent.assertTrue(torch.allclose(output_from_past_slice, output_from_no_past_slice, atol=1e-3))
257
258def prepare_config_and_inputs_for_common(self):
259config_and_inputs = self.prepare_config_and_inputs()
260(
261config,
262input_ids,
263token_type_ids,
264input_mask,
265sequence_labels,
266token_labels,
267choice_labels,
268) = config_and_inputs
269inputs_dict = {"input_ids": input_ids, "attention_mask": input_mask}
270return config, inputs_dict
271
272
273@require_torch
274class FalconModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMixin, unittest.TestCase):
275all_model_classes = (
276(
277FalconModel,
278FalconForCausalLM,
279FalconForSequenceClassification,
280FalconForTokenClassification,
281FalconForQuestionAnswering,
282)
283if is_torch_available()
284else ()
285)
286all_generative_model_classes = (FalconForCausalLM,) if is_torch_available() else ()
287pipeline_model_mapping = (
288{
289"feature-extraction": FalconModel,
290"question-answering": FalconForQuestionAnswering,
291"text-classification": FalconForSequenceClassification,
292"text-generation": FalconForCausalLM,
293"token-classification": FalconForTokenClassification,
294"zero-shot": FalconForSequenceClassification,
295}
296if is_torch_available()
297else {}
298)
299test_headmasking = False
300test_pruning = False
301
302# TODO (ydshieh): Check this. See https://app.circleci.com/pipelines/github/huggingface/transformers/79245/workflows/9490ef58-79c2-410d-8f51-e3495156cf9c/jobs/1012146
303def is_pipeline_test_to_skip(
304self, pipeline_test_casse_name, config_class, model_architecture, tokenizer_name, processor_name
305):
306return True
307
308def setUp(self):
309self.model_tester = FalconModelTester(self)
310self.config_tester = ConfigTester(self, config_class=FalconConfig, hidden_size=37)
311
312def test_config(self):
313self.config_tester.run_common_tests()
314
315def test_model(self):
316config_and_inputs = self.model_tester.prepare_config_and_inputs()
317self.model_tester.create_and_check_model(*config_and_inputs)
318
319def test_position_embedding_types(self):
320config, *inputs = self.model_tester.prepare_config_and_inputs()
321for alibi in [True, False]:
322config.alibi = alibi
323self.model_tester.create_and_check_model(config, *inputs)
324
325def test_falcon_sequence_classification_model(self):
326config, input_dict = self.model_tester.prepare_config_and_inputs_for_common()
327config.num_labels = 3
328input_ids = input_dict["input_ids"]
329attention_mask = input_ids.ne(1).to(torch_device)
330sequence_labels = ids_tensor([self.model_tester.batch_size], self.model_tester.type_sequence_label_size)
331model = FalconForSequenceClassification(config)
332model.to(torch_device)
333model.eval()
334result = model(input_ids, attention_mask=attention_mask, labels=sequence_labels)
335self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels))
336
337def test_falcon_sequence_classification_model_for_single_label(self):
338config, input_dict = self.model_tester.prepare_config_and_inputs_for_common()
339config.num_labels = 3
340config.problem_type = "single_label_classification"
341input_ids = input_dict["input_ids"]
342attention_mask = input_ids.ne(1).to(torch_device)
343sequence_labels = ids_tensor([self.model_tester.batch_size], self.model_tester.type_sequence_label_size)
344model = FalconForSequenceClassification(config)
345model.to(torch_device)
346model.eval()
347result = model(input_ids, attention_mask=attention_mask, labels=sequence_labels)
348self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels))
349
350def test_falcon_sequence_classification_model_for_multi_label(self):
351config, input_dict = self.model_tester.prepare_config_and_inputs_for_common()
352config.num_labels = 3
353config.problem_type = "multi_label_classification"
354input_ids = input_dict["input_ids"]
355attention_mask = input_ids.ne(1).to(torch_device)
356sequence_labels = ids_tensor(
357[self.model_tester.batch_size, config.num_labels], self.model_tester.type_sequence_label_size
358).to(torch.float)
359model = FalconForSequenceClassification(config)
360model.to(torch_device)
361model.eval()
362result = model(input_ids, attention_mask=attention_mask, labels=sequence_labels)
363self.assertEqual(result.logits.shape, (self.model_tester.batch_size, self.model_tester.num_labels))
364
365def test_past_key_values_format(self):
366# Falcon can have different numbers of KV-heads than the number of query heads, so we need
367# to override this test to use the right head counts.
368for model_class in self.all_generative_model_classes:
369config, inputs = self.model_tester.prepare_config_and_inputs_for_common()
370
371# If it doesn't support cache, pass the test
372if not hasattr(config, "use_cache"):
373return
374
375model = model_class(config).to(torch_device)
376if "use_cache" not in inputs:
377inputs["use_cache"] = True
378outputs = model(**inputs)
379
380# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
381if "past_key_values" not in outputs:
382return
383
384num_hidden_layers = (
385getattr(config, "decoder_layers", None)
386or getattr(config, "num_decoder_layers", None)
387or config.num_hidden_layers
388)
389num_attention_heads = getattr(config, "num_kv_heads", config.num_attention_heads)
390embed_dim = getattr(config, "d_model", config.hidden_size)
391per_head_embed_dim = embed_dim // num_attention_heads
392
393past_kv = outputs["past_key_values"]
394self.assertEqual(len(past_kv), num_hidden_layers)
395
396batch_size, seq_length = inputs["input_ids"].shape
397for i in range(num_hidden_layers):
398if config.new_decoder_architecture:
399num_attention_heads = config.num_attention_heads
400elif config.multi_query:
401num_attention_heads = 1
402self.assertEqual(len(past_kv[0]), 2) # K V for the decoder = 2
403self.assertEqual(
404past_kv[i][0].shape, (batch_size, num_attention_heads, seq_length, per_head_embed_dim)
405)
406self.assertEqual(
407past_kv[i][1].shape, (batch_size, num_attention_heads, seq_length, per_head_embed_dim)
408)
409
410@parameterized.expand([("linear",), ("dynamic",)])
411def test_model_rope_scaling(self, scaling_type):
412config, _ = self.model_tester.prepare_config_and_inputs_for_common()
413short_input = ids_tensor([1, 10], config.vocab_size)
414long_input = ids_tensor([1, int(config.max_position_embeddings * 1.5)], config.vocab_size)
415
416set_seed(42) # Fixed seed at init time so the two models get the same random weights
417original_model = FalconModel(config)
418original_model.to(torch_device)
419original_model.eval()
420original_short_output = original_model(short_input).last_hidden_state
421original_long_output = original_model(long_input).last_hidden_state
422
423set_seed(42) # Fixed seed at init time so the two models get the same random weights
424config.rope_scaling = {"type": scaling_type, "factor": 10.0}
425scaled_model = FalconModel(config)
426scaled_model.to(torch_device)
427scaled_model.eval()
428scaled_short_output = scaled_model(short_input).last_hidden_state
429scaled_long_output = scaled_model(long_input).last_hidden_state
430
431# Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
432# maximum sequence length, so the outputs for the short input should match.
433if scaling_type == "dynamic":
434self.assertTrue(torch.allclose(original_short_output, scaled_short_output, atol=1e-5))
435else:
436self.assertFalse(torch.allclose(original_short_output, scaled_short_output, atol=1e-5))
437
438# The output should be different for long inputs
439self.assertFalse(torch.allclose(original_long_output, scaled_long_output, atol=1e-5))
440
441@require_torch_sdpa
442@slow
443def test_eager_matches_sdpa_generate(self):
444max_new_tokens = 30
445
446if len(self.all_generative_model_classes) == 0:
447self.skipTest(f"{self.__class__.__name__} tests a model that does support generate: skipping this test")
448
449for model_class in self.all_generative_model_classes:
450if not model_class._supports_sdpa:
451self.skipTest(f"{model_class.__name__} does not support SDPA")
452
453config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
454
455dummy_input = inputs_dict[model_class.main_input_name]
456if dummy_input.dtype in [torch.float32, torch.bfloat16]:
457dummy_input = dummy_input.to(torch.float16)
458
459# make sure that all models have enough positions for generation
460if hasattr(config, "max_position_embeddings"):
461config.max_position_embeddings = max_new_tokens + dummy_input.shape[1] + 1
462
463model = model_class(config)
464
465with tempfile.TemporaryDirectory() as tmpdirname:
466model.save_pretrained(tmpdirname)
467
468dummy_attention_mask = inputs_dict.get("attention_mask", torch.ones_like(dummy_input))
469
470model_sdpa = model_class.from_pretrained(
471tmpdirname,
472torch_dtype=torch.float16,
473low_cpu_mem_usage=True,
474).to(torch_device)
475
476self.assertTrue(model_sdpa.config._attn_implementation == "sdpa")
477
478model_eager = model_class.from_pretrained(
479tmpdirname,
480torch_dtype=torch.float16,
481low_cpu_mem_usage=True,
482attn_implementation="eager",
483).to(torch_device)
484
485self.assertTrue(model_eager.config._attn_implementation == "eager")
486
487# NOTE: This check is disabled for Falcon as the non-SDPA/SDPA implementation is in the same class (legacy reason).
488# for name, submodule in model_eager.named_modules():
489# if "SdpaAttention" in submodule.__class__.__name__:
490# raise ValueError("The eager model should not have SDPA attention layers")
491
492# has_sdpa = False
493# for name, submodule in model_sdpa.named_modules():
494# if "SdpaAttention" in submodule.__class__.__name__:
495# has_sdpa = True
496# break
497# if not has_sdpa:
498# raise ValueError("The SDPA model should have SDPA attention layers")
499
500# Just test that a large cache works as expected
501res_eager = model_eager.generate(
502dummy_input, attention_mask=dummy_attention_mask, max_new_tokens=max_new_tokens, do_sample=False
503)
504
505res_sdpa = model_sdpa.generate(
506dummy_input, attention_mask=dummy_attention_mask, max_new_tokens=max_new_tokens, do_sample=False
507)
508
509self.assertTrue(torch.allclose(res_eager, res_sdpa))
510
511
512@require_torch
513class FalconLanguageGenerationTest(unittest.TestCase):
514@slow
515def test_lm_generate_falcon(self):
516tokenizer = AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b")
517model = FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b")
518model.eval()
519model.to(torch_device)
520inputs = tokenizer("My favorite food is", return_tensors="pt").to(torch_device)
521
522EXPECTED_OUTPUT = (
523"My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."
524)
525
526output_ids = model.generate(**inputs, do_sample=False, max_new_tokens=19)
527output_str = tokenizer.batch_decode(output_ids)[0]
528
529self.assertEqual(output_str, EXPECTED_OUTPUT)
530
531@slow
532def test_lm_generation_big_models(self):
533# The big models are way too big for the CI, so we use tiny random models that resemble their
534# architectures but with much smaller and fewer layers
535for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
536tokenizer = AutoTokenizer.from_pretrained(repo)
537model = FalconForCausalLM.from_pretrained(repo)
538model.eval()
539model.to(torch_device)
540inputs = tokenizer("My favorite food is", return_tensors="pt").to(torch_device)
541
542# We just test that these run without errors - the models are randomly initialized
543# and so the actual text outputs will be garbage
544model.generate(**inputs, do_sample=False, max_new_tokens=4)
545model.generate(**inputs, do_sample=True, max_new_tokens=4)
546model.generate(**inputs, num_beams=2, max_new_tokens=4)
547
548@slow
549def test_lm_generation_use_cache(self):
550# The big models are way too big for the CI, so we use tiny random models that resemble their
551# architectures but with much smaller and fewer layers
552with torch.no_grad():
553for repo in [
554"Rocketknight1/falcon-rw-1b",
555"Rocketknight1/tiny-random-falcon-7b",
556"Rocketknight1/tiny-random-falcon-40b",
557]:
558tokenizer = AutoTokenizer.from_pretrained(repo)
559model = FalconForCausalLM.from_pretrained(repo)
560model.eval()
561model.to(device=torch_device)
562inputs = tokenizer("My favorite food is", return_tensors="pt").to(torch_device)
563
564# Test results are the same with and without cache
565outputs_no_cache = model.generate(**inputs, do_sample=False, max_new_tokens=20, use_cache=False)
566outputs_cache = model.generate(**inputs, do_sample=False, max_new_tokens=20, use_cache=True)
567self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0)
568
569@require_bitsandbytes
570@slow
571def test_batched_generation(self):
572tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b", padding_side="left")
573tokenizer.pad_token = tokenizer.eos_token
574model = AutoModelForCausalLM.from_pretrained(
575"tiiuae/falcon-7b",
576device_map="auto",
577load_in_4bit=True,
578)
579
580test_text = "A sequence: 1, 2" # should generate the rest of the sequence
581
582unpadded_inputs = tokenizer([test_text], return_tensors="pt").to("cuda:0")
583unpadded_gen_out = model.generate(**unpadded_inputs, max_new_tokens=20)
584unpadded_gen_text = tokenizer.batch_decode(unpadded_gen_out, skip_special_tokens=True)
585
586dummy_text = "This is a longer text " * 2 # forces left-padding on `test_text`
587padded_inputs = tokenizer([test_text, dummy_text], return_tensors="pt", padding=True).to("cuda:0")
588padded_gen_out = model.generate(**padded_inputs, max_new_tokens=20)
589padded_gen_text = tokenizer.batch_decode(padded_gen_out, skip_special_tokens=True)
590
591expected_output = "A sequence: 1, 2, 3, 4, 5, 6, 7, 8, "
592self.assertLess(unpadded_inputs.input_ids.shape[-1], padded_inputs.input_ids.shape[-1]) # left-padding exists
593self.assertEqual(unpadded_gen_text[0], expected_output)
594self.assertEqual(padded_gen_text[0], expected_output)
595