code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,snake_case__=None ,snake_case__=True ,snake_case__=[0.5, 0.5, 0.5] ,snake_case__=[0.5, 0.5, 0.5] ,): SCREAMING_SNAKE_CASE_ : Optional[int] = size if size is not None else {'shortest_edge': 18} SCREAMING_SNAKE_CASE_ : List[Any] = crop_size if crop_size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : int = parent SCREAMING_SNAKE_CASE_ : Any = batch_size SCREAMING_SNAKE_CASE_ : Dict = num_channels SCREAMING_SNAKE_CASE_ : List[str] = image_size SCREAMING_SNAKE_CASE_ : str = min_resolution SCREAMING_SNAKE_CASE_ : str = max_resolution SCREAMING_SNAKE_CASE_ : List[str] = do_resize SCREAMING_SNAKE_CASE_ : Optional[Any] = size SCREAMING_SNAKE_CASE_ : Any = do_center_crop SCREAMING_SNAKE_CASE_ : str = crop_size SCREAMING_SNAKE_CASE_ : List[str] = do_normalize SCREAMING_SNAKE_CASE_ : Any = image_mean SCREAMING_SNAKE_CASE_ : List[Any] = image_std def snake_case ( self ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : List[str] = LevitImageProcessor if is_vision_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = LevitImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'image_mean' ) ) self.assertTrue(hasattr(snake_case__ ,'image_std' ) ) self.assertTrue(hasattr(snake_case__ ,'do_normalize' ) ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'do_center_crop' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'shortest_edge': 18} ) self.assertEqual(image_processor.crop_size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : int = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ,crop_size=84 ) self.assertEqual(image_processor.size ,{'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size ,{'height': 84, 'width': 84} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[Any] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : str = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : List[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[Any] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : str = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : int = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Dict = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : Union[str, Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) ,)
685
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
1
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def __UpperCAmelCase ( lowerCamelCase_ : Tuple ) -> Optional[Any]: """simple docstring""" return x + 2 class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'x = 3' SCREAMING_SNAKE_CASE_ : Optional[int] = {} SCREAMING_SNAKE_CASE_ : int = evaluate(snake_case__ ,{} ,state=snake_case__ ) assert result == 3 self.assertDictEqual(snake_case__ ,{'x': 3} ) SCREAMING_SNAKE_CASE_ : Dict = 'x = y' SCREAMING_SNAKE_CASE_ : List[str] = {'y': 5} SCREAMING_SNAKE_CASE_ : Tuple = evaluate(snake_case__ ,{} ,state=snake_case__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 5, 'y': 5} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = 'y = add_two(x)' SCREAMING_SNAKE_CASE_ : List[str] = {'x': 3} SCREAMING_SNAKE_CASE_ : int = evaluate(snake_case__ ,{'add_two': add_two} ,state=snake_case__ ) assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 3, 'y': 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ : Optional[Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) assert result is None assert "tried to execute add_two" in out.out def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = 'x = 3' SCREAMING_SNAKE_CASE_ : Union[str, Any] = {} SCREAMING_SNAKE_CASE_ : Union[str, Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) assert result == 3 self.assertDictEqual(snake_case__ ,{'x': 3} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = 'test_dict = {\'x\': x, \'y\': add_two(x)}' SCREAMING_SNAKE_CASE_ : int = {'x': 3} SCREAMING_SNAKE_CASE_ : Optional[int] = evaluate(snake_case__ ,{'add_two': add_two} ,state=snake_case__ ) self.assertDictEqual(snake_case__ ,{'x': 3, 'y': 5} ) self.assertDictEqual(snake_case__ ,{'x': 3, 'test_dict': {'x': 3, 'y': 5}} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = 'x = 3\ny = 5' SCREAMING_SNAKE_CASE_ : Union[str, Any] = {} SCREAMING_SNAKE_CASE_ : Optional[Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 3, 'y': 5} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = 'text = f\'This is x: {x}.\'' SCREAMING_SNAKE_CASE_ : Any = {'x': 3} SCREAMING_SNAKE_CASE_ : List[str] = evaluate(snake_case__ ,{} ,state=snake_case__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(snake_case__ ,{'x': 3, 'text': 'This is x: 3.'} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'if x <= 3:\n y = 2\nelse:\n y = 5' SCREAMING_SNAKE_CASE_ : Dict = {'x': 3} SCREAMING_SNAKE_CASE_ : Union[str, Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(snake_case__ ,{'x': 3, 'y': 2} ) SCREAMING_SNAKE_CASE_ : Dict = {'x': 8} SCREAMING_SNAKE_CASE_ : Union[str, Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 8, 'y': 5} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = 'test_list = [x, add_two(x)]' SCREAMING_SNAKE_CASE_ : Dict = {'x': 3} SCREAMING_SNAKE_CASE_ : Dict = evaluate(snake_case__ ,{'add_two': add_two} ,state=snake_case__ ) self.assertListEqual(snake_case__ ,[3, 5] ) self.assertDictEqual(snake_case__ ,{'x': 3, 'test_list': [3, 5]} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = 'y = x' SCREAMING_SNAKE_CASE_ : Dict = {'x': 3} SCREAMING_SNAKE_CASE_ : List[Any] = evaluate(snake_case__ ,{} ,state=snake_case__ ) assert result == 3 self.assertDictEqual(snake_case__ ,{'x': 3, 'y': 3} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'test_list = [x, add_two(x)]\ntest_list[1]' SCREAMING_SNAKE_CASE_ : Optional[Any] = {'x': 3} SCREAMING_SNAKE_CASE_ : int = evaluate(snake_case__ ,{'add_two': add_two} ,state=snake_case__ ) assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 3, 'test_list': [3, 5]} ) SCREAMING_SNAKE_CASE_ : Tuple = 'test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']' SCREAMING_SNAKE_CASE_ : List[Any] = {'x': 3} SCREAMING_SNAKE_CASE_ : Tuple = evaluate(snake_case__ ,{'add_two': add_two} ,state=snake_case__ ) assert result == 5 self.assertDictEqual(snake_case__ ,{'x': 3, 'test_dict': {'x': 3, 'y': 5}} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'x = 0\nfor i in range(3):\n x = i' SCREAMING_SNAKE_CASE_ : int = {} SCREAMING_SNAKE_CASE_ : Optional[int] = evaluate(snake_case__ ,{'range': range} ,state=snake_case__ ) assert result == 2 self.assertDictEqual(snake_case__ ,{'x': 2, 'i': 2} )
685
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
1
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) UpperCamelCase__ : int = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCamelCase__ : Dict = ''' Examples: ```py >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline >>> import torch >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior") >>> pipe_prior.to("cuda") >>> prompt = "red cat, 4k photo" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> zero_image_emb = out.negative_image_embeds >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder") >>> pipe.to("cuda") >>> image = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... height=768, ... width=768, ... num_inference_steps=50, ... ).images >>> image[0].save("cat.png") ``` ''' def __UpperCAmelCase ( lowerCamelCase_ : List[str] , lowerCamelCase_ : List[Any] , lowerCamelCase_ : Dict=8 ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 SCREAMING_SNAKE_CASE_ : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,): super().__init__() self.register_modules( unet=snake_case__ ,scheduler=snake_case__ ,movq=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = 2 ** (len(self.movq.config.block_out_channels ) - 1) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): if latents is None: SCREAMING_SNAKE_CASE_ : Optional[int] = randn_tensor(snake_case__ ,generator=snake_case__ ,device=snake_case__ ,dtype=snake_case__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) SCREAMING_SNAKE_CASE_ : Any = latents.to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = latents * scheduler.init_noise_sigma return latents def snake_case ( self ,snake_case__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) SCREAMING_SNAKE_CASE_ : Tuple = torch.device(F'cuda:{gpu_id}' ) SCREAMING_SNAKE_CASE_ : List[str] = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(snake_case__ ,snake_case__ ) def snake_case ( self ,snake_case__=0 ): if is_accelerate_available() and is_accelerate_version('>=' ,'0.17.0.dev0' ): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.' ) SCREAMING_SNAKE_CASE_ : Any = torch.device(F'cuda:{gpu_id}' ) if self.device.type != "cpu": self.to('cpu' ,silence_dtype_warnings=snake_case__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) SCREAMING_SNAKE_CASE_ : Union[str, Any] = None for cpu_offloaded_model in [self.unet, self.movq]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = cpu_offload_with_hook(snake_case__ ,snake_case__ ,prev_module_hook=snake_case__ ) # We'll offload the last model manually. SCREAMING_SNAKE_CASE_ : Dict = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def snake_case ( self ): if not hasattr(self.unet ,'_hf_hook' ): return self.device for module in self.unet.modules(): if ( hasattr(snake_case__ ,'_hf_hook' ) and hasattr(module._hf_hook ,'execution_device' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(snake_case__ ) def __call__( self ,snake_case__ ,snake_case__ ,snake_case__ = 512 ,snake_case__ = 512 ,snake_case__ = 100 ,snake_case__ = 4.0 ,snake_case__ = 1 ,snake_case__ = None ,snake_case__ = None ,snake_case__ = "pil" ,snake_case__ = True ,): SCREAMING_SNAKE_CASE_ : str = self._execution_device SCREAMING_SNAKE_CASE_ : Dict = guidance_scale > 1.0 if isinstance(snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat(snake_case__ ,dim=0 ) SCREAMING_SNAKE_CASE_ : Tuple = image_embeds.shape[0] * num_images_per_prompt if isinstance(snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : int = torch.cat(snake_case__ ,dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ : Tuple = image_embeds.repeat_interleave(snake_case__ ,dim=0 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = negative_image_embeds.repeat_interleave(snake_case__ ,dim=0 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([negative_image_embeds, image_embeds] ,dim=0 ).to(dtype=self.unet.dtype ,device=snake_case__ ) self.scheduler.set_timesteps(snake_case__ ,device=snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.scheduler.timesteps SCREAMING_SNAKE_CASE_ : int = self.unet.config.in_channels SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = downscale_height_and_width(snake_case__ ,snake_case__ ,self.movq_scale_factor ) # create initial latent SCREAMING_SNAKE_CASE_ : Any = self.prepare_latents( (batch_size, num_channels_latents, height, width) ,image_embeds.dtype ,snake_case__ ,snake_case__ ,snake_case__ ,self.scheduler ,) for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE_ : Dict = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE_ : int = {'image_embeds': image_embeds} SCREAMING_SNAKE_CASE_ : int = self.unet( sample=snake_case__ ,timestep=snake_case__ ,encoder_hidden_states=snake_case__ ,added_cond_kwargs=snake_case__ ,return_dict=snake_case__ ,)[0] if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = noise_pred.split(latents.shape[1] ,dim=1 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : str = variance_pred.chunk(2 ) SCREAMING_SNAKE_CASE_ : Dict = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) SCREAMING_SNAKE_CASE_ : int = torch.cat([noise_pred, variance_pred_text] ,dim=1 ) if not ( hasattr(self.scheduler.config ,'variance_type' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = noise_pred.split(latents.shape[1] ,dim=1 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE_ : int = self.scheduler.step( snake_case__ ,snake_case__ ,snake_case__ ,generator=snake_case__ ,)[0] # post-processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.movq.decode(snake_case__ ,force_not_quantize=snake_case__ )['sample'] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}' ) if output_type in ["np", "pil"]: SCREAMING_SNAKE_CASE_ : Union[str, Any] = image * 0.5 + 0.5 SCREAMING_SNAKE_CASE_ : Dict = image.clamp(0 ,1 ) SCREAMING_SNAKE_CASE_ : Tuple = image.cpu().permute(0 ,2 ,3 ,1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE_ : List[str] = self.numpy_to_pil(snake_case__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=snake_case__ )
685
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
1
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
from math import log from scipy.constants import Boltzmann, physical_constants UpperCamelCase__ : Any = 3_00 # TEMPERATURE (unit = K) def __UpperCAmelCase ( lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('Donor concentration should be positive' ) elif acceptor_conc <= 0: raise ValueError('Acceptor concentration should be positive' ) elif intrinsic_conc <= 0: raise ValueError('Intrinsic concentration should be positive' ) elif donor_conc <= intrinsic_conc: raise ValueError( 'Donor concentration should be greater than intrinsic concentration' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( 'Acceptor concentration should be greater than intrinsic concentration' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = get_activation('gelu' ) self.assertTrue(torch.allclose(gelu_python(snake_case__ ) ,torch_builtin(snake_case__ ) ) ) self.assertFalse(torch.allclose(gelu_python(snake_case__ ) ,gelu_new(snake_case__ ) ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = get_activation('gelu' ) SCREAMING_SNAKE_CASE_ : List[Any] = get_activation('gelu_10' ) SCREAMING_SNAKE_CASE_ : Dict = torch_builtin(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = geluaa(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = torch.where(y_gelu_aa < 10.0 ,1 ,0 ) self.assertTrue(torch.max(snake_case__ ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask ,y_gelu_aa * clipped_mask ) ) def snake_case ( self ): get_activation('gelu' ) get_activation('gelu_10' ) get_activation('gelu_fast' ) get_activation('gelu_new' ) get_activation('gelu_python' ) get_activation('gelu_pytorch_tanh' ) get_activation('linear' ) get_activation('mish' ) get_activation('quick_gelu' ) get_activation('relu' ) get_activation('sigmoid' ) get_activation('silu' ) get_activation('swish' ) get_activation('tanh' ) with self.assertRaises(snake_case__ ): get_activation('bogus' ) with self.assertRaises(snake_case__ ): get_activation(snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = get_activation('gelu' ) SCREAMING_SNAKE_CASE_ : Tuple = 1 SCREAMING_SNAKE_CASE_ : str = get_activation('gelu' ) self.assertEqual(acta.a ,1 ) with self.assertRaises(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = acta.a
685
class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [], [], [], ] def snake_case ( self ,snake_case__ ,snake_case__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError('Maximum queue size is 100' ) self.queues[priority].append(snake_case__ ) except IndexError: raise ValueError('Valid priorities are 0, 1, and 2' ) def snake_case ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError('All queues are empty' ) def __str__( self ): return "\n".join(F'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : List[str] = [] def snake_case ( self ,snake_case__ ): if len(self.queue ) == 100: raise OverFlowError('Maximum queue size is 100' ) self.queue.append(snake_case__ ) def snake_case ( self ): if not self.queue: raise UnderFlowError('The queue is empty' ) else: SCREAMING_SNAKE_CASE_ : List[Any] = min(self.queue ) self.queue.remove(snake_case__ ) return data def __str__( self ): return str(self.queue ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 1_00 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 1_28 ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(1_00 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(1_28 ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
685
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase__ : List[str] = { '''configuration_blenderbot_small''': [ '''BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlenderbotSmallConfig''', '''BlenderbotSmallOnnxConfig''', ], '''tokenization_blenderbot_small''': ['''BlenderbotSmallTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[Any] = ['''BlenderbotSmallTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[Any] = [ '''BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlenderbotSmallForCausalLM''', '''BlenderbotSmallForConditionalGeneration''', '''BlenderbotSmallModel''', '''BlenderbotSmallPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[int] = [ '''TFBlenderbotSmallForConditionalGeneration''', '''TFBlenderbotSmallModel''', '''TFBlenderbotSmallPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = [ '''FlaxBlenderbotSmallForConditionalGeneration''', '''FlaxBlenderbotSmallModel''', '''FlaxBlenderbotSmallPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_blenderbot_small import ( BLENDERBOT_SMALL_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotSmallConfig, BlenderbotSmallOnnxConfig, ) from .tokenization_blenderbot_small import BlenderbotSmallTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_small_fast import BlenderbotSmallTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot_small import ( BLENDERBOT_SMALL_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotSmallForCausalLM, BlenderbotSmallForConditionalGeneration, BlenderbotSmallModel, BlenderbotSmallPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot_small import ( TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel, TFBlenderbotSmallPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, FlaxBlenderbotSmallPreTrainedModel, ) else: import sys UpperCamelCase__ : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
1
import re def __UpperCAmelCase ( lowerCamelCase_ : str ) -> bool: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = re.compile( R'^(?:0|94|\+94|0{2}94)' R'7(0|1|2|4|5|6|7|8)' R'(-| |)' R'\d{7}$' ) return bool(re.search(lowerCamelCase_ , lowerCamelCase_ ) ) if __name__ == "__main__": UpperCamelCase__ : Optional[int] = '''0094702343221''' print(is_sri_lankan_phone_number(phone))
685
from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Tuple = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Any = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Dict = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] )
685
1
from math import loga def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if a < 0: raise ValueError('Input value must be a positive integer' ) elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be a \'int\' type' ) return 0 if (a == 0) else int(loga(a & -a ) ) if __name__ == "__main__": import doctest doctest.testmod()
685
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
1
from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse('''3.8'''): import importlib_metadata else: import importlib.metadata as importlib_metadata UpperCamelCase__ : Tuple = '''''' if version.parse(importlib_metadata.version('''jiwer''')) < version.parse('''2.3.0'''): class lowerCAmelCase_ ( tr.AbstractTransform ): def __init__( self ,snake_case__ = " " ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = sentence_delimiter def snake_case ( self ,snake_case__ ): return list(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = [] for sent_idx, sentence in enumerate(snake_case__ ): chars.extend(self.process_string(snake_case__ ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(snake_case__ ) - 1: chars.append(self.sentence_delimiter ) return chars UpperCamelCase__ : Tuple = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: UpperCamelCase__ : List[Any] = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) UpperCamelCase__ : Any = '''\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } ''' UpperCamelCase__ : List[str] = '''\ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system. CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information. Character error rate can be computed as: CER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct characters, N is the number of characters in the reference (N=S+D+C). CER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the performance of the ASR system with a CER of 0 being a perfect score. ''' UpperCamelCase__ : List[Any] = ''' Computes CER score of transcribed segments against references. Args: references: list of references for each speech input. predictions: list of transcribtions to score. concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result. Returns: (float): the character error rate Examples: >>> predictions = ["this is the prediction", "there is an other sample"] >>> references = ["this is the reference", "there is another one"] >>> cer = datasets.load_metric("cer") >>> cer_score = cer.compute(predictions=predictions, references=references) >>> print(cer_score) 0.34146341463414637 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Value('string' ,id='sequence' ), 'references': datasets.Value('string' ,id='sequence' ), } ) ,codebase_urls=['https://github.com/jitsi/jiwer/'] ,reference_urls=[ 'https://en.wikipedia.org/wiki/Word_error_rate', 'https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates', ] ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__=False ): if concatenate_texts: return jiwer.compute_measures( snake_case__ ,snake_case__ ,truth_transform=snake_case__ ,hypothesis_transform=snake_case__ ,)["wer"] SCREAMING_SNAKE_CASE_ : Tuple = 0 SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 for prediction, reference in zip(snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = jiwer.compute_measures( snake_case__ ,snake_case__ ,truth_transform=snake_case__ ,hypothesis_transform=snake_case__ ,) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
685
1
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging UpperCamelCase__ : Dict = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) UpperCamelCase__ : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'https://pypi.org/pypi/diffusers/json' SCREAMING_SNAKE_CASE_ : Optional[int] = json.loads(request.urlopen(lowerCamelCase_ ).read() )['releases'].keys() return sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : version.Version(lowerCamelCase_ ) ) def __UpperCAmelCase ( ) -> Tuple: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(lowerCamelCase_ ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : List[Any] = f.read() # Imports of the form `import .xxx` SCREAMING_SNAKE_CASE_ : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Unique-ify return list(set(lowerCamelCase_ ) ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : Union[str, Any] = [module_file] SCREAMING_SNAKE_CASE_ : Tuple = [] # Let's recurse through all relative imports while not no_change: SCREAMING_SNAKE_CASE_ : int = [] for f in files_to_check: new_imports.extend(get_relative_imports(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ).parent SCREAMING_SNAKE_CASE_ : int = [str(module_path / m ) for m in new_imports] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [f for f in new_import_files if f not in all_relative_imports] SCREAMING_SNAKE_CASE_ : Any = [F'{f}.py' for f in new_import_files] SCREAMING_SNAKE_CASE_ : Optional[int] = len(lowerCamelCase_ ) == 0 all_relative_imports.extend(lowerCamelCase_ ) return all_relative_imports def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() # Imports of the form `import xxx` SCREAMING_SNAKE_CASE_ : List[str] = re.findall('^\s*import\s+(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Only keep the top-level module SCREAMING_SNAKE_CASE_ : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(set(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = [] for imp in imports: try: importlib.import_module(lowerCamelCase_ ) except ImportError: missing_packages.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' F'{", ".join(lowerCamelCase_ )}. Run `pip install {" ".join(lowerCamelCase_ )}`' ) return get_relative_imports(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = module_path.replace(os.path.sep , '.' ) SCREAMING_SNAKE_CASE_ : Any = importlib.import_module(lowerCamelCase_ ) if class_name is None: return find_pipeline_class(lowerCamelCase_ ) return getattr(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" from ..pipelines import DiffusionPipeline SCREAMING_SNAKE_CASE_ : List[Any] = dict(inspect.getmembers(lowerCamelCase_ , inspect.isclass ) ) SCREAMING_SNAKE_CASE_ : List[str] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , lowerCamelCase_ ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:' F' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in' F' {loaded_module}.' ) SCREAMING_SNAKE_CASE_ : Any = cls return pipeline_class def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) if os.path.isfile(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = module_file_or_url SCREAMING_SNAKE_CASE_ : Dict = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: SCREAMING_SNAKE_CASE_ : List[str] = get_diffusers_versions() # cut ".dev0" SCREAMING_SNAKE_CASE_ : Dict = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: SCREAMING_SNAKE_CASE_ : List[Any] = latest_version if latest_version[1:] in available_versions else 'main' logger.info(F'Defaulting to latest_version: {revision}.' ) elif revision in available_versions: SCREAMING_SNAKE_CASE_ : int = F'v{revision}' elif revision == "main": SCREAMING_SNAKE_CASE_ : List[Any] = revision else: raise ValueError( F'`custom_revision`: {revision} does not exist. Please make sure to choose one of' F' {", ".join(available_versions + ["main"] )}.' ) # community pipeline on GitHub SCREAMING_SNAKE_CASE_ : Tuple = COMMUNITY_PIPELINES_URL.format(revision=lowerCamelCase_ , pipeline=lowerCamelCase_ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = cached_download( lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Tuple = 'git' SCREAMING_SNAKE_CASE_ : Dict = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise else: try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : List[str] = hf_hub_download( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise # Check we have all the requirements in our environment SCREAMING_SNAKE_CASE_ : Dict = check_imports(lowerCamelCase_ ) # Now we move the module inside our cached dynamic modules. SCREAMING_SNAKE_CASE_ : Union[str, Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = Path(lowerCamelCase_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(lowerCamelCase_ , submodule_path / module_file ) for module_needed in modules_needed: SCREAMING_SNAKE_CASE_ : Union[str, Any] = F'{module_needed}.py' shutil.copy(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = use_auth_token elif use_auth_token is True: SCREAMING_SNAKE_CASE_ : int = HfFolder.get_token() else: SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[Any] = model_info(lowerCamelCase_ , revision=lowerCamelCase_ , token=lowerCamelCase_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. SCREAMING_SNAKE_CASE_ : Any = submodule_path / commit_hash SCREAMING_SNAKE_CASE_ : List[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(lowerCamelCase_ ) if not (submodule_path / module_file).exists(): shutil.copy(lowerCamelCase_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( lowerCamelCase_ , F'{module_needed}.py' , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return os.path.join(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , **lowerCamelCase_ : Dict , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_cached_module_file( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return get_class_in_module(lowerCamelCase_ , final_module.replace('.py' , '' ) )
685
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int = 2 ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE_ : Optional[int] = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : str = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , lowerCamelCase_ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , lowerCamelCase_ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(lowerCamelCase_ ) ) , list(range(lowerCamelCase_ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
685
1
from ... import PretrainedConfig UpperCamelCase__ : List[str] = { '''sijunhe/nezha-cn-base''': '''https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json''', } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Dict = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP __a : Tuple = "nezha" def __init__( self ,snake_case__=21128 ,snake_case__=768 ,snake_case__=12 ,snake_case__=12 ,snake_case__=3072 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=64 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=1E-12 ,snake_case__=0.1 ,snake_case__=0 ,snake_case__=2 ,snake_case__=3 ,snake_case__=True ,**snake_case__ ,): super().__init__(pad_token_id=snake_case__ ,bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE_ : Dict = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_ : int = num_attention_heads SCREAMING_SNAKE_CASE_ : str = hidden_act SCREAMING_SNAKE_CASE_ : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE_ : int = max_relative_position SCREAMING_SNAKE_CASE_ : Any = type_vocab_size SCREAMING_SNAKE_CASE_ : List[str] = initializer_range SCREAMING_SNAKE_CASE_ : int = layer_norm_eps SCREAMING_SNAKE_CASE_ : Tuple = classifier_dropout SCREAMING_SNAKE_CASE_ : Union[str, Any] = use_cache
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
1
from functools import reduce UpperCamelCase__ : Tuple = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def __UpperCAmelCase ( lowerCamelCase_ : str = N ) -> int: """simple docstring""" return max( # mypy cannot properly interpret reduce int(reduce(lambda lowerCamelCase_ , lowerCamelCase_ : str(int(lowerCamelCase_ ) * int(lowerCamelCase_ ) ) , n[i : i + 13] ) ) for i in range(len(lowerCamelCase_ ) - 12 ) ) if __name__ == "__main__": print(F"""{solution() = }""")
685
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
1
import torch from diffusers import CMStochasticIterativeScheduler from .test_schedulers import SchedulerCommonTest class lowerCAmelCase_ ( lowerCamelCase_ ): __a : str = (CMStochasticIterativeScheduler,) __a : Optional[int] = 10 def snake_case ( self ,**snake_case__ ): SCREAMING_SNAKE_CASE_ : int = { 'num_train_timesteps': 201, 'sigma_min': 0.002, 'sigma_max': 80.0, } config.update(**snake_case__ ) return config def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = 10 SCREAMING_SNAKE_CASE_ : str = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[int] = self.scheduler_classes[0](**snake_case__ ) scheduler.set_timesteps(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = scheduler.timesteps[0] SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler.timesteps[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.dummy_sample SCREAMING_SNAKE_CASE_ : List[Any] = 0.1 * sample SCREAMING_SNAKE_CASE_ : Dict = scheduler.step(snake_case__ ,snake_case__ ,snake_case__ ).prev_sample SCREAMING_SNAKE_CASE_ : Dict = scheduler.step(snake_case__ ,snake_case__ ,snake_case__ ).prev_sample self.assertEqual(output_a.shape ,sample.shape ) self.assertEqual(output_a.shape ,output_a.shape ) def snake_case ( self ): for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def snake_case ( self ): for clip_denoised in [True, False]: self.check_over_configs(clip_denoised=snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Any = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Tuple = scheduler_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = 1 scheduler.set_timesteps(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = scheduler.timesteps SCREAMING_SNAKE_CASE_ : Dict = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ : List[str] = self.dummy_model() SCREAMING_SNAKE_CASE_ : Any = self.dummy_sample_deter * scheduler.init_noise_sigma for i, t in enumerate(snake_case__ ): # 1. scale model input SCREAMING_SNAKE_CASE_ : Any = scheduler.scale_model_input(snake_case__ ,snake_case__ ) # 2. predict noise residual SCREAMING_SNAKE_CASE_ : Optional[Any] = model(snake_case__ ,snake_case__ ) # 3. predict previous sample x_t-1 SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler.step(snake_case__ ,snake_case__ ,snake_case__ ,generator=snake_case__ ).prev_sample SCREAMING_SNAKE_CASE_ : Optional[Any] = pred_prev_sample SCREAMING_SNAKE_CASE_ : Dict = torch.sum(torch.abs(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_sum.item() - 192.7614 ) < 1E-2 assert abs(result_mean.item() - 0.2510 ) < 1E-3 def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Any = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Union[str, Any] = scheduler_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = [106, 0] scheduler.set_timesteps(timesteps=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler.timesteps SCREAMING_SNAKE_CASE_ : Any = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ : Tuple = self.dummy_model() SCREAMING_SNAKE_CASE_ : int = self.dummy_sample_deter * scheduler.init_noise_sigma for t in timesteps: # 1. scale model input SCREAMING_SNAKE_CASE_ : str = scheduler.scale_model_input(snake_case__ ,snake_case__ ) # 2. predict noise residual SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,snake_case__ ) # 3. predict previous sample x_t-1 SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler.step(snake_case__ ,snake_case__ ,snake_case__ ,generator=snake_case__ ).prev_sample SCREAMING_SNAKE_CASE_ : Any = pred_prev_sample SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.sum(torch.abs(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_sum.item() - 347.6357 ) < 1E-2 assert abs(result_mean.item() - 0.4527 ) < 1E-3 def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : int = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : List[Any] = scheduler_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = [39, 30, 12, 15, 0] with self.assertRaises(snake_case__ ,msg='`timesteps` must be in descending order.' ): scheduler.set_timesteps(timesteps=snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Tuple = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = [39, 30, 12, 1, 0] SCREAMING_SNAKE_CASE_ : int = len(snake_case__ ) with self.assertRaises(snake_case__ ,msg='Can only pass one of `num_inference_steps` or `timesteps`.' ): scheduler.set_timesteps(num_inference_steps=snake_case__ ,timesteps=snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case__ ,msg='`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}' ,): scheduler.set_timesteps(timesteps=snake_case__ )
685
def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> Tuple: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) else: return a * actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(lowerCamelCase_ , lowerCamelCase_ ) return actual_power(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": print(power(-2, -3))
685
1
import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process UpperCamelCase__ : str = logging.getLogger(__name__) UpperCamelCase__ : str = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) UpperCamelCase__ : str = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowerCAmelCase_ : __a : Optional[str] = field( default=lowerCamelCase_ , metadata={ "help": ( "The model checkpoint for weights initialization. Leave None if you want to train a model from" " scratch." ) } , ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(lowerCamelCase_ )} , ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class lowerCAmelCase_ : __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "The input training data file (a text file)."} ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={ "help": ( "The input training data files (multiple files in glob format). " "Very often splitting large files to smaller files can prevent tokenizer going out of memory" ) } , ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."} , ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "An optional input train ref data file for whole word mask in Chinese."} , ) __a : Optional[str] = field( default=lowerCamelCase_ , metadata={"help": "An optional input eval ref data file for whole word mask in Chinese."} , ) __a : bool = field( default=lowerCamelCase_ , metadata={"help": "Whether distinct lines of text in the dataset are to be handled as distinct sequences."} , ) __a : bool = field( default=lowerCamelCase_ , metadata={"help": "Train with masked-language modeling loss instead of language modeling."} ) __a : bool = field(default=lowerCamelCase_ , metadata={"help": "Whether ot not to use whole word mask."} ) __a : float = field( default=0.15 , metadata={"help": "Ratio of tokens to mask for masked language modeling loss"} ) __a : float = field( default=1 / 6 , metadata={ "help": ( "Ratio of length of a span of masked tokens to surrounding context length for permutation language" " modeling." ) } , ) __a : int = field( default=5 , metadata={"help": "Maximum length of a span of masked tokens for permutation language modeling."} ) __a : int = field( default=-1 , metadata={ "help": ( "Optional input sequence length after tokenization." "The training dataset will be truncated in block of this size for training." "Default to the model max input length for single sentence inputs (take into account special tokens)." ) } , ) __a : bool = field( default=lowerCamelCase_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def __UpperCAmelCase ( lowerCamelCase_ : DataTrainingArguments , lowerCamelCase_ : PreTrainedTokenizer , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[str] = None , ) -> Optional[Any]: """simple docstring""" def _dataset(lowerCamelCase_ : Dict , lowerCamelCase_ : List[Any]=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError('You need to set world whole masking and mlm to True for Chinese Whole Word Mask' ) return LineByLineWithRefDataset( tokenizer=lowerCamelCase_ , file_path=lowerCamelCase_ , block_size=args.block_size , ref_path=lowerCamelCase_ , ) return LineByLineTextDataset(tokenizer=lowerCamelCase_ , file_path=lowerCamelCase_ , block_size=args.block_size ) else: return TextDataset( tokenizer=lowerCamelCase_ , file_path=lowerCamelCase_ , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=lowerCamelCase_ , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(lowerCamelCase_ ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( 'Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file ' 'or remove the --do_eval argument.' ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F'Output directory ({training_args.output_dir}) already exists and is not empty. Use' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , lowerCamelCase_ ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: SCREAMING_SNAKE_CASE_ : List[str] = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE_ : Any = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = CONFIG_MAPPING[model_args.model_type]() logger.warning('You are instantiating a new config instance from scratch.' ) if model_args.tokenizer_name: SCREAMING_SNAKE_CASE_ : List[str] = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: SCREAMING_SNAKE_CASE_ : Optional[int] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( 'You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another' ' script, save it,and load it from here, using --tokenizer_name' ) if model_args.model_name_or_path: SCREAMING_SNAKE_CASE_ : Tuple = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=lowerCamelCase_ , cache_dir=model_args.cache_dir , ) else: logger.info('Training new model from scratch' ) SCREAMING_SNAKE_CASE_ : Dict = AutoModelWithLMHead.from_config(lowerCamelCase_ ) model.resize_token_embeddings(len(lowerCamelCase_ ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( 'BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the' '--mlm flag (masked language modeling).' ) if data_args.block_size <= 0: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.max_len # Our input block size will be the max possible for the model else: SCREAMING_SNAKE_CASE_ : List[str] = min(data_args.block_size , tokenizer.max_len ) # Get datasets SCREAMING_SNAKE_CASE_ : str = ( get_dataset(lowerCamelCase_ , tokenizer=lowerCamelCase_ , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) SCREAMING_SNAKE_CASE_ : str = ( get_dataset(lowerCamelCase_ , tokenizer=lowerCamelCase_ , evaluate=lowerCamelCase_ , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": SCREAMING_SNAKE_CASE_ : Dict = DataCollatorForPermutationLanguageModeling( tokenizer=lowerCamelCase_ , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: SCREAMING_SNAKE_CASE_ : int = DataCollatorForWholeWordMask( tokenizer=lowerCamelCase_ , mlm_probability=data_args.mlm_probability ) else: SCREAMING_SNAKE_CASE_ : List[str] = DataCollatorForLanguageModeling( tokenizer=lowerCamelCase_ , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer SCREAMING_SNAKE_CASE_ : Dict = Trainer( model=lowerCamelCase_ , args=lowerCamelCase_ , data_collator=lowerCamelCase_ , train_dataset=lowerCamelCase_ , eval_dataset=lowerCamelCase_ , prediction_loss_only=lowerCamelCase_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=lowerCamelCase_ ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation SCREAMING_SNAKE_CASE_ : Union[str, Any] = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) SCREAMING_SNAKE_CASE_ : List[Any] = trainer.evaluate() SCREAMING_SNAKE_CASE_ : int = math.exp(eval_output['eval_loss'] ) SCREAMING_SNAKE_CASE_ : int = {'perplexity': perplexity} SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join(training_args.output_dir , 'eval_results_lm.txt' ) if trainer.is_world_master(): with open(lowerCamelCase_ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , lowerCamelCase_ , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) results.update(lowerCamelCase_ ) return results def __UpperCAmelCase ( lowerCamelCase_ : Any ) -> List[Any]: """simple docstring""" main() if __name__ == "__main__": main()
685
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=True ,snake_case__=False ,snake_case__=True ,snake_case__=99 ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=16 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=4 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = parent SCREAMING_SNAKE_CASE_ : List[Any] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = seq_length SCREAMING_SNAKE_CASE_ : Tuple = is_training SCREAMING_SNAKE_CASE_ : List[str] = use_input_mask SCREAMING_SNAKE_CASE_ : List[str] = use_token_type_ids SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : int = vocab_size SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE_ : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Any = intermediate_size SCREAMING_SNAKE_CASE_ : Dict = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = type_vocab_size SCREAMING_SNAKE_CASE_ : int = type_sequence_label_size SCREAMING_SNAKE_CASE_ : int = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : List[str] = num_choices SCREAMING_SNAKE_CASE_ : Tuple = scope def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Dict = None if self.use_labels: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size] ,self.num_choices ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case ( self ): return LlamaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = LlamaModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,attention_mask=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaModel(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE_ : List[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,use_cache=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ : str = ids_tensor((self.batch_size, 3) ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor((self.batch_size, 3) ,vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_ids, next_tokens] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_mask, next_mask] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Dict = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] # select random slice SCREAMING_SNAKE_CASE_ : Any = ids_tensor((1,) ,output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ : str = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE_ : str = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __a : int = (LlamaForCausalLM,) if is_torch_available() else () __a : Any = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) __a : Tuple = False __a : Tuple = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaModelTester(self ) SCREAMING_SNAKE_CASE_ : List[str] = ConfigTester(self ,config_class=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_ : Optional[int] = type self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Dict = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : str = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Any = 3 SCREAMING_SNAKE_CASE_ : int = 'single_label_classification' SCREAMING_SNAKE_CASE_ : str = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Dict = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Tuple = 3 SCREAMING_SNAKE_CASE_ : str = 'multi_label_classification' SCREAMING_SNAKE_CASE_ : int = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Tuple = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor( [self.model_tester.batch_size, config.num_labels] ,self.model_tester.type_sequence_label_size ).to(torch.float ) SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def snake_case ( self ): pass @parameterized.expand([('linear',), ('dynamic',)] ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([1, 10] ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : Any = LlamaModel(snake_case__ ) original_model.to(snake_case__ ) original_model.eval() SCREAMING_SNAKE_CASE_ : int = original_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : List[Any] = original_model(snake_case__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : List[Any] = {'type': scaling_type, 'factor': 10.0} SCREAMING_SNAKE_CASE_ : int = LlamaModel(snake_case__ ) scaled_model.to(snake_case__ ) scaled_model.eval() SCREAMING_SNAKE_CASE_ : str = scaled_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : Optional[int] = scaled_model(snake_case__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : List[str] = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : int = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : Dict = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : str = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : int = model(torch.tensor(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] ,dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # fmt: off SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' SCREAMING_SNAKE_CASE_ : List[str] = 'Simply put, the theory of relativity states that ' SCREAMING_SNAKE_CASE_ : str = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.encode(snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Tuple = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' ,device_map='sequential' ,use_safetensors=snake_case__ ) # greedy generation outputs SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.generate(snake_case__ ,max_new_tokens=64 ,top_p=snake_case__ ,temperature=1 ,do_sample=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.decode(generated_ids[0] ,skip_special_tokens=snake_case__ ) self.assertEqual(snake_case__ ,snake_case__ )
685
1
from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES UpperCamelCase__ : Any = logging.get_logger(__name__) UpperCamelCase__ : List[str] = OrderedDict( [ # Base model mapping ('''albert''', '''FlaxAlbertModel'''), ('''bart''', '''FlaxBartModel'''), ('''beit''', '''FlaxBeitModel'''), ('''bert''', '''FlaxBertModel'''), ('''big_bird''', '''FlaxBigBirdModel'''), ('''blenderbot''', '''FlaxBlenderbotModel'''), ('''blenderbot-small''', '''FlaxBlenderbotSmallModel'''), ('''clip''', '''FlaxCLIPModel'''), ('''distilbert''', '''FlaxDistilBertModel'''), ('''electra''', '''FlaxElectraModel'''), ('''gpt-sw3''', '''FlaxGPT2Model'''), ('''gpt2''', '''FlaxGPT2Model'''), ('''gpt_neo''', '''FlaxGPTNeoModel'''), ('''gptj''', '''FlaxGPTJModel'''), ('''longt5''', '''FlaxLongT5Model'''), ('''marian''', '''FlaxMarianModel'''), ('''mbart''', '''FlaxMBartModel'''), ('''mt5''', '''FlaxMT5Model'''), ('''opt''', '''FlaxOPTModel'''), ('''pegasus''', '''FlaxPegasusModel'''), ('''regnet''', '''FlaxRegNetModel'''), ('''resnet''', '''FlaxResNetModel'''), ('''roberta''', '''FlaxRobertaModel'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormModel'''), ('''roformer''', '''FlaxRoFormerModel'''), ('''t5''', '''FlaxT5Model'''), ('''vision-text-dual-encoder''', '''FlaxVisionTextDualEncoderModel'''), ('''vit''', '''FlaxViTModel'''), ('''wav2vec2''', '''FlaxWav2Vec2Model'''), ('''whisper''', '''FlaxWhisperModel'''), ('''xglm''', '''FlaxXGLMModel'''), ('''xlm-roberta''', '''FlaxXLMRobertaModel'''), ] ) UpperCamelCase__ : List[str] = OrderedDict( [ # Model for pre-training mapping ('''albert''', '''FlaxAlbertForPreTraining'''), ('''bart''', '''FlaxBartForConditionalGeneration'''), ('''bert''', '''FlaxBertForPreTraining'''), ('''big_bird''', '''FlaxBigBirdForPreTraining'''), ('''electra''', '''FlaxElectraForPreTraining'''), ('''longt5''', '''FlaxLongT5ForConditionalGeneration'''), ('''mbart''', '''FlaxMBartForConditionalGeneration'''), ('''mt5''', '''FlaxMT5ForConditionalGeneration'''), ('''roberta''', '''FlaxRobertaForMaskedLM'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForMaskedLM'''), ('''roformer''', '''FlaxRoFormerForMaskedLM'''), ('''t5''', '''FlaxT5ForConditionalGeneration'''), ('''wav2vec2''', '''FlaxWav2Vec2ForPreTraining'''), ('''whisper''', '''FlaxWhisperForConditionalGeneration'''), ('''xlm-roberta''', '''FlaxXLMRobertaForMaskedLM'''), ] ) UpperCamelCase__ : str = OrderedDict( [ # Model for Masked LM mapping ('''albert''', '''FlaxAlbertForMaskedLM'''), ('''bart''', '''FlaxBartForConditionalGeneration'''), ('''bert''', '''FlaxBertForMaskedLM'''), ('''big_bird''', '''FlaxBigBirdForMaskedLM'''), ('''distilbert''', '''FlaxDistilBertForMaskedLM'''), ('''electra''', '''FlaxElectraForMaskedLM'''), ('''mbart''', '''FlaxMBartForConditionalGeneration'''), ('''roberta''', '''FlaxRobertaForMaskedLM'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForMaskedLM'''), ('''roformer''', '''FlaxRoFormerForMaskedLM'''), ('''xlm-roberta''', '''FlaxXLMRobertaForMaskedLM'''), ] ) UpperCamelCase__ : Dict = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ('''bart''', '''FlaxBartForConditionalGeneration'''), ('''blenderbot''', '''FlaxBlenderbotForConditionalGeneration'''), ('''blenderbot-small''', '''FlaxBlenderbotSmallForConditionalGeneration'''), ('''encoder-decoder''', '''FlaxEncoderDecoderModel'''), ('''longt5''', '''FlaxLongT5ForConditionalGeneration'''), ('''marian''', '''FlaxMarianMTModel'''), ('''mbart''', '''FlaxMBartForConditionalGeneration'''), ('''mt5''', '''FlaxMT5ForConditionalGeneration'''), ('''pegasus''', '''FlaxPegasusForConditionalGeneration'''), ('''t5''', '''FlaxT5ForConditionalGeneration'''), ] ) UpperCamelCase__ : Optional[Any] = OrderedDict( [ # Model for Image-classsification ('''beit''', '''FlaxBeitForImageClassification'''), ('''regnet''', '''FlaxRegNetForImageClassification'''), ('''resnet''', '''FlaxResNetForImageClassification'''), ('''vit''', '''FlaxViTForImageClassification'''), ] ) UpperCamelCase__ : Any = OrderedDict( [ ('''vision-encoder-decoder''', '''FlaxVisionEncoderDecoderModel'''), ] ) UpperCamelCase__ : str = OrderedDict( [ # Model for Causal LM mapping ('''bart''', '''FlaxBartForCausalLM'''), ('''bert''', '''FlaxBertForCausalLM'''), ('''big_bird''', '''FlaxBigBirdForCausalLM'''), ('''electra''', '''FlaxElectraForCausalLM'''), ('''gpt-sw3''', '''FlaxGPT2LMHeadModel'''), ('''gpt2''', '''FlaxGPT2LMHeadModel'''), ('''gpt_neo''', '''FlaxGPTNeoForCausalLM'''), ('''gptj''', '''FlaxGPTJForCausalLM'''), ('''opt''', '''FlaxOPTForCausalLM'''), ('''roberta''', '''FlaxRobertaForCausalLM'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForCausalLM'''), ('''xglm''', '''FlaxXGLMForCausalLM'''), ('''xlm-roberta''', '''FlaxXLMRobertaForCausalLM'''), ] ) UpperCamelCase__ : int = OrderedDict( [ # Model for Sequence Classification mapping ('''albert''', '''FlaxAlbertForSequenceClassification'''), ('''bart''', '''FlaxBartForSequenceClassification'''), ('''bert''', '''FlaxBertForSequenceClassification'''), ('''big_bird''', '''FlaxBigBirdForSequenceClassification'''), ('''distilbert''', '''FlaxDistilBertForSequenceClassification'''), ('''electra''', '''FlaxElectraForSequenceClassification'''), ('''mbart''', '''FlaxMBartForSequenceClassification'''), ('''roberta''', '''FlaxRobertaForSequenceClassification'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForSequenceClassification'''), ('''roformer''', '''FlaxRoFormerForSequenceClassification'''), ('''xlm-roberta''', '''FlaxXLMRobertaForSequenceClassification'''), ] ) UpperCamelCase__ : int = OrderedDict( [ # Model for Question Answering mapping ('''albert''', '''FlaxAlbertForQuestionAnswering'''), ('''bart''', '''FlaxBartForQuestionAnswering'''), ('''bert''', '''FlaxBertForQuestionAnswering'''), ('''big_bird''', '''FlaxBigBirdForQuestionAnswering'''), ('''distilbert''', '''FlaxDistilBertForQuestionAnswering'''), ('''electra''', '''FlaxElectraForQuestionAnswering'''), ('''mbart''', '''FlaxMBartForQuestionAnswering'''), ('''roberta''', '''FlaxRobertaForQuestionAnswering'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForQuestionAnswering'''), ('''roformer''', '''FlaxRoFormerForQuestionAnswering'''), ('''xlm-roberta''', '''FlaxXLMRobertaForQuestionAnswering'''), ] ) UpperCamelCase__ : Tuple = OrderedDict( [ # Model for Token Classification mapping ('''albert''', '''FlaxAlbertForTokenClassification'''), ('''bert''', '''FlaxBertForTokenClassification'''), ('''big_bird''', '''FlaxBigBirdForTokenClassification'''), ('''distilbert''', '''FlaxDistilBertForTokenClassification'''), ('''electra''', '''FlaxElectraForTokenClassification'''), ('''roberta''', '''FlaxRobertaForTokenClassification'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForTokenClassification'''), ('''roformer''', '''FlaxRoFormerForTokenClassification'''), ('''xlm-roberta''', '''FlaxXLMRobertaForTokenClassification'''), ] ) UpperCamelCase__ : Dict = OrderedDict( [ # Model for Multiple Choice mapping ('''albert''', '''FlaxAlbertForMultipleChoice'''), ('''bert''', '''FlaxBertForMultipleChoice'''), ('''big_bird''', '''FlaxBigBirdForMultipleChoice'''), ('''distilbert''', '''FlaxDistilBertForMultipleChoice'''), ('''electra''', '''FlaxElectraForMultipleChoice'''), ('''roberta''', '''FlaxRobertaForMultipleChoice'''), ('''roberta-prelayernorm''', '''FlaxRobertaPreLayerNormForMultipleChoice'''), ('''roformer''', '''FlaxRoFormerForMultipleChoice'''), ('''xlm-roberta''', '''FlaxXLMRobertaForMultipleChoice'''), ] ) UpperCamelCase__ : Optional[int] = OrderedDict( [ ('''bert''', '''FlaxBertForNextSentencePrediction'''), ] ) UpperCamelCase__ : Dict = OrderedDict( [ ('''speech-encoder-decoder''', '''FlaxSpeechEncoderDecoderModel'''), ('''whisper''', '''FlaxWhisperForConditionalGeneration'''), ] ) UpperCamelCase__ : List[Any] = OrderedDict( [ ('''whisper''', '''FlaxWhisperForAudioClassification'''), ] ) UpperCamelCase__ : Union[str, Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) UpperCamelCase__ : Optional[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) UpperCamelCase__ : Optional[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) UpperCamelCase__ : Optional[Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) UpperCamelCase__ : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase__ : int = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) UpperCamelCase__ : Optional[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) UpperCamelCase__ : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase__ : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) UpperCamelCase__ : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase__ : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) UpperCamelCase__ : Union[str, Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) UpperCamelCase__ : Optional[int] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) UpperCamelCase__ : Tuple = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Dict = FLAX_MODEL_MAPPING UpperCamelCase__ : int = auto_class_update(FlaxAutoModel) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : List[str] = FLAX_MODEL_FOR_PRETRAINING_MAPPING UpperCamelCase__ : Dict = auto_class_update(FlaxAutoModelForPreTraining, head_doc='''pretraining''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : List[str] = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING UpperCamelCase__ : Union[str, Any] = auto_class_update(FlaxAutoModelForCausalLM, head_doc='''causal language modeling''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : int = FLAX_MODEL_FOR_MASKED_LM_MAPPING UpperCamelCase__ : str = auto_class_update(FlaxAutoModelForMaskedLM, head_doc='''masked language modeling''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Any = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING UpperCamelCase__ : List[Any] = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc='''sequence-to-sequence language modeling''', checkpoint_for_example='''t5-base''' ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Any = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING UpperCamelCase__ : Tuple = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc='''sequence classification''' ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : int = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING UpperCamelCase__ : str = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc='''question answering''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Union[str, Any] = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase__ : Tuple = auto_class_update( FlaxAutoModelForTokenClassification, head_doc='''token classification''' ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : str = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING UpperCamelCase__ : Tuple = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc='''multiple choice''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Dict = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING UpperCamelCase__ : Union[str, Any] = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc='''next sentence prediction''' ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : Any = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING UpperCamelCase__ : Optional[int] = auto_class_update( FlaxAutoModelForImageClassification, head_doc='''image classification''' ) class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : int = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING UpperCamelCase__ : int = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc='''vision-to-text modeling''') class lowerCAmelCase_ ( _BaseAutoModelClass ): __a : List[str] = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING UpperCamelCase__ : Optional[int] = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc='''sequence-to-sequence speech-to-text modeling''' )
685
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''} UpperCamelCase__ : int = { '''vocab_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''', }, '''emoji_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''', }, } UpperCamelCase__ : str = { '''abeja/gpt-neox-japanese-2.7b''': 20_48, } def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Tuple ) -> List[str]: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[Any] = json.loads(f.read() ) SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : List[Any] = collections.OrderedDict() with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Any = f.readlines() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = b SCREAMING_SNAKE_CASE_ : Dict = idx for wd in b: SCREAMING_SNAKE_CASE_ : Any = idx return vocab, raw_vocab, ids_to_tokens, emoji class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = VOCAB_FILES_NAMES __a : List[str] = PRETRAINED_VOCAB_FILES_MAP __a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__="<|endoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__="<|startoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__=False ,**snake_case__ ,): super().__init__( unk_token=snake_case__ ,pad_token=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,do_clean_text=snake_case__ ,**snake_case__ ,) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained' ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google' ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) SCREAMING_SNAKE_CASE_ : str = do_clean_text SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = load_vocab_and_emoji(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = SubWordJapaneseTokenizer( vocab=self.vocab ,ids_to_tokens=self.ids_to_tokens ,emoji=self.emoji ) @property def snake_case ( self ): # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def snake_case ( self ): return dict(self.raw_vocab ,**self.added_tokens_encoder ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.tokenize(snake_case__ ,clean=self.do_clean_text ) def snake_case ( self ,snake_case__ ): return self.vocab.get(snake_case__ ,self.vocab.get(self.unk_token ) ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.convert_id_to_token(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = ''.join(snake_case__ ).strip() return out_string def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(snake_case__ ,add_special_tokens=snake_case__ ) + [self.eos_token_id] ) if len(snake_case__ ) > self.model_max_length: SCREAMING_SNAKE_CASE_ : List[Any] = input_ids[-self.model_max_length :] return input_ids def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 if os.path.isdir(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: SCREAMING_SNAKE_CASE_ : Tuple = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : str = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.' ' Please check that the vocabulary is not corrupted!' ) SCREAMING_SNAKE_CASE_ : Dict = token_index writer.write(','.join(snake_case__ ) + '\n' ) index += 1 with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: json.dump(self.emoji ,snake_case__ ) return vocab_file, emoji_file class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = vocab # same as swe SCREAMING_SNAKE_CASE_ : Optional[int] = ids_to_tokens # same as bpe SCREAMING_SNAKE_CASE_ : Dict = emoji SCREAMING_SNAKE_CASE_ : int = np.max([len(snake_case__ ) for w in self.vocab.keys()] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) SCREAMING_SNAKE_CASE_ : str = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' SCREAMING_SNAKE_CASE_ : int = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' SCREAMING_SNAKE_CASE_ : Tuple = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self ): return len(self.ids_to_tokens ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<URL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.content_repattera.sub('<EMAIL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<TEL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<PRICE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: SCREAMING_SNAKE_CASE_ : Union[str, Any] = content.replace('<BLOCK><BLOCK>' ,'<BLOCK>' ) return content def snake_case ( self ,snake_case__ ,snake_case__=False ): SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('\r\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\r' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : List[str] = text.replace('\t' ,'<TAB>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('—' ,'ー' ) SCREAMING_SNAKE_CASE_ : Optional[int] = text.replace('−' ,'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: SCREAMING_SNAKE_CASE_ : int = text.replace(snake_case__ ,snake_case__ ) if clean: SCREAMING_SNAKE_CASE_ : str = self.clean_text(snake_case__ ) def check_simbol(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 2: SCREAMING_SNAKE_CASE_ : str = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0XC2A1 and c <= 0XC2BF) or (c >= 0XC780 and c <= 0XC783) or (c >= 0XCAB9 and c <= 0XCBBF) or (c >= 0XCC80 and c <= 0XCDA2) ): return True return False def checkuae(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 3: SCREAMING_SNAKE_CASE_ : Dict = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0XE2_8080 and c <= 0XE2_B07F: return True return False SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : List[Any] = [] while pos < len(snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = min(len(snake_case__ ) ,pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 SCREAMING_SNAKE_CASE_ : List[Any] = [] # (token_id, token, pos) for e in range(snake_case__ ,snake_case__ ,-1 ): SCREAMING_SNAKE_CASE_ : str = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(snake_case__ ) > 2: SCREAMING_SNAKE_CASE_ : Optional[Any] = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(snake_case__ ) > 0: # the smallest token_id is adopted SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = sorted(snake_case__ ,key=lambda snake_case__ : x[0] )[0] result.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = e else: SCREAMING_SNAKE_CASE_ : Any = pos + 1 SCREAMING_SNAKE_CASE_ : Optional[int] = text[pos:end] if check_simbol(snake_case__ ): result.append('<KIGOU>' ) elif checkuae(snake_case__ ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) SCREAMING_SNAKE_CASE_ : int = end return result def snake_case ( self ,snake_case__ ,snake_case__="\n" ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Dict = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : Dict = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(snake_case__ ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(snake_case__ ) if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : int = ''.join(snake_case__ ) return text
685
1
import gc import unittest from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxControlNetModel.from_pretrained( 'lllyasviel/sd-controlnet-canny' ,from_pt=snake_case__ ,dtype=jnp.bfloataa ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = FlaxStableDiffusionControlNetPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' ,controlnet=snake_case__ ,from_pt=snake_case__ ,dtype=jnp.bfloataa ) SCREAMING_SNAKE_CASE_ : Optional[int] = controlnet_params SCREAMING_SNAKE_CASE_ : Any = 'bird' SCREAMING_SNAKE_CASE_ : Optional[Any] = jax.device_count() SCREAMING_SNAKE_CASE_ : int = pipe.prepare_text_inputs([prompts] * num_samples ) SCREAMING_SNAKE_CASE_ : List[Any] = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = pipe.prepare_image_inputs([canny_image] * num_samples ) SCREAMING_SNAKE_CASE_ : List[str] = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE_ : Tuple = jax.random.split(snake_case__ ,jax.device_count() ) SCREAMING_SNAKE_CASE_ : Optional[int] = replicate(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = shard(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = shard(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = pipe( prompt_ids=snake_case__ ,image=snake_case__ ,params=snake_case__ ,prng_seed=snake_case__ ,num_inference_steps=50 ,jit=snake_case__ ,).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) SCREAMING_SNAKE_CASE_ : Optional[Any] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE_ : Any = images[0, 253:256, 253:256, -1] SCREAMING_SNAKE_CASE_ : Dict = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE_ : Optional[int] = jnp.array( [0.167969, 0.116699, 0.081543, 0.154297, 0.132812, 0.108887, 0.169922, 0.169922, 0.205078] ) print(F'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = FlaxControlNetModel.from_pretrained( 'lllyasviel/sd-controlnet-openpose' ,from_pt=snake_case__ ,dtype=jnp.bfloataa ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = FlaxStableDiffusionControlNetPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' ,controlnet=snake_case__ ,from_pt=snake_case__ ,dtype=jnp.bfloataa ) SCREAMING_SNAKE_CASE_ : int = controlnet_params SCREAMING_SNAKE_CASE_ : str = 'Chef in the kitchen' SCREAMING_SNAKE_CASE_ : List[Any] = jax.device_count() SCREAMING_SNAKE_CASE_ : Tuple = pipe.prepare_text_inputs([prompts] * num_samples ) SCREAMING_SNAKE_CASE_ : Dict = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png' ) SCREAMING_SNAKE_CASE_ : Dict = pipe.prepare_image_inputs([pose_image] * num_samples ) SCREAMING_SNAKE_CASE_ : str = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = jax.random.split(snake_case__ ,jax.device_count() ) SCREAMING_SNAKE_CASE_ : Optional[Any] = replicate(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = shard(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = shard(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = pipe( prompt_ids=snake_case__ ,image=snake_case__ ,params=snake_case__ ,prng_seed=snake_case__ ,num_inference_steps=50 ,jit=snake_case__ ,).images assert images.shape == (jax.device_count(), 1, 768, 512, 3) SCREAMING_SNAKE_CASE_ : int = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE_ : Any = images[0, 253:256, 253:256, -1] SCREAMING_SNAKE_CASE_ : List[Any] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE_ : Tuple = jnp.array( [[0.271484, 0.261719, 0.275391, 0.277344, 0.279297, 0.291016, 0.294922, 0.302734, 0.302734]] ) print(F'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
685
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : int=() , lowerCamelCase_ : Union[str, Any]=None , lowerCamelCase_ : Optional[int]="no" , lowerCamelCase_ : Optional[Any]="29500" ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False if any(key.startswith('KAGGLE' ) for key in os.environ.keys() ): SCREAMING_SNAKE_CASE_ : str = True elif "IPython" in sys.modules: SCREAMING_SNAKE_CASE_ : Dict = 'google.colab' in str(sys.modules['IPython'].get_ipython() ) try: SCREAMING_SNAKE_CASE_ : Optional[int] = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F'Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.' ) if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME' , lowerCamelCase_ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ' 'your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if num_processes is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = 8 SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='TPU' ) print(F'Launching a training on {num_processes} TPU cores.' ) xmp.spawn(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on one CPU.' ) function(*lowerCamelCase_ ) else: if num_processes is None: raise ValueError( 'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ' 'inside your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if torch.cuda.is_initialized(): raise ValueError( 'To launch a multi-GPU training from your notebook, you need to avoid running any instruction ' 'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ' 'function.' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port=lowerCamelCase_ , mixed_precision=lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='MULTI_GPU' ) print(F'Launching training on {num_processes} GPUs.' ) try: start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( 'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ' 'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ' 'Please review your imports and test them when running the `notebook_launcher()` to identify ' 'which one is problematic.' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): SCREAMING_SNAKE_CASE_ : Optional[Any] = '1' print('Launching training on MPS.' ) elif torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on CPU.' ) function(*lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple=() , lowerCamelCase_ : str=2 ) -> Union[str, Any]: """simple docstring""" from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port='29500' , accelerate_mixed_precision='no' , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu='yes' , ): SCREAMING_SNAKE_CASE_ : Tuple = PrepareForLaunch(lowerCamelCase_ , debug=lowerCamelCase_ ) start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' )
685
1
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
from typing import TYPE_CHECKING from ....utils import _LazyModule UpperCamelCase__ : Tuple = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
685
1
from jiwer import compute_measures import datasets UpperCamelCase__ : str = '''\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } ''' UpperCamelCase__ : Optional[int] = '''\ Word error rate (WER) is a common metric of the performance of an automatic speech recognition system. The general difficulty of measuring performance lies in the fact that the recognized word sequence can have a different length from the reference word sequence (supposedly the correct one). The WER is derived from the Levenshtein distance, working at the word level instead of the phoneme level. The WER is a valuable tool for comparing different systems as well as for evaluating improvements within one system. This kind of measurement, however, provides no details on the nature of translation errors and further work is therefore required to identify the main source(s) of error and to focus any research effort. This problem is solved by first aligning the recognized word sequence with the reference (spoken) word sequence using dynamic string alignment. Examination of this issue is seen through a theory called the power law that states the correlation between perplexity and word error rate. Word error rate can then be computed as: WER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct words, N is the number of words in the reference (N=S+D+C). This value indicates the average number of errors per reference word. The lower the value, the better the performance of the ASR system with a WER of 0 being a perfect score. ''' UpperCamelCase__ : str = ''' Compute WER score of transcribed segments against references. Args: references: List of references for each speech input. predictions: List of transcriptions to score. concatenate_texts (bool, default=False): Whether to concatenate all input texts or compute WER iteratively. Returns: (float): the word error rate Examples: >>> predictions = ["this is the prediction", "there is an other sample"] >>> references = ["this is the reference", "there is another one"] >>> wer = datasets.load_metric("wer") >>> wer_score = wer.compute(predictions=predictions, references=references) >>> print(wer_score) 0.5 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Value('string' ,id='sequence' ), 'references': datasets.Value('string' ,id='sequence' ), } ) ,codebase_urls=['https://github.com/jitsi/jiwer/'] ,reference_urls=[ 'https://en.wikipedia.org/wiki/Word_error_rate', ] ,) def snake_case ( self ,snake_case__=None ,snake_case__=None ,snake_case__=False ): if concatenate_texts: return compute_measures(snake_case__ ,snake_case__ )["wer"] else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 0 for prediction, reference in zip(snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = compute_measures(snake_case__ ,snake_case__ ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
685
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = CLIPTokenizer __a : List[str] = CLIPTokenizerFast __a : List[str] = True __a : Tuple = {} __a : Tuple = False def snake_case ( self ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE_ : Union[str, Any] = dict(zip(snake_case__ ,range(len(snake_case__ ) ) ) ) SCREAMING_SNAKE_CASE_ : Any = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] SCREAMING_SNAKE_CASE_ : Any = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case__ ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case__ ) ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = 'lower newer' SCREAMING_SNAKE_CASE_ : Tuple = 'lower newer' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = CLIPTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) SCREAMING_SNAKE_CASE_ : List[Any] = 'lower newer' SCREAMING_SNAKE_CASE_ : Optional[Any] = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] SCREAMING_SNAKE_CASE_ : Any = tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ : Optional[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) @require_ftfy def snake_case ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Any = self.tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.rust_tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways SCREAMING_SNAKE_CASE_ : Dict = 'xa\u0303y' + ' ' + 'x\xe3y' SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of space type SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of line break type SCREAMING_SNAKE_CASE_ : Tuple = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) def snake_case ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` SCREAMING_SNAKE_CASE_ : Tuple = F'{text_of_1_token} {text_of_1_token}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(snake_case__ ) + 1, len(snake_case__ ) + 1 + len(snake_case__ )) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = F' {text}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(1, 1 + len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(snake_case__ ) + 1, 1 + len(snake_case__ ) + 1 + len(snake_case__ )) ,) def snake_case ( self ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(snake_case__ ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def snake_case ( self ): super().test_tokenization_python_rust_equals() def snake_case ( self ): # CLIP always lower cases letters pass
685
1
from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class lowerCAmelCase_ ( lowerCamelCase_ ): def __lt__( self ,snake_case__ ): return self[-1] < other[-1] def __eq__( self ,snake_case__ ): return self[-1] == other[-1] def __UpperCAmelCase ( lowerCamelCase_ : list ) -> list: """simple docstring""" SCREAMING_SNAKE_CASE_ : list[Stack] = [] # sort into stacks for element in collection: SCREAMING_SNAKE_CASE_ : List[str] = Stack([element] ) SCREAMING_SNAKE_CASE_ : int = bisect_left(lowerCamelCase_ , lowerCamelCase_ ) if i != len(lowerCamelCase_ ): stacks[i].append(lowerCamelCase_ ) else: stacks.append(lowerCamelCase_ ) # use a heap-based merge to merge stack efficiently SCREAMING_SNAKE_CASE_ : List[Any] = merge(*(reversed(lowerCamelCase_ ) for stack in stacks) ) return collection if __name__ == "__main__": UpperCamelCase__ : Optional[Any] = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase__ : Tuple = [int(item) for item in user_input.split(''',''')] print(patience_sort(unsorted))
685
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCAmelCase ( ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) SCREAMING_SNAKE_CASE_ : int = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(lowerCamelCase_ ) DownloadCommand.register_subcommand(lowerCamelCase_ ) EnvironmentCommand.register_subcommand(lowerCamelCase_ ) RunCommand.register_subcommand(lowerCamelCase_ ) ServeCommand.register_subcommand(lowerCamelCase_ ) UserCommands.register_subcommand(lowerCamelCase_ ) AddNewModelCommand.register_subcommand(lowerCamelCase_ ) AddNewModelLikeCommand.register_subcommand(lowerCamelCase_ ) LfsCommands.register_subcommand(lowerCamelCase_ ) PTtoTFCommand.register_subcommand(lowerCamelCase_ ) # Let's go SCREAMING_SNAKE_CASE_ : Optional[int] = parser.parse_args() if not hasattr(lowerCamelCase_ , 'func' ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE_ : Optional[Any] = args.func(lowerCamelCase_ ) service.run() if __name__ == "__main__": main()
685
1
from sklearn.metrics import mean_squared_error import datasets UpperCamelCase__ : Optional[int] = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' UpperCamelCase__ : str = '''\ Mean Squared Error(MSE) is the average of the square of difference between the predicted and actual values. ''' UpperCamelCase__ : str = ''' Args: predictions: array-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. references: array-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. sample_weight: array-like of shape (n_samples,), default=None Sample weights. multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average" Defines aggregating of multiple output values. Array-like value defines weights used to average errors. "raw_values" : Returns a full set of errors in case of multioutput input. "uniform_average" : Errors of all outputs are averaged with uniform weight. squared : bool, default=True If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value. Returns: mse : mean squared error. Examples: >>> mse_metric = datasets.load_metric("mse") >>> predictions = [2.5, 0.0, 2, 8] >>> references = [3, -0.5, 2, 7] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.375} >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False) >>> print(rmse_result) {\'mse\': 0.6123724356957945} If you\'re using multi-dimensional lists, then set the config as follows : >>> mse_metric = datasets.load_metric("mse", "multilist") >>> predictions = [[0.5, 1], [-1, 1], [7, -6]] >>> references = [[0, 2], [-1, 2], [8, -5]] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.7083333333333334} >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\') >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mse\': array([0.41666667, 1. ])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features(self._get_feature_types() ) ,reference_urls=[ 'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html' ] ,) def snake_case ( self ): if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('float' ) ), "references": datasets.Sequence(datasets.Value('float' ) ), } else: return { "predictions": datasets.Value('float' ), "references": datasets.Value('float' ), } def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__=None ,snake_case__="uniform_average" ,snake_case__=True ): SCREAMING_SNAKE_CASE_ : Any = mean_squared_error( snake_case__ ,snake_case__ ,sample_weight=snake_case__ ,multioutput=snake_case__ ,squared=snake_case__ ) return {"mse": mse}
685
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging UpperCamelCase__ : Dict = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) UpperCamelCase__ : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'https://pypi.org/pypi/diffusers/json' SCREAMING_SNAKE_CASE_ : Optional[int] = json.loads(request.urlopen(lowerCamelCase_ ).read() )['releases'].keys() return sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : version.Version(lowerCamelCase_ ) ) def __UpperCAmelCase ( ) -> Tuple: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(lowerCamelCase_ ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : List[Any] = f.read() # Imports of the form `import .xxx` SCREAMING_SNAKE_CASE_ : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Unique-ify return list(set(lowerCamelCase_ ) ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : Union[str, Any] = [module_file] SCREAMING_SNAKE_CASE_ : Tuple = [] # Let's recurse through all relative imports while not no_change: SCREAMING_SNAKE_CASE_ : int = [] for f in files_to_check: new_imports.extend(get_relative_imports(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ).parent SCREAMING_SNAKE_CASE_ : int = [str(module_path / m ) for m in new_imports] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [f for f in new_import_files if f not in all_relative_imports] SCREAMING_SNAKE_CASE_ : Any = [F'{f}.py' for f in new_import_files] SCREAMING_SNAKE_CASE_ : Optional[int] = len(lowerCamelCase_ ) == 0 all_relative_imports.extend(lowerCamelCase_ ) return all_relative_imports def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() # Imports of the form `import xxx` SCREAMING_SNAKE_CASE_ : List[str] = re.findall('^\s*import\s+(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Only keep the top-level module SCREAMING_SNAKE_CASE_ : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(set(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = [] for imp in imports: try: importlib.import_module(lowerCamelCase_ ) except ImportError: missing_packages.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' F'{", ".join(lowerCamelCase_ )}. Run `pip install {" ".join(lowerCamelCase_ )}`' ) return get_relative_imports(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = module_path.replace(os.path.sep , '.' ) SCREAMING_SNAKE_CASE_ : Any = importlib.import_module(lowerCamelCase_ ) if class_name is None: return find_pipeline_class(lowerCamelCase_ ) return getattr(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" from ..pipelines import DiffusionPipeline SCREAMING_SNAKE_CASE_ : List[Any] = dict(inspect.getmembers(lowerCamelCase_ , inspect.isclass ) ) SCREAMING_SNAKE_CASE_ : List[str] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , lowerCamelCase_ ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:' F' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in' F' {loaded_module}.' ) SCREAMING_SNAKE_CASE_ : Any = cls return pipeline_class def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) if os.path.isfile(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = module_file_or_url SCREAMING_SNAKE_CASE_ : Dict = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: SCREAMING_SNAKE_CASE_ : List[str] = get_diffusers_versions() # cut ".dev0" SCREAMING_SNAKE_CASE_ : Dict = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: SCREAMING_SNAKE_CASE_ : List[Any] = latest_version if latest_version[1:] in available_versions else 'main' logger.info(F'Defaulting to latest_version: {revision}.' ) elif revision in available_versions: SCREAMING_SNAKE_CASE_ : int = F'v{revision}' elif revision == "main": SCREAMING_SNAKE_CASE_ : List[Any] = revision else: raise ValueError( F'`custom_revision`: {revision} does not exist. Please make sure to choose one of' F' {", ".join(available_versions + ["main"] )}.' ) # community pipeline on GitHub SCREAMING_SNAKE_CASE_ : Tuple = COMMUNITY_PIPELINES_URL.format(revision=lowerCamelCase_ , pipeline=lowerCamelCase_ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = cached_download( lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Tuple = 'git' SCREAMING_SNAKE_CASE_ : Dict = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise else: try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : List[str] = hf_hub_download( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise # Check we have all the requirements in our environment SCREAMING_SNAKE_CASE_ : Dict = check_imports(lowerCamelCase_ ) # Now we move the module inside our cached dynamic modules. SCREAMING_SNAKE_CASE_ : Union[str, Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = Path(lowerCamelCase_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(lowerCamelCase_ , submodule_path / module_file ) for module_needed in modules_needed: SCREAMING_SNAKE_CASE_ : Union[str, Any] = F'{module_needed}.py' shutil.copy(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = use_auth_token elif use_auth_token is True: SCREAMING_SNAKE_CASE_ : int = HfFolder.get_token() else: SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[Any] = model_info(lowerCamelCase_ , revision=lowerCamelCase_ , token=lowerCamelCase_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. SCREAMING_SNAKE_CASE_ : Any = submodule_path / commit_hash SCREAMING_SNAKE_CASE_ : List[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(lowerCamelCase_ ) if not (submodule_path / module_file).exists(): shutil.copy(lowerCamelCase_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( lowerCamelCase_ , F'{module_needed}.py' , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return os.path.join(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , **lowerCamelCase_ : Dict , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_cached_module_file( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return get_class_in_module(lowerCamelCase_ , final_module.replace('.py' , '' ) )
685
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase__ : int = {'''configuration_vit_mae''': ['''VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMAEConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[int] = [ '''VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMAEForPreTraining''', '''ViTMAELayer''', '''ViTMAEModel''', '''ViTMAEPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[str] = [ '''TFViTMAEForPreTraining''', '''TFViTMAEModel''', '''TFViTMAEPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_mae import VIT_MAE_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMAEConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_mae import ( VIT_MAE_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMAEForPreTraining, ViTMAELayer, ViTMAEModel, ViTMAEPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit_mae import TFViTMAEForPreTraining, TFViTMAEModel, TFViTMAEPreTrainedModel else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "visual_bert" def __init__( self ,snake_case__=30522 ,snake_case__=768 ,snake_case__=512 ,snake_case__=12 ,snake_case__=12 ,snake_case__=3072 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=1E-12 ,snake_case__=False ,snake_case__=True ,snake_case__=1 ,snake_case__=0 ,snake_case__=2 ,**snake_case__ ,): super().__init__(pad_token_id=snake_case__ ,bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = vocab_size SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = visual_embedding_dim SCREAMING_SNAKE_CASE_ : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE_ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = initializer_range SCREAMING_SNAKE_CASE_ : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : Optional[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_ : int = bypass_transformer SCREAMING_SNAKE_CASE_ : Optional[Any] = special_visual_initialize
685
1
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=30 ,snake_case__=2 ,snake_case__=3 ,snake_case__=True ,snake_case__=True ,snake_case__=32 ,snake_case__=2 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=10 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=0.6 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : List[str] = parent SCREAMING_SNAKE_CASE_ : Optional[int] = batch_size SCREAMING_SNAKE_CASE_ : List[Any] = image_size SCREAMING_SNAKE_CASE_ : int = patch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : List[str] = is_training SCREAMING_SNAKE_CASE_ : Optional[int] = use_labels SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE_ : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_ : int = num_attention_heads SCREAMING_SNAKE_CASE_ : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Tuple = hidden_act SCREAMING_SNAKE_CASE_ : List[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE_ : Dict = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = mask_ratio SCREAMING_SNAKE_CASE_ : str = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ : Dict = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ : Tuple = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : List[str] = None if self.use_labels: SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : int = self.get_config() return config, pixel_values, labels def snake_case ( self ): return ViTMAEConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,decoder_hidden_size=self.hidden_size ,decoder_num_hidden_layers=self.num_hidden_layers ,decoder_num_attention_heads=self.num_attention_heads ,decoder_intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,mask_ratio=self.mask_ratio ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = TFViTMAEModel(config=snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,training=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = TFViTMAEForPreTraining(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ,training=snake_case__ ) # expected sequence length = num_patches SCREAMING_SNAKE_CASE_ : str = (self.image_size // self.patch_size) ** 2 SCREAMING_SNAKE_CASE_ : Any = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape ,(self.batch_size, num_patches, expected_num_channels) ) # test greyscale images SCREAMING_SNAKE_CASE_ : Union[str, Any] = 1 SCREAMING_SNAKE_CASE_ : Union[str, Any] = TFViTMAEForPreTraining(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ,training=snake_case__ ) SCREAMING_SNAKE_CASE_ : str = self.patch_size**2 self.parent.assertEqual(result.logits.shape ,(self.batch_size, num_patches, expected_num_channels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.prepare_config_and_inputs() ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : int = config_and_inputs SCREAMING_SNAKE_CASE_ : str = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __a : Dict = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} __a : str = False __a : Tuple = False __a : Union[str, Any] = False __a : Optional[int] = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = TFViTMAEModelTester(self ) SCREAMING_SNAKE_CASE_ : Any = ConfigTester(self ,config_class=snake_case__ ,has_text_modality=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='ViTMAE does not use inputs_embeds' ) def snake_case ( self ): pass def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : Dict = model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() ,(tf.keras.layers.Layer) ) SCREAMING_SNAKE_CASE_ : Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ ,tf.keras.layers.Layer ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : Tuple = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ : Tuple = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*snake_case__ ) def snake_case ( self ): # make the mask reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Dict = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : str = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : Tuple = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,noise=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = copy.deepcopy(self._prepare_for_class(snake_case__ ,snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = model(**snake_case__ ,noise=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = outputs_dict[0].numpy() SCREAMING_SNAKE_CASE_ : List[str] = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) ,1E-6 ) def snake_case ( self ): # make the mask reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Tuple = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : int = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = {} for k, v in inputs_dict.items(): if tf.is_tensor(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = v.numpy() else: SCREAMING_SNAKE_CASE_ : Tuple = np.array(snake_case__ ) return inputs_np_dict for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : str = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_numpy_arrays(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = model(snake_case__ ,noise=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = model(**snake_case__ ,noise=snake_case__ ) self.assert_outputs_same(snake_case__ ,snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): # make masks reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ : List[Any] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) SCREAMING_SNAKE_CASE_ : Tuple = tf.constant(snake_case__ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument SCREAMING_SNAKE_CASE_ : Optional[int] = tf_noise super().check_pt_tf_models(snake_case__ ,snake_case__ ,snake_case__ ) def snake_case ( self ): # make mask reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : str = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(snake_case__ ) if module_member_name.endswith('MainLayer' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('MainLayer' )] == model_class.__name__[: -len('Model' )] for module_member in (getattr(snake_case__ ,snake_case__ ),) if isinstance(snake_case__ ,snake_case__ ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(snake_case__ ,'_keras_serializable' ,snake_case__ ) } SCREAMING_SNAKE_CASE_ : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) SCREAMING_SNAKE_CASE_ : str = tf.convert_to_tensor(snake_case__ ) inputs_dict.update({'noise': noise} ) for main_layer_class in tf_main_layer_classes: SCREAMING_SNAKE_CASE_ : Any = main_layer_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = { name: tf.keras.Input(tensor.shape[1:] ,dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } SCREAMING_SNAKE_CASE_ : Union[str, Any] = tf.keras.Model(snake_case__ ,outputs=main_layer(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ : str = os.path.join(snake_case__ ,'keras_model.h5' ) model.save(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tf.keras.models.load_model( snake_case__ ,custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(snake_case__ ,tf.keras.Model ) SCREAMING_SNAKE_CASE_ : str = model(snake_case__ ) self.assert_outputs_same(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): # make mask reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[Any] = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : List[str] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : int = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ,noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": SCREAMING_SNAKE_CASE_ : Optional[int] = outputs.last_hidden_state.numpy() SCREAMING_SNAKE_CASE_ : Tuple = 0 else: SCREAMING_SNAKE_CASE_ : Any = outputs.logits.numpy() SCREAMING_SNAKE_CASE_ : int = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case__ ,saved_model=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = model_class.from_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model(snake_case__ ,noise=snake_case__ ) if model_class.__name__ == "TFViTMAEModel": SCREAMING_SNAKE_CASE_ : List[Any] = after_outputs['last_hidden_state'].numpy() SCREAMING_SNAKE_CASE_ : Dict = 0 else: SCREAMING_SNAKE_CASE_ : str = after_outputs['logits'].numpy() SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : Any = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(snake_case__ ,1E-5 ) def snake_case ( self ): # make mask reproducible np.random.seed(2 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : int = int((config.image_size // config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : str = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ,noise=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config SCREAMING_SNAKE_CASE_ : int = model_class.from_config(model.config ) SCREAMING_SNAKE_CASE_ : List[Any] = new_model(snake_case__ ) # Build model new_model.set_weights(model.get_weights() ) SCREAMING_SNAKE_CASE_ : str = new_model(snake_case__ ,noise=snake_case__ ) self.assert_outputs_same(snake_case__ ,snake_case__ ) @unittest.skip( reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.' ) def snake_case ( self ): pass @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load' ) def snake_case ( self ): pass @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = TFViTMAEModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(snake_case__ ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case ( self ): return ViTImageProcessor.from_pretrained('facebook/vit-mae-base' ) if is_vision_available() else None @slow def snake_case ( self ): # make random mask reproducible across the PT and TF model np.random.seed(2 ) SCREAMING_SNAKE_CASE_ : List[str] = TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = self.default_image_processor SCREAMING_SNAKE_CASE_ : List[Any] = prepare_img() SCREAMING_SNAKE_CASE_ : str = image_processor(images=snake_case__ ,return_tensors='tf' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) SCREAMING_SNAKE_CASE_ : Tuple = ViTMAEConfig() SCREAMING_SNAKE_CASE_ : List[Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.random.uniform(size=(1, num_patches) ) # forward pass SCREAMING_SNAKE_CASE_ : List[Any] = model(**snake_case__ ,noise=snake_case__ ) # verify the logits SCREAMING_SNAKE_CASE_ : Tuple = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : int = tf.convert_to_tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] ,snake_case__ ,atol=1E-4 )
685
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Union[str, Any]: """simple docstring""" def is_in_circle(lowerCamelCase_ : float , lowerCamelCase_ : float ) -> bool: SCREAMING_SNAKE_CASE_ : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE_ : Optional[int] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(lowerCamelCase_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE_ : Tuple = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Callable[[float], float] , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(lowerCamelCase_ , lowerCamelCase_ ) ) for _ in range(lowerCamelCase_ ) ) * (max_value - min_value) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 ) -> None: """simple docstring""" def identity_function(lowerCamelCase_ : float ) -> float: return x SCREAMING_SNAKE_CASE_ : str = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> None: """simple docstring""" def function_to_integrate(lowerCamelCase_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE_ : Dict = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=30 ,snake_case__=2 ,snake_case__=3 ,snake_case__=True ,snake_case__=True ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=10 ,snake_case__=0.02 ,snake_case__=None ,snake_case__=2 ,): SCREAMING_SNAKE_CASE_ : Tuple = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Optional[int] = image_size SCREAMING_SNAKE_CASE_ : Tuple = patch_size SCREAMING_SNAKE_CASE_ : Optional[int] = num_channels SCREAMING_SNAKE_CASE_ : Dict = is_training SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : Tuple = hidden_size SCREAMING_SNAKE_CASE_ : List[str] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[Any] = num_attention_heads SCREAMING_SNAKE_CASE_ : str = intermediate_size SCREAMING_SNAKE_CASE_ : str = hidden_act SCREAMING_SNAKE_CASE_ : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = type_sequence_label_size SCREAMING_SNAKE_CASE_ : int = initializer_range SCREAMING_SNAKE_CASE_ : str = scope SCREAMING_SNAKE_CASE_ : Optional[int] = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ : Tuple = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ : Tuple = num_patches + 1 def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : str = None if self.use_labels: SCREAMING_SNAKE_CASE_ : int = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Tuple = self.get_config() return config, pixel_values, labels def snake_case ( self ): return ViTConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,encoder_stride=self.encoder_stride ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = ViTModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = ViTForMaskedImageModeling(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ) self.parent.assertEqual( result.reconstruction.shape ,(self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ : str = 1 SCREAMING_SNAKE_CASE_ : List[str] = ViTForMaskedImageModeling(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ) self.parent.assertEqual(result.reconstruction.shape ,(self.batch_size, 1, self.image_size, self.image_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ : Optional[Any] = ViTForImageClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ : Optional[Any] = 1 SCREAMING_SNAKE_CASE_ : List[str] = ViTForImageClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_ : Tuple = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : List[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) __a : Optional[int] = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) __a : Dict = True __a : Union[str, Any] = False __a : str = False __a : Any = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = ViTModelTester(self ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ConfigTester(self ,config_class=snake_case__ ,has_text_modality=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def snake_case ( self ): pass def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : List[Any] = model_class(snake_case__ ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) SCREAMING_SNAKE_CASE_ : Any = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case__ ,nn.Linear ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : Dict = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ : Tuple = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ : Optional[int] = ['pixel_values'] self.assertListEqual(arg_names[:1] ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def snake_case ( self ): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : int = ViTModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def __UpperCAmelCase ( ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case ( self ): return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = self.default_image_processor SCREAMING_SNAKE_CASE_ : List[str] = prepare_img() SCREAMING_SNAKE_CASE_ : Optional[Any] = image_processor(images=snake_case__ ,return_tensors='pt' ).to(snake_case__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(**snake_case__ ) # verify the logits SCREAMING_SNAKE_CASE_ : Optional[int] = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(snake_case__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,snake_case__ ,atol=1E-4 ) ) @slow def snake_case ( self ): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. SCREAMING_SNAKE_CASE_ : Tuple = ViTModel.from_pretrained('facebook/dino-vits8' ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ViTImageProcessor.from_pretrained('facebook/dino-vits8' ,size=480 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_img() SCREAMING_SNAKE_CASE_ : Dict = image_processor(images=snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Dict = inputs.pixel_values.to(snake_case__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,interpolate_pos_encoding=snake_case__ ) # verify the logits SCREAMING_SNAKE_CASE_ : str = torch.Size((1, 3601, 384) ) self.assertEqual(outputs.last_hidden_state.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(snake_case__ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] ,snake_case__ ,atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = ViTModel.from_pretrained('facebook/dino-vits8' ,torch_dtype=torch.floataa ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Dict = self.default_image_processor SCREAMING_SNAKE_CASE_ : Any = prepare_img() SCREAMING_SNAKE_CASE_ : Any = image_processor(images=snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Tuple = inputs.pixel_values.to(snake_case__ ) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ )
685
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
1
def __UpperCAmelCase ( lowerCamelCase_ : list ) -> list: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = len(lowerCamelCase_ ) for _ in range(lowerCamelCase_ ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = arr[i + 1], arr[i] return arr if __name__ == "__main__": UpperCamelCase__ : Optional[Any] = list(range(10, 0, -1)) print(F"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
685
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
1
class lowerCAmelCase_ : def __init__( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = len(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = [0] * len_array if len_array > 0: SCREAMING_SNAKE_CASE_ : List[str] = array[0] for i in range(1 ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = self.prefix_sum[i - 1] + array[i] def snake_case ( self ,snake_case__ ,snake_case__ ): if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(snake_case__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
685
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
from math import log from scipy.constants import Boltzmann, physical_constants UpperCamelCase__ : Any = 3_00 # TEMPERATURE (unit = K) def __UpperCAmelCase ( lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('Donor concentration should be positive' ) elif acceptor_conc <= 0: raise ValueError('Acceptor concentration should be positive' ) elif intrinsic_conc <= 0: raise ValueError('Intrinsic concentration should be positive' ) elif donor_conc <= intrinsic_conc: raise ValueError( 'Donor concentration should be greater than intrinsic concentration' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( 'Acceptor concentration should be greater than intrinsic concentration' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : bool = False ) -> bool: """simple docstring""" if n == 2: return True if not n % 2 or n < 2: return False if n > 5 and n % 10 not in (1, 3, 7, 9): # can quickly check last digit return False if n > 3_31_70_44_06_46_79_88_73_85_96_19_81 and not allow_probable: raise ValueError( 'Warning: upper bound of deterministic test is exceeded. ' 'Pass allow_probable=True to allow probabilistic test. ' 'A return value of True indicates a probable prime.' ) # array bounds provided by analysis SCREAMING_SNAKE_CASE_ : List[Any] = [ 20_47, 1_37_36_53, 25_32_60_01, 32_15_03_17_51, 2_15_23_02_89_87_47, 3_47_47_49_66_03_83, 3_41_55_00_71_72_83_21, 1, 3_82_51_23_05_65_46_41_30_51, 1, 1, 31_86_65_85_78_34_03_11_51_16_74_61, 3_31_70_44_06_46_79_88_73_85_96_19_81, ] SCREAMING_SNAKE_CASE_ : int = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] for idx, _p in enumerate(lowerCamelCase_ , 1 ): if n < _p: # then we have our last prime to check SCREAMING_SNAKE_CASE_ : List[Any] = primes[:idx] break SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : str = n - 1, 0 # break up n -1 into a power of 2 (s) and # remaining odd component # essentially, solve for d * 2 ** s == n - 1 while d % 2 == 0: d //= 2 s += 1 for prime in plist: SCREAMING_SNAKE_CASE_ : Optional[int] = False for r in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Dict = pow(lowerCamelCase_ , d * 2**r , lowerCamelCase_ ) # see article for analysis explanation for m if (r == 0 and m == 1) or ((m + 1) % n == 0): SCREAMING_SNAKE_CASE_ : List[Any] = True # this loop will not determine compositeness break if pr: continue # if pr is False, then the above loop never evaluated to true, # and the n MUST be composite return False return True def __UpperCAmelCase ( ) -> None: """simple docstring""" assert not miller_rabin(5_61 ) assert miller_rabin(5_63 ) # 2047 assert not miller_rabin(83_82_01 ) assert miller_rabin(83_82_07 ) # 1_373_653 assert not miller_rabin(17_31_60_01 ) assert miller_rabin(17_31_60_17 ) # 25_326_001 assert not miller_rabin(30_78_38_66_41 ) assert miller_rabin(30_78_38_66_53 ) # 3_215_031_751 assert not miller_rabin(1_71_30_45_57_48_01 ) assert miller_rabin(1_71_30_45_57_48_19 ) # 2_152_302_898_747 assert not miller_rabin(2_77_97_99_72_83_07 ) assert miller_rabin(2_77_97_99_72_83_27 ) # 3_474_749_660_383 assert not miller_rabin(1_13_85_00_23_90_94_41 ) assert miller_rabin(1_13_85_00_23_90_95_27 ) # 341_550_071_728_321 assert not miller_rabin(1_27_50_41_01_88_48_80_43_51 ) assert miller_rabin(1_27_50_41_01_88_48_80_43_91 ) # 3_825_123_056_546_413_051 assert not miller_rabin(7_96_66_46_44_58_50_77_87_79_18_67 ) assert miller_rabin(7_96_66_46_44_58_50_77_87_79_19_51 ) # 318_665_857_834_031_151_167_461 assert not miller_rabin(55_28_40_67_74_46_64_78_97_66_03_33 ) assert miller_rabin(55_28_40_67_74_46_64_78_97_66_03_59 ) # 3_317_044_064_679_887_385_961_981 # upper limit for probabilistic test if __name__ == "__main__": test_miller_rabin()
685
class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [], [], [], ] def snake_case ( self ,snake_case__ ,snake_case__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError('Maximum queue size is 100' ) self.queues[priority].append(snake_case__ ) except IndexError: raise ValueError('Valid priorities are 0, 1, and 2' ) def snake_case ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError('All queues are empty' ) def __str__( self ): return "\n".join(F'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : List[str] = [] def snake_case ( self ,snake_case__ ): if len(self.queue ) == 100: raise OverFlowError('Maximum queue size is 100' ) self.queue.append(snake_case__ ) def snake_case ( self ): if not self.queue: raise UnderFlowError('The queue is empty' ) else: SCREAMING_SNAKE_CASE_ : List[Any] = min(self.queue ) self.queue.remove(snake_case__ ) return data def __str__( self ): return str(self.queue ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 1_00 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 1_28 ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(1_00 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(1_28 ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
685
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase__ : Any = {'''configuration_unispeech''': ['''UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''UniSpeechConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Union[str, Any] = [ '''UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST''', '''UniSpeechForCTC''', '''UniSpeechForPreTraining''', '''UniSpeechForSequenceClassification''', '''UniSpeechModel''', '''UniSpeechPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys UpperCamelCase__ : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): __a : int = "maskformer-swin" __a : Optional[Any] = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self ,snake_case__=224 ,snake_case__=4 ,snake_case__=3 ,snake_case__=96 ,snake_case__=[2, 2, 6, 2] ,snake_case__=[3, 6, 12, 24] ,snake_case__=7 ,snake_case__=4.0 ,snake_case__=True ,snake_case__=0.0 ,snake_case__=0.0 ,snake_case__=0.1 ,snake_case__="gelu" ,snake_case__=False ,snake_case__=0.02 ,snake_case__=1E-5 ,snake_case__=None ,snake_case__=None ,**snake_case__ ,): super().__init__(**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = image_size SCREAMING_SNAKE_CASE_ : Tuple = patch_size SCREAMING_SNAKE_CASE_ : List[Any] = num_channels SCREAMING_SNAKE_CASE_ : Any = embed_dim SCREAMING_SNAKE_CASE_ : Optional[int] = depths SCREAMING_SNAKE_CASE_ : Dict = len(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = num_heads SCREAMING_SNAKE_CASE_ : Dict = window_size SCREAMING_SNAKE_CASE_ : List[str] = mlp_ratio SCREAMING_SNAKE_CASE_ : Tuple = qkv_bias SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Any = drop_path_rate SCREAMING_SNAKE_CASE_ : List[str] = hidden_act SCREAMING_SNAKE_CASE_ : int = use_absolute_embeddings SCREAMING_SNAKE_CASE_ : str = layer_norm_eps SCREAMING_SNAKE_CASE_ : Tuple = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE_ : Tuple = int(embed_dim * 2 ** (len(snake_case__ ) - 1) ) SCREAMING_SNAKE_CASE_ : Any = ['stem'] + [F'stage{idx}' for idx in range(1 ,len(snake_case__ ) + 1 )] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = get_aligned_output_features_output_indices( out_features=snake_case__ ,out_indices=snake_case__ ,stage_names=self.stage_names )
685
from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Tuple = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Any = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Dict = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] )
685
1
import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def __UpperCAmelCase ( lowerCamelCase_ : Any ) -> Union[str, Any]: """simple docstring""" if is_torch_version('<' , '2.0.0' ) or not hasattr(lowerCamelCase_ , '_dynamo' ): return False return isinstance(lowerCamelCase_ , torch._dynamo.eval_frame.OptimizedModule ) def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : bool = True ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) SCREAMING_SNAKE_CASE_ : List[Any] = is_compiled_module(lowerCamelCase_ ) if is_compiled: SCREAMING_SNAKE_CASE_ : str = model SCREAMING_SNAKE_CASE_ : Tuple = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[Any] = model.module if not keep_fpaa_wrapper: SCREAMING_SNAKE_CASE_ : Optional[Any] = getattr(lowerCamelCase_ , 'forward' ) SCREAMING_SNAKE_CASE_ : List[Any] = model.__dict__.pop('_original_forward' , lowerCamelCase_ ) if original_forward is not None: while hasattr(lowerCamelCase_ , '__wrapped__' ): SCREAMING_SNAKE_CASE_ : Any = forward.__wrapped__ if forward == original_forward: break SCREAMING_SNAKE_CASE_ : Dict = forward if getattr(lowerCamelCase_ , '_converted_to_transformer_engine' , lowerCamelCase_ ): convert_model(lowerCamelCase_ , to_transformer_engine=lowerCamelCase_ ) if is_compiled: SCREAMING_SNAKE_CASE_ : List[Any] = model SCREAMING_SNAKE_CASE_ : Optional[int] = compiled_model return model def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" PartialState().wait_for_everyone() def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Any ) -> List[str]: """simple docstring""" if PartialState().distributed_type == DistributedType.TPU: xm.save(lowerCamelCase_ , lowerCamelCase_ ) elif PartialState().local_process_index == 0: torch.save(lowerCamelCase_ , lowerCamelCase_ ) @contextmanager def __UpperCAmelCase ( **lowerCamelCase_ : Union[str, Any] ) -> List[Any]: """simple docstring""" for key, value in kwargs.items(): SCREAMING_SNAKE_CASE_ : Tuple = str(lowerCamelCase_ ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> List[str]: """simple docstring""" if not hasattr(lowerCamelCase_ , '__qualname__' ) and not hasattr(lowerCamelCase_ , '__name__' ): SCREAMING_SNAKE_CASE_ : str = getattr(lowerCamelCase_ , '__class__' , lowerCamelCase_ ) if hasattr(lowerCamelCase_ , '__qualname__' ): return obj.__qualname__ if hasattr(lowerCamelCase_ , '__name__' ): return obj.__name__ return str(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> List[str]: """simple docstring""" for key, value in source.items(): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = destination.setdefault(lowerCamelCase_ , {} ) merge_dicts(lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE_ : Tuple = value return destination def __UpperCAmelCase ( lowerCamelCase_ : int = None ) -> bool: """simple docstring""" if port is None: SCREAMING_SNAKE_CASE_ : Tuple = 2_95_00 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(('localhost', port) ) == 0
685
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
1
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo UpperCamelCase__ : Dict = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' UpperCamelCase__ : List[str] = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' UpperCamelCase__ : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' ,id='token' ) ,id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' ,id='token' ) ,id='sequence' ) ,id='references' ), } ) ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ = 1 ,snake_case__ = 4 ,): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case__ ,hypotheses=snake_case__ ,min_len=snake_case__ ,max_len=snake_case__ ) }
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
685
1
UpperCamelCase__ : str = '''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/''' def __UpperCAmelCase ( lowerCamelCase_ : bytes ) -> bytes: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = F'a bytes-like object is required, not \'{data.__class__.__name__}\'' raise TypeError(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[str] = ''.join(bin(lowerCamelCase_ )[2:].zfill(8 ) for byte in data ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = len(lowerCamelCase_ ) % 6 != 0 if padding_needed: # The padding that will be added later SCREAMING_SNAKE_CASE_ : Optional[Any] = b'=' * ((6 - len(lowerCamelCase_ ) % 6) // 2) # Append binary_stream with arbitrary binary digits (0's by default) to make its # length a multiple of 6. binary_stream += "0" * (6 - len(lowerCamelCase_ ) % 6) else: SCREAMING_SNAKE_CASE_ : int = b'' # Encode every 6 binary digits to their corresponding Base64 character return ( "".join( B64_CHARSET[int(binary_stream[index : index + 6] , 2 )] for index in range(0 , len(lowerCamelCase_ ) , 6 ) ).encode() + padding ) def __UpperCAmelCase ( lowerCamelCase_ : str ) -> bytes: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Dict = ( 'argument should be a bytes-like object or ASCII string, ' F'not \'{encoded_data.__class__.__name__}\'' ) raise TypeError(lowerCamelCase_ ) # In case encoded_data is a bytes-like object, make sure it contains only # ASCII characters so we convert it to a string object if isinstance(lowerCamelCase_ , lowerCamelCase_ ): try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = encoded_data.decode('utf-8' ) except UnicodeDecodeError: raise ValueError('base64 encoded data should only contain ASCII characters' ) SCREAMING_SNAKE_CASE_ : int = encoded_data.count('=' ) # Check if the encoded string contains non base64 characters if padding: assert all( char in B64_CHARSET for char in encoded_data[:-padding] ), "Invalid base64 character(s) found." else: assert all( char in B64_CHARSET for char in encoded_data ), "Invalid base64 character(s) found." # Check the padding assert len(lowerCamelCase_ ) % 4 == 0 and padding < 3, "Incorrect padding" if padding: # Remove padding if there is one SCREAMING_SNAKE_CASE_ : Dict = encoded_data[:-padding] SCREAMING_SNAKE_CASE_ : Optional[Any] = ''.join( bin(B64_CHARSET.index(lowerCamelCase_ ) )[2:].zfill(6 ) for char in encoded_data )[: -padding * 2] else: SCREAMING_SNAKE_CASE_ : Any = ''.join( bin(B64_CHARSET.index(lowerCamelCase_ ) )[2:].zfill(6 ) for char in encoded_data ) SCREAMING_SNAKE_CASE_ : Tuple = [ int(binary_stream[index : index + 8] , 2 ) for index in range(0 , len(lowerCamelCase_ ) , 8 ) ] return bytes(lowerCamelCase_ ) if __name__ == "__main__": import doctest doctest.testmod()
685
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int = 2 ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE_ : Optional[int] = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : str = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , lowerCamelCase_ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , lowerCamelCase_ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(lowerCamelCase_ ) ) , list(range(lowerCamelCase_ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
685
1
UpperCamelCase__ : int = '''Alexander Joslin''' import operator as op from .stack import Stack def __UpperCAmelCase ( lowerCamelCase_ : str ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = {'*': op.mul, '/': op.truediv, '+': op.add, '-': op.sub} SCREAMING_SNAKE_CASE_ : Stack[int] = Stack() SCREAMING_SNAKE_CASE_ : Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(lowerCamelCase_ ) ) elif i in operators: # RULE 2 operator_stack.push(lowerCamelCase_ ) elif i == ")": # RULE 4 SCREAMING_SNAKE_CASE_ : List[str] = operator_stack.peek() operator_stack.pop() SCREAMING_SNAKE_CASE_ : Union[str, Any] = operand_stack.peek() operand_stack.pop() SCREAMING_SNAKE_CASE_ : int = operand_stack.peek() operand_stack.pop() SCREAMING_SNAKE_CASE_ : Tuple = operators[opr](lowerCamelCase_ , lowerCamelCase_ ) operand_stack.push(lowerCamelCase_ ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": UpperCamelCase__ : str = '''(5 + ((4 * 2) * (2 + 3)))''' # answer = 45 print(F"""{equation} = {dijkstras_two_stack_algorithm(equation)}""")
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
1
from __future__ import annotations from collections import deque class lowerCAmelCase_ : def __init__( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : list[dict] = [] self.adlist.append( {'value': '', 'next_states': [], 'fail_state': 0, 'output': []} ) for keyword in keywords: self.add_keyword(snake_case__ ) self.set_fail_transitions() def snake_case ( self ,snake_case__ ,snake_case__ ): for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = 0 for character in keyword: SCREAMING_SNAKE_CASE_ : Optional[Any] = self.find_next_state(snake_case__ ,snake_case__ ) if next_state is None: self.adlist.append( { 'value': character, 'next_states': [], 'fail_state': 0, 'output': [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) SCREAMING_SNAKE_CASE_ : Optional[int] = len(self.adlist ) - 1 else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = next_state self.adlist[current_state]["output"].append(snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : deque = deque() for node in self.adlist[0]["next_states"]: q.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 while q: SCREAMING_SNAKE_CASE_ : Dict = q.popleft() for child in self.adlist[r]["next_states"]: q.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = self.adlist[r]['fail_state'] while ( self.find_next_state(snake_case__ ,self.adlist[child]['value'] ) is None and state != 0 ): SCREAMING_SNAKE_CASE_ : Any = self.adlist[state]['fail_state'] SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.find_next_state( snake_case__ ,self.adlist[child]['value'] ) if self.adlist[child]["fail_state"] is None: SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : Any = ( self.adlist[child]['output'] + self.adlist[self.adlist[child]['fail_state']]['output'] ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : dict = {} # returns a dict with keywords and list of its occurrences SCREAMING_SNAKE_CASE_ : List[Any] = 0 for i in range(len(snake_case__ ) ): while ( self.find_next_state(snake_case__ ,string[i] ) is None and current_state != 0 ): SCREAMING_SNAKE_CASE_ : Tuple = self.adlist[current_state]['fail_state'] SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.find_next_state(snake_case__ ,string[i] ) if next_state is None: SCREAMING_SNAKE_CASE_ : int = 0 else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = next_state for key in self.adlist[current_state]["output"]: if key not in result: SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] result[key].append(i - len(snake_case__ ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
685
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> Tuple: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) else: return a * actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(lowerCamelCase_ , lowerCamelCase_ ) return actual_power(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": print(power(-2, -3))
685
1
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = qiskit.Aer.get_backend('aer_simulator' ) # Create a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : List[Any] = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Map the quantum measurement to the classical bits circuit.measure([0] , [0] ) # Execute the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {single_qubit_measure(1, 1)}""")
685
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=True ,snake_case__=False ,snake_case__=True ,snake_case__=99 ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=16 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=4 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = parent SCREAMING_SNAKE_CASE_ : List[Any] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = seq_length SCREAMING_SNAKE_CASE_ : Tuple = is_training SCREAMING_SNAKE_CASE_ : List[str] = use_input_mask SCREAMING_SNAKE_CASE_ : List[str] = use_token_type_ids SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : int = vocab_size SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE_ : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Any = intermediate_size SCREAMING_SNAKE_CASE_ : Dict = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = type_vocab_size SCREAMING_SNAKE_CASE_ : int = type_sequence_label_size SCREAMING_SNAKE_CASE_ : int = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : List[str] = num_choices SCREAMING_SNAKE_CASE_ : Tuple = scope def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Dict = None if self.use_labels: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size] ,self.num_choices ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case ( self ): return LlamaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = LlamaModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,attention_mask=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaModel(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE_ : List[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,use_cache=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ : str = ids_tensor((self.batch_size, 3) ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor((self.batch_size, 3) ,vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_ids, next_tokens] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_mask, next_mask] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Dict = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] # select random slice SCREAMING_SNAKE_CASE_ : Any = ids_tensor((1,) ,output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ : str = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE_ : str = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __a : int = (LlamaForCausalLM,) if is_torch_available() else () __a : Any = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) __a : Tuple = False __a : Tuple = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaModelTester(self ) SCREAMING_SNAKE_CASE_ : List[str] = ConfigTester(self ,config_class=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_ : Optional[int] = type self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Dict = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : str = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Any = 3 SCREAMING_SNAKE_CASE_ : int = 'single_label_classification' SCREAMING_SNAKE_CASE_ : str = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Dict = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Tuple = 3 SCREAMING_SNAKE_CASE_ : str = 'multi_label_classification' SCREAMING_SNAKE_CASE_ : int = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Tuple = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor( [self.model_tester.batch_size, config.num_labels] ,self.model_tester.type_sequence_label_size ).to(torch.float ) SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def snake_case ( self ): pass @parameterized.expand([('linear',), ('dynamic',)] ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([1, 10] ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : Any = LlamaModel(snake_case__ ) original_model.to(snake_case__ ) original_model.eval() SCREAMING_SNAKE_CASE_ : int = original_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : List[Any] = original_model(snake_case__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : List[Any] = {'type': scaling_type, 'factor': 10.0} SCREAMING_SNAKE_CASE_ : int = LlamaModel(snake_case__ ) scaled_model.to(snake_case__ ) scaled_model.eval() SCREAMING_SNAKE_CASE_ : str = scaled_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : Optional[int] = scaled_model(snake_case__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : List[str] = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : int = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : Dict = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : str = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : int = model(torch.tensor(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] ,dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # fmt: off SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' SCREAMING_SNAKE_CASE_ : List[str] = 'Simply put, the theory of relativity states that ' SCREAMING_SNAKE_CASE_ : str = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.encode(snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Tuple = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' ,device_map='sequential' ,use_safetensors=snake_case__ ) # greedy generation outputs SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.generate(snake_case__ ,max_new_tokens=64 ,top_p=snake_case__ ,temperature=1 ,do_sample=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.decode(generated_ids[0] ,skip_special_tokens=snake_case__ ) self.assertEqual(snake_case__ ,snake_case__ )
685
1
import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = AutoImageProcessor.from_pretrained('microsoft/dit-base-finetuned-rvlcdip' ) SCREAMING_SNAKE_CASE_ : List[Any] = AutoModelForImageClassification.from_pretrained('microsoft/dit-base-finetuned-rvlcdip' ) model.to(snake_case__ ) from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Tuple = load_dataset('nielsr/rvlcdip-demo' ) SCREAMING_SNAKE_CASE_ : Tuple = dataset['train'][0]['image'].convert('RGB' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = image_processor(snake_case__ ,return_tensors='pt' ).to(snake_case__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ : List[str] = model(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = outputs.logits SCREAMING_SNAKE_CASE_ : Optional[int] = torch.Size((1, 16) ) self.assertEqual(logits.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : int = torch.tensor( [-0.4158, -0.4092, -0.4347] ,device=snake_case__ ,dtype=torch.float ,) self.assertTrue(torch.allclose(logits[0, :3] ,snake_case__ ,atol=1E-4 ) )
685
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''} UpperCamelCase__ : int = { '''vocab_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''', }, '''emoji_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''', }, } UpperCamelCase__ : str = { '''abeja/gpt-neox-japanese-2.7b''': 20_48, } def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Tuple ) -> List[str]: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[Any] = json.loads(f.read() ) SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : List[Any] = collections.OrderedDict() with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Any = f.readlines() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = b SCREAMING_SNAKE_CASE_ : Dict = idx for wd in b: SCREAMING_SNAKE_CASE_ : Any = idx return vocab, raw_vocab, ids_to_tokens, emoji class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = VOCAB_FILES_NAMES __a : List[str] = PRETRAINED_VOCAB_FILES_MAP __a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__="<|endoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__="<|startoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__=False ,**snake_case__ ,): super().__init__( unk_token=snake_case__ ,pad_token=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,do_clean_text=snake_case__ ,**snake_case__ ,) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained' ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google' ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) SCREAMING_SNAKE_CASE_ : str = do_clean_text SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = load_vocab_and_emoji(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = SubWordJapaneseTokenizer( vocab=self.vocab ,ids_to_tokens=self.ids_to_tokens ,emoji=self.emoji ) @property def snake_case ( self ): # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def snake_case ( self ): return dict(self.raw_vocab ,**self.added_tokens_encoder ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.tokenize(snake_case__ ,clean=self.do_clean_text ) def snake_case ( self ,snake_case__ ): return self.vocab.get(snake_case__ ,self.vocab.get(self.unk_token ) ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.convert_id_to_token(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = ''.join(snake_case__ ).strip() return out_string def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(snake_case__ ,add_special_tokens=snake_case__ ) + [self.eos_token_id] ) if len(snake_case__ ) > self.model_max_length: SCREAMING_SNAKE_CASE_ : List[Any] = input_ids[-self.model_max_length :] return input_ids def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 if os.path.isdir(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: SCREAMING_SNAKE_CASE_ : Tuple = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : str = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.' ' Please check that the vocabulary is not corrupted!' ) SCREAMING_SNAKE_CASE_ : Dict = token_index writer.write(','.join(snake_case__ ) + '\n' ) index += 1 with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: json.dump(self.emoji ,snake_case__ ) return vocab_file, emoji_file class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = vocab # same as swe SCREAMING_SNAKE_CASE_ : Optional[int] = ids_to_tokens # same as bpe SCREAMING_SNAKE_CASE_ : Dict = emoji SCREAMING_SNAKE_CASE_ : int = np.max([len(snake_case__ ) for w in self.vocab.keys()] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) SCREAMING_SNAKE_CASE_ : str = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' SCREAMING_SNAKE_CASE_ : int = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' SCREAMING_SNAKE_CASE_ : Tuple = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self ): return len(self.ids_to_tokens ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<URL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.content_repattera.sub('<EMAIL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<TEL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<PRICE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: SCREAMING_SNAKE_CASE_ : Union[str, Any] = content.replace('<BLOCK><BLOCK>' ,'<BLOCK>' ) return content def snake_case ( self ,snake_case__ ,snake_case__=False ): SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('\r\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\r' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : List[str] = text.replace('\t' ,'<TAB>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('—' ,'ー' ) SCREAMING_SNAKE_CASE_ : Optional[int] = text.replace('−' ,'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: SCREAMING_SNAKE_CASE_ : int = text.replace(snake_case__ ,snake_case__ ) if clean: SCREAMING_SNAKE_CASE_ : str = self.clean_text(snake_case__ ) def check_simbol(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 2: SCREAMING_SNAKE_CASE_ : str = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0XC2A1 and c <= 0XC2BF) or (c >= 0XC780 and c <= 0XC783) or (c >= 0XCAB9 and c <= 0XCBBF) or (c >= 0XCC80 and c <= 0XCDA2) ): return True return False def checkuae(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 3: SCREAMING_SNAKE_CASE_ : Dict = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0XE2_8080 and c <= 0XE2_B07F: return True return False SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : List[Any] = [] while pos < len(snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = min(len(snake_case__ ) ,pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 SCREAMING_SNAKE_CASE_ : List[Any] = [] # (token_id, token, pos) for e in range(snake_case__ ,snake_case__ ,-1 ): SCREAMING_SNAKE_CASE_ : str = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(snake_case__ ) > 2: SCREAMING_SNAKE_CASE_ : Optional[Any] = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(snake_case__ ) > 0: # the smallest token_id is adopted SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = sorted(snake_case__ ,key=lambda snake_case__ : x[0] )[0] result.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = e else: SCREAMING_SNAKE_CASE_ : Any = pos + 1 SCREAMING_SNAKE_CASE_ : Optional[int] = text[pos:end] if check_simbol(snake_case__ ): result.append('<KIGOU>' ) elif checkuae(snake_case__ ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) SCREAMING_SNAKE_CASE_ : int = end return result def snake_case ( self ,snake_case__ ,snake_case__="\n" ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Dict = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : Dict = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(snake_case__ ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(snake_case__ ) if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : int = ''.join(snake_case__ ) return text
685
1
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging UpperCamelCase__ : int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ = 101 ): SCREAMING_SNAKE_CASE_ : Tuple = length def __len__( self ): return self.length def __getitem__( self ,snake_case__ ): return i class lowerCAmelCase_ : def __call__( self ,snake_case__ ): return {"input_ids": torch.tensor(snake_case__ ), "labels": torch.tensor(snake_case__ )} class lowerCAmelCase_ ( nn.Module ): def __init__( self ): super().__init__() # Add some (unused) params otherwise DDP will complain. SCREAMING_SNAKE_CASE_ : Optional[int] = nn.Linear(120 ,80 ) def snake_case ( self ,snake_case__ ,snake_case__=None ): if labels is not None: return torch.tensor(0.0 ,device=input_ids.device ), input_ids else: return input_ids class lowerCAmelCase_ ( lowerCamelCase_ ): @require_torch_neuroncore def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = F'--nproc_per_node=2\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n '.split() SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE_ : List[Any] = F'--output_dir {output_dir}'.split() SCREAMING_SNAKE_CASE_ : str = ['torchrun'] + distributed_args + args execute_subprocess_async(snake_case__ ,env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowerCAmelCase_ ( lowerCamelCase_ ): @require_torch_multi_gpu def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = F'--nproc_per_node={torch.cuda.device_count()}\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n '.split() SCREAMING_SNAKE_CASE_ : int = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE_ : Optional[Any] = F'--output_dir {output_dir}'.split() SCREAMING_SNAKE_CASE_ : List[Any] = ['torchrun'] + distributed_args + args execute_subprocess_async(snake_case__ ,env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py UpperCamelCase__ : Dict = HfArgumentParser((TrainingArguments,)) UpperCamelCase__ : str = parser.parse_args_into_dataclasses()[0] logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ F"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [1_01, 40, 7]: UpperCamelCase__ : Tuple = DummyDataset(dataset_length) def __UpperCAmelCase ( lowerCamelCase_ : EvalPrediction ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = list(range(len(lowerCamelCase_ ) ) ) SCREAMING_SNAKE_CASE_ : Any = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( 'Predictions and/or labels do not match expected results:\n - predictions: ' F'{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}' ) return {"success": success} UpperCamelCase__ : Any = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) UpperCamelCase__ : Optional[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) UpperCamelCase__ : str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) UpperCamelCase__ : Optional[int] = 2 UpperCamelCase__ : Union[str, Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) UpperCamelCase__ : Tuple = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) UpperCamelCase__ : List[Any] = None
685
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : int=() , lowerCamelCase_ : Union[str, Any]=None , lowerCamelCase_ : Optional[int]="no" , lowerCamelCase_ : Optional[Any]="29500" ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False if any(key.startswith('KAGGLE' ) for key in os.environ.keys() ): SCREAMING_SNAKE_CASE_ : str = True elif "IPython" in sys.modules: SCREAMING_SNAKE_CASE_ : Dict = 'google.colab' in str(sys.modules['IPython'].get_ipython() ) try: SCREAMING_SNAKE_CASE_ : Optional[int] = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F'Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.' ) if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME' , lowerCamelCase_ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ' 'your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if num_processes is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = 8 SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='TPU' ) print(F'Launching a training on {num_processes} TPU cores.' ) xmp.spawn(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on one CPU.' ) function(*lowerCamelCase_ ) else: if num_processes is None: raise ValueError( 'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ' 'inside your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if torch.cuda.is_initialized(): raise ValueError( 'To launch a multi-GPU training from your notebook, you need to avoid running any instruction ' 'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ' 'function.' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port=lowerCamelCase_ , mixed_precision=lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='MULTI_GPU' ) print(F'Launching training on {num_processes} GPUs.' ) try: start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( 'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ' 'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ' 'Please review your imports and test them when running the `notebook_launcher()` to identify ' 'which one is problematic.' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): SCREAMING_SNAKE_CASE_ : Optional[Any] = '1' print('Launching training on MPS.' ) elif torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on CPU.' ) function(*lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple=() , lowerCamelCase_ : str=2 ) -> Union[str, Any]: """simple docstring""" from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port='29500' , accelerate_mixed_precision='no' , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu='yes' , ): SCREAMING_SNAKE_CASE_ : Tuple = PrepareForLaunch(lowerCamelCase_ , debug=lowerCamelCase_ ) start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' )
685
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : Tuple = logging.get_logger(__name__) UpperCamelCase__ : Dict = {'''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase__ : Any = { '''tokenizer_file''': { '''bigscience/tokenizer''': '''https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json''', '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json''', }, } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = VOCAB_FILES_NAMES __a : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __a : Optional[Any] = ["input_ids", "attention_mask"] __a : int = None def __init__( self ,snake_case__=None ,snake_case__=None ,snake_case__=None ,snake_case__="<unk>" ,snake_case__="<s>" ,snake_case__="</s>" ,snake_case__="<pad>" ,snake_case__=False ,snake_case__=False ,**snake_case__ ,): super().__init__( snake_case__ ,snake_case__ ,tokenizer_file=snake_case__ ,unk_token=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,pad_token=snake_case__ ,add_prefix_space=snake_case__ ,clean_up_tokenization_spaces=snake_case__ ,**snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' ,snake_case__ ) != add_prefix_space: SCREAMING_SNAKE_CASE_ : List[str] = getattr(snake_case__ ,pre_tok_state.pop('type' ) ) SCREAMING_SNAKE_CASE_ : List[Any] = add_prefix_space SCREAMING_SNAKE_CASE_ : Any = pre_tok_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = add_prefix_space def snake_case ( self ,*snake_case__ ,**snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = kwargs.get('is_split_into_words' ,snake_case__ ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with' ' pretokenized inputs.' ) return super()._batch_encode_plus(*snake_case__ ,**snake_case__ ) def snake_case ( self ,*snake_case__ ,**snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = kwargs.get('is_split_into_words' ,snake_case__ ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with' ' pretokenized inputs.' ) return super()._encode_plus(*snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : List[Any] = self._tokenizer.model.save(snake_case__ ,name=snake_case__ ) return tuple(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(snake_case__ ,add_special_tokens=snake_case__ ) + [self.eos_token_id] ) if len(snake_case__ ) > self.model_max_length: SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_ids[-self.model_max_length :] return input_ids
685
from typing import TYPE_CHECKING from ....utils import _LazyModule UpperCamelCase__ : Tuple = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
685
1
import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename UpperCamelCase__ : Any = '''http://www.mocksite.com/file1.txt''' UpperCamelCase__ : Any = '''"text": ["foo", "foo"]''' UpperCamelCase__ : Union[str, Any] = '''6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8''' class lowerCAmelCase_ : __a : Any = 2_00 __a : Tuple = {"Content-Length": "100"} __a : List[Any] = {} def snake_case ( self ,**snake_case__ ): return [bytes(snake_case__ ,'utf-8' )] def __UpperCAmelCase ( *lowerCamelCase_ : Dict , **lowerCamelCase_ : List[str] ) -> Optional[int]: """simple docstring""" return MockResponse() @pytest.mark.parametrize('urls_type' , [str, list, dict] ) def __UpperCAmelCase ( lowerCamelCase_ : Dict , lowerCamelCase_ : str , lowerCamelCase_ : Tuple ) -> Dict: """simple docstring""" import requests monkeypatch.setattr(lowerCamelCase_ , 'request' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = URL if issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = url elif issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [url] elif issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[Any] = {'train': url} SCREAMING_SNAKE_CASE_ : Optional[Any] = 'dummy' SCREAMING_SNAKE_CASE_ : Tuple = 'downloads' SCREAMING_SNAKE_CASE_ : int = tmp_path SCREAMING_SNAKE_CASE_ : str = DownloadConfig( cache_dir=os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , use_etag=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Dict = DownloadManager(dataset_name=lowerCamelCase_ , download_config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = dl_manager.download(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = urls for downloaded_paths in [downloaded_paths]: if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[Any] = [downloaded_paths] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [urls] elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): assert "train" in downloaded_paths.keys() SCREAMING_SNAKE_CASE_ : int = downloaded_paths.values() SCREAMING_SNAKE_CASE_ : Dict = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(lowerCamelCase_ , lowerCamelCase_ ): assert downloaded_path == dl_manager.downloaded_paths[input_url] SCREAMING_SNAKE_CASE_ : Dict = Path(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() SCREAMING_SNAKE_CASE_ : List[Any] = downloaded_path.read_text() assert content == CONTENT SCREAMING_SNAKE_CASE_ : List[Any] = downloaded_path.with_suffix('.json' ) assert metadata_downloaded_path.exists() SCREAMING_SNAKE_CASE_ : Any = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize('paths_type' , [str, list, dict] ) def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : List[str] , lowerCamelCase_ : Tuple ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) if issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = filename elif issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[str] = [filename] elif issubclass(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = {'train': filename} SCREAMING_SNAKE_CASE_ : Dict = 'dummy' SCREAMING_SNAKE_CASE_ : Any = xz_file.parent SCREAMING_SNAKE_CASE_ : int = 'extracted' SCREAMING_SNAKE_CASE_ : Optional[int] = DownloadConfig( cache_dir=lowerCamelCase_ , use_etag=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = DownloadManager(dataset_name=lowerCamelCase_ , download_config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dl_manager.extract(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Dict = paths for extracted_paths in [extracted_paths]: if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[str] = [extracted_paths] SCREAMING_SNAKE_CASE_ : Dict = [paths] elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): assert "train" in extracted_paths.keys() SCREAMING_SNAKE_CASE_ : Tuple = extracted_paths.values() SCREAMING_SNAKE_CASE_ : Dict = paths.values() assert extracted_paths for extracted_path, input_path in zip(lowerCamelCase_ , lowerCamelCase_ ): assert extracted_path == dl_manager.extracted_paths[input_path] SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = extracted_path.parts assert parts[-1] == hash_url_to_filename(lowerCamelCase_ , etag=lowerCamelCase_ ) assert parts[-2] == extracted_subdir assert extracted_path.exists() SCREAMING_SNAKE_CASE_ : str = extracted_path.read_text() SCREAMING_SNAKE_CASE_ : List[Any] = text_file.read_text() assert extracted_file_content == expected_file_content def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Dict ) -> Union[str, Any]: """simple docstring""" assert path.endswith('.jsonl' ) for num_items, line in enumerate(lowerCamelCase_ , start=1 ): SCREAMING_SNAKE_CASE_ : Optional[Any] = json.loads(line.decode('utf-8' ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize('archive_jsonl' , ['tar_jsonl_path', 'zip_jsonl_path'] ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : List[str] ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = request.getfixturevalue(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(lowerCamelCase_ ) , start=1 ): _test_jsonl(lowerCamelCase_ , lowerCamelCase_ ) assert num_jsonl == 2 @pytest.mark.parametrize('archive_nested_jsonl' , ['tar_nested_jsonl_path', 'zip_nested_jsonl_path'] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : str ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = request.getfixturevalue(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(lowerCamelCase_ ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(lowerCamelCase_ ) , start=1 ): _test_jsonl(lowerCamelCase_ , lowerCamelCase_ ) assert num_tar == 1 assert num_jsonl == 2 def __UpperCAmelCase ( lowerCamelCase_ : Any ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(lowerCamelCase_ ) , start=1 ): assert os.path.basename(lowerCamelCase_ ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
685
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = CLIPTokenizer __a : List[str] = CLIPTokenizerFast __a : List[str] = True __a : Tuple = {} __a : Tuple = False def snake_case ( self ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE_ : Union[str, Any] = dict(zip(snake_case__ ,range(len(snake_case__ ) ) ) ) SCREAMING_SNAKE_CASE_ : Any = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] SCREAMING_SNAKE_CASE_ : Any = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case__ ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case__ ) ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = 'lower newer' SCREAMING_SNAKE_CASE_ : Tuple = 'lower newer' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = CLIPTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) SCREAMING_SNAKE_CASE_ : List[Any] = 'lower newer' SCREAMING_SNAKE_CASE_ : Optional[Any] = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] SCREAMING_SNAKE_CASE_ : Any = tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ : Optional[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) @require_ftfy def snake_case ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Any = self.tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.rust_tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways SCREAMING_SNAKE_CASE_ : Dict = 'xa\u0303y' + ' ' + 'x\xe3y' SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of space type SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of line break type SCREAMING_SNAKE_CASE_ : Tuple = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) def snake_case ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` SCREAMING_SNAKE_CASE_ : Tuple = F'{text_of_1_token} {text_of_1_token}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(snake_case__ ) + 1, len(snake_case__ ) + 1 + len(snake_case__ )) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = F' {text}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(1, 1 + len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(snake_case__ ) + 1, 1 + len(snake_case__ ) + 1 + len(snake_case__ )) ,) def snake_case ( self ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(snake_case__ ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def snake_case ( self ): super().test_tokenization_python_rust_equals() def snake_case ( self ): # CLIP always lower cases letters pass
685
1
from typing import Dict, Optional import numpy as np import datasets UpperCamelCase__ : Optional[Any] = ''' IoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union between the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation, the mean IoU of the image is calculated by taking the IoU of each class and averaging them. ''' UpperCamelCase__ : List[Any] = ''' Args: predictions (`List[ndarray]`): List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. references (`List[ndarray]`): List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size. num_labels (`int`): Number of classes (categories). ignore_index (`int`): Index that will be ignored during evaluation. nan_to_num (`int`, *optional*): If specified, NaN values will be replaced by the number defined by the user. label_map (`dict`, *optional*): If specified, dictionary mapping old label indices to new label indices. reduce_labels (`bool`, *optional*, defaults to `False`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255. Returns: `Dict[str, float | ndarray]` comprising various elements: - *mean_iou* (`float`): Mean Intersection-over-Union (IoU averaged over all categories). - *mean_accuracy* (`float`): Mean accuracy (averaged over all categories). - *overall_accuracy* (`float`): Overall accuracy on all images. - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`): Per category accuracy. - *per_category_iou* (`ndarray` of shape `(num_labels,)`): Per category IoU. Examples: >>> import numpy as np >>> mean_iou = datasets.load_metric("mean_iou") >>> # suppose one has 3 different segmentation maps predicted >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]]) >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]]) >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]]) >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]]) >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]]) >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]]) >>> predicted = [predicted_1, predicted_2, predicted_3] >>> ground_truth = [actual_1, actual_2, actual_3] >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False) >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])} ''' UpperCamelCase__ : Optional[int] = '''\ @software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020, author = {{MMSegmentation Contributors}}, license = {Apache-2.0}, month = {7}, title = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}}, url = {https://github.com/open-mmlab/mmsegmentation}, year = {2020} }''' def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str , lowerCamelCase_ : Tuple , lowerCamelCase_ : bool , lowerCamelCase_ : Optional[Dict[int, int]] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" if label_map is not None: for old_id, new_id in label_map.items(): SCREAMING_SNAKE_CASE_ : Any = new_id # turn into Numpy arrays SCREAMING_SNAKE_CASE_ : int = np.array(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Dict = np.array(lowerCamelCase_ ) if reduce_labels: SCREAMING_SNAKE_CASE_ : Dict = 2_55 SCREAMING_SNAKE_CASE_ : Optional[Any] = label - 1 SCREAMING_SNAKE_CASE_ : Optional[int] = 2_55 SCREAMING_SNAKE_CASE_ : Tuple = label != ignore_index SCREAMING_SNAKE_CASE_ : int = np.not_equal(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = pred_label[mask] SCREAMING_SNAKE_CASE_ : List[str] = np.array(lowerCamelCase_ )[mask] SCREAMING_SNAKE_CASE_ : Optional[Any] = pred_label[pred_label == label] SCREAMING_SNAKE_CASE_ : Optional[Any] = np.histogram(lowerCamelCase_ , bins=lowerCamelCase_ , range=(0, num_labels - 1) )[0] SCREAMING_SNAKE_CASE_ : List[Any] = np.histogram(lowerCamelCase_ , bins=lowerCamelCase_ , range=(0, num_labels - 1) )[0] SCREAMING_SNAKE_CASE_ : List[Any] = np.histogram(lowerCamelCase_ , bins=lowerCamelCase_ , range=(0, num_labels - 1) )[0] SCREAMING_SNAKE_CASE_ : Union[str, Any] = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def __UpperCAmelCase ( lowerCamelCase_ : Any , lowerCamelCase_ : Dict , lowerCamelCase_ : Dict , lowerCamelCase_ : bool , lowerCamelCase_ : Optional[Dict[int, int]] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = np.zeros((num_labels,) , dtype=np.floataa ) SCREAMING_SNAKE_CASE_ : List[Any] = np.zeros((num_labels,) , dtype=np.floataa ) SCREAMING_SNAKE_CASE_ : Tuple = np.zeros((num_labels,) , dtype=np.floataa ) SCREAMING_SNAKE_CASE_ : Optional[Any] = np.zeros((num_labels,) , dtype=np.floataa ) for result, gt_seg_map in zip(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = intersect_and_union( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def __UpperCAmelCase ( lowerCamelCase_ : Dict , lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : bool , lowerCamelCase_ : Optional[int] = None , lowerCamelCase_ : Optional[Dict[int, int]] = None , lowerCamelCase_ : bool = False , ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = total_intersect_and_union( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # compute metrics SCREAMING_SNAKE_CASE_ : Dict = {} SCREAMING_SNAKE_CASE_ : Dict = total_area_intersect.sum() / total_area_label.sum() SCREAMING_SNAKE_CASE_ : Union[str, Any] = total_area_intersect / total_area_union SCREAMING_SNAKE_CASE_ : Dict = total_area_intersect / total_area_label SCREAMING_SNAKE_CASE_ : Optional[Any] = np.nanmean(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.nanmean(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = all_acc SCREAMING_SNAKE_CASE_ : str = iou SCREAMING_SNAKE_CASE_ : List[Any] = acc if nan_to_num is not None: SCREAMING_SNAKE_CASE_ : Dict = {metric: np.nan_to_num(lowerCamelCase_ , nan=lowerCamelCase_ ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { 'predictions': datasets.Sequence(datasets.Sequence(datasets.Value('uint16' ) ) ), 'references': datasets.Sequence(datasets.Sequence(datasets.Value('uint16' ) ) ), } ) ,reference_urls=[ 'https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py' ] ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__ = None ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : str = mean_iou( results=snake_case__ ,gt_seg_maps=snake_case__ ,num_labels=snake_case__ ,ignore_index=snake_case__ ,nan_to_num=snake_case__ ,label_map=snake_case__ ,reduce_labels=snake_case__ ,) return iou_result
685
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCAmelCase ( ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) SCREAMING_SNAKE_CASE_ : int = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(lowerCamelCase_ ) DownloadCommand.register_subcommand(lowerCamelCase_ ) EnvironmentCommand.register_subcommand(lowerCamelCase_ ) RunCommand.register_subcommand(lowerCamelCase_ ) ServeCommand.register_subcommand(lowerCamelCase_ ) UserCommands.register_subcommand(lowerCamelCase_ ) AddNewModelCommand.register_subcommand(lowerCamelCase_ ) AddNewModelLikeCommand.register_subcommand(lowerCamelCase_ ) LfsCommands.register_subcommand(lowerCamelCase_ ) PTtoTFCommand.register_subcommand(lowerCamelCase_ ) # Let's go SCREAMING_SNAKE_CASE_ : Optional[int] = parser.parse_args() if not hasattr(lowerCamelCase_ , 'func' ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE_ : Optional[Any] = args.func(lowerCamelCase_ ) service.run() if __name__ == "__main__": main()
685
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL UpperCamelCase__ : Optional[Any] = logging.get_logger(__name__) class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = ["pixel_values"] def __init__( self ,snake_case__ = True ,snake_case__ = None ,snake_case__ = PIL.Image.BICUBIC ,snake_case__ = True ,snake_case__ = None ,snake_case__ = 1 / 255 ,snake_case__ = True ,snake_case__ = True ,snake_case__ = None ,snake_case__ = None ,**snake_case__ ,): super().__init__(**snake_case__ ) SCREAMING_SNAKE_CASE_ : str = size if size is not None else {'height': 256, 'width': 256} SCREAMING_SNAKE_CASE_ : Optional[Any] = get_size_dict(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = crop_size if crop_size is not None else {'height': 224, 'width': 224} SCREAMING_SNAKE_CASE_ : Union[str, Any] = get_size_dict(snake_case__ ,param_name='crop_size' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = do_resize SCREAMING_SNAKE_CASE_ : Optional[Any] = size SCREAMING_SNAKE_CASE_ : str = resample SCREAMING_SNAKE_CASE_ : str = do_center_crop SCREAMING_SNAKE_CASE_ : Optional[Any] = crop_size SCREAMING_SNAKE_CASE_ : Dict = do_rescale SCREAMING_SNAKE_CASE_ : List[Any] = rescale_factor SCREAMING_SNAKE_CASE_ : Optional[int] = do_normalize SCREAMING_SNAKE_CASE_ : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN SCREAMING_SNAKE_CASE_ : Tuple = image_std if image_std is not None else IMAGENET_STANDARD_STD def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ = PIL.Image.BICUBIC ,snake_case__ = None ,**snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = get_size_dict(snake_case__ ) if "height" not in size or "width" not in size: raise ValueError(F'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}' ) return resize( snake_case__ ,size=(size['height'], size['width']) ,resample=snake_case__ ,data_format=snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ = None ,**snake_case__ ,): SCREAMING_SNAKE_CASE_ : Tuple = get_size_dict(snake_case__ ) if "height" not in size or "width" not in size: raise ValueError(F'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}' ) return center_crop(snake_case__ ,size=(size['height'], size['width']) ,data_format=snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ = None ,**snake_case__ ,): return rescale(snake_case__ ,scale=snake_case__ ,data_format=snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,**snake_case__ ,): return normalize(snake_case__ ,mean=snake_case__ ,std=snake_case__ ,data_format=snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ,snake_case__ = None ,snake_case__ = None ,snake_case__=None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = None ,snake_case__ = ChannelDimension.FIRST ,**snake_case__ ,): SCREAMING_SNAKE_CASE_ : Tuple = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE_ : str = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE_ : str = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE_ : Optional[int] = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE_ : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE_ : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE_ : List[Any] = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE_ : Tuple = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else self.size SCREAMING_SNAKE_CASE_ : Any = get_size_dict(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE_ : Union[str, Any] = get_size_dict(snake_case__ ,param_name='crop_size' ) SCREAMING_SNAKE_CASE_ : List[Any] = make_list_of_images(snake_case__ ) if not valid_images(snake_case__ ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.' ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE_ : List[Any] = [to_numpy_array(snake_case__ ) for image in images] if do_resize: SCREAMING_SNAKE_CASE_ : Dict = [self.resize(image=snake_case__ ,size=snake_case__ ,resample=snake_case__ ) for image in images] if do_center_crop: SCREAMING_SNAKE_CASE_ : Optional[Any] = [self.center_crop(image=snake_case__ ,size=snake_case__ ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE_ : List[str] = [self.rescale(image=snake_case__ ,scale=snake_case__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE_ : List[Any] = [self.normalize(image=snake_case__ ,mean=snake_case__ ,std=snake_case__ ) for image in images] SCREAMING_SNAKE_CASE_ : Tuple = [to_channel_dimension_format(snake_case__ ,snake_case__ ) for image in images] SCREAMING_SNAKE_CASE_ : Any = {'pixel_values': images} return BatchFeature(data=snake_case__ ,tensor_type=snake_case__ )
685
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging UpperCamelCase__ : Dict = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) UpperCamelCase__ : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'https://pypi.org/pypi/diffusers/json' SCREAMING_SNAKE_CASE_ : Optional[int] = json.loads(request.urlopen(lowerCamelCase_ ).read() )['releases'].keys() return sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : version.Version(lowerCamelCase_ ) ) def __UpperCAmelCase ( ) -> Tuple: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(lowerCamelCase_ ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : List[Any] = f.read() # Imports of the form `import .xxx` SCREAMING_SNAKE_CASE_ : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Unique-ify return list(set(lowerCamelCase_ ) ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : Union[str, Any] = [module_file] SCREAMING_SNAKE_CASE_ : Tuple = [] # Let's recurse through all relative imports while not no_change: SCREAMING_SNAKE_CASE_ : int = [] for f in files_to_check: new_imports.extend(get_relative_imports(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ).parent SCREAMING_SNAKE_CASE_ : int = [str(module_path / m ) for m in new_imports] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [f for f in new_import_files if f not in all_relative_imports] SCREAMING_SNAKE_CASE_ : Any = [F'{f}.py' for f in new_import_files] SCREAMING_SNAKE_CASE_ : Optional[int] = len(lowerCamelCase_ ) == 0 all_relative_imports.extend(lowerCamelCase_ ) return all_relative_imports def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() # Imports of the form `import xxx` SCREAMING_SNAKE_CASE_ : List[str] = re.findall('^\s*import\s+(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Only keep the top-level module SCREAMING_SNAKE_CASE_ : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(set(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = [] for imp in imports: try: importlib.import_module(lowerCamelCase_ ) except ImportError: missing_packages.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' F'{", ".join(lowerCamelCase_ )}. Run `pip install {" ".join(lowerCamelCase_ )}`' ) return get_relative_imports(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = module_path.replace(os.path.sep , '.' ) SCREAMING_SNAKE_CASE_ : Any = importlib.import_module(lowerCamelCase_ ) if class_name is None: return find_pipeline_class(lowerCamelCase_ ) return getattr(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" from ..pipelines import DiffusionPipeline SCREAMING_SNAKE_CASE_ : List[Any] = dict(inspect.getmembers(lowerCamelCase_ , inspect.isclass ) ) SCREAMING_SNAKE_CASE_ : List[str] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , lowerCamelCase_ ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:' F' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in' F' {loaded_module}.' ) SCREAMING_SNAKE_CASE_ : Any = cls return pipeline_class def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) if os.path.isfile(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = module_file_or_url SCREAMING_SNAKE_CASE_ : Dict = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: SCREAMING_SNAKE_CASE_ : List[str] = get_diffusers_versions() # cut ".dev0" SCREAMING_SNAKE_CASE_ : Dict = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: SCREAMING_SNAKE_CASE_ : List[Any] = latest_version if latest_version[1:] in available_versions else 'main' logger.info(F'Defaulting to latest_version: {revision}.' ) elif revision in available_versions: SCREAMING_SNAKE_CASE_ : int = F'v{revision}' elif revision == "main": SCREAMING_SNAKE_CASE_ : List[Any] = revision else: raise ValueError( F'`custom_revision`: {revision} does not exist. Please make sure to choose one of' F' {", ".join(available_versions + ["main"] )}.' ) # community pipeline on GitHub SCREAMING_SNAKE_CASE_ : Tuple = COMMUNITY_PIPELINES_URL.format(revision=lowerCamelCase_ , pipeline=lowerCamelCase_ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = cached_download( lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Tuple = 'git' SCREAMING_SNAKE_CASE_ : Dict = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise else: try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : List[str] = hf_hub_download( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise # Check we have all the requirements in our environment SCREAMING_SNAKE_CASE_ : Dict = check_imports(lowerCamelCase_ ) # Now we move the module inside our cached dynamic modules. SCREAMING_SNAKE_CASE_ : Union[str, Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = Path(lowerCamelCase_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(lowerCamelCase_ , submodule_path / module_file ) for module_needed in modules_needed: SCREAMING_SNAKE_CASE_ : Union[str, Any] = F'{module_needed}.py' shutil.copy(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = use_auth_token elif use_auth_token is True: SCREAMING_SNAKE_CASE_ : int = HfFolder.get_token() else: SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[Any] = model_info(lowerCamelCase_ , revision=lowerCamelCase_ , token=lowerCamelCase_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. SCREAMING_SNAKE_CASE_ : Any = submodule_path / commit_hash SCREAMING_SNAKE_CASE_ : List[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(lowerCamelCase_ ) if not (submodule_path / module_file).exists(): shutil.copy(lowerCamelCase_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( lowerCamelCase_ , F'{module_needed}.py' , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return os.path.join(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , **lowerCamelCase_ : Dict , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_cached_module_file( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return get_class_in_module(lowerCamelCase_ , final_module.replace('.py' , '' ) )
685
1
from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : str = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "informer" __a : Optional[int] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self ,snake_case__ = None ,snake_case__ = None ,snake_case__ = "student_t" ,snake_case__ = "nll" ,snake_case__ = 1 ,snake_case__ = None ,snake_case__ = "mean" ,snake_case__ = 0 ,snake_case__ = 0 ,snake_case__ = 0 ,snake_case__ = 0 ,snake_case__ = None ,snake_case__ = None ,snake_case__ = 64 ,snake_case__ = 32 ,snake_case__ = 32 ,snake_case__ = 2 ,snake_case__ = 2 ,snake_case__ = 2 ,snake_case__ = 2 ,snake_case__ = True ,snake_case__ = "gelu" ,snake_case__ = 0.05 ,snake_case__ = 0.1 ,snake_case__ = 0.1 ,snake_case__ = 0.1 ,snake_case__ = 0.1 ,snake_case__ = 100 ,snake_case__ = 0.02 ,snake_case__=True ,snake_case__ = "prob" ,snake_case__ = 5 ,snake_case__ = True ,**snake_case__ ,): # time series specific configuration SCREAMING_SNAKE_CASE_ : Dict = prediction_length SCREAMING_SNAKE_CASE_ : Union[str, Any] = context_length or prediction_length SCREAMING_SNAKE_CASE_ : Tuple = distribution_output SCREAMING_SNAKE_CASE_ : Union[str, Any] = loss SCREAMING_SNAKE_CASE_ : Optional[Any] = input_size SCREAMING_SNAKE_CASE_ : Dict = num_time_features SCREAMING_SNAKE_CASE_ : Any = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] SCREAMING_SNAKE_CASE_ : List[str] = scaling SCREAMING_SNAKE_CASE_ : int = num_dynamic_real_features SCREAMING_SNAKE_CASE_ : Optional[int] = num_static_real_features SCREAMING_SNAKE_CASE_ : List[str] = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( 'The cardinality should be a list of the same length as `num_static_categorical_features`' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = cardinality else: SCREAMING_SNAKE_CASE_ : List[Any] = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(snake_case__ ) != num_static_categorical_features: raise ValueError( 'The embedding dimension should be a list of the same length as `num_static_categorical_features`' ) SCREAMING_SNAKE_CASE_ : int = embedding_dimension else: SCREAMING_SNAKE_CASE_ : Optional[int] = [min(50 ,(cat + 1) // 2 ) for cat in self.cardinality] SCREAMING_SNAKE_CASE_ : int = num_parallel_samples # Transformer architecture configuration SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_size * len(self.lags_sequence ) + self._number_of_features SCREAMING_SNAKE_CASE_ : Optional[int] = d_model SCREAMING_SNAKE_CASE_ : List[Any] = encoder_attention_heads SCREAMING_SNAKE_CASE_ : List[str] = decoder_attention_heads SCREAMING_SNAKE_CASE_ : List[Any] = encoder_ffn_dim SCREAMING_SNAKE_CASE_ : List[str] = decoder_ffn_dim SCREAMING_SNAKE_CASE_ : Any = encoder_layers SCREAMING_SNAKE_CASE_ : Tuple = decoder_layers SCREAMING_SNAKE_CASE_ : Optional[int] = dropout SCREAMING_SNAKE_CASE_ : Tuple = attention_dropout SCREAMING_SNAKE_CASE_ : List[str] = activation_dropout SCREAMING_SNAKE_CASE_ : Dict = encoder_layerdrop SCREAMING_SNAKE_CASE_ : int = decoder_layerdrop SCREAMING_SNAKE_CASE_ : Dict = activation_function SCREAMING_SNAKE_CASE_ : str = init_std SCREAMING_SNAKE_CASE_ : List[Any] = use_cache # Informer SCREAMING_SNAKE_CASE_ : Optional[Any] = attention_type SCREAMING_SNAKE_CASE_ : Optional[Any] = sampling_factor SCREAMING_SNAKE_CASE_ : str = distil super().__init__(is_encoder_decoder=snake_case__ ,**snake_case__ ) @property def snake_case ( self ): return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
685
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "visual_bert" def __init__( self ,snake_case__=30522 ,snake_case__=768 ,snake_case__=512 ,snake_case__=12 ,snake_case__=12 ,snake_case__=3072 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=1E-12 ,snake_case__=False ,snake_case__=True ,snake_case__=1 ,snake_case__=0 ,snake_case__=2 ,**snake_case__ ,): super().__init__(pad_token_id=snake_case__ ,bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = vocab_size SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = visual_embedding_dim SCREAMING_SNAKE_CASE_ : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE_ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = initializer_range SCREAMING_SNAKE_CASE_ : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : Optional[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_ : int = bypass_transformer SCREAMING_SNAKE_CASE_ : Optional[Any] = special_visual_initialize
685
1
from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
685
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Union[str, Any]: """simple docstring""" def is_in_circle(lowerCamelCase_ : float , lowerCamelCase_ : float ) -> bool: SCREAMING_SNAKE_CASE_ : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE_ : Optional[int] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(lowerCamelCase_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE_ : Tuple = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Callable[[float], float] , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(lowerCamelCase_ , lowerCamelCase_ ) ) for _ in range(lowerCamelCase_ ) ) * (max_value - min_value) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 ) -> None: """simple docstring""" def identity_function(lowerCamelCase_ : float ) -> float: return x SCREAMING_SNAKE_CASE_ : str = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> None: """simple docstring""" def function_to_integrate(lowerCamelCase_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE_ : Dict = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
def __UpperCAmelCase ( lowerCamelCase_ : Optional[Any] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = sum(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = True for i in range(1 , s + 1 ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): SCREAMING_SNAKE_CASE_ : Optional[Any] = dp[i][j - 1] if arr[i - 1] <= j: SCREAMING_SNAKE_CASE_ : List[str] = dp[i][j] or dp[i - 1][j - arr[i - 1]] for j in range(int(s / 2 ) , -1 , -1 ): if dp[n][j] is True: SCREAMING_SNAKE_CASE_ : Tuple = s - 2 * j break return diff
685
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
1
import logging import math from functools import partial from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union import torch from .tensor_utils import tensor_tree_map, tree_map def __UpperCAmelCase ( lowerCamelCase_ : Union[dict, list, tuple, torch.Tensor] ) -> List[Tuple[int, ...]]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = [] if isinstance(lowerCamelCase_ , lowerCamelCase_ ): for v in tree.values(): shapes.extend(_fetch_dims(lowerCamelCase_ ) ) elif isinstance(lowerCamelCase_ , (list, tuple) ): for t in tree: shapes.extend(_fetch_dims(lowerCamelCase_ ) ) elif isinstance(lowerCamelCase_ , torch.Tensor ): shapes.append(tree.shape ) else: raise ValueError('Not supported' ) return shapes @torch.jit.ignore def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Tuple[int, ...] ) -> Tuple[int, ...]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = [] for d in reversed(lowerCamelCase_ ): idx.append(flat_idx % d ) SCREAMING_SNAKE_CASE_ : Tuple = flat_idx // d return tuple(reversed(lowerCamelCase_ ) ) @torch.jit.ignore def __UpperCAmelCase ( lowerCamelCase_ : Sequence[int] , lowerCamelCase_ : Sequence[int] , lowerCamelCase_ : Sequence[int] , lowerCamelCase_ : Optional[Sequence[bool]] = None , lowerCamelCase_ : Optional[Sequence[bool]] = None , ) -> List[Tuple[slice, ...]]: """simple docstring""" def reduce_edge_list(lowerCamelCase_ : List[bool] ) -> None: SCREAMING_SNAKE_CASE_ : List[str] = True for i in range(len(lowerCamelCase_ ) ): SCREAMING_SNAKE_CASE_ : List[Any] = -1 * (i + 1) l[reversed_idx] &= tally SCREAMING_SNAKE_CASE_ : str = l[reversed_idx] if start_edges is None: SCREAMING_SNAKE_CASE_ : Optional[int] = [s == 0 for s in start] reduce_edge_list(lowerCamelCase_ ) if end_edges is None: SCREAMING_SNAKE_CASE_ : List[str] = [e == (d - 1) for e, d in zip(lowerCamelCase_ , lowerCamelCase_ )] reduce_edge_list(lowerCamelCase_ ) # Base cases. Either start/end are empty and we're done, or the final, # one-dimensional tensor can be simply sliced if len(lowerCamelCase_ ) == 0: return [()] elif len(lowerCamelCase_ ) == 1: return [(slice(start[0] , end[0] + 1 ),)] SCREAMING_SNAKE_CASE_ : List[Tuple[slice, ...]] = [] SCREAMING_SNAKE_CASE_ : List[slice] = [] # Dimensions common to start and end can be selected directly for s, e in zip(lowerCamelCase_ , lowerCamelCase_ ): if s == e: path_list.append(slice(lowerCamelCase_ , s + 1 ) ) else: break SCREAMING_SNAKE_CASE_ : Tuple[slice, ...] = tuple(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = len(lowerCamelCase_ ) # start == end, and we're done if divergence_idx == len(lowerCamelCase_ ): return [path] def upper() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None SCREAMING_SNAKE_CASE_ : int = start[divergence_idx] return tuple( path + (slice(lowerCamelCase_ , sdi + 1 ),) + s for s in _get_minimal_slice_set( start[divergence_idx + 1 :] , [d - 1 for d in dims[divergence_idx + 1 :]] , dims[divergence_idx + 1 :] , start_edges=start_edges[divergence_idx + 1 :] , end_edges=[True for _ in end_edges[divergence_idx + 1 :]] , ) ) def lower() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None SCREAMING_SNAKE_CASE_ : str = end[divergence_idx] return tuple( path + (slice(lowerCamelCase_ , edi + 1 ),) + s for s in _get_minimal_slice_set( [0 for _ in start[divergence_idx + 1 :]] , end[divergence_idx + 1 :] , dims[divergence_idx + 1 :] , start_edges=[True for _ in start_edges[divergence_idx + 1 :]] , end_edges=end_edges[divergence_idx + 1 :] , ) ) # If both start and end are at the edges of the subtree rooted at # divergence_idx, we can just select the whole subtree at once if start_edges[divergence_idx] and end_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] + 1 ),) ) # If just start is at the edge, we can grab almost all of the subtree, # treating only the ragged bottom edge as an edge case elif start_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] ),) ) slices.extend(lower() ) # Analogous to the previous case, but the top is ragged this time elif end_edges[divergence_idx]: slices.extend(upper() ) slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] + 1 ),) ) # If both sides of the range are ragged, we need to handle both sides # separately. If there's contiguous meat in between them, we can index it # in one big chunk else: slices.extend(upper() ) SCREAMING_SNAKE_CASE_ : Optional[Any] = end[divergence_idx] - start[divergence_idx] if middle_ground > 1: slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] ),) ) slices.extend(lower() ) return slices @torch.jit.ignore def __UpperCAmelCase ( lowerCamelCase_ : torch.Tensor , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int ) -> torch.Tensor: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = t.shape[:no_batch_dims] SCREAMING_SNAKE_CASE_ : int = list(_flat_idx_to_idx(lowerCamelCase_ , lowerCamelCase_ ) ) # _get_minimal_slice_set is inclusive SCREAMING_SNAKE_CASE_ : Any = list(_flat_idx_to_idx(flat_end - 1 , lowerCamelCase_ ) ) # Get an ordered list of slices to perform SCREAMING_SNAKE_CASE_ : List[str] = _get_minimal_slice_set( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = [t[s] for s in slices] return torch.cat([s.view((-1,) + t.shape[no_batch_dims:] ) for s in sliced_tensors] ) def __UpperCAmelCase ( lowerCamelCase_ : Callable , lowerCamelCase_ : Dict[str, Any] , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : bool = False , lowerCamelCase_ : Any = None , lowerCamelCase_ : bool = False , ) -> Any: """simple docstring""" if not (len(lowerCamelCase_ ) > 0): raise ValueError('Must provide at least one input' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = [shape[:no_batch_dims] for shape in _fetch_dims(lowerCamelCase_ )] SCREAMING_SNAKE_CASE_ : List[str] = tuple([max(lowerCamelCase_ ) for s in zip(*lowerCamelCase_ )] ) def _prep_inputs(lowerCamelCase_ : torch.Tensor ) -> torch.Tensor: if not low_mem: if not sum(t.shape[:no_batch_dims] ) == no_batch_dims: SCREAMING_SNAKE_CASE_ : List[Any] = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = t.reshape(-1 , *t.shape[no_batch_dims:] ) else: SCREAMING_SNAKE_CASE_ : Any = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) return t SCREAMING_SNAKE_CASE_ : Dict[str, Any] = tensor_tree_map(_prep_inputs , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = None if _out is not None: SCREAMING_SNAKE_CASE_ : Tuple = tensor_tree_map(lambda lowerCamelCase_ : t.view([-1] + list(t.shape[no_batch_dims:] ) ) , _out ) SCREAMING_SNAKE_CASE_ : List[Any] = 1 for d in orig_batch_dims: flat_batch_dim *= d SCREAMING_SNAKE_CASE_ : Union[str, Any] = flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0) def _select_chunk(lowerCamelCase_ : torch.Tensor ) -> torch.Tensor: return t[i : i + chunk_size] if t.shape[0] != 1 else t SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 SCREAMING_SNAKE_CASE_ : int = prepped_outputs for _ in range(lowerCamelCase_ ): # Chunk the input if not low_mem: SCREAMING_SNAKE_CASE_ : int = _select_chunk else: SCREAMING_SNAKE_CASE_ : Dict = partial( _chunk_slice , flat_start=lowerCamelCase_ , flat_end=min(lowerCamelCase_ , i + chunk_size ) , no_batch_dims=len(lowerCamelCase_ ) , ) SCREAMING_SNAKE_CASE_ : Dict[str, Any] = tensor_tree_map(lowerCamelCase_ , lowerCamelCase_ ) # Run the layer on the chunk SCREAMING_SNAKE_CASE_ : List[Any] = layer(**lowerCamelCase_ ) # Allocate space for the output if out is None: SCREAMING_SNAKE_CASE_ : List[Any] = tensor_tree_map(lambda lowerCamelCase_ : t.new_zeros((flat_batch_dim,) + t.shape[1:] ) , lowerCamelCase_ ) # Put the chunk in its pre-allocated space if isinstance(lowerCamelCase_ , lowerCamelCase_ ): def assign(lowerCamelCase_ : dict , lowerCamelCase_ : dict ) -> None: for k, v in da.items(): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): assign(lowerCamelCase_ , da[k] ) else: if _add_into_out: v[i : i + chunk_size] += da[k] else: SCREAMING_SNAKE_CASE_ : List[Any] = da[k] assign(lowerCamelCase_ , lowerCamelCase_ ) elif isinstance(lowerCamelCase_ , lowerCamelCase_ ): for xa, xa in zip(lowerCamelCase_ , lowerCamelCase_ ): if _add_into_out: xa[i : i + chunk_size] += xa else: SCREAMING_SNAKE_CASE_ : int = xa elif isinstance(lowerCamelCase_ , torch.Tensor ): if _add_into_out: out[i : i + chunk_size] += output_chunk else: SCREAMING_SNAKE_CASE_ : str = output_chunk else: raise ValueError('Not supported' ) i += chunk_size SCREAMING_SNAKE_CASE_ : Any = tensor_tree_map(lambda lowerCamelCase_ : t.view(orig_batch_dims + t.shape[1:] ) , lowerCamelCase_ ) return out class lowerCAmelCase_ : def __init__( self ,snake_case__ = 512 ,): SCREAMING_SNAKE_CASE_ : Any = max_chunk_size SCREAMING_SNAKE_CASE_ : Optional[int] = None SCREAMING_SNAKE_CASE_ : Optional[tuple] = None def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): logging.info('Tuning chunk size...' ) if min_chunk_size >= self.max_chunk_size: return min_chunk_size SCREAMING_SNAKE_CASE_ : List[int] = [2**l for l in range(int(math.log(self.max_chunk_size ,2 ) ) + 1 )] SCREAMING_SNAKE_CASE_ : str = [c for c in candidates if c > min_chunk_size] SCREAMING_SNAKE_CASE_ : Optional[int] = [min_chunk_size] + candidates candidates[-1] += 4 def test_chunk_size(snake_case__ ) -> bool: try: with torch.no_grad(): fn(*snake_case__ ,chunk_size=snake_case__ ) return True except RuntimeError: return False SCREAMING_SNAKE_CASE_ : List[str] = 0 SCREAMING_SNAKE_CASE_ : Optional[Any] = len(snake_case__ ) - 1 while i > min_viable_chunk_size_index: SCREAMING_SNAKE_CASE_ : Any = test_chunk_size(candidates[i] ) if not viable: SCREAMING_SNAKE_CASE_ : Optional[int] = (min_viable_chunk_size_index + i) // 2 else: SCREAMING_SNAKE_CASE_ : Dict = i SCREAMING_SNAKE_CASE_ : List[Any] = (i + len(snake_case__ ) - 1) // 2 return candidates[min_viable_chunk_size_index] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = True for aa, aa in zip(snake_case__ ,snake_case__ ): assert type(snake_case__ ) == type(snake_case__ ) if isinstance(snake_case__ ,(list, tuple) ): consistent &= self._compare_arg_caches(snake_case__ ,snake_case__ ) elif isinstance(snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = [v for _, v in sorted(aa.items() ,key=lambda snake_case__ : x[0] )] SCREAMING_SNAKE_CASE_ : int = [v for _, v in sorted(aa.items() ,key=lambda snake_case__ : x[0] )] consistent &= self._compare_arg_caches(snake_case__ ,snake_case__ ) else: consistent &= aa == aa return consistent def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[int] = True SCREAMING_SNAKE_CASE_ : tuple = tree_map(lambda snake_case__ : a.shape if isinstance(snake_case__ ,torch.Tensor ) else a ,snake_case__ ,snake_case__ ) if self.cached_arg_data is not None: # If args have changed shape/value, we need to re-tune assert len(self.cached_arg_data ) == len(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self._compare_arg_caches(self.cached_arg_data ,snake_case__ ) else: # Otherwise, we can reuse the precomputed value SCREAMING_SNAKE_CASE_ : Optional[Any] = False if not consistent: SCREAMING_SNAKE_CASE_ : str = self._determine_favorable_chunk_size( snake_case__ ,snake_case__ ,snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = arg_data assert self.cached_chunk_size is not None return self.cached_chunk_size
685
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
1
import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument UpperCamelCase__ : Union[str, Any] = { '''/attention/''': '''/0/SelfAttention/''', '''/self_attention/''': '''/0/SelfAttention/''', '''/encoder_decoder_attention/''': '''/1/EncDecAttention/''', '''value''': '''v''', '''query''': '''q''', '''key''': '''k''', '''out''': '''o''', '''pre_self_attention_layer_norm''': '''0/layer_norm''', '''pre_cross_attention_layer_norm''': '''1/layer_norm''', '''pre_attention_layer_norm''': '''0/layer_norm''', # previously 1, but seems wrong '''token_embedder''': '''shared''', '''encoder_norm''': '''final_layer_norm''', '''decoder_norm''': '''final_layer_norm''', '''relpos_bias/rel_embedding''': '''block/0/layer/0/SelfAttention/relative_attention_bias/weight''', '''router/router_weights/w/''': '''router/classifier/''', '''roer/roer_weights/w/''': '''router/classifier/''', '''logits_dense''': '''lm_head''', } def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = list(s_dict.keys() ) for key in keys: SCREAMING_SNAKE_CASE_ : Union[str, Any] = R'.*/layers_(\d+)' SCREAMING_SNAKE_CASE_ : str = key if re.match(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Dict = re.sub(R'layers_(\d+)' , R'block/\1/layer' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = R'(encoder|decoder)\/' if re.match(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = re.match(lowerCamelCase_ , lowerCamelCase_ ).groups() if groups[0] == "encoder": SCREAMING_SNAKE_CASE_ : Union[str, Any] = re.sub(R'/mlp/' , R'/1/mlp/' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = re.sub(R'/pre_mlp_layer_norm/' , R'/1/layer_norm/' , lowerCamelCase_ ) elif groups[0] == "decoder": SCREAMING_SNAKE_CASE_ : str = re.sub(R'/mlp/' , R'/2/mlp/' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = re.sub(R'/pre_mlp_layer_norm/' , R'/2/layer_norm/' , lowerCamelCase_ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: SCREAMING_SNAKE_CASE_ : List[Any] = new_key.replace(lowerCamelCase_ , lowerCamelCase_ ) print(F'{key} -> {new_key}' ) SCREAMING_SNAKE_CASE_ : Tuple = s_dict.pop(lowerCamelCase_ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: SCREAMING_SNAKE_CASE_ : List[Any] = s_dict[ 'encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: SCREAMING_SNAKE_CASE_ : Union[str, Any] = s_dict[ 'decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: SCREAMING_SNAKE_CASE_ : List[str] = s_dict[key].shape[0] SCREAMING_SNAKE_CASE_ : Any = s_dict[key] for idx in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = expert_weihts[idx] print(F'{key} -> {key.replace("expert/" , "nested fstring" )}' ) s_dict.pop(lowerCamelCase_ ) return s_dict UpperCamelCase__ : List[str] = { '''NUM_ENCODER_LAYERS''': '''num_layers''', '''NUM_DECODER_LAYERS''': '''num_decoder_layers''', '''NUM_HEADS''': '''num_heads''', '''HEAD_DIM''': '''d_kv''', '''EMBED_DIM''': '''d_model''', '''MLP_DIM''': '''d_ff''', '''NUM_SELECTED_EXPERTS''': '''num_selected_experts''', '''NUM_ENCODER_SPARSE_LAYERS''': '''num_sparse_encoder_layers''', '''NUM_DECODER_SPARSE_LAYERS''': '''num_sparse_decoder_layers''', '''dense.MlpBlock.activations''': '''feed_forward_proj''', } def __UpperCAmelCase ( lowerCamelCase_ : Any , lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" import regex as re with open(lowerCamelCase_ , 'r' ) as f: SCREAMING_SNAKE_CASE_ : Any = f.read() SCREAMING_SNAKE_CASE_ : str = re.findall(R'(.*) = ([0-9.]*)' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": SCREAMING_SNAKE_CASE_ : List[str] = float(lowerCamelCase_ ) if '.' in value else int(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.findall(R'(.*activations) = \(\'(.*)\',\)' , lowerCamelCase_ )[0] SCREAMING_SNAKE_CASE_ : Dict = str(activation[1] ) SCREAMING_SNAKE_CASE_ : str = num_experts SCREAMING_SNAKE_CASE_ : List[str] = SwitchTransformersConfig(**lowerCamelCase_ ) return config def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : Union[str, Any]=None , lowerCamelCase_ : List[str]="./" , lowerCamelCase_ : int=8 ) -> Optional[Any]: """simple docstring""" print(F'Loading flax weights from : {flax_checkpoint_path}' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = checkpoints.load_tax_checkpoint(lowerCamelCase_ ) if gin_file is not None: SCREAMING_SNAKE_CASE_ : str = convert_gin_to_config(lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE_ : List[Any] = SwitchTransformersConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = SwitchTransformersForConditionalGeneration(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = flax_params['target'] SCREAMING_SNAKE_CASE_ : List[str] = flatten_dict(lowerCamelCase_ , sep='/' ) SCREAMING_SNAKE_CASE_ : Tuple = rename_keys(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = unflatten_dict(lowerCamelCase_ , sep='/' ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(lowerCamelCase_ , lowerCamelCase_ ) print(F'Save PyTorch model to {pytorch_dump_path}' ) pt_model.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": UpperCamelCase__ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--switch_t5x_checkpoint_path''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the''' ''' model architecture. If not provided, a `gin_file` has to be provided.''' ), ) parser.add_argument( '''--gin_file''', default=None, type=str, required=False, help='''Path to the gin config file. If not provided, a `config_file` has to be passed ''', ) parser.add_argument( '''--config_name''', default=None, type=str, required=False, help='''Config name of SwitchTransformers model.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output pytorch model.''' ) parser.add_argument('''--num_experts''', default=8, type=int, required=False, help='''Number of experts''') UpperCamelCase__ : int = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
685
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
1
from collections import defaultdict class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 SCREAMING_SNAKE_CASE_ : Optional[int] = [ [-1 for i in range(total + 1 )] for j in range(2 ** len(snake_case__ ) ) ] SCREAMING_SNAKE_CASE_ : Any = defaultdict(snake_case__ ) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 SCREAMING_SNAKE_CASE_ : List[str] = (1 << len(snake_case__ )) - 1 def snake_case ( self ,snake_case__ ,snake_case__ ): # if mask == self.finalmask all persons are distributed tasks, return 1 if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement SCREAMING_SNAKE_CASE_ : List[Any] = self.count_ways_until(snake_case__ ,task_no + 1 ) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) ,task_no + 1 ) # save the value. SCREAMING_SNAKE_CASE_ : Union[str, Any] = total_ways_util return self.dp[mask][task_no] def snake_case ( self ,snake_case__ ): # Store the list of persons for each task for i in range(len(snake_case__ ) ): for j in task_performed[i]: self.task[j].append(snake_case__ ) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 ,1 ) if __name__ == "__main__": UpperCamelCase__ : List[Any] = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. UpperCamelCase__ : Dict = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
685
from math import log from scipy.constants import Boltzmann, physical_constants UpperCamelCase__ : Any = 3_00 # TEMPERATURE (unit = K) def __UpperCAmelCase ( lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('Donor concentration should be positive' ) elif acceptor_conc <= 0: raise ValueError('Acceptor concentration should be positive' ) elif intrinsic_conc <= 0: raise ValueError('Intrinsic concentration should be positive' ) elif donor_conc <= intrinsic_conc: raise ValueError( 'Donor concentration should be greater than intrinsic concentration' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( 'Acceptor concentration should be greater than intrinsic concentration' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase__ : Dict = '''▁''' UpperCamelCase__ : Union[str, Any] = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : str = BertGenerationTokenizer __a : Optional[int] = False __a : str = True def snake_case ( self ): super().setUp() SCREAMING_SNAKE_CASE_ : Tuple = BertGenerationTokenizer(snake_case__ ,keep_accents=snake_case__ ) tokenizer.save_pretrained(self.tmpdirname ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = '<s>' SCREAMING_SNAKE_CASE_ : Any = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case__ ) ,snake_case__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case__ ) ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,'<unk>' ) self.assertEqual(vocab_keys[1] ,'<s>' ) self.assertEqual(vocab_keys[-1] ,'<pad>' ) self.assertEqual(len(snake_case__ ) ,1002 ) def snake_case ( self ): self.assertEqual(self.get_tokenizer().vocab_size ,1000 ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = BertGenerationTokenizer(snake_case__ ,keep_accents=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(snake_case__ ,['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(snake_case__ ) ,[285, 46, 10, 170, 382] ,) SCREAMING_SNAKE_CASE_ : int = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( snake_case__ ,[ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] ,) SCREAMING_SNAKE_CASE_ : Any = tokenizer.convert_tokens_to_ids(snake_case__ ) self.assertListEqual( snake_case__ ,[8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] ,) SCREAMING_SNAKE_CASE_ : str = tokenizer.convert_ids_to_tokens(snake_case__ ) self.assertListEqual( snake_case__ ,[ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] ,) @cached_property def snake_case ( self ): return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = 'Hello World!' SCREAMING_SNAKE_CASE_ : List[Any] = [18536, 2260, 101] self.assertListEqual(snake_case__ ,self.big_tokenizer.encode(snake_case__ ) ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(snake_case__ ,self.big_tokenizer.encode(snake_case__ ) ) @require_torch @slow def snake_case ( self ): import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence SCREAMING_SNAKE_CASE_ : str = list(self.big_tokenizer.get_vocab().keys() )[:10] SCREAMING_SNAKE_CASE_ : str = ' '.join(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.big_tokenizer.encode_plus(snake_case__ ,return_tensors='pt' ,return_token_type_ids=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] ,return_tensors='pt' ,return_token_type_ids=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = BertGenerationConfig() SCREAMING_SNAKE_CASE_ : int = BertGenerationEncoder(snake_case__ ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**snake_case__ ) model(**snake_case__ ) @slow def snake_case ( self ): # fmt: off SCREAMING_SNAKE_CASE_ : Union[str, Any] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=snake_case__ ,model_name='google/bert_for_seq_generation_L-24_bbc_encoder' ,revision='c817d1fd1be2ffa69431227a1fe320544943d4db' ,)
685
class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [], [], [], ] def snake_case ( self ,snake_case__ ,snake_case__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError('Maximum queue size is 100' ) self.queues[priority].append(snake_case__ ) except IndexError: raise ValueError('Valid priorities are 0, 1, and 2' ) def snake_case ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError('All queues are empty' ) def __str__( self ): return "\n".join(F'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : List[str] = [] def snake_case ( self ,snake_case__ ): if len(self.queue ) == 100: raise OverFlowError('Maximum queue size is 100' ) self.queue.append(snake_case__ ) def snake_case ( self ): if not self.queue: raise UnderFlowError('The queue is empty' ) else: SCREAMING_SNAKE_CASE_ : List[Any] = min(self.queue ) self.queue.remove(snake_case__ ) return data def __str__( self ): return str(self.queue ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 1_00 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 1_28 ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(1_00 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(1_28 ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
685
1
from __future__ import annotations import numpy as np def __UpperCAmelCase ( lowerCamelCase_ : np.ndarray ) -> tuple[np.ndarray, np.ndarray]: """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = np.shape(lowerCamelCase_ ) if rows != columns: SCREAMING_SNAKE_CASE_ : List[Any] = ( '\'table\' has to be of square shaped array but got a ' F'{rows}x{columns} array:\n{table}' ) raise ValueError(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.zeros((rows, columns) ) for i in range(lowerCamelCase_ ): for j in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = sum(lower[i][k] * upper[k][j] for k in range(lowerCamelCase_ ) ) if upper[j][j] == 0: raise ArithmeticError('No LU decomposition exists' ) SCREAMING_SNAKE_CASE_ : Any = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ : Union[str, Any] = 1 for j in range(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[str] = sum(lower[i][k] * upper[k][j] for k in range(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : Any = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
685
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
1
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __UpperCAmelCase ( ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = { 'repo_name': ['test_repo1', 'test_repo2', 'test_repo3'], 'path': ['test_1.py', 'test_2.py', 'unit_test.py'], 'content': ['a ' * 20, 'a ' * 30, 'b ' * 7], } SCREAMING_SNAKE_CASE_ : Union[str, Any] = Dataset.from_dict(lowerCamelCase_ ) return dataset class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = get_dataset() SCREAMING_SNAKE_CASE_ : List[str] = make_duplicate_clusters(snake_case__ ,0.85 ) self.assertEqual(len(duplicate_clusters[0] ) ,2 ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = get_dataset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = deduplicate_dataset(snake_case__ ) self.assertEqual(len(snake_case__ ) ,2 ) print(snake_case__ ) self.assertEqual(duplicate_clusters[0][0]['copies'] ,2 ) self.assertEqual(duplicate_clusters[0][0]['is_extreme'] ,snake_case__ )
685
from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Tuple = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Any = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Dict = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] )
685
1
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available from .timesteps import ( fastaa_timesteps, smartaa_timesteps, smartaa_timesteps, smartaaa_timesteps, smartaaa_timesteps, superaa_timesteps, superaa_timesteps, superaaa_timesteps, ) @dataclass class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[List[PIL.Image.Image], np.ndarray] __a : Optional[List[bool]] __a : Optional[List[bool]] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_if import IFPipeline from .pipeline_if_imgaimg import IFImgaImgPipeline from .pipeline_if_imgaimg_superresolution import IFImgaImgSuperResolutionPipeline from .pipeline_if_inpainting import IFInpaintingPipeline from .pipeline_if_inpainting_superresolution import IFInpaintingSuperResolutionPipeline from .pipeline_if_superresolution import IFSuperResolutionPipeline from .safety_checker import IFSafetyChecker from .watermark import IFWatermarker
685
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
1
UpperCamelCase__ : Dict = 0 # The first color of the flag. UpperCamelCase__ : Tuple = 1 # The second color of the flag. UpperCamelCase__ : Union[str, Any] = 2 # The third color of the flag. UpperCamelCase__ : Dict = (red, white, blue) def __UpperCAmelCase ( lowerCamelCase_ : list ) -> list: """simple docstring""" if not sequence: return [] if len(lowerCamelCase_ ) == 1: return list(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = 0 SCREAMING_SNAKE_CASE_ : Tuple = len(lowerCamelCase_ ) - 1 SCREAMING_SNAKE_CASE_ : List[Any] = 0 while mid <= high: if sequence[mid] == colors[0]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : str = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = sequence[high], sequence[mid] high -= 1 else: SCREAMING_SNAKE_CASE_ : Any = F'The elements inside the sequence must contains only {colors} values' raise ValueError(lowerCamelCase_ ) return sequence if __name__ == "__main__": import doctest doctest.testmod() UpperCamelCase__ : Union[str, Any] = input('''Enter numbers separated by commas:\n''').strip() UpperCamelCase__ : List[str] = [int(item.strip()) for item in user_input.split(''',''')] print(F"""{dutch_national_flag_sort(unsorted)}""")
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
685
1
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def __UpperCAmelCase ( lowerCamelCase_ : List[str] , lowerCamelCase_ : Any ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = checkpoint SCREAMING_SNAKE_CASE_ : Optional[Any] = {} SCREAMING_SNAKE_CASE_ : Tuple = vae_state_dict['encoder.conv_in.weight'] SCREAMING_SNAKE_CASE_ : Optional[int] = vae_state_dict['encoder.conv_in.bias'] SCREAMING_SNAKE_CASE_ : Optional[int] = vae_state_dict['encoder.conv_out.weight'] SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict['encoder.conv_out.bias'] SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict['encoder.norm_out.weight'] SCREAMING_SNAKE_CASE_ : List[str] = vae_state_dict['encoder.norm_out.bias'] SCREAMING_SNAKE_CASE_ : Any = vae_state_dict['decoder.conv_in.weight'] SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict['decoder.conv_in.bias'] SCREAMING_SNAKE_CASE_ : Union[str, Any] = vae_state_dict['decoder.conv_out.weight'] SCREAMING_SNAKE_CASE_ : Union[str, Any] = vae_state_dict['decoder.conv_out.bias'] SCREAMING_SNAKE_CASE_ : List[str] = vae_state_dict['decoder.norm_out.weight'] SCREAMING_SNAKE_CASE_ : Dict = vae_state_dict['decoder.norm_out.bias'] SCREAMING_SNAKE_CASE_ : Dict = vae_state_dict['quant_conv.weight'] SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict['quant_conv.bias'] SCREAMING_SNAKE_CASE_ : Dict = vae_state_dict['post_quant_conv.weight'] SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict['post_quant_conv.bias'] # Retrieves the keys for the encoder down blocks only SCREAMING_SNAKE_CASE_ : List[Any] = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'encoder.down' in layer} ) SCREAMING_SNAKE_CASE_ : int = { layer_id: [key for key in vae_state_dict if F'down.{layer_id}' in key] for layer_id in range(lowerCamelCase_ ) } # Retrieves the keys for the decoder up blocks only SCREAMING_SNAKE_CASE_ : Optional[Any] = len({'.'.join(layer.split('.' )[:3] ) for layer in vae_state_dict if 'decoder.up' in layer} ) SCREAMING_SNAKE_CASE_ : List[Any] = { layer_id: [key for key in vae_state_dict if F'up.{layer_id}' in key] for layer_id in range(lowerCamelCase_ ) } for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = [key for key in down_blocks[i] if F'down.{i}' in key and F'down.{i}.downsample' not in key] if F'encoder.down.{i}.downsample.conv.weight' in vae_state_dict: SCREAMING_SNAKE_CASE_ : Dict = vae_state_dict.pop( F'encoder.down.{i}.downsample.conv.weight' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = vae_state_dict.pop( F'encoder.down.{i}.downsample.conv.bias' ) SCREAMING_SNAKE_CASE_ : str = renew_vae_resnet_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = {'old': F'down.{i}.block', 'new': F'down_blocks.{i}.resnets'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = [key for key in vae_state_dict if 'encoder.mid.block' in key] SCREAMING_SNAKE_CASE_ : Dict = 2 for i in range(1 , num_mid_res_blocks + 1 ): SCREAMING_SNAKE_CASE_ : Optional[int] = [key for key in mid_resnets if F'encoder.mid.block_{i}' in key] SCREAMING_SNAKE_CASE_ : Optional[int] = renew_vae_resnet_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = {'old': F'mid.block_{i}', 'new': F'mid_block.resnets.{i - 1}'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = [key for key in vae_state_dict if 'encoder.mid.attn' in key] SCREAMING_SNAKE_CASE_ : int = renew_vae_attention_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) conv_attn_to_linear(lowerCamelCase_ ) for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Dict = num_up_blocks - 1 - i SCREAMING_SNAKE_CASE_ : Dict = [ key for key in up_blocks[block_id] if F'up.{block_id}' in key and F'up.{block_id}.upsample' not in key ] if F'decoder.up.{block_id}.upsample.conv.weight' in vae_state_dict: SCREAMING_SNAKE_CASE_ : List[Any] = vae_state_dict[ F'decoder.up.{block_id}.upsample.conv.weight' ] SCREAMING_SNAKE_CASE_ : Any = vae_state_dict[ F'decoder.up.{block_id}.upsample.conv.bias' ] SCREAMING_SNAKE_CASE_ : List[Any] = renew_vae_resnet_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = {'old': F'up.{block_id}.block', 'new': F'up_blocks.{i}.resnets'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = [key for key in vae_state_dict if 'decoder.mid.block' in key] SCREAMING_SNAKE_CASE_ : List[Any] = 2 for i in range(1 , num_mid_res_blocks + 1 ): SCREAMING_SNAKE_CASE_ : List[str] = [key for key in mid_resnets if F'decoder.mid.block_{i}' in key] SCREAMING_SNAKE_CASE_ : Tuple = renew_vae_resnet_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = {'old': F'mid.block_{i}', 'new': F'mid_block.resnets.{i - 1}'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : int = [key for key in vae_state_dict if 'decoder.mid.attn' in key] SCREAMING_SNAKE_CASE_ : str = renew_vae_attention_paths(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = {'old': 'mid.attn_1', 'new': 'mid_block.attentions.0'} assign_to_checkpoint(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , additional_replacements=[meta_path] , config=lowerCamelCase_ ) conv_attn_to_linear(lowerCamelCase_ ) return new_checkpoint def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = requests.get( ' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml' ) SCREAMING_SNAKE_CASE_ : Any = io.BytesIO(r.content ) SCREAMING_SNAKE_CASE_ : Optional[Any] = OmegaConf.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[str] = 5_12 SCREAMING_SNAKE_CASE_ : str = 'cuda' if torch.cuda.is_available() else 'cpu' if checkpoint_path.endswith('safetensors' ): from safetensors import safe_open SCREAMING_SNAKE_CASE_ : Tuple = {} with safe_open(lowerCamelCase_ , framework='pt' , device='cpu' ) as f: for key in f.keys(): SCREAMING_SNAKE_CASE_ : Optional[int] = f.get_tensor(lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(lowerCamelCase_ , map_location=lowerCamelCase_ )['state_dict'] # Convert the VAE model. SCREAMING_SNAKE_CASE_ : Optional[Any] = create_vae_diffusers_config(lowerCamelCase_ , image_size=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = custom_convert_ldm_vae_checkpoint(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = AutoencoderKL(**lowerCamelCase_ ) vae.load_state_dict(lowerCamelCase_ ) vae.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": UpperCamelCase__ : Dict = argparse.ArgumentParser() parser.add_argument('''--vae_pt_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''') parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''') UpperCamelCase__ : str = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
685
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int = 2 ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE_ : Optional[int] = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : str = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , lowerCamelCase_ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , lowerCamelCase_ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(lowerCamelCase_ ) ) , list(range(lowerCamelCase_ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
685
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Tuple = { '''configuration_pix2struct''': [ '''PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Pix2StructConfig''', '''Pix2StructTextConfig''', '''Pix2StructVisionConfig''', ], '''processing_pix2struct''': ['''Pix2StructProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Optional[int] = ['''Pix2StructImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : str = [ '''PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Pix2StructPreTrainedModel''', '''Pix2StructForConditionalGeneration''', '''Pix2StructVisionModel''', '''Pix2StructTextModel''', ] if TYPE_CHECKING: from .configuration_pixastruct import ( PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP, PixaStructConfig, PixaStructTextConfig, PixaStructVisionConfig, ) from .processing_pixastruct import PixaStructProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_pixastruct import PixaStructImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pixastruct import ( PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST, PixaStructForConditionalGeneration, PixaStructPreTrainedModel, PixaStructTextModel, PixaStructVisionModel, ) else: import sys UpperCamelCase__ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
1
from heapq import heappop, heappush import numpy as np def __UpperCAmelCase ( lowerCamelCase_ : np.ndarray , lowerCamelCase_ : tuple[int, int] , lowerCamelCase_ : tuple[int, int] , lowerCamelCase_ : bool , ) -> tuple[float | int, list[tuple[int, int]]]: """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = grid.shape SCREAMING_SNAKE_CASE_ : List[str] = [-1, 1, 0, 0] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = [(0, source)], set() SCREAMING_SNAKE_CASE_ : Optional[int] = np.full((rows, cols) , np.inf ) SCREAMING_SNAKE_CASE_ : Tuple = 0 SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.empty((rows, cols) , dtype=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = None while queue: ((SCREAMING_SNAKE_CASE_) , (SCREAMING_SNAKE_CASE_)) : Dict = heappop(lowerCamelCase_ ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: SCREAMING_SNAKE_CASE_ : List[Any] = [] while (x, y) != source: path.append((x, y) ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = predecessors[x, y] path.append(lowerCamelCase_ ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(lowerCamelCase_ ) ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: SCREAMING_SNAKE_CASE_ : List[str] = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(lowerCamelCase_ , (dist + 1, (nx, ny)) ) SCREAMING_SNAKE_CASE_ : int = dist + 1 SCREAMING_SNAKE_CASE_ : Optional[int] = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
685
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
1
from __future__ import annotations import inspect import unittest from typing import List, Tuple from transformers import RegNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=3 ,snake_case__=32 ,snake_case__=3 ,snake_case__=10 ,snake_case__=[10, 20, 30, 40] ,snake_case__=[1, 1, 2, 1] ,snake_case__=True ,snake_case__=True ,snake_case__="relu" ,snake_case__=3 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : List[Any] = num_channels SCREAMING_SNAKE_CASE_ : List[Any] = embeddings_size SCREAMING_SNAKE_CASE_ : int = hidden_sizes SCREAMING_SNAKE_CASE_ : Optional[int] = depths SCREAMING_SNAKE_CASE_ : List[Any] = is_training SCREAMING_SNAKE_CASE_ : Tuple = use_labels SCREAMING_SNAKE_CASE_ : List[str] = hidden_act SCREAMING_SNAKE_CASE_ : Union[str, Any] = num_labels SCREAMING_SNAKE_CASE_ : List[str] = scope SCREAMING_SNAKE_CASE_ : Any = len(snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ : Tuple = None if self.use_labels: SCREAMING_SNAKE_CASE_ : List[str] = ids_tensor([self.batch_size] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Any = self.get_config() return config, pixel_values, labels def snake_case ( self ): return RegNetConfig( num_channels=self.num_channels ,embeddings_size=self.embeddings_size ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,hidden_act=self.hidden_act ,num_labels=self.num_labels ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = TFRegNetModel(config=snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,training=snake_case__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE_ : Union[str, Any] = TFRegNetForImageClassification(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,labels=snake_case__ ,training=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = config_and_inputs SCREAMING_SNAKE_CASE_ : Union[str, Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else () __a : Union[str, Any] = ( {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification} if is_tf_available() else {} ) __a : Optional[Any] = False __a : Optional[int] = False __a : Dict = False __a : int = False __a : Optional[int] = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = TFRegNetModelTester(self ) SCREAMING_SNAKE_CASE_ : List[Any] = ConfigTester(self ,config_class=snake_case__ ,has_text_modality=snake_case__ ) def snake_case ( self ): return @unittest.skip(reason='RegNet does not use inputs_embeds' ) def snake_case ( self ): pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 ,reason='TF does not support backprop for grouped convolutions on CPU.' ,) @slow def snake_case ( self ): super().test_keras_fit() @unittest.skip(reason='RegNet does not support input and output embeddings' ) def snake_case ( self ): pass def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : List[Any] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ : Any = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ : Optional[Any] = ['pixel_values'] self.assertListEqual(arg_names[:1] ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): def check_hidden_states_output(snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = model(**self._prepare_for_class(snake_case__ ,snake_case__ ) ,training=snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.num_stages self.assertEqual(len(snake_case__ ) ,expected_num_stages + 1 ) # RegNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[self.model_tester.image_size // 2, self.model_tester.image_size // 2] ,) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[Any] = ['basic', 'bottleneck'] for model_class in self.all_model_classes: for layer_type in layers_type: SCREAMING_SNAKE_CASE_ : str = layer_type SCREAMING_SNAKE_CASE_ : Optional[int] = True check_hidden_states_output(snake_case__ ,snake_case__ ,snake_case__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_ : Dict = True check_hidden_states_output(snake_case__ ,snake_case__ ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = self.model_tester.prepare_config_and_inputs_for_common() def check_equivalence(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__={} ): SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,return_dict=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,return_dict=snake_case__ ,**snake_case__ ).to_tuple() def recursive_check(snake_case__ ,snake_case__ ): if isinstance(snake_case__ ,(List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(snake_case__ ,snake_case__ ): recursive_check(snake_case__ ,snake_case__ ) elif tuple_object is None: return else: self.assertTrue( all(tf.equal(snake_case__ ,snake_case__ ) ) ,msg=( 'Tuple and dict output are not equal. Difference:' F' {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}' ) ,) recursive_check(snake_case__ ,snake_case__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : int = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : int = self._prepare_for_class(snake_case__ ,snake_case__ ) check_equivalence(snake_case__ ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = self._prepare_for_class(snake_case__ ,snake_case__ ,return_labels=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = self._prepare_for_class(snake_case__ ,snake_case__ ,return_labels=snake_case__ ) check_equivalence(snake_case__ ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self._prepare_for_class(snake_case__ ,snake_case__ ) check_equivalence(snake_case__ ,snake_case__ ,snake_case__ ,{'output_hidden_states': True} ) SCREAMING_SNAKE_CASE_ : List[str] = self._prepare_for_class(snake_case__ ,snake_case__ ,return_labels=snake_case__ ) SCREAMING_SNAKE_CASE_ : str = self._prepare_for_class(snake_case__ ,snake_case__ ,return_labels=snake_case__ ) check_equivalence(snake_case__ ,snake_case__ ,snake_case__ ,{'output_hidden_states': True} ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case__ ) @slow def snake_case ( self ): for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : Any = TFRegNetModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) def __UpperCAmelCase ( ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case ( self ): return ( AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) SCREAMING_SNAKE_CASE_ : Optional[int] = self.default_image_processor SCREAMING_SNAKE_CASE_ : Optional[int] = prepare_img() SCREAMING_SNAKE_CASE_ : str = image_processor(images=snake_case__ ,return_tensors='tf' ) # forward pass SCREAMING_SNAKE_CASE_ : Optional[Any] = model(**snake_case__ ,training=snake_case__ ) # verify the logits SCREAMING_SNAKE_CASE_ : Tuple = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = tf.constant([-0.4180, -1.5051, -3.4836] ) tf.debugging.assert_near(outputs.logits[0, :3] ,snake_case__ ,atol=1E-4 )
685
def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> Tuple: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) else: return a * actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(lowerCamelCase_ , lowerCamelCase_ ) return actual_power(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": print(power(-2, -3))
685
1
import math import os import sys def __UpperCAmelCase ( lowerCamelCase_ : str ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = '' try: with open(lowerCamelCase_ , 'rb' ) as binary_file: SCREAMING_SNAKE_CASE_ : Any = binary_file.read() for dat in data: SCREAMING_SNAKE_CASE_ : List[str] = F'{dat:08b}' result += curr_byte return result except OSError: print('File not accessible' ) sys.exit() def __UpperCAmelCase ( lowerCamelCase_ : dict[str, str] , lowerCamelCase_ : str , lowerCamelCase_ : int , lowerCamelCase_ : str ) -> None: """simple docstring""" lexicon.pop(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[str] = last_match_id if math.loga(lowerCamelCase_ ).is_integer(): for curr_key in lexicon: SCREAMING_SNAKE_CASE_ : Optional[Any] = '0' + lexicon[curr_key] SCREAMING_SNAKE_CASE_ : int = bin(lowerCamelCase_ )[2:] def __UpperCAmelCase ( lowerCamelCase_ : str ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = {'0': '0', '1': '1'} SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = '', '' SCREAMING_SNAKE_CASE_ : List[Any] = len(lowerCamelCase_ ) for i in range(len(lowerCamelCase_ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue SCREAMING_SNAKE_CASE_ : Dict = lexicon[curr_string] result += last_match_id add_key_to_lexicon(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) index += 1 SCREAMING_SNAKE_CASE_ : str = '' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": SCREAMING_SNAKE_CASE_ : Tuple = lexicon[curr_string] result += last_match_id return result def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = os.path.getsize(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = bin(lowerCamelCase_ )[2:] SCREAMING_SNAKE_CASE_ : List[str] = len(lowerCamelCase_ ) return "0" * (length_length - 1) + file_length_binary + compressed def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> None: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = 8 try: with open(lowerCamelCase_ , 'wb' ) as opened_file: SCREAMING_SNAKE_CASE_ : Dict = [ to_write[i : i + byte_length] for i in range(0 , len(lowerCamelCase_ ) , lowerCamelCase_ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('10000000' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(lowerCamelCase_ , 2 ).to_bytes(1 , byteorder='big' ) ) except OSError: print('File not accessible' ) sys.exit() def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> None: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = read_file_binary(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = compress_data(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = add_file_length(lowerCamelCase_ , lowerCamelCase_ ) write_file_binary(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
685
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=True ,snake_case__=False ,snake_case__=True ,snake_case__=99 ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=16 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=4 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = parent SCREAMING_SNAKE_CASE_ : List[Any] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = seq_length SCREAMING_SNAKE_CASE_ : Tuple = is_training SCREAMING_SNAKE_CASE_ : List[str] = use_input_mask SCREAMING_SNAKE_CASE_ : List[str] = use_token_type_ids SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : int = vocab_size SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE_ : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Any = intermediate_size SCREAMING_SNAKE_CASE_ : Dict = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = type_vocab_size SCREAMING_SNAKE_CASE_ : int = type_sequence_label_size SCREAMING_SNAKE_CASE_ : int = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : List[str] = num_choices SCREAMING_SNAKE_CASE_ : Tuple = scope def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Dict = None if self.use_labels: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size] ,self.num_choices ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case ( self ): return LlamaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = LlamaModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,attention_mask=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaModel(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE_ : List[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,use_cache=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ : str = ids_tensor((self.batch_size, 3) ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor((self.batch_size, 3) ,vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_ids, next_tokens] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_mask, next_mask] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Dict = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] # select random slice SCREAMING_SNAKE_CASE_ : Any = ids_tensor((1,) ,output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ : str = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE_ : str = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __a : int = (LlamaForCausalLM,) if is_torch_available() else () __a : Any = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) __a : Tuple = False __a : Tuple = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaModelTester(self ) SCREAMING_SNAKE_CASE_ : List[str] = ConfigTester(self ,config_class=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_ : Optional[int] = type self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Dict = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : str = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Any = 3 SCREAMING_SNAKE_CASE_ : int = 'single_label_classification' SCREAMING_SNAKE_CASE_ : str = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Dict = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Tuple = 3 SCREAMING_SNAKE_CASE_ : str = 'multi_label_classification' SCREAMING_SNAKE_CASE_ : int = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Tuple = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor( [self.model_tester.batch_size, config.num_labels] ,self.model_tester.type_sequence_label_size ).to(torch.float ) SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def snake_case ( self ): pass @parameterized.expand([('linear',), ('dynamic',)] ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([1, 10] ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : Any = LlamaModel(snake_case__ ) original_model.to(snake_case__ ) original_model.eval() SCREAMING_SNAKE_CASE_ : int = original_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : List[Any] = original_model(snake_case__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : List[Any] = {'type': scaling_type, 'factor': 10.0} SCREAMING_SNAKE_CASE_ : int = LlamaModel(snake_case__ ) scaled_model.to(snake_case__ ) scaled_model.eval() SCREAMING_SNAKE_CASE_ : str = scaled_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : Optional[int] = scaled_model(snake_case__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : List[str] = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : int = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : Dict = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : str = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : int = model(torch.tensor(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] ,dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # fmt: off SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' SCREAMING_SNAKE_CASE_ : List[str] = 'Simply put, the theory of relativity states that ' SCREAMING_SNAKE_CASE_ : str = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.encode(snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Tuple = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' ,device_map='sequential' ,use_safetensors=snake_case__ ) # greedy generation outputs SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.generate(snake_case__ ,max_new_tokens=64 ,top_p=snake_case__ ,temperature=1 ,do_sample=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.decode(generated_ids[0] ,skip_special_tokens=snake_case__ ) self.assertEqual(snake_case__ ,snake_case__ )
685
1
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Union[str, Any]: """simple docstring""" def is_in_circle(lowerCamelCase_ : float , lowerCamelCase_ : float ) -> bool: SCREAMING_SNAKE_CASE_ : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE_ : Optional[int] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(lowerCamelCase_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE_ : Tuple = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Callable[[float], float] , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(lowerCamelCase_ , lowerCamelCase_ ) ) for _ in range(lowerCamelCase_ ) ) * (max_value - min_value) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 ) -> None: """simple docstring""" def identity_function(lowerCamelCase_ : float ) -> float: return x SCREAMING_SNAKE_CASE_ : str = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> None: """simple docstring""" def function_to_integrate(lowerCamelCase_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE_ : Dict = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
685
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''} UpperCamelCase__ : int = { '''vocab_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''', }, '''emoji_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''', }, } UpperCamelCase__ : str = { '''abeja/gpt-neox-japanese-2.7b''': 20_48, } def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Tuple ) -> List[str]: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[Any] = json.loads(f.read() ) SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : List[Any] = collections.OrderedDict() with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Any = f.readlines() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = b SCREAMING_SNAKE_CASE_ : Dict = idx for wd in b: SCREAMING_SNAKE_CASE_ : Any = idx return vocab, raw_vocab, ids_to_tokens, emoji class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = VOCAB_FILES_NAMES __a : List[str] = PRETRAINED_VOCAB_FILES_MAP __a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__="<|endoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__="<|startoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__=False ,**snake_case__ ,): super().__init__( unk_token=snake_case__ ,pad_token=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,do_clean_text=snake_case__ ,**snake_case__ ,) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained' ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google' ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) SCREAMING_SNAKE_CASE_ : str = do_clean_text SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = load_vocab_and_emoji(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = SubWordJapaneseTokenizer( vocab=self.vocab ,ids_to_tokens=self.ids_to_tokens ,emoji=self.emoji ) @property def snake_case ( self ): # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def snake_case ( self ): return dict(self.raw_vocab ,**self.added_tokens_encoder ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.tokenize(snake_case__ ,clean=self.do_clean_text ) def snake_case ( self ,snake_case__ ): return self.vocab.get(snake_case__ ,self.vocab.get(self.unk_token ) ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.convert_id_to_token(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = ''.join(snake_case__ ).strip() return out_string def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(snake_case__ ,add_special_tokens=snake_case__ ) + [self.eos_token_id] ) if len(snake_case__ ) > self.model_max_length: SCREAMING_SNAKE_CASE_ : List[Any] = input_ids[-self.model_max_length :] return input_ids def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 if os.path.isdir(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: SCREAMING_SNAKE_CASE_ : Tuple = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : str = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.' ' Please check that the vocabulary is not corrupted!' ) SCREAMING_SNAKE_CASE_ : Dict = token_index writer.write(','.join(snake_case__ ) + '\n' ) index += 1 with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: json.dump(self.emoji ,snake_case__ ) return vocab_file, emoji_file class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = vocab # same as swe SCREAMING_SNAKE_CASE_ : Optional[int] = ids_to_tokens # same as bpe SCREAMING_SNAKE_CASE_ : Dict = emoji SCREAMING_SNAKE_CASE_ : int = np.max([len(snake_case__ ) for w in self.vocab.keys()] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) SCREAMING_SNAKE_CASE_ : str = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' SCREAMING_SNAKE_CASE_ : int = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' SCREAMING_SNAKE_CASE_ : Tuple = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self ): return len(self.ids_to_tokens ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<URL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.content_repattera.sub('<EMAIL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<TEL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<PRICE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: SCREAMING_SNAKE_CASE_ : Union[str, Any] = content.replace('<BLOCK><BLOCK>' ,'<BLOCK>' ) return content def snake_case ( self ,snake_case__ ,snake_case__=False ): SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('\r\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\r' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : List[str] = text.replace('\t' ,'<TAB>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('—' ,'ー' ) SCREAMING_SNAKE_CASE_ : Optional[int] = text.replace('−' ,'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: SCREAMING_SNAKE_CASE_ : int = text.replace(snake_case__ ,snake_case__ ) if clean: SCREAMING_SNAKE_CASE_ : str = self.clean_text(snake_case__ ) def check_simbol(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 2: SCREAMING_SNAKE_CASE_ : str = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0XC2A1 and c <= 0XC2BF) or (c >= 0XC780 and c <= 0XC783) or (c >= 0XCAB9 and c <= 0XCBBF) or (c >= 0XCC80 and c <= 0XCDA2) ): return True return False def checkuae(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 3: SCREAMING_SNAKE_CASE_ : Dict = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0XE2_8080 and c <= 0XE2_B07F: return True return False SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : List[Any] = [] while pos < len(snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = min(len(snake_case__ ) ,pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 SCREAMING_SNAKE_CASE_ : List[Any] = [] # (token_id, token, pos) for e in range(snake_case__ ,snake_case__ ,-1 ): SCREAMING_SNAKE_CASE_ : str = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(snake_case__ ) > 2: SCREAMING_SNAKE_CASE_ : Optional[Any] = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(snake_case__ ) > 0: # the smallest token_id is adopted SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = sorted(snake_case__ ,key=lambda snake_case__ : x[0] )[0] result.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = e else: SCREAMING_SNAKE_CASE_ : Any = pos + 1 SCREAMING_SNAKE_CASE_ : Optional[int] = text[pos:end] if check_simbol(snake_case__ ): result.append('<KIGOU>' ) elif checkuae(snake_case__ ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) SCREAMING_SNAKE_CASE_ : int = end return result def snake_case ( self ,snake_case__ ,snake_case__="\n" ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Dict = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : Dict = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(snake_case__ ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(snake_case__ ) if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : int = ''.join(snake_case__ ) return text
685
1
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=True ,snake_case__=True ,snake_case__=True ,snake_case__=True ,snake_case__=False ,snake_case__=False ,snake_case__=False ,snake_case__=2 ,snake_case__=99 ,snake_case__=0 ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=12 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=4 ,snake_case__="last" ,snake_case__=None ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : Tuple = parent SCREAMING_SNAKE_CASE_ : Optional[Any] = batch_size SCREAMING_SNAKE_CASE_ : Dict = seq_length SCREAMING_SNAKE_CASE_ : Union[str, Any] = is_training SCREAMING_SNAKE_CASE_ : Tuple = use_input_lengths SCREAMING_SNAKE_CASE_ : List[str] = use_token_type_ids SCREAMING_SNAKE_CASE_ : Tuple = use_labels SCREAMING_SNAKE_CASE_ : Tuple = gelu_activation SCREAMING_SNAKE_CASE_ : Optional[int] = sinusoidal_embeddings SCREAMING_SNAKE_CASE_ : List[str] = causal SCREAMING_SNAKE_CASE_ : List[str] = asm SCREAMING_SNAKE_CASE_ : int = n_langs SCREAMING_SNAKE_CASE_ : List[Any] = vocab_size SCREAMING_SNAKE_CASE_ : List[str] = n_special SCREAMING_SNAKE_CASE_ : str = hidden_size SCREAMING_SNAKE_CASE_ : str = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = max_position_embeddings SCREAMING_SNAKE_CASE_ : List[Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : Any = type_sequence_label_size SCREAMING_SNAKE_CASE_ : Any = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE_ : int = summary_type SCREAMING_SNAKE_CASE_ : List[str] = use_proj SCREAMING_SNAKE_CASE_ : Union[str, Any] = scope def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : List[str] = None if self.use_input_lengths: SCREAMING_SNAKE_CASE_ : Tuple = ( ids_tensor([self.batch_size] ,vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length SCREAMING_SNAKE_CASE_ : List[str] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : str = ids_tensor([self.batch_size, self.seq_length] ,self.n_langs ) SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Any = None SCREAMING_SNAKE_CASE_ : Optional[Any] = None if self.use_labels: SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size] ,2 ).float() SCREAMING_SNAKE_CASE_ : List[str] = ids_tensor([self.batch_size] ,self.num_choices ) SCREAMING_SNAKE_CASE_ : List[str] = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def snake_case ( self ): return FlaubertConfig( vocab_size=self.vocab_size ,n_special=self.n_special ,emb_dim=self.hidden_size ,n_layers=self.num_hidden_layers ,n_heads=self.num_attention_heads ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,gelu_activation=self.gelu_activation ,sinusoidal_embeddings=self.sinusoidal_embeddings ,asm=self.asm ,causal=self.causal ,n_langs=self.n_langs ,max_position_embeddings=self.max_position_embeddings ,initializer_range=self.initializer_range ,summary_type=self.summary_type ,use_proj=self.use_proj ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Any = FlaubertModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : str = model(snake_case__ ,lengths=snake_case__ ,langs=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model(snake_case__ ,langs=snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaubertWithLMHeadModel(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,token_type_ids=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.loss.shape ,() ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Any = FlaubertForQuestionAnsweringSimple(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,start_positions=snake_case__ ,end_positions=snake_case__ ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaubertForQuestionAnswering(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[int] = model(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = model( snake_case__ ,start_positions=snake_case__ ,end_positions=snake_case__ ,cls_index=snake_case__ ,is_impossible=snake_case__ ,p_mask=snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = model( snake_case__ ,start_positions=snake_case__ ,end_positions=snake_case__ ,cls_index=snake_case__ ,is_impossible=snake_case__ ,) ((SCREAMING_SNAKE_CASE_) , ) : Optional[int] = result_with_labels.to_tuple() SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,start_positions=snake_case__ ,end_positions=snake_case__ ) ((SCREAMING_SNAKE_CASE_) , ) : Optional[Any] = result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape ,() ) self.parent.assertEqual(result.start_top_log_probs.shape ,(self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape ,(self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape ,(self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape ,(self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape ,(self.batch_size,) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[int] = FlaubertForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.loss.shape ,() ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.type_sequence_label_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.num_labels SCREAMING_SNAKE_CASE_ : Dict = FlaubertForTokenClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : List[str] = self.num_choices SCREAMING_SNAKE_CASE_ : int = FlaubertForMultipleChoice(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = input_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() SCREAMING_SNAKE_CASE_ : Union[str, Any] = token_type_ids.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() SCREAMING_SNAKE_CASE_ : Any = input_mask.unsqueeze(1 ).expand(-1 ,self.num_choices ,-1 ).contiguous() SCREAMING_SNAKE_CASE_ : Dict = model( snake_case__ ,attention_mask=snake_case__ ,token_type_ids=snake_case__ ,labels=snake_case__ ,) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_choices) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE_ : Tuple = { 'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths, 'attention_mask': input_mask, } return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Dict = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) __a : Optional[int] = ( { "feature-extraction": FlaubertModel, "fill-mask": FlaubertWithLMHeadModel, "question-answering": FlaubertForQuestionAnsweringSimple, "text-classification": FlaubertForSequenceClassification, "token-classification": FlaubertForTokenClassification, "zero-shot": FlaubertForSequenceClassification, } if is_torch_available() else {} ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('Fast' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__=False ): SCREAMING_SNAKE_CASE_ : List[Any] = super()._prepare_for_class(snake_case__ ,snake_case__ ,return_labels=snake_case__ ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.zeros( self.model_tester.batch_size ,dtype=torch.long ,device=snake_case__ ) return inputs_dict def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = FlaubertModelTester(self ) SCREAMING_SNAKE_CASE_ : List[Any] = ConfigTester(self ,config_class=snake_case__ ,emb_dim=37 ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case__ ) @slow def snake_case ( self ): for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : List[Any] = FlaubertModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) @slow @require_torch_gpu def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return SCREAMING_SNAKE_CASE_ : List[str] = True SCREAMING_SNAKE_CASE_ : str = model_class(config=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.jit.trace( snake_case__ ,(inputs_dict['input_ids'].to('cpu' ), inputs_dict['attention_mask'].to('cpu' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(snake_case__ ,os.path.join(snake_case__ ,'traced_model.pt' ) ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.jit.load(os.path.join(snake_case__ ,'traced_model.pt' ) ,map_location=snake_case__ ) loaded(inputs_dict['input_ids'].to(snake_case__ ) ,inputs_dict['attention_mask'].to(snake_case__ ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaubertModel.from_pretrained('flaubert/flaubert_base_cased' ) SCREAMING_SNAKE_CASE_ : int = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ )[0] SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.Size((1, 11, 768) ) self.assertEqual(output.shape ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.tensor( [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] ,snake_case__ ,atol=1E-4 ) )
685
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : int=() , lowerCamelCase_ : Union[str, Any]=None , lowerCamelCase_ : Optional[int]="no" , lowerCamelCase_ : Optional[Any]="29500" ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False if any(key.startswith('KAGGLE' ) for key in os.environ.keys() ): SCREAMING_SNAKE_CASE_ : str = True elif "IPython" in sys.modules: SCREAMING_SNAKE_CASE_ : Dict = 'google.colab' in str(sys.modules['IPython'].get_ipython() ) try: SCREAMING_SNAKE_CASE_ : Optional[int] = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F'Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.' ) if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME' , lowerCamelCase_ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ' 'your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if num_processes is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = 8 SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='TPU' ) print(F'Launching a training on {num_processes} TPU cores.' ) xmp.spawn(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on one CPU.' ) function(*lowerCamelCase_ ) else: if num_processes is None: raise ValueError( 'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ' 'inside your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if torch.cuda.is_initialized(): raise ValueError( 'To launch a multi-GPU training from your notebook, you need to avoid running any instruction ' 'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ' 'function.' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port=lowerCamelCase_ , mixed_precision=lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='MULTI_GPU' ) print(F'Launching training on {num_processes} GPUs.' ) try: start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( 'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ' 'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ' 'Please review your imports and test them when running the `notebook_launcher()` to identify ' 'which one is problematic.' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): SCREAMING_SNAKE_CASE_ : Optional[Any] = '1' print('Launching training on MPS.' ) elif torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on CPU.' ) function(*lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple=() , lowerCamelCase_ : str=2 ) -> Union[str, Any]: """simple docstring""" from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port='29500' , accelerate_mixed_precision='no' , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu='yes' , ): SCREAMING_SNAKE_CASE_ : Tuple = PrepareForLaunch(lowerCamelCase_ , debug=lowerCamelCase_ ) start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' )
685
1
from __future__ import annotations from functools import lru_cache from math import ceil UpperCamelCase__ : Union[str, Any] = 1_00 UpperCamelCase__ : Union[str, Any] = set(range(3, NUM_PRIMES, 2)) primes.add(2) UpperCamelCase__ : int for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=1_00 ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> set[int]: """simple docstring""" if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} SCREAMING_SNAKE_CASE_ : set[int] = set() SCREAMING_SNAKE_CASE_ : int SCREAMING_SNAKE_CASE_ : int for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def __UpperCAmelCase ( lowerCamelCase_ : int = 50_00 ) -> int | None: """simple docstring""" for number_to_partition in range(1 , lowerCamelCase_ ): if len(partition(lowerCamelCase_ ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(F"""{solution() = }""")
685
from typing import TYPE_CHECKING from ....utils import _LazyModule UpperCamelCase__ : Tuple = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
685
1
import math def __UpperCAmelCase ( lowerCamelCase_ : int ) -> list: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = [True] * n SCREAMING_SNAKE_CASE_ : List[str] = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False SCREAMING_SNAKE_CASE_ : Optional[Any] = True for i in range(3 , int(n**0.5 + 1 ) , 2 ): SCREAMING_SNAKE_CASE_ : Optional[int] = i * 2 while index < n: SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : List[str] = index + i SCREAMING_SNAKE_CASE_ : int = [2] for i in range(3 , lowerCamelCase_ , 2 ): if is_prime[i]: primes.append(lowerCamelCase_ ) return primes def __UpperCAmelCase ( lowerCamelCase_ : int = 99_99_66_66_33_33 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = math.floor(math.sqrt(lowerCamelCase_ ) ) + 1_00 SCREAMING_SNAKE_CASE_ : Optional[int] = prime_sieve(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = 0 SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Union[str, Any] = primes[prime_index] while (last_prime**2) <= limit: SCREAMING_SNAKE_CASE_ : Optional[int] = primes[prime_index + 1] SCREAMING_SNAKE_CASE_ : Optional[Any] = last_prime**2 SCREAMING_SNAKE_CASE_ : str = next_prime**2 # Get numbers divisible by lps(current) SCREAMING_SNAKE_CASE_ : str = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) SCREAMING_SNAKE_CASE_ : List[Any] = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps SCREAMING_SNAKE_CASE_ : Any = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair SCREAMING_SNAKE_CASE_ : Union[str, Any] = next_prime prime_index += 1 return matches_sum if __name__ == "__main__": print(solution())
685
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = CLIPTokenizer __a : List[str] = CLIPTokenizerFast __a : List[str] = True __a : Tuple = {} __a : Tuple = False def snake_case ( self ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE_ : Union[str, Any] = dict(zip(snake_case__ ,range(len(snake_case__ ) ) ) ) SCREAMING_SNAKE_CASE_ : Any = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] SCREAMING_SNAKE_CASE_ : Any = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case__ ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case__ ) ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = 'lower newer' SCREAMING_SNAKE_CASE_ : Tuple = 'lower newer' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = CLIPTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) SCREAMING_SNAKE_CASE_ : List[Any] = 'lower newer' SCREAMING_SNAKE_CASE_ : Optional[Any] = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] SCREAMING_SNAKE_CASE_ : Any = tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ : Optional[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) @require_ftfy def snake_case ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Any = self.tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.rust_tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways SCREAMING_SNAKE_CASE_ : Dict = 'xa\u0303y' + ' ' + 'x\xe3y' SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of space type SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of line break type SCREAMING_SNAKE_CASE_ : Tuple = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) def snake_case ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` SCREAMING_SNAKE_CASE_ : Tuple = F'{text_of_1_token} {text_of_1_token}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(snake_case__ ) + 1, len(snake_case__ ) + 1 + len(snake_case__ )) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = F' {text}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(1, 1 + len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(snake_case__ ) + 1, 1 + len(snake_case__ ) + 1 + len(snake_case__ )) ,) def snake_case ( self ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(snake_case__ ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def snake_case ( self ): super().test_tokenization_python_rust_equals() def snake_case ( self ): # CLIP always lower cases letters pass
685
1
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : str = LxmertTokenizer __a : Optional[int] = LxmertTokenizerFast __a : str = True __a : Tuple = True def snake_case ( self ): super().setUp() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ '[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : int = 'UNwant\u00E9d,running' SCREAMING_SNAKE_CASE_ : int = 'unwanted, running' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE_ : int = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(snake_case__ ,['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,[7, 4, 5, 10, 8, 9] ) def snake_case ( self ): if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_ : Dict = self.get_tokenizer() SCREAMING_SNAKE_CASE_ : Optional[Any] = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ : Dict = 'I was born in 92000, and this is falsé.' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ) SCREAMING_SNAKE_CASE_ : int = rust_tokenizer.encode(snake_case__ ,add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ : str = tokenizer.encode(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ )
685
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCAmelCase ( ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) SCREAMING_SNAKE_CASE_ : int = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(lowerCamelCase_ ) DownloadCommand.register_subcommand(lowerCamelCase_ ) EnvironmentCommand.register_subcommand(lowerCamelCase_ ) RunCommand.register_subcommand(lowerCamelCase_ ) ServeCommand.register_subcommand(lowerCamelCase_ ) UserCommands.register_subcommand(lowerCamelCase_ ) AddNewModelCommand.register_subcommand(lowerCamelCase_ ) AddNewModelLikeCommand.register_subcommand(lowerCamelCase_ ) LfsCommands.register_subcommand(lowerCamelCase_ ) PTtoTFCommand.register_subcommand(lowerCamelCase_ ) # Let's go SCREAMING_SNAKE_CASE_ : Optional[int] = parser.parse_args() if not hasattr(lowerCamelCase_ , 'func' ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE_ : Optional[Any] = args.func(lowerCamelCase_ ) service.run() if __name__ == "__main__": main()
685
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging UpperCamelCase__ : int = logging.get_logger(__name__) UpperCamelCase__ : int = { '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''', } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : int = "bloom" __a : Any = ["past_key_values"] __a : int = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self ,snake_case__=250880 ,snake_case__=64 ,snake_case__=2 ,snake_case__=8 ,snake_case__=1E-5 ,snake_case__=0.02 ,snake_case__=True ,snake_case__=1 ,snake_case__=2 ,snake_case__=False ,snake_case__=0.0 ,snake_case__=0.0 ,snake_case__=1 ,snake_case__=False ,**snake_case__ ,): SCREAMING_SNAKE_CASE_ : List[str] = vocab_size # Backward compatibility with n_embed kwarg SCREAMING_SNAKE_CASE_ : Optional[int] = kwargs.pop('n_embed' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = hidden_size if n_embed is None else n_embed SCREAMING_SNAKE_CASE_ : Tuple = n_layer SCREAMING_SNAKE_CASE_ : Dict = n_head SCREAMING_SNAKE_CASE_ : Dict = layer_norm_epsilon SCREAMING_SNAKE_CASE_ : Tuple = initializer_range SCREAMING_SNAKE_CASE_ : List[str] = use_cache SCREAMING_SNAKE_CASE_ : Any = pretraining_tp SCREAMING_SNAKE_CASE_ : Optional[int] = apply_residual_connection_post_layernorm SCREAMING_SNAKE_CASE_ : Dict = hidden_dropout SCREAMING_SNAKE_CASE_ : Optional[int] = attention_dropout SCREAMING_SNAKE_CASE_ : List[str] = bos_token_id SCREAMING_SNAKE_CASE_ : Optional[int] = eos_token_id SCREAMING_SNAKE_CASE_ : Union[str, Any] = slow_but_exact super().__init__(bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) class lowerCAmelCase_ ( lowerCamelCase_ ): __a : str = version.parse("1.12" ) def __init__( self ,snake_case__ ,snake_case__ = "default" ,snake_case__ = None ,snake_case__ = False ,): super().__init__(snake_case__ ,task=snake_case__ ,patching_specs=snake_case__ ,use_past=snake_case__ ) if not getattr(self._config ,'pad_token_id' ,snake_case__ ): # TODO: how to do that better? SCREAMING_SNAKE_CASE_ : int = 0 @property def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(snake_case__ ,direction='inputs' ,inverted_values_shape=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = {0: 'batch', 1: 'past_sequence + sequence'} else: SCREAMING_SNAKE_CASE_ : List[Any] = {0: 'batch', 1: 'sequence'} return common_inputs @property def snake_case ( self ): return self._config.n_layer @property def snake_case ( self ): return self._config.n_head @property def snake_case ( self ): return 1E-3 def snake_case ( self ,snake_case__ ,snake_case__ = -1 ,snake_case__ = -1 ,snake_case__ = False ,snake_case__ = None ,): SCREAMING_SNAKE_CASE_ : List[str] = super(snake_case__ ,self ).generate_dummy_inputs( snake_case__ ,batch_size=snake_case__ ,seq_length=snake_case__ ,is_pair=snake_case__ ,framework=snake_case__ ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE_ : Optional[int] = OrderedDict({'input_ids': common_inputs['input_ids']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = common_inputs['input_ids'].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._config.hidden_size // self.num_attention_heads SCREAMING_SNAKE_CASE_ : Dict = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) SCREAMING_SNAKE_CASE_ : Tuple = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) SCREAMING_SNAKE_CASE_ : Optional[Any] = [ (torch.zeros(snake_case__ ), torch.zeros(snake_case__ )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE_ : Tuple = common_inputs['attention_mask'] if self.use_past: SCREAMING_SNAKE_CASE_ : str = ordered_inputs['attention_mask'].dtype SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat( [ordered_inputs['attention_mask'], torch.ones(snake_case__ ,snake_case__ ,dtype=snake_case__ )] ,dim=1 ) return ordered_inputs @property def snake_case ( self ): return 13
685
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging UpperCamelCase__ : Dict = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) UpperCamelCase__ : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'https://pypi.org/pypi/diffusers/json' SCREAMING_SNAKE_CASE_ : Optional[int] = json.loads(request.urlopen(lowerCamelCase_ ).read() )['releases'].keys() return sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : version.Version(lowerCamelCase_ ) ) def __UpperCAmelCase ( ) -> Tuple: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(lowerCamelCase_ ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : List[Any] = f.read() # Imports of the form `import .xxx` SCREAMING_SNAKE_CASE_ : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Unique-ify return list(set(lowerCamelCase_ ) ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : Union[str, Any] = [module_file] SCREAMING_SNAKE_CASE_ : Tuple = [] # Let's recurse through all relative imports while not no_change: SCREAMING_SNAKE_CASE_ : int = [] for f in files_to_check: new_imports.extend(get_relative_imports(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ).parent SCREAMING_SNAKE_CASE_ : int = [str(module_path / m ) for m in new_imports] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [f for f in new_import_files if f not in all_relative_imports] SCREAMING_SNAKE_CASE_ : Any = [F'{f}.py' for f in new_import_files] SCREAMING_SNAKE_CASE_ : Optional[int] = len(lowerCamelCase_ ) == 0 all_relative_imports.extend(lowerCamelCase_ ) return all_relative_imports def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() # Imports of the form `import xxx` SCREAMING_SNAKE_CASE_ : List[str] = re.findall('^\s*import\s+(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Only keep the top-level module SCREAMING_SNAKE_CASE_ : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(set(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = [] for imp in imports: try: importlib.import_module(lowerCamelCase_ ) except ImportError: missing_packages.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' F'{", ".join(lowerCamelCase_ )}. Run `pip install {" ".join(lowerCamelCase_ )}`' ) return get_relative_imports(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = module_path.replace(os.path.sep , '.' ) SCREAMING_SNAKE_CASE_ : Any = importlib.import_module(lowerCamelCase_ ) if class_name is None: return find_pipeline_class(lowerCamelCase_ ) return getattr(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" from ..pipelines import DiffusionPipeline SCREAMING_SNAKE_CASE_ : List[Any] = dict(inspect.getmembers(lowerCamelCase_ , inspect.isclass ) ) SCREAMING_SNAKE_CASE_ : List[str] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , lowerCamelCase_ ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:' F' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in' F' {loaded_module}.' ) SCREAMING_SNAKE_CASE_ : Any = cls return pipeline_class def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) if os.path.isfile(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = module_file_or_url SCREAMING_SNAKE_CASE_ : Dict = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: SCREAMING_SNAKE_CASE_ : List[str] = get_diffusers_versions() # cut ".dev0" SCREAMING_SNAKE_CASE_ : Dict = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: SCREAMING_SNAKE_CASE_ : List[Any] = latest_version if latest_version[1:] in available_versions else 'main' logger.info(F'Defaulting to latest_version: {revision}.' ) elif revision in available_versions: SCREAMING_SNAKE_CASE_ : int = F'v{revision}' elif revision == "main": SCREAMING_SNAKE_CASE_ : List[Any] = revision else: raise ValueError( F'`custom_revision`: {revision} does not exist. Please make sure to choose one of' F' {", ".join(available_versions + ["main"] )}.' ) # community pipeline on GitHub SCREAMING_SNAKE_CASE_ : Tuple = COMMUNITY_PIPELINES_URL.format(revision=lowerCamelCase_ , pipeline=lowerCamelCase_ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = cached_download( lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Tuple = 'git' SCREAMING_SNAKE_CASE_ : Dict = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise else: try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : List[str] = hf_hub_download( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise # Check we have all the requirements in our environment SCREAMING_SNAKE_CASE_ : Dict = check_imports(lowerCamelCase_ ) # Now we move the module inside our cached dynamic modules. SCREAMING_SNAKE_CASE_ : Union[str, Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = Path(lowerCamelCase_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(lowerCamelCase_ , submodule_path / module_file ) for module_needed in modules_needed: SCREAMING_SNAKE_CASE_ : Union[str, Any] = F'{module_needed}.py' shutil.copy(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = use_auth_token elif use_auth_token is True: SCREAMING_SNAKE_CASE_ : int = HfFolder.get_token() else: SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[Any] = model_info(lowerCamelCase_ , revision=lowerCamelCase_ , token=lowerCamelCase_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. SCREAMING_SNAKE_CASE_ : Any = submodule_path / commit_hash SCREAMING_SNAKE_CASE_ : List[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(lowerCamelCase_ ) if not (submodule_path / module_file).exists(): shutil.copy(lowerCamelCase_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( lowerCamelCase_ , F'{module_needed}.py' , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return os.path.join(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , **lowerCamelCase_ : Dict , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_cached_module_file( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return get_class_in_module(lowerCamelCase_ , final_module.replace('.py' , '' ) )
685
1
from manim import * class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = Rectangle(height=0.5 ,width=0.5 ) SCREAMING_SNAKE_CASE_ : List[str] = Rectangle(height=0.46 ,width=0.46 ).set_stroke(width=0 ) SCREAMING_SNAKE_CASE_ : List[str] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE_ : Optional[Any] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE_ : str = VGroup(*snake_case__ ).arrange(snake_case__ ,buff=0 ) SCREAMING_SNAKE_CASE_ : Any = VGroup(*snake_case__ ).arrange(snake_case__ ,buff=0 ) SCREAMING_SNAKE_CASE_ : Any = VGroup(snake_case__ ,snake_case__ ).arrange(snake_case__ ,buff=0 ) SCREAMING_SNAKE_CASE_ : int = Text('CPU' ,font_size=24 ) SCREAMING_SNAKE_CASE_ : Dict = Group(snake_case__ ,snake_case__ ).arrange(snake_case__ ,buff=0.5 ,aligned_edge=snake_case__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = [mem.copy() for i in range(1 )] SCREAMING_SNAKE_CASE_ : Tuple = VGroup(*snake_case__ ).arrange(snake_case__ ,buff=0 ) SCREAMING_SNAKE_CASE_ : Tuple = Text('GPU' ,font_size=24 ) SCREAMING_SNAKE_CASE_ : List[Any] = Group(snake_case__ ,snake_case__ ).arrange(snake_case__ ,buff=0.5 ,aligned_edge=snake_case__ ) gpu.align_to(snake_case__ ,snake_case__ ) gpu.set_x(gpu.get_x() - 1 ) self.add(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = [mem.copy() for i in range(6 )] SCREAMING_SNAKE_CASE_ : Dict = VGroup(*snake_case__ ).arrange(snake_case__ ,buff=0 ) SCREAMING_SNAKE_CASE_ : List[str] = Text('Model' ,font_size=24 ) SCREAMING_SNAKE_CASE_ : Any = Group(snake_case__ ,snake_case__ ).arrange(snake_case__ ,buff=0.5 ,aligned_edge=snake_case__ ) model.move_to([3, -1.0, 0] ) self.play( Create(snake_case__ ,run_time=1 ) ,Create(snake_case__ ,run_time=1 ) ,Create(snake_case__ ,run_time=1 ) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = MarkupText( F'First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.' ,font_size=24 ,) SCREAMING_SNAKE_CASE_ : str = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) SCREAMING_SNAKE_CASE_ : int = MarkupText( F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model' ,font_size=18 ,) key_text.move_to([-5, 2.4, 0] ) step_a.move_to([2, 2, 0] ) self.play(Write(snake_case__ ,run_time=2.5 ) ,Write(snake_case__ ) ,Write(snake_case__ ) ) self.add(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : Dict = [] SCREAMING_SNAKE_CASE_ : Tuple = [] for i, rect in enumerate(snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = Rectangle(height=0.46 ,width=0.46 ).set_stroke(width=0.0 ).set_fill(snake_case__ ,opacity=0.7 ) cpu_target.move_to(snake_case__ ) cpu_target.generate_target() SCREAMING_SNAKE_CASE_ : int = 0.46 / 4 SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0.46 / 3 if i == 0: cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) ,buff=0.02 ,direction=snake_case__ ) cpu_target.target.set_x(cpu_target.target.get_x() + 0.1 ) elif i == 3: cpu_target.target.next_to(cpu_targs[0].target ,direction=snake_case__ ,buff=0.0 ) else: cpu_target.target.next_to(cpu_targs[i - 1].target ,direction=snake_case__ ,buff=0.0 ) cpu_targs.append(snake_case__ ) first_animations.append(rect.animate(run_time=0.5 ).set_stroke(snake_case__ ) ) second_animations.append(MoveToTarget(snake_case__ ,run_time=1.5 ) ) self.play(*snake_case__ ) self.play(*snake_case__ ) self.wait()
685
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "visual_bert" def __init__( self ,snake_case__=30522 ,snake_case__=768 ,snake_case__=512 ,snake_case__=12 ,snake_case__=12 ,snake_case__=3072 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=1E-12 ,snake_case__=False ,snake_case__=True ,snake_case__=1 ,snake_case__=0 ,snake_case__=2 ,**snake_case__ ,): super().__init__(pad_token_id=snake_case__ ,bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = vocab_size SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = visual_embedding_dim SCREAMING_SNAKE_CASE_ : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE_ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = initializer_range SCREAMING_SNAKE_CASE_ : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : Optional[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_ : int = bypass_transformer SCREAMING_SNAKE_CASE_ : Optional[Any] = special_visual_initialize
685
1
import string from math import logaa def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = document.translate( str.maketrans('' , '' , string.punctuation ) ).replace('\n' , '' ) SCREAMING_SNAKE_CASE_ : str = document_without_punctuation.split(' ' ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> tuple[int, int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = corpus.lower().translate( str.maketrans('' , '' , string.punctuation ) ) # strip all punctuation and replace it with '' SCREAMING_SNAKE_CASE_ : Any = corpus_without_punctuation.split('\n' ) SCREAMING_SNAKE_CASE_ : List[Any] = term.lower() return (len([doc for doc in docs if term in doc] ), len(lowerCamelCase_ )) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : Union[str, Any]=False ) -> float: """simple docstring""" if smoothing: if n == 0: raise ValueError('log10(0) is undefined.' ) return round(1 + logaa(n / (1 + df) ) , 3 ) if df == 0: raise ZeroDivisionError('df must be > 0' ) elif n == 0: raise ValueError('log10(0) is undefined.' ) return round(logaa(n / df ) , 3 ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> float: """simple docstring""" return round(tf * idf , 3 )
685
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Union[str, Any]: """simple docstring""" def is_in_circle(lowerCamelCase_ : float , lowerCamelCase_ : float ) -> bool: SCREAMING_SNAKE_CASE_ : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE_ : Optional[int] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(lowerCamelCase_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE_ : Tuple = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Callable[[float], float] , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(lowerCamelCase_ , lowerCamelCase_ ) ) for _ in range(lowerCamelCase_ ) ) * (max_value - min_value) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 ) -> None: """simple docstring""" def identity_function(lowerCamelCase_ : float ) -> float: return x SCREAMING_SNAKE_CASE_ : str = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> None: """simple docstring""" def function_to_integrate(lowerCamelCase_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE_ : Dict = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "Wav2Vec2FeatureExtractor" __a : List[str] = "AutoTokenizer" def __init__( self ,snake_case__ ,snake_case__ ): super().__init__(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = self.feature_extractor SCREAMING_SNAKE_CASE_ : Any = False @classmethod def snake_case ( cls ,snake_case__ ,**snake_case__ ): try: return super().from_pretrained(snake_case__ ,**snake_case__ ) except OSError: warnings.warn( F'Loading a tokenizer inside {cls.__name__} from a config that does not' ' include a `tokenizer_class` attribute is deprecated and will be ' 'removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`' ' attribute to either your `config.json` or `tokenizer_config.json` ' 'file to suppress this warning: ' ,snake_case__ ,) SCREAMING_SNAKE_CASE_ : List[str] = WavaVecaFeatureExtractor.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = WavaVecaCTCTokenizer.from_pretrained(snake_case__ ,**snake_case__ ) return cls(feature_extractor=snake_case__ ,tokenizer=snake_case__ ) def __call__( self ,*snake_case__ ,**snake_case__ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*snake_case__ ,**snake_case__ ) if "raw_speech" in kwargs: warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.' ) SCREAMING_SNAKE_CASE_ : Tuple = kwargs.pop('raw_speech' ) else: SCREAMING_SNAKE_CASE_ : Any = kwargs.pop('audio' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = kwargs.pop('sampling_rate' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = kwargs.pop('text' ,snake_case__ ) if len(snake_case__ ) > 0: SCREAMING_SNAKE_CASE_ : str = args[0] SCREAMING_SNAKE_CASE_ : Optional[int] = args[1:] if audio is None and text is None: raise ValueError('You need to specify either an `audio` or `text` input to process.' ) if audio is not None: SCREAMING_SNAKE_CASE_ : int = self.feature_extractor(snake_case__ ,*snake_case__ ,sampling_rate=snake_case__ ,**snake_case__ ) if text is not None: SCREAMING_SNAKE_CASE_ : int = self.tokenizer(snake_case__ ,**snake_case__ ) if text is None: return inputs elif audio is None: return encodings else: SCREAMING_SNAKE_CASE_ : Optional[int] = encodings['input_ids'] return inputs def snake_case ( self ,*snake_case__ ,**snake_case__ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : int = kwargs.pop('input_features' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = kwargs.pop('labels' ,snake_case__ ) if len(snake_case__ ) > 0: SCREAMING_SNAKE_CASE_ : Union[str, Any] = args[0] SCREAMING_SNAKE_CASE_ : str = args[1:] if input_features is not None: SCREAMING_SNAKE_CASE_ : Any = self.feature_extractor.pad(snake_case__ ,*snake_case__ ,**snake_case__ ) if labels is not None: SCREAMING_SNAKE_CASE_ : Dict = self.tokenizer.pad(snake_case__ ,**snake_case__ ) if labels is None: return input_features elif input_features is None: return labels else: SCREAMING_SNAKE_CASE_ : str = labels['input_ids'] return input_features def snake_case ( self ,*snake_case__ ,**snake_case__ ): return self.tokenizer.batch_decode(*snake_case__ ,**snake_case__ ) def snake_case ( self ,*snake_case__ ,**snake_case__ ): return self.tokenizer.decode(*snake_case__ ,**snake_case__ ) @contextmanager def snake_case ( self ): warnings.warn( '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ' 'labels by using the argument `text` of the regular `__call__` method (either in the same call as ' 'your audio inputs, or in a separate call.' ) SCREAMING_SNAKE_CASE_ : Optional[int] = True SCREAMING_SNAKE_CASE_ : Optional[Any] = self.tokenizer yield SCREAMING_SNAKE_CASE_ : Any = self.feature_extractor SCREAMING_SNAKE_CASE_ : int = False
685
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
1
def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : str ) -> list: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] for i in range(len(lowerCamelCase_ ) - pat_len + 1 ): SCREAMING_SNAKE_CASE_ : Dict = True for j in range(lowerCamelCase_ ): if s[i + j] != pattern[j]: SCREAMING_SNAKE_CASE_ : str = False break if match_found: position.append(lowerCamelCase_ ) return position if __name__ == "__main__": assert naive_pattern_search('''ABCDEFG''', '''DE''') == [3] print(naive_pattern_search('''ABAAABCDBBABCDDEBCABC''', '''ABC'''))
685
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
1
import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class lowerCAmelCase_ : @staticmethod def snake_case ( *snake_case__ ,**snake_case__ ): pass @is_pipeline_test @require_vision class lowerCAmelCase_ ( unittest.TestCase ): @require_torch def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = pipeline( model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' ,) SCREAMING_SNAKE_CASE_ : Tuple = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) SCREAMING_SNAKE_CASE_ : str = image_classifier(snake_case__ ,candidate_labels=['a', 'b', 'c'] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(snake_case__ ) ,[ [{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'b'}, {'score': 0.333, 'label': 'c'}], [{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'c'}, {'score': 0.333, 'label': 'b'}], ] ,) SCREAMING_SNAKE_CASE_ : str = image_classifier([image] * 5 ,candidate_labels=['A', 'B', 'C'] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case__ ) ,[ [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], ] ,) @require_tf def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = pipeline( model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' ,framework='tf' ) SCREAMING_SNAKE_CASE_ : str = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) SCREAMING_SNAKE_CASE_ : int = image_classifier(snake_case__ ,candidate_labels=['a', 'b', 'c'] ) self.assertEqual( nested_simplify(snake_case__ ) ,[{'score': 0.333, 'label': 'a'}, {'score': 0.333, 'label': 'b'}, {'score': 0.333, 'label': 'c'}] ,) SCREAMING_SNAKE_CASE_ : List[str] = image_classifier([image] * 5 ,candidate_labels=['A', 'B', 'C'] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case__ ) ,[ [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], [ {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, {'score': 0.333, 'label': ANY(snake_case__ )}, ], ] ,) @slow @require_torch def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = pipeline( task='zero-shot-image-classification' ,model='openai/clip-vit-base-patch32' ,) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE_ : str = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) SCREAMING_SNAKE_CASE_ : str = image_classifier(snake_case__ ,candidate_labels=['cat', 'plane', 'remote'] ) self.assertEqual( nested_simplify(snake_case__ ) ,[ {'score': 0.511, 'label': 'remote'}, {'score': 0.485, 'label': 'cat'}, {'score': 0.004, 'label': 'plane'}, ] ,) SCREAMING_SNAKE_CASE_ : List[Any] = image_classifier([image] * 5 ,candidate_labels=['cat', 'plane', 'remote'] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case__ ) ,[ [ {'score': 0.511, 'label': 'remote'}, {'score': 0.485, 'label': 'cat'}, {'score': 0.004, 'label': 'plane'}, ], ] * 5 ,) @slow @require_tf def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = pipeline( task='zero-shot-image-classification' ,model='openai/clip-vit-base-patch32' ,framework='tf' ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE_ : Optional[int] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) SCREAMING_SNAKE_CASE_ : Tuple = image_classifier(snake_case__ ,candidate_labels=['cat', 'plane', 'remote'] ) self.assertEqual( nested_simplify(snake_case__ ) ,[ {'score': 0.511, 'label': 'remote'}, {'score': 0.485, 'label': 'cat'}, {'score': 0.004, 'label': 'plane'}, ] ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = image_classifier([image] * 5 ,candidate_labels=['cat', 'plane', 'remote'] ,batch_size=2 ) self.assertEqual( nested_simplify(snake_case__ ) ,[ [ {'score': 0.511, 'label': 'remote'}, {'score': 0.485, 'label': 'cat'}, {'score': 0.004, 'label': 'plane'}, ], ] * 5 ,)
685
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
1
import json import os import tempfile from unittest.mock import patch import torch from torch.utils.data import DataLoader, TensorDataset from accelerate import DistributedType, infer_auto_device_map, init_empty_weights from accelerate.accelerator import Accelerator from accelerate.state import GradientState, PartialState from accelerate.test_utils import require_bnb, require_multi_gpu, slow from accelerate.test_utils.testing import AccelerateTestCase, require_cuda from accelerate.utils import patch_environment def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.nn.Linear(2 , 4 ) SCREAMING_SNAKE_CASE_ : Tuple = torch.optim.AdamW(model.parameters() , lr=1.0 ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.optim.lr_scheduler.OneCycleLR(lowerCamelCase_ , max_lr=0.0_1 , steps_per_epoch=2 , epochs=1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = DataLoader(TensorDataset(torch.tensor([1, 2, 3] ) ) ) SCREAMING_SNAKE_CASE_ : Dict = DataLoader(TensorDataset(torch.tensor([4, 5, 6] ) ) ) return model, optimizer, scheduler, train_dl, valid_dl def __UpperCAmelCase ( lowerCamelCase_ : Dict ) -> str: """simple docstring""" return (model.weight.abs().sum() + model.bias.abs().sum()).item() def __UpperCAmelCase ( lowerCamelCase_ : List[str] ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.nn.Linear(*tuple(model.weight.T.shape ) ).state_dict() model.load_state_dict(lowerCamelCase_ ) class lowerCAmelCase_ ( lowerCamelCase_ ): @require_cuda def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = Accelerator() assert PartialState._shared_state["_cpu"] is False assert PartialState._shared_state["device"].type == "cuda" with self.assertRaises(snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = Accelerator(cpu=snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = Accelerator() SCREAMING_SNAKE_CASE_ : Optional[int] = GradientState() assert state.num_steps == 1 SCREAMING_SNAKE_CASE_ : Optional[int] = 4 assert state.num_steps == 4 assert state.sync_gradients is True SCREAMING_SNAKE_CASE_ : Dict = False assert state.sync_gradients is False GradientState._reset_state() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = create_components() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Any = accelerator.prepare(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) self.assertTrue(prepared_model in accelerator._models ) self.assertTrue(prepared_optimizer in accelerator._optimizers ) self.assertTrue(prepared_scheduler in accelerator._schedulers ) self.assertTrue(prepared_train_dl in accelerator._dataloaders ) self.assertTrue(prepared_valid_dl in accelerator._dataloaders ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = create_components() accelerator.prepare(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) accelerator.free_memory() self.assertTrue(len(accelerator._models ) == 0 ) self.assertTrue(len(accelerator._optimizers ) == 0 ) self.assertTrue(len(accelerator._schedulers ) == 0 ) self.assertTrue(len(accelerator._dataloaders ) == 0 ) def snake_case ( self ): PartialState._reset_state() # Mock torch.cuda.set_device to avoid an exception as the device doesn't exist def noop(*snake_case__ ,**snake_case__ ): pass with patch('torch.cuda.set_device' ,snake_case__ ), patch_environment(ACCELERATE_TORCH_DEVICE='cuda:64' ): SCREAMING_SNAKE_CASE_ : Tuple = Accelerator() self.assertEqual(str(accelerator.state.device ) ,'cuda:64' ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = create_components() accelerator.prepare(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = get_signature(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(snake_case__ ) # make sure random weights don't match load_random_weights(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) > 1E-3 ) # make sure loaded weights match accelerator.load_state(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) < 1E-3 ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = create_components() accelerator.prepare(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = get_signature(snake_case__ ) # saving hook def save_config(snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = {'class_name': models[0].__class__.__name__} with open(os.path.join(snake_case__ ,'data.json' ) ,'w' ) as f: json.dump(snake_case__ ,snake_case__ ) # loading hook def load_config(snake_case__ ,snake_case__ ): with open(os.path.join(snake_case__ ,'data.json' ) ,'r' ) as f: SCREAMING_SNAKE_CASE_ : Tuple = json.load(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = config['class_name'] SCREAMING_SNAKE_CASE_ : str = accelerator.register_save_state_pre_hook(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = accelerator.register_load_state_pre_hook(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(snake_case__ ) # make sure random weights don't match with hooks load_random_weights(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) > 1E-3 ) # random class name to verify correct one is loaded SCREAMING_SNAKE_CASE_ : List[str] = 'random' # make sure loaded weights match with hooks accelerator.load_state(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) < 1E-3 ) # mode.class_name is loaded from config self.assertTrue(model.class_name == model.__class__.__name__ ) # remove hooks save_hook.remove() load_hook.remove() with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(snake_case__ ) # make sure random weights don't match with hooks removed load_random_weights(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) > 1E-3 ) # random class name to verify correct one is loaded SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'random' # make sure loaded weights match with hooks removed accelerator.load_state(snake_case__ ) self.assertTrue(abs(model_signature - get_signature(snake_case__ ) ) < 1E-3 ) # mode.class_name is NOT loaded from config self.assertTrue(model.class_name != model.__class__.__name__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = create_components() SCREAMING_SNAKE_CASE_ : List[Any] = None # This should work SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = accelerator.prepare( snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) self.assertTrue(dummy_obj is None ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = Accelerator() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = create_components() SCREAMING_SNAKE_CASE_ : Any = [1, 2, 3] # This should work SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = accelerator.prepare( snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Dummy object should have `_is_accelerate_prepared` set to `True`' ,) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Model is missing `_is_accelerator_prepared` or is set to `False`' ,) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Optimizer is missing `_is_accelerator_prepared` or is set to `False`' ,) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Scheduler is missing `_is_accelerator_prepared` or is set to `False`' ,) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Train Dataloader is missing `_is_accelerator_prepared` or is set to `False`' ,) self.assertEqual( getattr(snake_case__ ,'_is_accelerate_prepared' ,snake_case__ ) ,snake_case__ ,'Valid Dataloader is missing `_is_accelerator_prepared` or is set to `False`' ,) @slow @require_bnb def snake_case ( self ): from transformers import AutoModelForCausalLM SCREAMING_SNAKE_CASE_ : Dict = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,load_in_abit=snake_case__ ,device_map={'': 0} ,) SCREAMING_SNAKE_CASE_ : List[str] = Accelerator() # This should work SCREAMING_SNAKE_CASE_ : List[str] = accelerator.prepare(snake_case__ ) @slow @require_bnb def snake_case ( self ): from transformers import AutoModelForCausalLM SCREAMING_SNAKE_CASE_ : Any = Accelerator() with init_empty_weights(): SCREAMING_SNAKE_CASE_ : Any = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,) model.tie_weights() SCREAMING_SNAKE_CASE_ : List[Any] = infer_auto_device_map(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 'cpu' SCREAMING_SNAKE_CASE_ : Union[str, Any] = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,device_map=snake_case__ ,load_in_abit=snake_case__ ,llm_inta_enable_fpaa_cpu_offload=snake_case__ ) # This should not work and get value error with self.assertRaises(snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = accelerator.prepare(snake_case__ ) @slow @require_bnb @require_multi_gpu def snake_case ( self ): from transformers import AutoModelForCausalLM SCREAMING_SNAKE_CASE_ : Optional[int] = {'distributed_type': DistributedType.MULTI_GPU} with init_empty_weights(): SCREAMING_SNAKE_CASE_ : Union[str, Any] = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,) model.tie_weights() SCREAMING_SNAKE_CASE_ : List[str] = infer_auto_device_map(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = 1 SCREAMING_SNAKE_CASE_ : int = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,load_in_abit=snake_case__ ,device_map=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Dict = Accelerator() # This should not work and get value error with self.assertRaises(snake_case__ ): SCREAMING_SNAKE_CASE_ : str = accelerator.prepare(snake_case__ ) PartialState._reset_state() @slow @require_bnb @require_multi_gpu def snake_case ( self ): from transformers import AutoModelForCausalLM with init_empty_weights(): SCREAMING_SNAKE_CASE_ : Tuple = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,) SCREAMING_SNAKE_CASE_ : Any = infer_auto_device_map(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = 1 SCREAMING_SNAKE_CASE_ : Optional[int] = AutoModelForCausalLM.from_pretrained( 'EleutherAI/gpt-neo-125m' ,load_in_abit=snake_case__ ,device_map=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Dict = Accelerator() # This should work SCREAMING_SNAKE_CASE_ : List[str] = accelerator.prepare(snake_case__ ) @require_cuda def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.nn.Linear(10 ,10 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.optim.SGD(model.parameters() ,lr=0.01 ) SCREAMING_SNAKE_CASE_ : Dict = Accelerator(cpu=snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = accelerator.prepare(snake_case__ )
685
from math import log from scipy.constants import Boltzmann, physical_constants UpperCamelCase__ : Any = 3_00 # TEMPERATURE (unit = K) def __UpperCAmelCase ( lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('Donor concentration should be positive' ) elif acceptor_conc <= 0: raise ValueError('Acceptor concentration should be positive' ) elif intrinsic_conc <= 0: raise ValueError('Intrinsic concentration should be positive' ) elif donor_conc <= intrinsic_conc: raise ValueError( 'Donor concentration should be greater than intrinsic concentration' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( 'Acceptor concentration should be greater than intrinsic concentration' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = int(number**0.5 ) return number == sq * sq def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : int ) -> tuple[int, int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den SCREAMING_SNAKE_CASE_ : int = x_den * y_den * z_den SCREAMING_SNAKE_CASE_ : int = gcd(lowerCamelCase_ , lowerCamelCase_ ) top //= hcf bottom //= hcf return top, bottom def __UpperCAmelCase ( lowerCamelCase_ : int = 35 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : set = set() SCREAMING_SNAKE_CASE_ : int SCREAMING_SNAKE_CASE_ : Fraction = Fraction(0 ) SCREAMING_SNAKE_CASE_ : tuple[int, int] for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 SCREAMING_SNAKE_CASE_ : int = x_num * y_den + x_den * y_num SCREAMING_SNAKE_CASE_ : Optional[int] = x_den * y_den SCREAMING_SNAKE_CASE_ : List[str] = gcd(lowerCamelCase_ , lowerCamelCase_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: SCREAMING_SNAKE_CASE_ : Union[str, Any] = add_three( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) unique_s.add(lowerCamelCase_ ) # n=2 SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) SCREAMING_SNAKE_CASE_ : Tuple = x_den * x_den * y_den * y_den if is_sq(lowerCamelCase_ ) and is_sq(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Any = int(sqrt(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = int(sqrt(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : Tuple = gcd(lowerCamelCase_ , lowerCamelCase_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: SCREAMING_SNAKE_CASE_ : Union[str, Any] = add_three( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) unique_s.add(lowerCamelCase_ ) # n=-1 SCREAMING_SNAKE_CASE_ : int = x_num * y_num SCREAMING_SNAKE_CASE_ : Optional[int] = x_den * y_num + x_num * y_den SCREAMING_SNAKE_CASE_ : Optional[int] = gcd(lowerCamelCase_ , lowerCamelCase_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: SCREAMING_SNAKE_CASE_ : Union[str, Any] = add_three( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) unique_s.add(lowerCamelCase_ ) # n=2 SCREAMING_SNAKE_CASE_ : Tuple = x_num * x_num * y_num * y_num SCREAMING_SNAKE_CASE_ : Any = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(lowerCamelCase_ ) and is_sq(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Dict = int(sqrt(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = int(sqrt(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = gcd(lowerCamelCase_ , lowerCamelCase_ ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: SCREAMING_SNAKE_CASE_ : Tuple = add_three( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) unique_s.add(lowerCamelCase_ ) for num, den in unique_s: total += Fraction(lowerCamelCase_ , lowerCamelCase_ ) return total.denominator + total.numerator if __name__ == "__main__": print(F"""{solution() = }""")
685
class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [], [], [], ] def snake_case ( self ,snake_case__ ,snake_case__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError('Maximum queue size is 100' ) self.queues[priority].append(snake_case__ ) except IndexError: raise ValueError('Valid priorities are 0, 1, and 2' ) def snake_case ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError('All queues are empty' ) def __str__( self ): return "\n".join(F'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : List[str] = [] def snake_case ( self ,snake_case__ ): if len(self.queue ) == 100: raise OverFlowError('Maximum queue size is 100' ) self.queue.append(snake_case__ ) def snake_case ( self ): if not self.queue: raise UnderFlowError('The queue is empty' ) else: SCREAMING_SNAKE_CASE_ : List[Any] = min(self.queue ) self.queue.remove(snake_case__ ) return data def __str__( self ): return str(self.queue ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 1_00 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 1_28 ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(1_00 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(1_28 ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
685
1
import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor UpperCamelCase__ : str = logging.get_logger(__name__) class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,*snake_case__ ,**snake_case__ ): warnings.warn( 'The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use OwlViTImageProcessor instead.' ,snake_case__ ,) super().__init__(*snake_case__ ,**snake_case__ )
685
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
1
import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : str , lowerCamelCase_ : Tuple , lowerCamelCase_ : Optional[int]=None , lowerCamelCase_ : Any=None , lowerCamelCase_ : List[str]=None , lowerCamelCase_ : Optional[int]=None , lowerCamelCase_ : List[str]=None , ) -> Optional[int]: """simple docstring""" if attention_mask is None: SCREAMING_SNAKE_CASE_ : Optional[int] = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ : Tuple = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=lowerCamelCase_ ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase_ ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ : List[Any] = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=lowerCamelCase_ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=False ,snake_case__=99 ,snake_case__=16 ,snake_case__=2 ,snake_case__=4 ,snake_case__=4 ,snake_case__="relu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=0.0 ,snake_case__=0.0 ,snake_case__=20 ,snake_case__=2 ,snake_case__=1 ,snake_case__=0 ,): SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : Dict = batch_size SCREAMING_SNAKE_CASE_ : List[str] = seq_length SCREAMING_SNAKE_CASE_ : Any = is_training SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : List[Any] = vocab_size SCREAMING_SNAKE_CASE_ : int = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : List[str] = intermediate_size SCREAMING_SNAKE_CASE_ : List[Any] = hidden_act SCREAMING_SNAKE_CASE_ : List[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Tuple = encoder_layerdrop SCREAMING_SNAKE_CASE_ : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE_ : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE_ : Optional[int] = eos_token_id SCREAMING_SNAKE_CASE_ : Dict = pad_token_id SCREAMING_SNAKE_CASE_ : List[Any] = bos_token_id def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = self.eos_token_id # Eos Token SCREAMING_SNAKE_CASE_ : str = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_ids.clamp(self.pad_token_id + 1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = decoder_input_ids.clamp(self.pad_token_id + 1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_config() SCREAMING_SNAKE_CASE_ : Optional[int] = prepare_mam_aaa_inputs_dict(snake_case__ ,snake_case__ ,snake_case__ ) return config, inputs_dict def snake_case ( self ): return MaMaaaConfig( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,encoder_layerdrop=self.encoder_layerdrop ,decoder_layerdrop=self.decoder_layerdrop ,max_position_embeddings=self.max_position_embeddings ,eos_token_id=self.eos_token_id ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = self.prepare_config_and_inputs() return config, inputs_dict def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = MaMaaaModel(config=snake_case__ ).get_decoder().to(snake_case__ ).eval() SCREAMING_SNAKE_CASE_ : Dict = inputs_dict['input_ids'] SCREAMING_SNAKE_CASE_ : int = inputs_dict['attention_mask'] SCREAMING_SNAKE_CASE_ : Tuple = inputs_dict['head_mask'] # first forward pass SCREAMING_SNAKE_CASE_ : Any = model(snake_case__ ,attention_mask=snake_case__ ,head_mask=snake_case__ ,use_cache=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor((self.batch_size, 3) ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor((self.batch_size, 3) ,2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ : Optional[int] = torch.cat([input_ids, next_tokens] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Any = torch.cat([attention_mask, next_attn_mask] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model(snake_case__ ,attention_mask=snake_case__ )['last_hidden_state'] SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,attention_mask=snake_case__ ,past_key_values=snake_case__ )[ 'last_hidden_state' ] # select random slice SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor((1,) ,output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE_ : List[Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-2 ) ) def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = MaMaaaModel(config=snake_case__ ).to(snake_case__ ).eval() SCREAMING_SNAKE_CASE_ : int = model(**snake_case__ ) SCREAMING_SNAKE_CASE_ : str = outputs.encoder_last_hidden_state SCREAMING_SNAKE_CASE_ : List[Any] = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ : Dict = model.get_encoder() encoder.save_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = MaMaaaEncoder.from_pretrained(snake_case__ ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = encoder(inputs_dict['input_ids'] ,attention_mask=inputs_dict['attention_mask'] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ : Dict = model.get_decoder() decoder.save_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = MaMaaaDecoder.from_pretrained(snake_case__ ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = decoder( input_ids=inputs_dict['decoder_input_ids'] ,attention_mask=inputs_dict['decoder_attention_mask'] ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=inputs_dict['attention_mask'] ,)[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) __a : Dict = (MaMaaaForConditionalGeneration,) if is_torch_available() else () __a : Optional[int] = ( { "conversational": MaMaaaForConditionalGeneration, "feature-extraction": MaMaaaModel, "summarization": MaMaaaForConditionalGeneration, "text2text-generation": MaMaaaForConditionalGeneration, "translation": MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) __a : int = True __a : str = True __a : Optional[Any] = False __a : str = False def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = MaMaaaModelTester(self ) SCREAMING_SNAKE_CASE_ : Any = ConfigTester(self ,config_class=snake_case__ ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : str = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ : Optional[int] = model_class(snake_case__ ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = model_class.from_pretrained(snake_case__ ,output_loading_info=snake_case__ ) self.assertEqual(info['missing_keys'] ,[] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): SCREAMING_SNAKE_CASE_ : Optional[Any] = model_class(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = copy.deepcopy(self._prepare_for_class(snake_case__ ,snake_case__ ) ) if not self.is_encoder_decoder: SCREAMING_SNAKE_CASE_ : Optional[Any] = inputs['input_ids'] del inputs["input_ids"] else: SCREAMING_SNAKE_CASE_ : List[str] = inputs['input_ids'] SCREAMING_SNAKE_CASE_ : List[str] = inputs.get('decoder_input_ids' ,snake_case__ ) del inputs["input_ids"] inputs.pop('decoder_input_ids' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model.get_input_embeddings() if not self.is_encoder_decoder: SCREAMING_SNAKE_CASE_ : Union[str, Any] = wte(snake_case__ ) else: SCREAMING_SNAKE_CASE_ : List[str] = wte(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = wte(snake_case__ ) with torch.no_grad(): model(**snake_case__ )[0] def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Any = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = MaMaaaForConditionalGeneration(snake_case__ ).eval().to(snake_case__ ) if torch_device == "cuda": model.half() model.generate(snake_case__ ,attention_mask=snake_case__ ) model.generate(num_beams=4 ,do_sample=snake_case__ ,early_stopping=snake_case__ ,num_return_sequences=3 ) def __UpperCAmelCase ( lowerCamelCase_ : Optional[Any] ) -> Optional[Any]: """simple docstring""" return torch.tensor(lowerCamelCase_ , dtype=torch.long , device=lowerCamelCase_ ) UpperCamelCase__ : List[str] = 1e-4 @require_torch @require_sentencepiece @require_tokenizers @slow class lowerCAmelCase_ ( unittest.TestCase ): @cached_property def snake_case ( self ): return MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = MaMaaaModel.from_pretrained('facebook/m2m100_418M' ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = _long_tensor([[128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38, 2]] ) SCREAMING_SNAKE_CASE_ : Any = _long_tensor([[2, 128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38]] ) SCREAMING_SNAKE_CASE_ : Optional[int] = prepare_mam_aaa_inputs_dict(model.config ,snake_case__ ,snake_case__ ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ : int = model(**snake_case__ )[0] SCREAMING_SNAKE_CASE_ : str = torch.Size((1, 11, 1024) ) self.assertEqual(output.shape ,snake_case__ ) # change to expected output here SCREAMING_SNAKE_CASE_ : Tuple = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] ,device=snake_case__ ) self.assertTrue(torch.allclose(output[:, :3, :3] ,snake_case__ ,atol=snake_case__ ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(snake_case__ ) # change to intended input SCREAMING_SNAKE_CASE_ : Any = _long_tensor([[128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38, 2]] ) SCREAMING_SNAKE_CASE_ : str = _long_tensor([[2, 128028, 98, 12, 30527, 2732, 159, 7755, 61904, 39144, 38]] ) SCREAMING_SNAKE_CASE_ : List[Any] = prepare_mam_aaa_inputs_dict(model.config ,snake_case__ ,snake_case__ ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ : List[str] = model(**snake_case__ )[0] SCREAMING_SNAKE_CASE_ : List[str] = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape ,snake_case__ ) # change to expected output here SCREAMING_SNAKE_CASE_ : str = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] ,device=snake_case__ ) self.assertTrue(torch.allclose(output[:, :3, :3] ,snake_case__ ,atol=snake_case__ ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = MaMaaaForConditionalGeneration.from_pretrained('facebook/m2m100_418M' ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = MaMaaaTokenizer.from_pretrained('facebook/m2m100_418M' ,src_lang='fr' ,tgt_lang='en' ) SCREAMING_SNAKE_CASE_ : List[str] = [ 'L\'affaire NSA souligne l\'absence totale de débat sur le renseignement', 'Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.', 'Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent' ' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de' ' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.', ] # The below article tests that we don't add any hypotheses outside of the top n_beams SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer(snake_case__ ,padding=snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : str = model.generate( input_ids=dct['input_ids'].to(snake_case__ ) ,attention_mask=dct['attention_mask'].to(snake_case__ ) ,num_beams=5 ,forced_bos_token_id=tokenizer.get_lang_id('en' ) ,) SCREAMING_SNAKE_CASE_ : List[str] = [ 'The NSA case highlights the total absence of intelligence debate', 'I think there are two levels of response from the French government.', 'When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.' ' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all' ' communications in France.', ] SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.batch_decode( hypotheses_batch.tolist() ,clean_up_tokenization_spaces=snake_case__ ,skip_special_tokens=snake_case__ ) assert generated == expected_en
685
from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Tuple = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Any = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Dict = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] )
685
1
from __future__ import annotations def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> list[str]: """simple docstring""" if partitions <= 0: raise ValueError('partitions must be a positive number!' ) if partitions > number_of_bytes: raise ValueError('partitions can not > number_of_bytes!' ) SCREAMING_SNAKE_CASE_ : Optional[int] = number_of_bytes // partitions SCREAMING_SNAKE_CASE_ : Dict = [] for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : str = i * bytes_per_partition + 1 SCREAMING_SNAKE_CASE_ : List[str] = ( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(F'{start_bytes}-{end_bytes}' ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
685
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ ): __a : List[str] = "pixel_values" __a : Tuple = False __a : Tuple = TimmBackboneConfig def __init__( self ,snake_case__ ,**snake_case__ ): requires_backends(self ,'timm' ) super().__init__(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = config if config.backbone is None: raise ValueError('backbone is not set in the config. Please set it to a timm model name.' ) if config.backbone not in timm.list_models(): raise ValueError(F'backbone {config.backbone} is not supported by timm.' ) if hasattr(snake_case__ ,'out_features' ) and config.out_features is not None: raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.' ) SCREAMING_SNAKE_CASE_ : int = getattr(snake_case__ ,'use_pretrained_backbone' ,snake_case__ ) if pretrained is None: raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.' ) # We just take the final layer by default. This matches the default for the transformers models. SCREAMING_SNAKE_CASE_ : Optional[int] = config.out_indices if getattr(snake_case__ ,'out_indices' ,snake_case__ ) is not None else (-1,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = timm.create_model( config.backbone ,pretrained=snake_case__ ,features_only=config.features_only ,in_chans=config.num_channels ,out_indices=snake_case__ ,**snake_case__ ,) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. SCREAMING_SNAKE_CASE_ : str = self._backbone.return_layers SCREAMING_SNAKE_CASE_ : Union[str, Any] = {layer['module']: str(snake_case__ ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(snake_case__ ) @classmethod def snake_case ( cls ,snake_case__ ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['vision', 'timm'] ) from ...models.timm_backbone import TimmBackboneConfig SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('config' ,TimmBackboneConfig() ) SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('use_timm_backbone' ,snake_case__ ) if not use_timm: raise ValueError('use_timm_backbone must be True for timm backbones' ) SCREAMING_SNAKE_CASE_ : List[Any] = kwargs.pop('num_channels' ,config.num_channels ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = kwargs.pop('features_only' ,config.features_only ) SCREAMING_SNAKE_CASE_ : str = kwargs.pop('use_pretrained_backbone' ,config.use_pretrained_backbone ) SCREAMING_SNAKE_CASE_ : Dict = kwargs.pop('out_indices' ,config.out_indices ) SCREAMING_SNAKE_CASE_ : List[str] = TimmBackboneConfig( backbone=snake_case__ ,num_channels=snake_case__ ,features_only=snake_case__ ,use_pretrained_backbone=snake_case__ ,out_indices=snake_case__ ,) return super()._from_config(snake_case__ ,**snake_case__ ) def snake_case ( self ,snake_case__ ): pass def snake_case ( self ,snake_case__ ,snake_case__=None ,snake_case__=None ,snake_case__=None ,**snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = return_dict if return_dict is not None else self.config.use_return_dict SCREAMING_SNAKE_CASE_ : List[Any] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) SCREAMING_SNAKE_CASE_ : List[Any] = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError('Cannot output attentions for timm backbones at the moment' ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone SCREAMING_SNAKE_CASE_ : List[Any] = self._all_layers SCREAMING_SNAKE_CASE_ : Tuple = self._backbone(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self._return_layers SCREAMING_SNAKE_CASE_ : int = tuple(hidden_states[i] for i in self.out_indices ) else: SCREAMING_SNAKE_CASE_ : Optional[Any] = self._backbone(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : Dict = tuple(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tuple(snake_case__ ) if hidden_states is not None else None if not return_dict: SCREAMING_SNAKE_CASE_ : Tuple = (feature_maps,) if output_hidden_states: SCREAMING_SNAKE_CASE_ : List[str] = output + (hidden_states,) return output return BackboneOutput(feature_maps=snake_case__ ,hidden_states=snake_case__ ,attentions=snake_case__ )
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
685
1
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int = 2 ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE_ : Optional[int] = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : str = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , lowerCamelCase_ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , lowerCamelCase_ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(lowerCamelCase_ ) ) , list(range(lowerCamelCase_ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
685
1
import argparse import torch from transformers import ( UniSpeechSatConfig, UniSpeechSatForAudioFrameClassification, UniSpeechSatForSequenceClassification, UniSpeechSatForXVector, WavaVecaFeatureExtractor, logging, ) logging.set_verbosity_info() UpperCamelCase__ : List[str] = logging.get_logger(__name__) def __UpperCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Any , lowerCamelCase_ : int ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = UniSpeechSatForSequenceClassification.from_pretrained(lowerCamelCase_ , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[str] = downstream_dict['projector.weight'] SCREAMING_SNAKE_CASE_ : Dict = downstream_dict['projector.bias'] SCREAMING_SNAKE_CASE_ : List[str] = downstream_dict['model.post_net.linear.weight'] SCREAMING_SNAKE_CASE_ : str = downstream_dict['model.post_net.linear.bias'] return model def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : List[str] , lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = UniSpeechSatForAudioFrameClassification.from_pretrained(lowerCamelCase_ , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Any = downstream_dict['model.linear.weight'] SCREAMING_SNAKE_CASE_ : str = downstream_dict['model.linear.bias'] return model def __UpperCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Optional[int] ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = UniSpeechSatForXVector.from_pretrained(lowerCamelCase_ , config=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = downstream_dict['connector.weight'] SCREAMING_SNAKE_CASE_ : Dict = downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): SCREAMING_SNAKE_CASE_ : str = downstream_dict[ F'model.framelevel_feature_extractor.module.{i}.kernel.weight' ] SCREAMING_SNAKE_CASE_ : int = downstream_dict[F'model.framelevel_feature_extractor.module.{i}.kernel.bias'] SCREAMING_SNAKE_CASE_ : int = downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] SCREAMING_SNAKE_CASE_ : Any = downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] SCREAMING_SNAKE_CASE_ : List[str] = downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] SCREAMING_SNAKE_CASE_ : Tuple = downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] SCREAMING_SNAKE_CASE_ : Tuple = downstream_dict['objective.W'] return model @torch.no_grad() def __UpperCAmelCase ( lowerCamelCase_ : Dict , lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : Dict , lowerCamelCase_ : Optional[Any] ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = torch.load(lowerCamelCase_ , map_location='cpu' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = checkpoint['Downstream'] SCREAMING_SNAKE_CASE_ : Any = UniSpeechSatConfig.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = WavaVecaFeatureExtractor.from_pretrained( lowerCamelCase_ , return_attention_mask=lowerCamelCase_ , do_normalize=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = hf_config.architectures[0] if arch.endswith('ForSequenceClassification' ): SCREAMING_SNAKE_CASE_ : int = convert_classification(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) elif arch.endswith('ForAudioFrameClassification' ): SCREAMING_SNAKE_CASE_ : str = convert_diarization(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) elif arch.endswith('ForXVector' ): SCREAMING_SNAKE_CASE_ : int = convert_xvector(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: raise NotImplementedError(F'S3PRL weights conversion is not supported for {arch}' ) if hf_config.use_weighted_layer_sum: SCREAMING_SNAKE_CASE_ : Dict = checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(lowerCamelCase_ ) hf_model.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": UpperCamelCase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument( '''--base_model_name''', default=None, type=str, help='''Name of the huggingface pretrained base model.''' ) parser.add_argument('''--config_path''', default=None, type=str, help='''Path to the huggingface classifier config.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to the s3prl checkpoint.''') parser.add_argument('''--model_dump_path''', default=None, type=str, help='''Path to the final converted model.''') UpperCamelCase__ : List[str] = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
1
from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class lowerCAmelCase_ ( unittest.TestCase ): @slow def snake_case ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE_ : List[Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = TFAutoModel.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = AutoModel.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE_ : Union[str, Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = TFAutoModelForPreTraining.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = AutoModelForPreTraining.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : str = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = TFAutoModelForCausalLM.from_pretrained(snake_case__ ,from_pt=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = TFAutoModelForCausalLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = AutoModelForCausalLM.from_pretrained(snake_case__ ,from_tf=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = AutoModelForCausalLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : List[str] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = TFAutoModelWithLMHead.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = AutoModelWithLMHead.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : str = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = TFAutoModelForMaskedLM.from_pretrained(snake_case__ ,from_pt=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[int] = TFAutoModelForMaskedLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = AutoModelForMaskedLM.from_pretrained(snake_case__ ,from_tf=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = AutoModelForMaskedLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ : Any = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = TFAutoModelForSeqaSeqLM.from_pretrained(snake_case__ ,from_pt=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ ,from_tf=snake_case__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = AutoModelForSeqaSeqLM.from_pretrained( snake_case__ ,output_loading_info=snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE_ : Optional[Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = TFAutoModelForSequenceClassification.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = AutoModelForSequenceClassification.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) @slow def snake_case ( self ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: SCREAMING_SNAKE_CASE_ : Union[str, Any] = AutoConfig.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = TFAutoModelForQuestionAnswering.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = AutoModelForQuestionAnswering.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsNotNone(snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = TFAutoModelWithLMHead.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) self.assertEqual(model.num_parameters() ,14410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) ,14410 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = AutoModelWithLMHead.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) self.assertEqual(model.num_parameters() ,14410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) ,14410 ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = TFAutoModelWithLMHead.from_pretrained(snake_case__ ,from_pt=snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) self.assertEqual(model.num_parameters() ,14410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) ,14410 ) SCREAMING_SNAKE_CASE_ : str = AutoModelWithLMHead.from_pretrained(snake_case__ ,from_tf=snake_case__ ) self.assertIsInstance(snake_case__ ,snake_case__ ) self.assertEqual(model.num_parameters() ,14410 ) self.assertEqual(model.num_parameters(only_trainable=snake_case__ ) ,14410 )
685
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> Tuple: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) else: return a * actual_power(lowerCamelCase_ , int(b / 2 ) ) * actual_power(lowerCamelCase_ , int(b / 2 ) ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(lowerCamelCase_ , lowerCamelCase_ ) return actual_power(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": print(power(-2, -3))
685
1
def __UpperCAmelCase ( lowerCamelCase_ : float ) -> float: """simple docstring""" if edge <= 0 or not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('Length must be a positive.' ) return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2) def __UpperCAmelCase ( lowerCamelCase_ : float ) -> float: """simple docstring""" if edge <= 0 or not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('Length must be a positive.' ) return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3) if __name__ == "__main__": import doctest doctest.testmod()
685
import unittest from parameterized import parameterized from transformers import LlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=True ,snake_case__=False ,snake_case__=True ,snake_case__=99 ,snake_case__=32 ,snake_case__=5 ,snake_case__=4 ,snake_case__=37 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=16 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=3 ,snake_case__=4 ,snake_case__=None ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = parent SCREAMING_SNAKE_CASE_ : List[Any] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = seq_length SCREAMING_SNAKE_CASE_ : Tuple = is_training SCREAMING_SNAKE_CASE_ : List[str] = use_input_mask SCREAMING_SNAKE_CASE_ : List[str] = use_token_type_ids SCREAMING_SNAKE_CASE_ : Optional[Any] = use_labels SCREAMING_SNAKE_CASE_ : int = vocab_size SCREAMING_SNAKE_CASE_ : Optional[int] = hidden_size SCREAMING_SNAKE_CASE_ : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Any = intermediate_size SCREAMING_SNAKE_CASE_ : Dict = hidden_act SCREAMING_SNAKE_CASE_ : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Tuple = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = type_vocab_size SCREAMING_SNAKE_CASE_ : int = type_sequence_label_size SCREAMING_SNAKE_CASE_ : int = initializer_range SCREAMING_SNAKE_CASE_ : Tuple = num_labels SCREAMING_SNAKE_CASE_ : List[str] = num_choices SCREAMING_SNAKE_CASE_ : Tuple = scope def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Dict = None if self.use_labels: SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) SCREAMING_SNAKE_CASE_ : Tuple = ids_tensor([self.batch_size] ,self.num_choices ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case ( self ): return LlamaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,is_decoder=snake_case__ ,initializer_range=self.initializer_range ,) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = LlamaModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[Any] = model(snake_case__ ,attention_mask=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaModel(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Optional[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Dict = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ,): SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : int = True SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE_ : List[Any] = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,use_cache=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ : str = ids_tensor((self.batch_size, 3) ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Any = ids_tensor((self.batch_size, 3) ,vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_ids, next_tokens] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Optional[Any] = torch.cat([input_mask, next_mask] ,dim=-1 ) SCREAMING_SNAKE_CASE_ : Dict = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] SCREAMING_SNAKE_CASE_ : Tuple = model( snake_case__ ,attention_mask=snake_case__ ,encoder_hidden_states=snake_case__ ,encoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,output_hidden_states=snake_case__ ,)['hidden_states'][0] # select random slice SCREAMING_SNAKE_CASE_ : Any = ids_tensor((1,) ,output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ : str = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE_ : str = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ( SCREAMING_SNAKE_CASE_ ) , ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE_ : List[Any] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else () __a : int = (LlamaForCausalLM,) if is_torch_available() else () __a : Any = ( { "feature-extraction": LlamaModel, "text-classification": LlamaForSequenceClassification, "text-generation": LlamaForCausalLM, "zero-shot": LlamaForSequenceClassification, } if is_torch_available() else {} ) __a : Tuple = False __a : Tuple = False def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaModelTester(self ) SCREAMING_SNAKE_CASE_ : List[str] = ConfigTester(self ,config_class=snake_case__ ,hidden_size=37 ) def snake_case ( self ): self.config_tester.run_common_tests() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE_ : Optional[int] = type self.model_tester.create_and_check_model(*snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Dict = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : str = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Tuple = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Any = 3 SCREAMING_SNAKE_CASE_ : int = 'single_label_classification' SCREAMING_SNAKE_CASE_ : str = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Dict = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : List[str] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Tuple = 3 SCREAMING_SNAKE_CASE_ : str = 'multi_label_classification' SCREAMING_SNAKE_CASE_ : int = input_dict['input_ids'] SCREAMING_SNAKE_CASE_ : Tuple = input_ids.ne(1 ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ids_tensor( [self.model_tester.batch_size, config.num_labels] ,self.model_tester.type_sequence_label_size ).to(torch.float ) SCREAMING_SNAKE_CASE_ : Optional[int] = LlamaForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() SCREAMING_SNAKE_CASE_ : Union[str, Any] = model(snake_case__ ,attention_mask=snake_case__ ,labels=snake_case__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('LLaMA buffers include complex numbers, which breaks this test' ) def snake_case ( self ): pass @parameterized.expand([('linear',), ('dynamic',)] ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor([1, 10] ,config.vocab_size ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : Any = LlamaModel(snake_case__ ) original_model.to(snake_case__ ) original_model.eval() SCREAMING_SNAKE_CASE_ : int = original_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : List[Any] = original_model(snake_case__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights SCREAMING_SNAKE_CASE_ : List[Any] = {'type': scaling_type, 'factor': 10.0} SCREAMING_SNAKE_CASE_ : int = LlamaModel(snake_case__ ) scaled_model.to(snake_case__ ) scaled_model.eval() SCREAMING_SNAKE_CASE_ : str = scaled_model(snake_case__ ).last_hidden_state SCREAMING_SNAKE_CASE_ : Optional[int] = scaled_model(snake_case__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(snake_case__ ,snake_case__ ,atol=1E-5 ) ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : List[Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : List[str] = model(torch.tensor([input_ids] ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : int = torch.tensor([[-6.6550, -4.1227, -4.9859, -3.2406, 0.8262, -3.0033, 1.2964, -3.3699]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-12.8281, -7.4453, -0.4639, -8.0625, -7.2500, -8.0000, -6.4883, -7.7695, -7.8438, -7.0312, -6.2188, -7.1328, -1.8496, 1.9961, -8.6250, -6.7227, -12.8281, -6.9492, -7.0742, -7.7852, -7.5820, -7.9062, -6.9375, -7.9805, -8.3438, -8.1562, -8.0469, -7.6250, -7.7422, -7.3398,] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Dict = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([[-2.0622, -1.2794, -1.1638, -0.9788, -1.4603, -1.0238, -1.7893, -1.4411]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[str] = torch.tensor([-8.1406, -8.0547, 2.7461, -1.2344, -0.1448, -1.8262, -1.0020, -1.8154, -1.6895, -1.8516, -2.3574, -0.9277, 3.7598, 6.5742, -1.2998, -0.1177, -8.1406, -2.9688, -2.9199, -3.1699, -3.5254, -2.3555, -2.7988, -3.4141, -2.8262, -4.5195, -3.3379, -3.3164, -2.7832, -3.0273] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : Union[str, Any] = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : Tuple = model(torch.tensor(snake_case__ ) ) # Expected mean on dim = -1 SCREAMING_SNAKE_CASE_ : Dict = torch.tensor([[-0.8562, -1.8520, -0.7551, -0.4162, -1.5161, -1.2038, -2.4823, -2.3254]] ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # slicing logits[0, 0, 0:30] # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor([-2.2227, 4.8828, 0.9023, -0.4578, -0.7871, -0.1033, -0.6221, -0.5786, -0.7803, -1.0674, -1.2920, -0.1570, 0.8008, 2.0723, -0.9497, 0.2771, -2.2227, -0.7612, -1.4346, -1.2061, -1.6426, -0.3000, -0.7139, -1.1934, -1.8691, -1.6973, -1.5947, -1.2705, -0.3523, -0.5513] ) # fmt: on torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) @unittest.skip( 'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = [1, 306, 4658, 278, 6593, 310, 2834, 338] SCREAMING_SNAKE_CASE_ : str = LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf' ,device_map='auto' ) SCREAMING_SNAKE_CASE_ : int = model(torch.tensor(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.tensor( [[-4.2327, -3.3360, -4.6665, -4.7631, -1.8180, -3.4170, -1.4211, -3.1810]] ,dtype=torch.floataa ) torch.testing.assert_close(out.mean(-1 ) ,snake_case__ ,atol=1E-2 ,rtol=1E-2 ) # fmt: off SCREAMING_SNAKE_CASE_ : Optional[int] = torch.tensor([-9.4922, -3.9551, 1.7998, -5.6758, -5.1055, -5.8984, -4.8320, -6.8086, -6.5391, -5.6172, -5.5820, -5.5352, 1.7881, 3.6289, -6.5117, -3.4785, -9.5000, -6.0352, -6.8125, -6.0195, -6.6836, -5.4727, -6.2812, -6.0391, -7.3398, -7.4297, -7.4844, -6.5820, -5.8789, -5.5312] ) # fmt: on torch.testing.assert_close(out[0, 0, :30] ,snake_case__ ,atol=1E-5 ,rtol=1E-5 ) @unittest.skip('Model is curently gated' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi' SCREAMING_SNAKE_CASE_ : List[str] = 'Simply put, the theory of relativity states that ' SCREAMING_SNAKE_CASE_ : str = LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf' ) SCREAMING_SNAKE_CASE_ : Optional[int] = tokenizer.encode(snake_case__ ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Tuple = LlamaForCausalLM.from_pretrained( 'meta-llama/Llama-2-13b-chat-hf' ,device_map='sequential' ,use_safetensors=snake_case__ ) # greedy generation outputs SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.generate(snake_case__ ,max_new_tokens=64 ,top_p=snake_case__ ,temperature=1 ,do_sample=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.decode(generated_ids[0] ,skip_special_tokens=snake_case__ ) self.assertEqual(snake_case__ ,snake_case__ )
685
1
import tempfile import unittest from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from transformers.testing_utils import ( is_torch_available, require_optimum, require_torch, slow, ) if is_torch_available(): import torch @require_torch @require_optimum @slow class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'hf-internal-testing/tiny-random-t5' SCREAMING_SNAKE_CASE_ : Tuple = AutoTokenizer.from_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer('This is me' ,return_tensors='pt' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.to_bettertransformer() self.assertTrue(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules() ) ) SCREAMING_SNAKE_CASE_ : List[str] = model.generate(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = model.reverse_bettertransformer() self.assertFalse(any('BetterTransformer' in mod.__class__.__name__ for _, mod in model.named_modules() ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ ) self.assertFalse( any('BetterTransformer' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) SCREAMING_SNAKE_CASE_ : Tuple = model_reloaded.generate(**snake_case__ ) self.assertTrue(torch.allclose(snake_case__ ,snake_case__ ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = 'hf-internal-testing/tiny-random-t5' SCREAMING_SNAKE_CASE_ : str = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(snake_case__ ): model.save_pretrained(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = model.reverse_bettertransformer() model.save_pretrained(snake_case__ )
685
import collections import json import os import re from typing import TYPE_CHECKING, List, Optional, Tuple import numpy as np from ...tokenization_utils_fast import PreTrainedTokenizer from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCamelCase__ : Optional[int] = logging.get_logger(__name__) UpperCamelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''} UpperCamelCase__ : int = { '''vocab_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''', }, '''emoji_file''': { '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''', }, } UpperCamelCase__ : str = { '''abeja/gpt-neox-japanese-2.7b''': 20_48, } def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Tuple ) -> List[str]: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[Any] = json.loads(f.read() ) SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : Dict = collections.OrderedDict() SCREAMING_SNAKE_CASE_ : List[Any] = collections.OrderedDict() with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Any = f.readlines() SCREAMING_SNAKE_CASE_ : Union[str, Any] = [[t.rstrip('\n' )] if (t == ',' or ',' not in t) else t.rstrip('\n' ).split(',' ) for t in token] for idx, b in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[int] = b SCREAMING_SNAKE_CASE_ : Dict = idx for wd in b: SCREAMING_SNAKE_CASE_ : Any = idx return vocab, raw_vocab, ids_to_tokens, emoji class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Union[str, Any] = VOCAB_FILES_NAMES __a : List[str] = PRETRAINED_VOCAB_FILES_MAP __a : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__="<|endoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__="<|startoftext|>" ,snake_case__="<|endoftext|>" ,snake_case__=False ,**snake_case__ ,): super().__init__( unk_token=snake_case__ ,pad_token=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,do_clean_text=snake_case__ ,**snake_case__ ,) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a vocabulary file at path \'{vocab_file}\'. To load the vocabulary from a Google pretrained' ' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) if not os.path.isfile(snake_case__ ): raise ValueError( F'Can\'t find a emoji file at path \'{emoji_file}\'. To load the emoji information from a Google' ' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`' ) SCREAMING_SNAKE_CASE_ : str = do_clean_text SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = load_vocab_and_emoji(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = SubWordJapaneseTokenizer( vocab=self.vocab ,ids_to_tokens=self.ids_to_tokens ,emoji=self.emoji ) @property def snake_case ( self ): # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab return len(self.raw_vocab ) def snake_case ( self ): return dict(self.raw_vocab ,**self.added_tokens_encoder ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.tokenize(snake_case__ ,clean=self.do_clean_text ) def snake_case ( self ,snake_case__ ): return self.vocab.get(snake_case__ ,self.vocab.get(self.unk_token ) ) def snake_case ( self ,snake_case__ ): return self.subword_tokenizer.convert_id_to_token(snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : str = ''.join(snake_case__ ).strip() return out_string def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(snake_case__ ,add_special_tokens=snake_case__ ) + [self.eos_token_id] ) if len(snake_case__ ) > self.model_max_length: SCREAMING_SNAKE_CASE_ : List[Any] = input_ids[-self.model_max_length :] return input_ids def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 if os.path.isdir(snake_case__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'] ) else: SCREAMING_SNAKE_CASE_ : Tuple = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : str = ( (filename_prefix + '-' if filename_prefix else '') + save_directory + VOCAB_FILES_NAMES['emoji_file'] ) with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: for token_index, token in self.ids_to_tokens.items(): if index != token_index: logger.warning( F'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.' ' Please check that the vocabulary is not corrupted!' ) SCREAMING_SNAKE_CASE_ : Dict = token_index writer.write(','.join(snake_case__ ) + '\n' ) index += 1 with open(snake_case__ ,'w' ,encoding='utf-8' ) as writer: json.dump(self.emoji ,snake_case__ ) return vocab_file, emoji_file class lowerCAmelCase_ ( lowerCamelCase_ ): def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = vocab # same as swe SCREAMING_SNAKE_CASE_ : Optional[int] = ids_to_tokens # same as bpe SCREAMING_SNAKE_CASE_ : Dict = emoji SCREAMING_SNAKE_CASE_ : int = np.max([len(snake_case__ ) for w in self.vocab.keys()] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = re.compile(R'(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile(R'[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : str = re.compile( R'(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*' ) SCREAMING_SNAKE_CASE_ : List[str] = re.compile( R'((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*' ) SCREAMING_SNAKE_CASE_ : str = '─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿' SCREAMING_SNAKE_CASE_ : int = '▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟' SCREAMING_SNAKE_CASE_ : Tuple = str.maketrans({k: '<BLOCK>' for k in keisen + blocks} ) def __len__( self ): return len(self.ids_to_tokens ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<URL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = self.content_repattera.sub('<EMAIL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<TEL>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.content_repattera.sub('<DATE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = self.content_repattera.sub('<PRICE>' ,snake_case__ ) SCREAMING_SNAKE_CASE_ : str = content.translate(self.content_transa ) while "<BLOCK><BLOCK>" in content: SCREAMING_SNAKE_CASE_ : Union[str, Any] = content.replace('<BLOCK><BLOCK>' ,'<BLOCK>' ) return content def snake_case ( self ,snake_case__ ,snake_case__=False ): SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace(' ' ,'<SP>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('\r\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\n' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = text.replace('\r' ,'<BR>' ) SCREAMING_SNAKE_CASE_ : List[str] = text.replace('\t' ,'<TAB>' ) SCREAMING_SNAKE_CASE_ : List[Any] = text.replace('—' ,'ー' ) SCREAMING_SNAKE_CASE_ : Optional[int] = text.replace('−' ,'ー' ) for k, v in self.emoji["emoji"].items(): if k in text: SCREAMING_SNAKE_CASE_ : int = text.replace(snake_case__ ,snake_case__ ) if clean: SCREAMING_SNAKE_CASE_ : str = self.clean_text(snake_case__ ) def check_simbol(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 2: SCREAMING_SNAKE_CASE_ : str = (int(e[0] ) << 8) + int(e[1] ) if ( (c >= 0XC2A1 and c <= 0XC2BF) or (c >= 0XC780 and c <= 0XC783) or (c >= 0XCAB9 and c <= 0XCBBF) or (c >= 0XCC80 and c <= 0XCDA2) ): return True return False def checkuae(snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = x.encode() if len(snake_case__ ) == 1 and len(snake_case__ ) == 3: SCREAMING_SNAKE_CASE_ : Dict = (int(e[0] ) << 16) + (int(e[1] ) << 8) + int(e[2] ) if c >= 0XE2_8080 and c <= 0XE2_B07F: return True return False SCREAMING_SNAKE_CASE_ : int = 0 SCREAMING_SNAKE_CASE_ : List[Any] = [] while pos < len(snake_case__ ): SCREAMING_SNAKE_CASE_ : List[Any] = min(len(snake_case__ ) ,pos + self.maxlen + 1 ) if text[pos] == '<' else pos + 3 SCREAMING_SNAKE_CASE_ : List[Any] = [] # (token_id, token, pos) for e in range(snake_case__ ,snake_case__ ,-1 ): SCREAMING_SNAKE_CASE_ : str = text[pos:e] if wd in self.vocab: if wd[0] == "<" and len(snake_case__ ) > 2: SCREAMING_SNAKE_CASE_ : Optional[Any] = [(self.vocab[wd], wd, e)] break else: candidates.append((self.vocab[wd], wd, e) ) if len(snake_case__ ) > 0: # the smallest token_id is adopted SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = sorted(snake_case__ ,key=lambda snake_case__ : x[0] )[0] result.append(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = e else: SCREAMING_SNAKE_CASE_ : Any = pos + 1 SCREAMING_SNAKE_CASE_ : Optional[int] = text[pos:end] if check_simbol(snake_case__ ): result.append('<KIGOU>' ) elif checkuae(snake_case__ ): result.append('<U2000U2BFF>' ) else: for i in wd.encode('utf-8' ): result.append('<|byte%d|>' % i ) SCREAMING_SNAKE_CASE_ : int = end return result def snake_case ( self ,snake_case__ ,snake_case__="\n" ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Dict = self.ids_to_tokens[index][0] if word[:6] == "<|byte" and word[-2:] == "|>": byte_tokens.append(int(word[6:-2] ) ) else: if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : Dict = [] if word[:7] == "<|emoji" and word[-2:] == "|>": words.append(self.emoji['emoji_inv'][word] ) elif word == "<SP>": words.append(' ' ) elif word == "<BR>": words.append(snake_case__ ) elif word == "<TAB>": words.append('\t' ) elif word == "<BLOCK>": words.append('▀' ) elif word == "<KIGOU>": words.append('ǀ' ) elif word == "<U2000U2BFF>": words.append('‖' ) else: words.append(snake_case__ ) if len(snake_case__ ) > 0: words.append(bytearray(snake_case__ ).decode('utf-8' ,errors='replace' ) ) SCREAMING_SNAKE_CASE_ : int = ''.join(snake_case__ ) return text
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def __UpperCAmelCase ( lowerCamelCase_ : int = 50_00 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = [(i * (3 * i - 1)) // 2 for i in range(1 , lowerCamelCase_ )] for i, pentagonal_i in enumerate(lowerCamelCase_ ): for j in range(lowerCamelCase_ , len(lowerCamelCase_ ) ): SCREAMING_SNAKE_CASE_ : int = pentagonal_nums[j] SCREAMING_SNAKE_CASE_ : Optional[int] = pentagonal_i + pentagonal_j SCREAMING_SNAKE_CASE_ : Dict = pentagonal_j - pentagonal_i if is_pentagonal(lowerCamelCase_ ) and is_pentagonal(lowerCamelCase_ ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
685
import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : int=() , lowerCamelCase_ : Union[str, Any]=None , lowerCamelCase_ : Optional[int]="no" , lowerCamelCase_ : Optional[Any]="29500" ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = False SCREAMING_SNAKE_CASE_ : Optional[Any] = False if any(key.startswith('KAGGLE' ) for key in os.environ.keys() ): SCREAMING_SNAKE_CASE_ : str = True elif "IPython" in sys.modules: SCREAMING_SNAKE_CASE_ : Dict = 'google.colab' in str(sys.modules['IPython'].get_ipython() ) try: SCREAMING_SNAKE_CASE_ : Optional[int] = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( F'Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.' ) if (in_colab or in_kaggle) and (os.environ.get('TPU_NAME' , lowerCamelCase_ ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside ' 'your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if num_processes is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = 8 SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='TPU' ) print(F'Launching a training on {num_processes} TPU cores.' ) xmp.spawn(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on one CPU.' ) function(*lowerCamelCase_ ) else: if num_processes is None: raise ValueError( 'You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.' ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( 'To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized ' 'inside your training function. Restart your notebook and make sure no cells initializes an ' '`Accelerator`.' ) if torch.cuda.is_initialized(): raise ValueError( 'To launch a multi-GPU training from your notebook, you need to avoid running any instruction ' 'using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA ' 'function.' ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port=lowerCamelCase_ , mixed_precision=lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : int = PrepareForLaunch(lowerCamelCase_ , distributed_type='MULTI_GPU' ) print(F'Launching training on {num_processes} GPUs.' ) try: start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( 'CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. ' 'This likely stems from an outside import causing issues once the `notebook_launcher()` is called. ' 'Please review your imports and test them when running the `notebook_launcher()` to identify ' 'which one is problematic.' ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): SCREAMING_SNAKE_CASE_ : Optional[Any] = '1' print('Launching training on MPS.' ) elif torch.cuda.is_available(): print('Launching training on one GPU.' ) else: print('Launching training on CPU.' ) function(*lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple=() , lowerCamelCase_ : str=2 ) -> Union[str, Any]: """simple docstring""" from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=lowerCamelCase_ , master_addr='127.0.01' , master_port='29500' , accelerate_mixed_precision='no' , accelerate_debug_rdv_file=tmp_file.name , accelerate_use_cpu='yes' , ): SCREAMING_SNAKE_CASE_ : Tuple = PrepareForLaunch(lowerCamelCase_ , debug=lowerCamelCase_ ) start_processes(lowerCamelCase_ , args=lowerCamelCase_ , nprocs=lowerCamelCase_ , start_method='fork' )
685
1
import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : List[str] = ProphetNetTokenizer __a : List[str] = False def snake_case ( self ): super().setUp() SCREAMING_SNAKE_CASE_ : str = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] SCREAMING_SNAKE_CASE_ : str = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : int = 'UNwant\u00E9d,running' SCREAMING_SNAKE_CASE_ : Tuple = 'unwanted, running' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Optional[Any] = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(snake_case__ ,['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,[9, 6, 7, 12, 10, 11] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) ,['ah', '\u535A', '\u63A8', 'zz'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) ,['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) ,['hello'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = BasicTokenizer(do_lower_case=snake_case__ ,strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) ,['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) ,['h\u00E9llo'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = BasicTokenizer(do_lower_case=snake_case__ ,strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) ,['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) ,['hello'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) ,['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) ,['hello'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) ,['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = BasicTokenizer(do_lower_case=snake_case__ ,strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) ,['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = BasicTokenizer(do_lower_case=snake_case__ ,strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) ,['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = BasicTokenizer(do_lower_case=snake_case__ ,never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) ,['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] SCREAMING_SNAKE_CASE_ : List[Any] = {} for i, token in enumerate(snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = i SCREAMING_SNAKE_CASE_ : Union[str, Any] = WordpieceTokenizer(vocab=snake_case__ ,unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) ,[] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) ,['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) ,['[UNK]', 'runn', '##ing'] ) @require_torch def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] SCREAMING_SNAKE_CASE_ : int = [1037, 2146, 20423, 2005, 7680, 7849, 3989, 1012, 102] SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer(snake_case__ ,padding=snake_case__ ,return_tensors='pt' ) self.assertIsInstance(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(batch.input_ids.numpy()[0] ) self.assertListEqual(snake_case__ ,snake_case__ ) self.assertEqual((2, 9) ,batch.input_ids.shape ) self.assertEqual((2, 9) ,batch.attention_mask.shape ) def snake_case ( self ): self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def snake_case ( self ): self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def snake_case ( self ): self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) SCREAMING_SNAKE_CASE_ : Dict = tokenizer.encode('sequence builders' ,add_special_tokens=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer.encode('multi-sequence build' ,add_special_tokens=snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer.build_inputs_with_special_tokens(snake_case__ ,snake_case__ ) assert encoded_sentence == text + [102] assert encoded_pair == text + [102] + text_a + [102]
685
from typing import TYPE_CHECKING from ....utils import _LazyModule UpperCamelCase__ : Tuple = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int = 2_00_00_00 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = [0 for i in range(n + 1 )] SCREAMING_SNAKE_CASE_ : List[Any] = 1 SCREAMING_SNAKE_CASE_ : str = 1 for i in range(2 , int(n**0.5 ) + 1 ): if primality_list[i] == 0: for j in range(i * i , n + 1 , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[str] = 1 SCREAMING_SNAKE_CASE_ : str = 0 for i in range(lowerCamelCase_ ): if primality_list[i] == 0: sum_of_primes += i return sum_of_primes if __name__ == "__main__": print(F"""{solution() = }""")
685
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = CLIPTokenizer __a : List[str] = CLIPTokenizerFast __a : List[str] = True __a : Tuple = {} __a : Tuple = False def snake_case ( self ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE_ : List[Any] = ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE_ : Union[str, Any] = dict(zip(snake_case__ ,range(len(snake_case__ ) ) ) ) SCREAMING_SNAKE_CASE_ : Any = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>'] SCREAMING_SNAKE_CASE_ : Any = {'unk_token': '<unk>'} SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) SCREAMING_SNAKE_CASE_ : Any = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case__ ) + '\n' ) with open(self.merges_file ,'w' ,encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case__ ) ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,**snake_case__ ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = 'lower newer' SCREAMING_SNAKE_CASE_ : Tuple = 'lower newer' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = CLIPTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) SCREAMING_SNAKE_CASE_ : List[Any] = 'lower newer' SCREAMING_SNAKE_CASE_ : Optional[Any] = ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>'] SCREAMING_SNAKE_CASE_ : Any = tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ : Optional[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,snake_case__ ) @require_ftfy def snake_case ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Any = self.tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = self.rust_tokenizer_class.from_pretrained(snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = 'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.' SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways SCREAMING_SNAKE_CASE_ : Dict = 'xa\u0303y' + ' ' + 'x\xe3y' SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : str = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of space type SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '\u0009', # (horizontal tab, '\t') '\u000B', # (vertical tab) '\u000C', # (form feed) '\u0020', # (space, ' ') '\u200E', # (left-to-right mark):w '\u200F', # (right-to-left mark) ] for unicode_seq in spaces_unicodes: SCREAMING_SNAKE_CASE_ : Union[str, Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) # Test that the tokenization is identical on unicode of line break type SCREAMING_SNAKE_CASE_ : Tuple = [ '\u000A', # (line feed, '\n') '\r\n', # (carriage return and line feed, '\r\n') '\u000D', # (carriage return, '\r') '\r', # (carriage return, '\r') '\u000D', # (carriage return, '\r') '\u2028', # (line separator) '\u2029', # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer_s.tokenize(snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = tokenizer_r.tokenize(snake_case__ ) self.assertListEqual(snake_case__ ,snake_case__ ) def snake_case ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): SCREAMING_SNAKE_CASE_ : Optional[int] = 'hello' # `hello` is a token in the vocabulary of `pretrained_name` SCREAMING_SNAKE_CASE_ : Tuple = F'{text_of_1_token} {text_of_1_token}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : str = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(0, len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(len(snake_case__ ) + 1, len(snake_case__ ) + 1 + len(snake_case__ )) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = F' {text}' SCREAMING_SNAKE_CASE_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( snake_case__ ,use_fast=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = tokenizer_r(snake_case__ ,return_offsets_mapping=snake_case__ ,add_special_tokens=snake_case__ ) self.assertEqual(encoding.offset_mapping[0] ,(1, 1 + len(snake_case__ )) ) self.assertEqual( encoding.offset_mapping[1] ,(1 + len(snake_case__ ) + 1, 1 + len(snake_case__ ) + 1 + len(snake_case__ )) ,) def snake_case ( self ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(snake_case__ ) as context: self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer' ) self.assertTrue( context.exception.args[0].startswith( 'The `backend_tokenizer` provided does not match the expected format.' ) ) @require_ftfy def snake_case ( self ): super().test_tokenization_python_rust_equals() def snake_case ( self ): # CLIP always lower cases letters pass
685
1
from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets UpperCamelCase__ : Tuple = '''\ @inproceedings{wang2019glue, title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding}, author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.}, note={In the Proceedings of ICLR.}, year={2019} } ''' UpperCamelCase__ : Dict = '''\ GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. ''' UpperCamelCase__ : Optional[Any] = ''' Compute GLUE evaluation metric associated to each GLUE dataset. Args: predictions: list of predictions to score. Each translation should be tokenized into a list of tokens. references: list of lists of references for each translation. Each reference should be tokenized into a list of tokens. Returns: depending on the GLUE subset, one or several of: "accuracy": Accuracy "f1": F1 score "pearson": Pearson Correlation "spearmanr": Spearman Correlation "matthews_correlation": Matthew Correlation Examples: >>> glue_metric = datasets.load_metric(\'glue\', \'sst2\') # \'sst2\' or any of ["mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"] >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'mrpc\') # \'mrpc\' or \'qqp\' >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0, \'f1\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'stsb\') >>> references = [0., 1., 2., 3., 4., 5.] >>> predictions = [0., 1., 2., 3., 4., 5.] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print({"pearson": round(results["pearson"], 2), "spearmanr": round(results["spearmanr"], 2)}) {\'pearson\': 1.0, \'spearmanr\': 1.0} >>> glue_metric = datasets.load_metric(\'glue\', \'cola\') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = glue_metric.compute(predictions=predictions, references=references) >>> print(results) {\'matthews_correlation\': 1.0} ''' def __UpperCAmelCase ( lowerCamelCase_ : Any , lowerCamelCase_ : List[Any] ) -> Dict: """simple docstring""" return float((preds == labels).mean() ) def __UpperCAmelCase ( lowerCamelCase_ : Tuple , lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = simple_accuracy(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = float(fa_score(y_true=lowerCamelCase_ , y_pred=lowerCamelCase_ ) ) return { "accuracy": acc, "f1": fa, } def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Any ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = float(pearsonr(lowerCamelCase_ , lowerCamelCase_ )[0] ) SCREAMING_SNAKE_CASE_ : List[str] = float(spearmanr(lowerCamelCase_ , lowerCamelCase_ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): def snake_case ( self ): if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( 'You should supply a configuration name selected in ' '["sst2", "mnli", "mnli_mismatched", "mnli_matched", ' '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]' ) return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Value('int64' if self.config_name != 'stsb' else 'float32' ), 'references': datasets.Value('int64' if self.config_name != 'stsb' else 'float32' ), } ) ,codebase_urls=[] ,reference_urls=[] ,format='numpy' ,) def snake_case ( self ,snake_case__ ,snake_case__ ): if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(snake_case__ ,snake_case__ )} elif self.config_name == "stsb": return pearson_and_spearman(snake_case__ ,snake_case__ ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(snake_case__ ,snake_case__ ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(snake_case__ ,snake_case__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["sst2", "mnli", "mnli_mismatched", "mnli_matched", ' '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]' )
685
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def __UpperCAmelCase ( ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = ArgumentParser('Transformers CLI tool' , usage='transformers-cli <command> [<args>]' ) SCREAMING_SNAKE_CASE_ : int = parser.add_subparsers(help='transformers-cli command helpers' ) # Register commands ConvertCommand.register_subcommand(lowerCamelCase_ ) DownloadCommand.register_subcommand(lowerCamelCase_ ) EnvironmentCommand.register_subcommand(lowerCamelCase_ ) RunCommand.register_subcommand(lowerCamelCase_ ) ServeCommand.register_subcommand(lowerCamelCase_ ) UserCommands.register_subcommand(lowerCamelCase_ ) AddNewModelCommand.register_subcommand(lowerCamelCase_ ) AddNewModelLikeCommand.register_subcommand(lowerCamelCase_ ) LfsCommands.register_subcommand(lowerCamelCase_ ) PTtoTFCommand.register_subcommand(lowerCamelCase_ ) # Let's go SCREAMING_SNAKE_CASE_ : Optional[int] = parser.parse_args() if not hasattr(lowerCamelCase_ , 'func' ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE_ : Optional[Any] = args.func(lowerCamelCase_ ) service.run() if __name__ == "__main__": main()
685
1
import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotConfig, is_flax_available from transformers.testing_utils import jax_device, require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html UpperCamelCase__ : int = '''platform''' import jax import jax.numpy as jnp from transformers import BlenderbotTokenizer from transformers.models.blenderbot.modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, shift_tokens_right, ) def __UpperCAmelCase ( lowerCamelCase_ : Any , lowerCamelCase_ : List[Any] , lowerCamelCase_ : List[str]=None , lowerCamelCase_ : List[str]=None , lowerCamelCase_ : Optional[int]=None , lowerCamelCase_ : Tuple=None , lowerCamelCase_ : Tuple=None , lowerCamelCase_ : Tuple=None , ) -> int: """simple docstring""" if attention_mask is None: SCREAMING_SNAKE_CASE_ : int = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ : Dict = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: SCREAMING_SNAKE_CASE_ : int = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ : List[Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ : List[Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class lowerCAmelCase_ : def __init__( self ,snake_case__ ,snake_case__=13 ,snake_case__=7 ,snake_case__=True ,snake_case__=False ,snake_case__=99 ,snake_case__=16 ,snake_case__=2 ,snake_case__=4 ,snake_case__=4 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=32 ,snake_case__=2 ,snake_case__=1 ,snake_case__=0 ,snake_case__=0.02 ,): SCREAMING_SNAKE_CASE_ : Any = parent SCREAMING_SNAKE_CASE_ : str = batch_size SCREAMING_SNAKE_CASE_ : Optional[Any] = seq_length SCREAMING_SNAKE_CASE_ : List[str] = is_training SCREAMING_SNAKE_CASE_ : Union[str, Any] = use_labels SCREAMING_SNAKE_CASE_ : Tuple = vocab_size SCREAMING_SNAKE_CASE_ : List[str] = hidden_size SCREAMING_SNAKE_CASE_ : int = num_hidden_layers SCREAMING_SNAKE_CASE_ : Dict = num_attention_heads SCREAMING_SNAKE_CASE_ : List[Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE_ : str = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[Any] = max_position_embeddings SCREAMING_SNAKE_CASE_ : List[str] = eos_token_id SCREAMING_SNAKE_CASE_ : List[str] = pad_token_id SCREAMING_SNAKE_CASE_ : int = bos_token_id SCREAMING_SNAKE_CASE_ : Optional[Any] = initializer_range def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) ,3 ,self.vocab_size ) SCREAMING_SNAKE_CASE_ : Optional[int] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) ,dtype=np.intaa )) ,-1 ) SCREAMING_SNAKE_CASE_ : List[str] = shift_tokens_right(snake_case__ ,1 ,2 ) SCREAMING_SNAKE_CASE_ : Any = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_id=self.eos_token_id ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,initializer_range=self.initializer_range ,use_cache=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_blenderbot_inputs_dict(snake_case__ ,snake_case__ ,snake_case__ ) return config, inputs_dict def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[Any] = self.prepare_config_and_inputs() return config, inputs_dict def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Tuple = 20 SCREAMING_SNAKE_CASE_ : List[Any] = model_class_name(snake_case__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.encode(inputs_dict['input_ids'] ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Any = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) SCREAMING_SNAKE_CASE_ : Any = model.init_cache(decoder_input_ids.shape[0] ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) ,dtype='i4' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.decode( decoder_input_ids[:, :-1] ,snake_case__ ,decoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,decoder_position_ids=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Dict = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype='i4' ) SCREAMING_SNAKE_CASE_ : str = model.decode( decoder_input_ids[:, -1:] ,snake_case__ ,decoder_attention_mask=snake_case__ ,past_key_values=outputs_cache.past_key_values ,decoder_position_ids=snake_case__ ,) SCREAMING_SNAKE_CASE_ : int = model.decode(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=F'Max diff is {diff}' ) def snake_case ( self ,snake_case__ ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Any = 20 SCREAMING_SNAKE_CASE_ : Union[str, Any] = model_class_name(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = model.encode(inputs_dict['input_ids'] ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) SCREAMING_SNAKE_CASE_ : int = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] ,axis=-1 ,) SCREAMING_SNAKE_CASE_ : Optional[Any] = model.init_cache(decoder_input_ids.shape[0] ,snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] ,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) ,) SCREAMING_SNAKE_CASE_ : Any = model.decode( decoder_input_ids[:, :-1] ,snake_case__ ,decoder_attention_mask=snake_case__ ,past_key_values=snake_case__ ,decoder_position_ids=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Dict = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] ,dtype='i4' ) SCREAMING_SNAKE_CASE_ : List[Any] = model.decode( decoder_input_ids[:, -1:] ,snake_case__ ,past_key_values=outputs_cache.past_key_values ,decoder_attention_mask=snake_case__ ,decoder_position_ids=snake_case__ ,) SCREAMING_SNAKE_CASE_ : List[Any] = model.decode(snake_case__ ,snake_case__ ,decoder_attention_mask=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 ,msg=F'Max diff is {diff}' ) @require_flax class lowerCAmelCase_ ( unittest.TestCase ): __a : Dict = 99 def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[Any] = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] ,dtype=np.intaa ,) SCREAMING_SNAKE_CASE_ : Any = input_ids.shape[0] SCREAMING_SNAKE_CASE_ : Tuple = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=24 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=32 ,decoder_ffn_dim=32 ,max_position_embeddings=48 ,eos_token_id=2 ,pad_token_id=1 ,bos_token_id=0 ,) return config, input_ids, batch_size def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = self._get_config_and_data() SCREAMING_SNAKE_CASE_ : Optional[int] = FlaxBlenderbotForConditionalGeneration(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = lm_model(input_ids=snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['logits'].shape ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = BlenderbotConfig( vocab_size=self.vocab_size ,d_model=14 ,encoder_layers=2 ,decoder_layers=2 ,encoder_attention_heads=2 ,decoder_attention_heads=2 ,encoder_ffn_dim=8 ,decoder_ffn_dim=8 ,max_position_embeddings=48 ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxBlenderbotForConditionalGeneration(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] ,dtype=np.intaa ) SCREAMING_SNAKE_CASE_ : Optional[int] = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] ,dtype=np.intaa ) SCREAMING_SNAKE_CASE_ : str = lm_model(input_ids=snake_case__ ,decoder_input_ids=snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = (*summary.shape, config.vocab_size) self.assertEqual(outputs['logits'].shape ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] ,dtype=np.intaa ) SCREAMING_SNAKE_CASE_ : Optional[int] = shift_tokens_right(snake_case__ ,1 ,2 ) SCREAMING_SNAKE_CASE_ : Tuple = np.equal(snake_case__ ,1 ).astype(np.floataa ).sum() SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.equal(snake_case__ ,1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape ,input_ids.shape ) self.assertEqual(snake_case__ ,n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] ,2 ).all() ) @require_flax class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase , lowerCamelCase_ ): __a : Dict = True __a : List[str] = ( ( FlaxBlenderbotModel, FlaxBlenderbotForConditionalGeneration, ) if is_flax_available() else () ) __a : Optional[Any] = (FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else () def snake_case ( self ): SCREAMING_SNAKE_CASE_ : List[str] = FlaxBlenderbotModelTester(self ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(snake_case__ ,snake_case__ ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(snake_case__ ,snake_case__ ,snake_case__ ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE_ : Dict = self._prepare_for_class(snake_case__ ,snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = model_class(snake_case__ ) @jax.jit def encode_jitted(snake_case__ ,snake_case__=None ,**snake_case__ ): return model.encode(input_ids=snake_case__ ,attention_mask=snake_case__ ) with self.subTest('JIT Enabled' ): SCREAMING_SNAKE_CASE_ : Optional[Any] = encode_jitted(**snake_case__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): SCREAMING_SNAKE_CASE_ : Tuple = encode_jitted(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) ,len(snake_case__ ) ) for jitted_output, output in zip(snake_case__ ,snake_case__ ): self.assertEqual(jitted_output.shape ,output.shape ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = model_class(snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = model.encode(inputs_dict['input_ids'] ,inputs_dict['attention_mask'] ) SCREAMING_SNAKE_CASE_ : int = { 'decoder_input_ids': inputs_dict['decoder_input_ids'], 'decoder_attention_mask': inputs_dict['decoder_attention_mask'], 'encoder_outputs': encoder_outputs, } @jax.jit def decode_jitted(snake_case__ ,snake_case__ ,snake_case__ ): return model.decode( decoder_input_ids=snake_case__ ,decoder_attention_mask=snake_case__ ,encoder_outputs=snake_case__ ,) with self.subTest('JIT Enabled' ): SCREAMING_SNAKE_CASE_ : List[Any] = decode_jitted(**snake_case__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): SCREAMING_SNAKE_CASE_ : str = decode_jitted(**snake_case__ ).to_tuple() self.assertEqual(len(snake_case__ ) ,len(snake_case__ ) ) for jitted_output, output in zip(snake_case__ ,snake_case__ ): self.assertEqual(jitted_output.shape ,output.shape ) @slow def snake_case ( self ): for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE_ : List[Any] = model_class_name.from_pretrained('facebook/blenderbot-400M-distill' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids SCREAMING_SNAKE_CASE_ : Union[str, Any] = np.ones((1, 1) ) * model.config.eos_token_id SCREAMING_SNAKE_CASE_ : int = model(snake_case__ ) self.assertIsNotNone(snake_case__ ) @unittest.skipUnless(jax_device != 'cpu' ,'3B test too slow on CPU.' ) @slow def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = {'num_beams': 1, 'early_stopping': True, 'min_length': 15, 'max_length': 25} SCREAMING_SNAKE_CASE_ : str = {'skip_special_tokens': True, 'clean_up_tokenization_spaces': True} SCREAMING_SNAKE_CASE_ : List[str] = FlaxBlenderbotForConditionalGeneration.from_pretrained('facebook/blenderbot-3B' ,from_pt=snake_case__ ) SCREAMING_SNAKE_CASE_ : Tuple = BlenderbotTokenizer.from_pretrained('facebook/blenderbot-3B' ) SCREAMING_SNAKE_CASE_ : List[str] = ['Sam'] SCREAMING_SNAKE_CASE_ : Dict = tokenizer(snake_case__ ,return_tensors='jax' ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = model.generate(**snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : int = 'Sam is a great name. It means "sun" in Gaelic.' SCREAMING_SNAKE_CASE_ : Optional[Any] = tokenizer.batch_decode(snake_case__ ,**snake_case__ ) assert generated_txt[0].strip() == tgt_text
685
import importlib import inspect import json import os import re import shutil import sys from pathlib import Path from typing import Dict, Optional, Union from urllib import request from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info from packaging import version from .. import __version__ from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging UpperCamelCase__ : Dict = ( '''https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py''' ) UpperCamelCase__ : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def __UpperCAmelCase ( ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = 'https://pypi.org/pypi/diffusers/json' SCREAMING_SNAKE_CASE_ : Optional[int] = json.loads(request.urlopen(lowerCamelCase_ ).read() )['releases'].keys() return sorted(lowerCamelCase_ , key=lambda lowerCamelCase_ : version.Version(lowerCamelCase_ ) ) def __UpperCAmelCase ( ) -> Tuple: """simple docstring""" if HF_MODULES_CACHE in sys.path: return sys.path.append(lowerCamelCase_ ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = Path(lowerCamelCase_ ) / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] ) -> Any: """simple docstring""" init_hf_modules() SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ) / name # If the parent module does not exist yet, recursively create it. if not dynamic_module_path.parent.exists(): create_dynamic_module(dynamic_module_path.parent ) os.makedirs(lowerCamelCase_ , exist_ok=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = dynamic_module_path / '__init__.py' if not init_path.exists(): init_path.touch() def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : List[Any] = f.read() # Imports of the form `import .xxx` SCREAMING_SNAKE_CASE_ : Tuple = re.findall('^\s*import\s+\.(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from .xxx import yyy` relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Unique-ify return list(set(lowerCamelCase_ ) ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = False SCREAMING_SNAKE_CASE_ : Union[str, Any] = [module_file] SCREAMING_SNAKE_CASE_ : Tuple = [] # Let's recurse through all relative imports while not no_change: SCREAMING_SNAKE_CASE_ : int = [] for f in files_to_check: new_imports.extend(get_relative_imports(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : int = Path(lowerCamelCase_ ).parent SCREAMING_SNAKE_CASE_ : int = [str(module_path / m ) for m in new_imports] SCREAMING_SNAKE_CASE_ : Union[str, Any] = [f for f in new_import_files if f not in all_relative_imports] SCREAMING_SNAKE_CASE_ : Any = [F'{f}.py' for f in new_import_files] SCREAMING_SNAKE_CASE_ : Optional[int] = len(lowerCamelCase_ ) == 0 all_relative_imports.extend(lowerCamelCase_ ) return all_relative_imports def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" with open(lowerCamelCase_ , 'r' , encoding='utf-8' ) as f: SCREAMING_SNAKE_CASE_ : Optional[int] = f.read() # Imports of the form `import xxx` SCREAMING_SNAKE_CASE_ : List[str] = re.findall('^\s*import\s+(\S+)\s*$' , lowerCamelCase_ , flags=re.MULTILINE ) # Imports of the form `from xxx import yyy` imports += re.findall('^\s*from\s+(\S+)\s+import' , lowerCamelCase_ , flags=re.MULTILINE ) # Only keep the top-level module SCREAMING_SNAKE_CASE_ : List[str] = [imp.split('.' )[0] for imp in imports if not imp.startswith('.' )] # Unique-ify and test we got them all SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(set(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE_ : List[str] = [] for imp in imports: try: importlib.import_module(lowerCamelCase_ ) except ImportError: missing_packages.append(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: raise ImportError( 'This modeling file requires the following packages that were not found in your environment: ' F'{", ".join(lowerCamelCase_ )}. Run `pip install {" ".join(lowerCamelCase_ )}`' ) return get_relative_imports(lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Tuple ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = module_path.replace(os.path.sep , '.' ) SCREAMING_SNAKE_CASE_ : Any = importlib.import_module(lowerCamelCase_ ) if class_name is None: return find_pipeline_class(lowerCamelCase_ ) return getattr(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] ) -> Optional[int]: """simple docstring""" from ..pipelines import DiffusionPipeline SCREAMING_SNAKE_CASE_ : List[Any] = dict(inspect.getmembers(lowerCamelCase_ , inspect.isclass ) ) SCREAMING_SNAKE_CASE_ : List[str] = None for cls_name, cls in cls_members.items(): if ( cls_name != DiffusionPipeline.__name__ and issubclass(cls , lowerCamelCase_ ) and cls.__module__.split('.' )[0] != "diffusers" ): if pipeline_class is not None: raise ValueError( F'Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:' F' {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in' F' {loaded_module}.' ) SCREAMING_SNAKE_CASE_ : Any = cls return pipeline_class def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(lowerCamelCase_ , lowerCamelCase_ ) if os.path.isfile(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = module_file_or_url SCREAMING_SNAKE_CASE_ : Dict = 'local' elif pretrained_model_name_or_path.count('/' ) == 0: SCREAMING_SNAKE_CASE_ : List[str] = get_diffusers_versions() # cut ".dev0" SCREAMING_SNAKE_CASE_ : Dict = 'v' + '.'.join(__version__.split('.' )[:3] ) # retrieve github version that matches if revision is None: SCREAMING_SNAKE_CASE_ : List[Any] = latest_version if latest_version[1:] in available_versions else 'main' logger.info(F'Defaulting to latest_version: {revision}.' ) elif revision in available_versions: SCREAMING_SNAKE_CASE_ : int = F'v{revision}' elif revision == "main": SCREAMING_SNAKE_CASE_ : List[Any] = revision else: raise ValueError( F'`custom_revision`: {revision} does not exist. Please make sure to choose one of' F' {", ".join(available_versions + ["main"] )}.' ) # community pipeline on GitHub SCREAMING_SNAKE_CASE_ : Tuple = COMMUNITY_PIPELINES_URL.format(revision=lowerCamelCase_ , pipeline=lowerCamelCase_ ) try: SCREAMING_SNAKE_CASE_ : Union[str, Any] = cached_download( lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Tuple = 'git' SCREAMING_SNAKE_CASE_ : Dict = pretrained_model_name_or_path + '.py' except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise else: try: # Load from URL or cache if already cached SCREAMING_SNAKE_CASE_ : List[str] = hf_hub_download( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , proxies=lowerCamelCase_ , resume_download=lowerCamelCase_ , local_files_only=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : Optional[int] = os.path.join('local' , '--'.join(pretrained_model_name_or_path.split('/' ) ) ) except EnvironmentError: logger.error(F'Could not locate the {module_file} inside {pretrained_model_name_or_path}.' ) raise # Check we have all the requirements in our environment SCREAMING_SNAKE_CASE_ : Dict = check_imports(lowerCamelCase_ ) # Now we move the module inside our cached dynamic modules. SCREAMING_SNAKE_CASE_ : Union[str, Any] = DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule create_dynamic_module(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = Path(lowerCamelCase_ ) / full_submodule if submodule == "local" or submodule == "git": # We always copy local files (we could hash the file to see if there was a change, and give them the name of # that hash, to only copy when there is a modification but it seems overkill for now). # The only reason we do the copy is to avoid putting too many folders in sys.path. shutil.copy(lowerCamelCase_ , submodule_path / module_file ) for module_needed in modules_needed: SCREAMING_SNAKE_CASE_ : Union[str, Any] = F'{module_needed}.py' shutil.copy(os.path.join(lowerCamelCase_ , lowerCamelCase_ ) , submodule_path / module_needed ) else: # Get the commit hash # TODO: we will get this info in the etag soon, so retrieve it from there and not here. if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = use_auth_token elif use_auth_token is True: SCREAMING_SNAKE_CASE_ : int = HfFolder.get_token() else: SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : List[Any] = model_info(lowerCamelCase_ , revision=lowerCamelCase_ , token=lowerCamelCase_ ).sha # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the # benefit of versioning. SCREAMING_SNAKE_CASE_ : Any = submodule_path / commit_hash SCREAMING_SNAKE_CASE_ : List[Any] = full_submodule + os.path.sep + commit_hash create_dynamic_module(lowerCamelCase_ ) if not (submodule_path / module_file).exists(): shutil.copy(lowerCamelCase_ , submodule_path / module_file ) # Make sure we also have every file with relative for module_needed in modules_needed: if not (submodule_path / module_needed).exists(): get_cached_module_file( lowerCamelCase_ , F'{module_needed}.py' , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return os.path.join(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, os.PathLike] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : Optional[Union[str, os.PathLike]] = None , lowerCamelCase_ : bool = False , lowerCamelCase_ : bool = False , lowerCamelCase_ : Optional[Dict[str, str]] = None , lowerCamelCase_ : Optional[Union[bool, str]] = None , lowerCamelCase_ : Optional[str] = None , lowerCamelCase_ : bool = False , **lowerCamelCase_ : Dict , ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_cached_module_file( lowerCamelCase_ , lowerCamelCase_ , cache_dir=lowerCamelCase_ , force_download=lowerCamelCase_ , resume_download=lowerCamelCase_ , proxies=lowerCamelCase_ , use_auth_token=lowerCamelCase_ , revision=lowerCamelCase_ , local_files_only=lowerCamelCase_ , ) return get_class_in_module(lowerCamelCase_ , final_module.replace('.py' , '' ) )
685
1
def __UpperCAmelCase ( lowerCamelCase_ : int = 2_00 ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = [1, 2, 5, 10, 20, 50, 1_00, 2_00] SCREAMING_SNAKE_CASE_ : Optional[Any] = [0] * (pence + 1) SCREAMING_SNAKE_CASE_ : Union[str, Any] = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(lowerCamelCase_ , pence + 1 , 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(2_00) == 7_36_82
685
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ : Dict = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = { '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''', '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''', '''uclanlp/visualbert-vqa-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''', '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''', '''uclanlp/visualbert-vcr-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json''' ), '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''', '''uclanlp/visualbert-nlvr2-coco-pre''': ( '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json''' ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Optional[int] = "visual_bert" def __init__( self ,snake_case__=30522 ,snake_case__=768 ,snake_case__=512 ,snake_case__=12 ,snake_case__=12 ,snake_case__=3072 ,snake_case__="gelu" ,snake_case__=0.1 ,snake_case__=0.1 ,snake_case__=512 ,snake_case__=2 ,snake_case__=0.02 ,snake_case__=1E-12 ,snake_case__=False ,snake_case__=True ,snake_case__=1 ,snake_case__=0 ,snake_case__=2 ,**snake_case__ ,): super().__init__(pad_token_id=snake_case__ ,bos_token_id=snake_case__ ,eos_token_id=snake_case__ ,**snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = vocab_size SCREAMING_SNAKE_CASE_ : Dict = max_position_embeddings SCREAMING_SNAKE_CASE_ : str = hidden_size SCREAMING_SNAKE_CASE_ : Optional[Any] = visual_embedding_dim SCREAMING_SNAKE_CASE_ : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE_ : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE_ : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE_ : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE_ : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE_ : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ : Optional[int] = initializer_range SCREAMING_SNAKE_CASE_ : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE_ : Optional[Any] = layer_norm_eps SCREAMING_SNAKE_CASE_ : int = bypass_transformer SCREAMING_SNAKE_CASE_ : Optional[Any] = special_visual_initialize
685
1
import os import pytest from datasets import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, ) UpperCamelCase__ : List[Any] = pytest.mark.integration @pytest.mark.parametrize('path' , ['paws', 'csv'] ) def __UpperCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : str ) -> Union[str, Any]: """simple docstring""" inspect_dataset(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = path + '.py' assert script_name in os.listdir(lowerCamelCase_ ) assert "__pycache__" not in os.listdir(lowerCamelCase_ ) @pytest.mark.filterwarnings('ignore:inspect_metric is deprecated:FutureWarning' ) @pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning' ) @pytest.mark.parametrize('path' , ['accuracy'] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Optional[Any] ) -> List[str]: """simple docstring""" inspect_metric(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[str] = path + '.py' assert script_name in os.listdir(lowerCamelCase_ ) assert "__pycache__" not in os.listdir(lowerCamelCase_ ) @pytest.mark.parametrize( 'path, config_name, expected_splits' , [ ('squad', 'plain_text', ['train', 'validation']), ('dalle-mini/wit', 'dalle-mini--wit', ['train']), ('paws', 'labeled_final', ['train', 'test', 'validation']), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Tuple ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = get_dataset_config_info(lowerCamelCase_ , config_name=lowerCamelCase_ ) assert info.config_name == config_name assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( 'path, config_name, expected_exception' , [ ('paws', None, ValueError), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Dict , lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : Union[str, Any] ) -> int: """simple docstring""" with pytest.raises(lowerCamelCase_ ): get_dataset_config_info(lowerCamelCase_ , config_name=lowerCamelCase_ ) @pytest.mark.parametrize( 'path, expected' , [ ('squad', 'plain_text'), ('acronym_identification', 'default'), ('lhoestq/squad', 'plain_text'), ('lhoestq/test', 'default'), ('lhoestq/demo1', 'lhoestq--demo1'), ('dalle-mini/wit', 'dalle-mini--wit'), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : int ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = get_dataset_config_names(lowerCamelCase_ ) assert expected in config_names @pytest.mark.parametrize( 'path, expected_configs, expected_splits_in_first_config' , [ ('squad', ['plain_text'], ['train', 'validation']), ('dalle-mini/wit', ['dalle-mini--wit'], ['train']), ('paws', ['labeled_final', 'labeled_swap', 'unlabeled_final'], ['train', 'test', 'validation']), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : List[Any] , lowerCamelCase_ : Optional[Any] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = get_dataset_infos(lowerCamelCase_ ) assert list(infos.keys() ) == expected_configs SCREAMING_SNAKE_CASE_ : List[Any] = expected_configs[0] assert expected_config in infos SCREAMING_SNAKE_CASE_ : str = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits_in_first_config @pytest.mark.parametrize( 'path, expected_config, expected_splits' , [ ('squad', 'plain_text', ['train', 'validation']), ('dalle-mini/wit', 'dalle-mini--wit', ['train']), ('paws', 'labeled_final', ['train', 'test', 'validation']), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Union[str, Any] , lowerCamelCase_ : str , lowerCamelCase_ : Tuple ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : int = get_dataset_infos(lowerCamelCase_ ) assert expected_config in infos SCREAMING_SNAKE_CASE_ : Optional[Any] = infos[expected_config] assert info.config_name == expected_config assert list(info.splits.keys() ) == expected_splits @pytest.mark.parametrize( 'path, config_name, expected_exception' , [ ('paws', None, ValueError), ] , ) def __UpperCAmelCase ( lowerCamelCase_ : Optional[int] , lowerCamelCase_ : str , lowerCamelCase_ : Optional[Any] ) -> Union[str, Any]: """simple docstring""" with pytest.raises(lowerCamelCase_ ): get_dataset_split_names(lowerCamelCase_ , config_name=lowerCamelCase_ )
685
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def __UpperCAmelCase ( lowerCamelCase_ : int ) -> Union[str, Any]: """simple docstring""" def is_in_circle(lowerCamelCase_ : float , lowerCamelCase_ : float ) -> bool: SCREAMING_SNAKE_CASE_ : Any = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle SCREAMING_SNAKE_CASE_ : Optional[int] = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(lowerCamelCase_ ) ) # The ratio of the area for circle to square is pi/4. SCREAMING_SNAKE_CASE_ : Tuple = proportion * 4 print(F'The estimated value of pi is {pi_estimate}' ) print(F'The numpy value of pi is {pi}' ) print(F'The total error is {abs(pi - pi_estimate )}' ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Callable[[float], float] , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 , ) -> float: """simple docstring""" return mean( function_to_integrate(uniform(lowerCamelCase_ , lowerCamelCase_ ) ) for _ in range(lowerCamelCase_ ) ) * (max_value - min_value) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : float = 0.0 , lowerCamelCase_ : float = 1.0 ) -> None: """simple docstring""" def identity_function(lowerCamelCase_ : float ) -> float: return x SCREAMING_SNAKE_CASE_ : str = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F'Estimating area under y=x where x varies from {min_value} to {max_value}' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {expected_value}' ) print(F'Total error is {abs(estimated_value - expected_value )}' ) print('******************' ) def __UpperCAmelCase ( lowerCamelCase_ : int ) -> None: """simple docstring""" def function_to_integrate(lowerCamelCase_ : float ) -> float: return sqrt(4.0 - x * x ) SCREAMING_SNAKE_CASE_ : Dict = area_under_curve_estimator( lowerCamelCase_ , lowerCamelCase_ , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F'Estimated value is {estimated_value}' ) print(F'Expected value is {pi}' ) print(F'Total error is {abs(estimated_value - pi )}' ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = inspect.getfile(accelerate.test_utils ) SCREAMING_SNAKE_CASE_ : Optional[Any] = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'external_deps', 'test_metrics.py'] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 SCREAMING_SNAKE_CASE_ : List[Any] = test_metrics @require_cpu def snake_case ( self ): debug_launcher(self.test_metrics.main ,num_processes=1 ) @require_cpu def snake_case ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def snake_case ( self ): self.test_metrics.main() @require_multi_gpu def snake_case ( self ): print(F'Found {torch.cuda.device_count()} devices.' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = ['torchrun', F'--nproc_per_node={torch.cuda.device_count()}', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(snake_case__ ,env=os.environ.copy() )
685
import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=3 ,snake_case__=18 ,snake_case__=30 ,snake_case__=400 ,snake_case__=True ,snake_case__=None ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : Union[str, Any] = size if size is not None else {'height': 18, 'width': 18} SCREAMING_SNAKE_CASE_ : str = parent SCREAMING_SNAKE_CASE_ : List[str] = batch_size SCREAMING_SNAKE_CASE_ : Tuple = num_channels SCREAMING_SNAKE_CASE_ : Dict = image_size SCREAMING_SNAKE_CASE_ : Optional[int] = min_resolution SCREAMING_SNAKE_CASE_ : int = max_resolution SCREAMING_SNAKE_CASE_ : Dict = do_resize SCREAMING_SNAKE_CASE_ : Dict = size SCREAMING_SNAKE_CASE_ : str = apply_ocr def snake_case ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def snake_case ( self ): SCREAMING_SNAKE_CASE_ : int = LayoutLMvaImageProcessingTester(self ) @property def snake_case ( self ): return self.image_processor_tester.prepare_image_processor_dict() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case__ ,'do_resize' ) ) self.assertTrue(hasattr(snake_case__ ,'size' ) ) self.assertTrue(hasattr(snake_case__ ,'apply_ocr' ) ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size ,{'height': 18, 'width': 18} ) SCREAMING_SNAKE_CASE_ : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ,size=42 ) self.assertEqual(image_processor.size ,{'height': 42, 'width': 42} ) def snake_case ( self ): pass def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE_ : Union[str, Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ) self.assertEqual( encoding.pixel_values.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) self.assertIsInstance(encoding.words ,snake_case__ ) self.assertIsInstance(encoding.boxes ,snake_case__ ) # Test batched SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE_ : int = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,numpify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[str] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # Initialize image_processing SCREAMING_SNAKE_CASE_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester ,equal_resolution=snake_case__ ,torchify=snake_case__ ) for image in image_inputs: self.assertIsInstance(snake_case__ ,torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE_ : Tuple = image_processing(image_inputs[0] ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) # Test batched SCREAMING_SNAKE_CASE_ : List[Any] = image_processing(snake_case__ ,return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape ,( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) ,) def snake_case ( self ): # with apply_OCR = True SCREAMING_SNAKE_CASE_ : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset SCREAMING_SNAKE_CASE_ : Optional[Any] = load_dataset('hf-internal-testing/fixtures_docvqa' ,split='test' ) SCREAMING_SNAKE_CASE_ : str = Image.open(ds[0]['file'] ).convert('RGB' ) SCREAMING_SNAKE_CASE_ : Any = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) ,len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 SCREAMING_SNAKE_CASE_ : Any = [['11:14', 'to', '11:39', 'a.m', '11:39', 'to', '11:44', 'a.m.', '11:44', 'a.m.', 'to', '12:25', 'p.m.', '12:25', 'to', '12:58', 'p.m.', '12:58', 'to', '4:00', 'p.m.', '2:00', 'to', '5:00', 'p.m.', 'Coffee', 'Break', 'Coffee', 'will', 'be', 'served', 'for', 'men', 'and', 'women', 'in', 'the', 'lobby', 'adjacent', 'to', 'exhibit', 'area.', 'Please', 'move', 'into', 'exhibit', 'area.', '(Exhibits', 'Open)', 'TRRF', 'GENERAL', 'SESSION', '(PART', '|)', 'Presiding:', 'Lee', 'A.', 'Waller', 'TRRF', 'Vice', 'President', '“Introductory', 'Remarks”', 'Lee', 'A.', 'Waller,', 'TRRF', 'Vice', 'Presi-', 'dent', 'Individual', 'Interviews', 'with', 'TRRF', 'Public', 'Board', 'Members', 'and', 'Sci-', 'entific', 'Advisory', 'Council', 'Mem-', 'bers', 'Conducted', 'by', 'TRRF', 'Treasurer', 'Philip', 'G.', 'Kuehn', 'to', 'get', 'answers', 'which', 'the', 'public', 'refrigerated', 'warehousing', 'industry', 'is', 'looking', 'for.', 'Plus', 'questions', 'from', 'the', 'floor.', 'Dr.', 'Emil', 'M.', 'Mrak,', 'University', 'of', 'Cal-', 'ifornia,', 'Chairman,', 'TRRF', 'Board;', 'Sam', 'R.', 'Cecil,', 'University', 'of', 'Georgia', 'College', 'of', 'Agriculture;', 'Dr.', 'Stanley', 'Charm,', 'Tufts', 'University', 'School', 'of', 'Medicine;', 'Dr.', 'Robert', 'H.', 'Cotton,', 'ITT', 'Continental', 'Baking', 'Company;', 'Dr.', 'Owen', 'Fennema,', 'University', 'of', 'Wis-', 'consin;', 'Dr.', 'Robert', 'E.', 'Hardenburg,', 'USDA.', 'Questions', 'and', 'Answers', 'Exhibits', 'Open', 'Capt.', 'Jack', 'Stoney', 'Room', 'TRRF', 'Scientific', 'Advisory', 'Council', 'Meeting', 'Ballroom', 'Foyer']] # noqa: E231 SCREAMING_SNAKE_CASE_ : Any = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words ,snake_case__ ) self.assertListEqual(encoding.boxes ,snake_case__ ) # with apply_OCR = False SCREAMING_SNAKE_CASE_ : Optional[int] = LayoutLMvaImageProcessor(apply_ocr=snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = image_processing(snake_case__ ,return_tensors='pt' ) self.assertEqual(encoding.pixel_values.shape ,(1, 3, 224, 224) )
685
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available UpperCamelCase__ : List[Any] = {'''configuration_speech_encoder_decoder''': ['''SpeechEncoderDecoderConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = ['''SpeechEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : int = ['''FlaxSpeechEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys UpperCamelCase__ : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
1
from collections.abc import Callable import numpy as np def __UpperCAmelCase ( lowerCamelCase_ : Callable , lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float ) -> np.array: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = int(np.ceil((x_end - xa) / step_size ) ) SCREAMING_SNAKE_CASE_ : Tuple = np.zeros((n + 1,) ) SCREAMING_SNAKE_CASE_ : Dict = ya SCREAMING_SNAKE_CASE_ : List[str] = xa for k in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Optional[Any] = y[k] + step_size * ode_func(lowerCamelCase_ , y[k] ) SCREAMING_SNAKE_CASE_ : List[Any] = y[k] + ( (step_size / 2) * (ode_func(lowerCamelCase_ , y[k] ) + ode_func(x + step_size , lowerCamelCase_ )) ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
685
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 1_00 * 2**20, 9_00 * 2**20] ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : int , lowerCamelCase_ : List[Any] ) -> int: """simple docstring""" if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_ : str = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_ : Union[str, Any] = False SCREAMING_SNAKE_CASE_ : List[Any] = is_small_dataset(lowerCamelCase_ ) assert result == expected
685
1
import importlib import math import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Tuple, Union import flax import jax.numpy as jnp from ..utils import BaseOutput UpperCamelCase__ : Any = '''scheduler_config.json''' class lowerCAmelCase_ ( lowerCamelCase_ ): __a : Tuple = 1 __a : Optional[Any] = 2 __a : str = 3 __a : Any = 4 __a : List[str] = 5 @dataclass class lowerCAmelCase_ ( lowerCamelCase_ ): __a : jnp.ndarray class lowerCAmelCase_ : __a : Tuple = SCHEDULER_CONFIG_NAME __a : List[Any] = ["dtype"] __a : str = [] __a : Tuple = True @classmethod def snake_case ( cls ,snake_case__ = None ,snake_case__ = None ,snake_case__=False ,**snake_case__ ,): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = cls.load_config( pretrained_model_name_or_path=snake_case__ ,subfolder=snake_case__ ,return_unused_kwargs=snake_case__ ,**snake_case__ ,) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Tuple = cls.from_config(snake_case__ ,return_unused_kwargs=snake_case__ ,**snake_case__ ) if hasattr(snake_case__ ,'create_state' ) and getattr(snake_case__ ,'has_state' ,snake_case__ ): SCREAMING_SNAKE_CASE_ : List[str] = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def snake_case ( self ,snake_case__ ,snake_case__ = False ,**snake_case__ ): self.save_config(save_directory=snake_case__ ,push_to_hub=snake_case__ ,**snake_case__ ) @property def snake_case ( self ): return self._get_compatibles() @classmethod def snake_case ( cls ): SCREAMING_SNAKE_CASE_ : Optional[Any] = list(set([cls.__name__] + cls._compatibles ) ) SCREAMING_SNAKE_CASE_ : Optional[int] = importlib.import_module(__name__.split('.' )[0] ) SCREAMING_SNAKE_CASE_ : Optional[int] = [ getattr(snake_case__ ,snake_case__ ) for c in compatible_classes_str if hasattr(snake_case__ ,snake_case__ ) ] return compatible_classes def __UpperCAmelCase ( lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : Tuple[int] ) -> jnp.ndarray: """simple docstring""" assert len(lowerCamelCase_ ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(lowerCamelCase_ ) - x.ndim) ) , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : int , lowerCamelCase_ : Optional[int]=0.9_9_9 , lowerCamelCase_ : Optional[Any]=jnp.floataa ) -> jnp.ndarray: """simple docstring""" def alpha_bar(lowerCamelCase_ : str ): return math.cos((time_step + 0.0_0_8) / 1.0_0_8 * math.pi / 2 ) ** 2 SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : Tuple = i / num_diffusion_timesteps SCREAMING_SNAKE_CASE_ : Optional[Any] = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(lowerCamelCase_ ) / alpha_bar(lowerCamelCase_ ) , lowerCamelCase_ ) ) return jnp.array(lowerCamelCase_ , dtype=lowerCamelCase_ ) @flax.struct.dataclass class lowerCAmelCase_ : __a : jnp.ndarray __a : jnp.ndarray __a : jnp.ndarray @classmethod def snake_case ( cls ,snake_case__ ): SCREAMING_SNAKE_CASE_ : int = scheduler.config if config.trained_betas is not None: SCREAMING_SNAKE_CASE_ : Any = jnp.asarray(config.trained_betas ,dtype=scheduler.dtype ) elif config.beta_schedule == "linear": SCREAMING_SNAKE_CASE_ : Dict = jnp.linspace(config.beta_start ,config.beta_end ,config.num_train_timesteps ,dtype=scheduler.dtype ) elif config.beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( jnp.linspace( config.beta_start**0.5 ,config.beta_end**0.5 ,config.num_train_timesteps ,dtype=scheduler.dtype ) ** 2 ) elif config.beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule SCREAMING_SNAKE_CASE_ : int = betas_for_alpha_bar(config.num_train_timesteps ,dtype=scheduler.dtype ) else: raise NotImplementedError( F'beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}' ) SCREAMING_SNAKE_CASE_ : Any = 1.0 - betas SCREAMING_SNAKE_CASE_ : Dict = jnp.cumprod(snake_case__ ,axis=0 ) return cls( alphas=snake_case__ ,betas=snake_case__ ,alphas_cumprod=snake_case__ ,) def __UpperCAmelCase ( lowerCamelCase_ : CommonSchedulerState , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = state.alphas_cumprod SCREAMING_SNAKE_CASE_ : Any = alphas_cumprod[timesteps] ** 0.5 SCREAMING_SNAKE_CASE_ : Dict = sqrt_alpha_prod.flatten() SCREAMING_SNAKE_CASE_ : str = broadcast_to_shape_from_left(lowerCamelCase_ , original_samples.shape ) SCREAMING_SNAKE_CASE_ : Optional[Any] = (1 - alphas_cumprod[timesteps]) ** 0.5 SCREAMING_SNAKE_CASE_ : Dict = sqrt_one_minus_alpha_prod.flatten() SCREAMING_SNAKE_CASE_ : int = broadcast_to_shape_from_left(lowerCamelCase_ , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def __UpperCAmelCase ( lowerCamelCase_ : CommonSchedulerState , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = get_sqrt_alpha_prod(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Dict = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def __UpperCAmelCase ( lowerCamelCase_ : CommonSchedulerState , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray , lowerCamelCase_ : jnp.ndarray ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = get_sqrt_alpha_prod(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : List[Any] = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
685
from math import log from scipy.constants import Boltzmann, physical_constants UpperCamelCase__ : Any = 3_00 # TEMPERATURE (unit = K) def __UpperCAmelCase ( lowerCamelCase_ : float , lowerCamelCase_ : float , lowerCamelCase_ : float , ) -> float: """simple docstring""" if donor_conc <= 0: raise ValueError('Donor concentration should be positive' ) elif acceptor_conc <= 0: raise ValueError('Acceptor concentration should be positive' ) elif intrinsic_conc <= 0: raise ValueError('Intrinsic concentration should be positive' ) elif donor_conc <= intrinsic_conc: raise ValueError( 'Donor concentration should be greater than intrinsic concentration' ) elif acceptor_conc <= intrinsic_conc: raise ValueError( 'Acceptor concentration should be greater than intrinsic concentration' ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
685
1
import itertools import random import unittest import numpy as np from transformers import ASTFeatureExtractor from transformers.testing_utils import require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin UpperCamelCase__ : Union[str, Any] = random.Random() if is_torch_available(): import torch def __UpperCAmelCase ( lowerCamelCase_ : str , lowerCamelCase_ : Optional[Any]=1.0 , lowerCamelCase_ : str=None , lowerCamelCase_ : Optional[int]=None ) -> List[Any]: """simple docstring""" if rng is None: SCREAMING_SNAKE_CASE_ : Union[str, Any] = global_rng SCREAMING_SNAKE_CASE_ : Any = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowerCAmelCase_ ( unittest.TestCase ): def __init__( self ,snake_case__ ,snake_case__=7 ,snake_case__=400 ,snake_case__=2000 ,snake_case__=1 ,snake_case__=0.0 ,snake_case__=16000 ,snake_case__=True ,snake_case__=True ,): SCREAMING_SNAKE_CASE_ : int = parent SCREAMING_SNAKE_CASE_ : List[Any] = batch_size SCREAMING_SNAKE_CASE_ : Any = min_seq_length SCREAMING_SNAKE_CASE_ : List[str] = max_seq_length SCREAMING_SNAKE_CASE_ : Optional[int] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) SCREAMING_SNAKE_CASE_ : List[str] = feature_size SCREAMING_SNAKE_CASE_ : List[str] = padding_value SCREAMING_SNAKE_CASE_ : Dict = sampling_rate SCREAMING_SNAKE_CASE_ : str = return_attention_mask SCREAMING_SNAKE_CASE_ : Any = do_normalize def snake_case ( self ): return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def snake_case ( self ,snake_case__=False ,snake_case__=False ): def _flatten(snake_case__ ): return list(itertools.chain(*snake_case__ ) ) if equal_length: SCREAMING_SNAKE_CASE_ : Tuple = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size SCREAMING_SNAKE_CASE_ : int = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length ,self.max_seq_length ,self.seq_length_diff ) ] if numpify: SCREAMING_SNAKE_CASE_ : str = [np.asarray(snake_case__ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = ASTFeatureExtractor def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = ASTFeatureExtractionTester(self ) def snake_case ( self ): # Tests that all call wrap to encode_plus and batch_encode_plus SCREAMING_SNAKE_CASE_ : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 SCREAMING_SNAKE_CASE_ : Dict = [floats_list((1, x) )[0] for x in range(800 ,1400 ,200 )] SCREAMING_SNAKE_CASE_ : Any = [np.asarray(snake_case__ ) for speech_input in speech_inputs] # Test not batched input SCREAMING_SNAKE_CASE_ : Optional[Any] = feat_extract(speech_inputs[0] ,return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ : List[Any] = feat_extract(np_speech_inputs[0] ,return_tensors='np' ).input_values self.assertTrue(np.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) # Test batched SCREAMING_SNAKE_CASE_ : int = feat_extract(snake_case__ ,padding=snake_case__ ,return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ : int = feat_extract(snake_case__ ,padding=snake_case__ ,return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(snake_case__ ,snake_case__ ): self.assertTrue(np.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) # Test 2-D numpy arrays are batched. SCREAMING_SNAKE_CASE_ : Optional[int] = [floats_list((1, x) )[0] for x in (800, 800, 800)] SCREAMING_SNAKE_CASE_ : str = np.asarray(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = feat_extract(snake_case__ ,return_tensors='np' ).input_values SCREAMING_SNAKE_CASE_ : List[Any] = feat_extract(snake_case__ ,return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(snake_case__ ,snake_case__ ): self.assertTrue(np.allclose(snake_case__ ,snake_case__ ,atol=1E-3 ) ) @require_torch def snake_case ( self ): import torch SCREAMING_SNAKE_CASE_ : int = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) SCREAMING_SNAKE_CASE_ : List[Any] = np.random.rand(100 ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ : List[Any] = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: SCREAMING_SNAKE_CASE_ : List[str] = feature_extractor.pad([{'input_values': inputs}] ,return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) SCREAMING_SNAKE_CASE_ : str = feature_extractor.pad([{'input_values': inputs}] ,return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def snake_case ( self ,snake_case__ ): from datasets import load_dataset SCREAMING_SNAKE_CASE_ : int = load_dataset('hf-internal-testing/librispeech_asr_dummy' ,'clean' ,split='validation' ) # automatic decoding with librispeech SCREAMING_SNAKE_CASE_ : List[str] = ds.sort('id' ).select(range(snake_case__ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] @require_torch def snake_case ( self ): # fmt: off SCREAMING_SNAKE_CASE_ : Dict = torch.tensor( [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776, -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133, -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936, -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869] ) # fmt: on SCREAMING_SNAKE_CASE_ : Tuple = self._load_datasamples(1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ASTFeatureExtractor() SCREAMING_SNAKE_CASE_ : Tuple = feature_extractor(snake_case__ ,return_tensors='pt' ).input_values self.assertEquals(input_values.shape ,(1, 1024, 128) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] ,snake_case__ ,atol=1E-4 ) )
685
class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ ( lowerCamelCase_ ): pass class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ [], [], [], ] def snake_case ( self ,snake_case__ ,snake_case__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError('Maximum queue size is 100' ) self.queues[priority].append(snake_case__ ) except IndexError: raise ValueError('Valid priorities are 0, 1, and 2' ) def snake_case ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError('All queues are empty' ) def __str__( self ): return "\n".join(F'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class lowerCAmelCase_ : def __init__( self ): SCREAMING_SNAKE_CASE_ : List[str] = [] def snake_case ( self ,snake_case__ ): if len(self.queue ) == 100: raise OverFlowError('Maximum queue size is 100' ) self.queue.append(snake_case__ ) def snake_case ( self ): if not self.queue: raise UnderFlowError('The queue is empty' ) else: SCREAMING_SNAKE_CASE_ : List[Any] = min(self.queue ) self.queue.remove(snake_case__ ) return data def __str__( self ): return str(self.queue ) def __UpperCAmelCase ( ) -> str: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = FixedPriorityQueue() fpq.enqueue(0 , 10 ) fpq.enqueue(1 , 70 ) fpq.enqueue(0 , 1_00 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 64 ) fpq.enqueue(0 , 1_28 ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(lowerCamelCase_ ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __UpperCAmelCase ( ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(1_00 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(1_28 ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(lowerCamelCase_ ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
685
1
import gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Optional[int] = KandinskyVaaPipeline __a : List[str] = [ "image_embeds", "negative_image_embeds", ] __a : Union[str, Any] = ["image_embeds", "negative_image_embeds"] __a : List[Any] = [ "generator", "height", "width", "latents", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] __a : Tuple = False @property def snake_case ( self ): return 32 @property def snake_case ( self ): return 32 @property def snake_case ( self ): return self.time_input_dim @property def snake_case ( self ): return self.time_input_dim * 4 @property def snake_case ( self ): return 100 @property def snake_case ( self ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ : List[Any] = { 'in_channels': 4, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } SCREAMING_SNAKE_CASE_ : Union[str, Any] = UNetaDConditionModel(**snake_case__ ) return model @property def snake_case ( self ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def snake_case ( self ): torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ : Tuple = VQModel(**self.dummy_movq_kwargs ) return model def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Any = self.dummy_unet SCREAMING_SNAKE_CASE_ : List[str] = self.dummy_movq SCREAMING_SNAKE_CASE_ : List[Any] = DDIMScheduler( num_train_timesteps=1000 ,beta_schedule='linear' ,beta_start=0.00085 ,beta_end=0.012 ,clip_sample=snake_case__ ,set_alpha_to_one=snake_case__ ,steps_offset=1 ,prediction_type='epsilon' ,thresholding=snake_case__ ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = { 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def snake_case ( self ,snake_case__ ,snake_case__=0 ): SCREAMING_SNAKE_CASE_ : List[str] = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(snake_case__ ) ).to(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = floats_tensor((1, self.text_embedder_hidden_size) ,rng=random.Random(seed + 1 ) ).to( snake_case__ ) if str(snake_case__ ).startswith('mps' ): SCREAMING_SNAKE_CASE_ : int = torch.manual_seed(snake_case__ ) else: SCREAMING_SNAKE_CASE_ : Dict = torch.Generator(device=snake_case__ ).manual_seed(snake_case__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = { 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'guidance_scale': 4.0, 'num_inference_steps': 2, 'output_type': 'np', } return inputs def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Dict = 'cpu' SCREAMING_SNAKE_CASE_ : List[Any] = self.get_dummy_components() SCREAMING_SNAKE_CASE_ : Optional[int] = self.pipeline_class(**snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) SCREAMING_SNAKE_CASE_ : Dict = pipe(**self.get_dummy_inputs(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = output.images SCREAMING_SNAKE_CASE_ : int = pipe( **self.get_dummy_inputs(snake_case__ ) ,return_dict=snake_case__ ,)[0] SCREAMING_SNAKE_CASE_ : Tuple = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ : str = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_ : List[str] = np.array( [0.6237976, 1.0, 0.36441332, 1.0, 0.70639634, 0.29877186, 0.85652125, 0.5216843, 0.54454046] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def snake_case ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case ( self ): SCREAMING_SNAKE_CASE_ : str = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy' ) SCREAMING_SNAKE_CASE_ : int = KandinskyVaaPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-prior' ,torch_dtype=torch.floataa ) pipe_prior.to(snake_case__ ) SCREAMING_SNAKE_CASE_ : List[str] = KandinskyVaaPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-2-decoder' ,torch_dtype=torch.floataa ) SCREAMING_SNAKE_CASE_ : List[Any] = pipeline.to(snake_case__ ) pipeline.set_progress_bar_config(disable=snake_case__ ) SCREAMING_SNAKE_CASE_ : Any = 'red cat, 4k photo' SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.Generator(device='cuda' ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = pipe_prior( snake_case__ ,generator=snake_case__ ,num_inference_steps=5 ,negative_prompt='' ,).to_tuple() SCREAMING_SNAKE_CASE_ : Any = torch.Generator(device='cuda' ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ : List[str] = pipeline( image_embeds=snake_case__ ,negative_image_embeds=snake_case__ ,generator=snake_case__ ,num_inference_steps=100 ,output_type='np' ,) SCREAMING_SNAKE_CASE_ : List[Any] = output.images[0] assert image.shape == (512, 512, 3) assert_mean_pixel_difference(snake_case__ ,snake_case__ )
685
def __UpperCAmelCase ( lowerCamelCase_ : int = 10_00 ) -> int: """simple docstring""" return sum(e for e in range(3 , lowerCamelCase_ ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"""{solution() = }""")
685
1
import math from datetime import datetime, timedelta def __UpperCAmelCase ( lowerCamelCase_ : int ) -> datetime: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = year % 19 SCREAMING_SNAKE_CASE_ : Union[str, Any] = year % 4 SCREAMING_SNAKE_CASE_ : Optional[int] = year % 7 SCREAMING_SNAKE_CASE_ : str = math.floor(year / 1_00 ) SCREAMING_SNAKE_CASE_ : Dict = math.floor((13 + 8 * leap_day_inhibits) / 25 ) SCREAMING_SNAKE_CASE_ : Tuple = leap_day_inhibits / 4 SCREAMING_SNAKE_CASE_ : List[str] = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 SCREAMING_SNAKE_CASE_ : Any = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 SCREAMING_SNAKE_CASE_ : Any = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon SCREAMING_SNAKE_CASE_ : Optional[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(lowerCamelCase_ , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(lowerCamelCase_ , 4 , 18 ) else: return datetime(lowerCamelCase_ , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (19_94, 20_00, 20_10, 20_21, 20_23): UpperCamelCase__ : List[Any] = '''will be''' if year > datetime.now().year else '''was''' print(F"""Easter in {year} {tense} {gauss_easter(year)}""")
685
from ..utils import DummyObject, requires_backends class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Tuple = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[str] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Any = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Union[str, Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : List[Any] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Dict = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : Optional[int] = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) class lowerCAmelCase_ ( metaclass=lowerCamelCase_ ): __a : str = ["flax"] def __init__( self ,*snake_case__ ,**snake_case__ ): requires_backends(self ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] ) @classmethod def snake_case ( cls ,*snake_case__ ,**snake_case__ ): requires_backends(cls ,['flax'] )
685
1
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCamelCase__ : str = logging.getLogger(__name__) @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : str __a : str __a : Optional[str] = None __a : Optional[str] = None __a : Optional[str] = None @dataclass(frozen=lowerCamelCase_ ) class lowerCAmelCase_ : __a : List[int] __a : Optional[List[int]] = None __a : Optional[List[int]] = None __a : Optional[Union[int, float]] = None __a : Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCAmelCase_ ( lowerCamelCase_ ): __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = None ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[Any] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : List[str] = os.path.join( snake_case__ ,'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' ,tokenizer.__class__.__name__ ,str(snake_case__ ) ,snake_case__ ,) ,) SCREAMING_SNAKE_CASE_ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Any = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. SCREAMING_SNAKE_CASE_ : Dict = cached_features_file + '.lock' with FileLock(snake_case__ ): if os.path.exists(snake_case__ ) and not overwrite_cache: logger.info(F'Loading features from cached file {cached_features_file}' ) SCREAMING_SNAKE_CASE_ : Optional[int] = torch.load(snake_case__ ) else: logger.info(F'Creating features from dataset file at {data_dir}' ) SCREAMING_SNAKE_CASE_ : List[Any] = ( processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) ) logger.info('Training examples: %s' ,len(snake_case__ ) ) SCREAMING_SNAKE_CASE_ : List[str] = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) logger.info('Saving features into cached file %s' ,snake_case__ ) torch.save(self.features ,snake_case__ ) def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCAmelCase_ : __a : List[InputFeatures] def __init__( self ,snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ = 128 ,snake_case__=False ,snake_case__ = False ,): SCREAMING_SNAKE_CASE_ : Optional[int] = hans_processors[task]() SCREAMING_SNAKE_CASE_ : Optional[int] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list[2], label_list[1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = label_list SCREAMING_SNAKE_CASE_ : int = processor.get_dev_examples(snake_case__ ) if evaluate else processor.get_train_examples(snake_case__ ) SCREAMING_SNAKE_CASE_ : int = hans_convert_examples_to_features(snake_case__ ,snake_case__ ,snake_case__ ,snake_case__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) ,desc='convert examples to features' ): if ex_index % 10000 == 0: logger.info('Writing example %d of %d' % (ex_index, len(snake_case__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) SCREAMING_SNAKE_CASE_ : List[Any] = tf.data.Dataset.from_generator( snake_case__ ,( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) ,( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) ,) def snake_case ( self ): return self.dataset def __len__( self ): return len(self.features ) def __getitem__( self ,snake_case__ ): return self.features[i] def snake_case ( self ): return self.label_list class lowerCAmelCase_ ( lowerCamelCase_ ): def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_train_set.txt' ) ) ,'train' ) def snake_case ( self ,snake_case__ ): return self._create_examples(self._read_tsv(os.path.join(snake_case__ ,'heuristics_evaluation_set.txt' ) ) ,'dev' ) def snake_case ( self ): return ["contradiction", "entailment", "neutral"] def snake_case ( self ,snake_case__ ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Optional[int] = [] for i, line in enumerate(snake_case__ ): if i == 0: continue SCREAMING_SNAKE_CASE_ : List[str] = '%s-%s' % (set_type, line[0]) SCREAMING_SNAKE_CASE_ : Dict = line[5] SCREAMING_SNAKE_CASE_ : Dict = line[6] SCREAMING_SNAKE_CASE_ : Tuple = line[7][2:] if line[7].startswith('ex' ) else line[7] SCREAMING_SNAKE_CASE_ : Optional[int] = line[0] examples.append(InputExample(guid=snake_case__ ,text_a=snake_case__ ,text_b=snake_case__ ,label=snake_case__ ,pairID=snake_case__ ) ) return examples def __UpperCAmelCase ( lowerCamelCase_ : List[InputExample] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int , lowerCamelCase_ : PreTrainedTokenizer , ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = {label: i for i, label in enumerate(lowerCamelCase_ )} SCREAMING_SNAKE_CASE_ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(lowerCamelCase_ ) , desc='convert examples to features' ): if ex_index % 1_00_00 == 0: logger.info('Writing example %d' % (ex_index) ) SCREAMING_SNAKE_CASE_ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=lowerCamelCase_ , max_length=lowerCamelCase_ , padding='max_length' , truncation=lowerCamelCase_ , return_overflowing_tokens=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE_ : List[Any] = label_map[example.label] if example.label in label_map else 0 SCREAMING_SNAKE_CASE_ : List[str] = int(example.pairID ) features.append(InputFeatures(**lowerCamelCase_ , label=lowerCamelCase_ , pairID=lowerCamelCase_ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F'guid: {example}' ) logger.info(F'features: {features[i]}' ) return features UpperCamelCase__ : str = { '''hans''': 3, } UpperCamelCase__ : Dict = { '''hans''': HansProcessor, }
685
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate UpperCamelCase__ : Union[str, Any] = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('''''', '''|''', '''|'''), datarow=DataRow('''''', '''|''', '''|'''), padding=1, with_header_hide=None, ) UpperCamelCase__ : Dict = [] UpperCamelCase__ : Any = [] UpperCamelCase__ : Optional[Any] = {'''type''': '''section''', '''text''': {'''type''': '''plain_text''', '''text''': '''No failed tests! 🤗''', '''emoji''': True}} UpperCamelCase__ : Any = [ { '''type''': '''header''', '''text''': { '''type''': '''plain_text''', '''text''': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", '''emoji''': True, }, } ] UpperCamelCase__ : Union[str, Any] = 0 for log in Path().glob('''*.log'''): UpperCamelCase__ : Optional[int] = 0 with open(log, '''r''') as f: for line in f: UpperCamelCase__ : Any = json.loads(line) if line.get('''nodeid''', '''''') != "": UpperCamelCase__ : Tuple = line['''nodeid'''] if line.get('''duration''', None) is not None: UpperCamelCase__ : List[Any] = F"""{line["duration"]:.4f}""" if line.get('''outcome''', '''''') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('''_''')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) UpperCamelCase__ : Tuple = [] log.unlink() UpperCamelCase__ : List[Any] = '''''' UpperCamelCase__ : List[str] = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" UpperCamelCase__ : List[Any] = [] UpperCamelCase__ : Optional[int] = {} for test in failed_tests: UpperCamelCase__ : str = test[0].split('''::''') UpperCamelCase__ : List[Any] = data[0].split('''/''')[-1] if data[0] not in filesafailed: UpperCamelCase__ : int = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) UpperCamelCase__ : str = [test[0] for test in failed_table] UpperCamelCase__ : Union[str, Any] = list(set(files)) # Count number of instances in failed_tests UpperCamelCase__ : Dict = [] for file in individual_files: table.append([file, len(filesafailed[file])]) UpperCamelCase__ : str = tabulate( table, headers=['''Test Location''', '''Num Failed'''], tablefmt=hf_table_format, stralign='''right''', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: UpperCamelCase__ : List[Any] = '''Too many failed tests, please see the full report in the Action results.''' UpperCamelCase__ : Optional[Any] = len(err) + 10 UpperCamelCase__ : List[str] = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: UpperCamelCase__ : Optional[Any] = '''No failed tests! 🤗''' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('''TEST_TYPE''', '''''') != "": from slack_sdk import WebClient UpperCamelCase__ : int = WebClient(token=os.environ['''SLACK_API_TOKEN''']) if message != "No failed tests! 🤗": UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': message, }, } payload.append(md_report) UpperCamelCase__ : Optional[int] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': '''*For more details:*''', }, '''accessory''': { '''type''': '''button''', '''text''': { '''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True, }, '''url''': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) UpperCamelCase__ : Optional[Any] = { '''type''': '''context''', '''elements''': [ { '''type''': '''plain_text''', '''text''': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) UpperCamelCase__ : Tuple = client.chat_postMessage(channel='''#accelerate-ci-daily''', text=message, blocks=payload) UpperCamelCase__ : Any = response.data['''ts'''] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name UpperCamelCase__ : int = '''''' for i, row in enumerate(test_failures): if row[0] != test_class: UpperCamelCase__ : str = row[0] else: UpperCamelCase__ : str = '''''' UpperCamelCase__ : Optional[Any] = { '''type''': '''section''', '''text''': { '''type''': '''mrkdwn''', '''text''': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='''#accelerate-ci-daily''', thread_ts=ts, blocks=[payload], )
685
1
import os import unittest from transformers import FunnelTokenizer, FunnelTokenizerFast from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCAmelCase_ ( lowerCamelCase_ , unittest.TestCase ): __a : Union[str, Any] = FunnelTokenizer __a : int = FunnelTokenizerFast __a : List[str] = True __a : int = True def snake_case ( self ): super().setUp() SCREAMING_SNAKE_CASE_ : str = [ '<unk>', '<cls>', '<sep>', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file ,'w' ,encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def snake_case ( self ,**snake_case__ ): return FunnelTokenizer.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,**snake_case__ ): return FunnelTokenizerFast.from_pretrained(self.tmpdirname ,**snake_case__ ) def snake_case ( self ,snake_case__ ): SCREAMING_SNAKE_CASE_ : Dict = 'UNwant\u00E9d,running' SCREAMING_SNAKE_CASE_ : List[str] = 'unwanted, running' return input_text, output_text def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Tuple = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE_ : List[Any] = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(snake_case__ ,['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) ,[7, 4, 5, 10, 8, 9] ) def snake_case ( self ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = self.get_tokenizers(do_lower_case=snake_case__ ) for tokenizer in tokenizers: SCREAMING_SNAKE_CASE_ : Dict = tokenizer('UNwant\u00E9d,running' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = len(inputs['input_ids'] ) - 1 self.assertListEqual(inputs['token_type_ids'] ,[2] + [0] * sentence_len ) SCREAMING_SNAKE_CASE_ : Any = tokenizer('UNwant\u00E9d,running' ,'UNwant\u00E9d,running' ) self.assertListEqual(inputs['token_type_ids'] ,[2] + [0] * sentence_len + [1] * sentence_len )
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> int: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise TypeError('Input value must be an \'int\' type' ) SCREAMING_SNAKE_CASE_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
685
1
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging UpperCamelCase__ : List[Any] = logging.get_logger(__name__) def __UpperCAmelCase ( lowerCamelCase_ : Optional[Any] , lowerCamelCase_ : List[Any] ) -> Tuple: """simple docstring""" try: with open(lowerCamelCase_ , 'rb' ) as flax_state_f: SCREAMING_SNAKE_CASE_ : Dict = from_bytes(lowerCamelCase_ , flax_state_f.read() ) except UnpicklingError as e: try: with open(lowerCamelCase_ ) as f: if f.read().startswith('version' ): raise OSError( 'You seem to have cloned a repository without having git-lfs installed. Please' ' install git-lfs and run `git lfs install` followed by `git lfs pull` in the' ' folder you cloned.' ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(lowerCamelCase_ , lowerCamelCase_ ) def __UpperCAmelCase ( lowerCamelCase_ : List[Any] , lowerCamelCase_ : Optional[Any] ) -> int: """simple docstring""" try: import torch # noqa: F401 except ImportError: logger.error( 'Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see' ' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation' ' instructions.' ) raise # check if we have bf16 weights SCREAMING_SNAKE_CASE_ : Tuple = flatten_dict(jax.tree_util.tree_map(lambda lowerCamelCase_ : x.dtype == jnp.bfloataa , lowerCamelCase_ ) ).values() if any(lowerCamelCase_ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( 'Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ' 'before loading those in PyTorch model.' ) SCREAMING_SNAKE_CASE_ : str = jax.tree_util.tree_map( lambda lowerCamelCase_ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = '' SCREAMING_SNAKE_CASE_ : Union[str, Any] = flatten_dict(lowerCamelCase_ , sep='.' ) SCREAMING_SNAKE_CASE_ : Dict = pt_model.state_dict() # keep track of unexpected & missing keys SCREAMING_SNAKE_CASE_ : Union[str, Any] = [] SCREAMING_SNAKE_CASE_ : int = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): SCREAMING_SNAKE_CASE_ : Optional[Any] = flax_key_tuple.split('.' ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: SCREAMING_SNAKE_CASE_ : str = flax_key_tuple_array[:-1] + ['weight'] SCREAMING_SNAKE_CASE_ : Any = jnp.transpose(lowerCamelCase_ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": SCREAMING_SNAKE_CASE_ : Dict = flax_key_tuple_array[:-1] + ['weight'] SCREAMING_SNAKE_CASE_ : List[str] = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": SCREAMING_SNAKE_CASE_ : int = flax_key_tuple_array[:-1] + ['weight'] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE_ : List[str] = ( flax_key_tuple_string.replace('_0' , '.0' ) .replace('_1' , '.1' ) .replace('_2' , '.2' ) .replace('_3' , '.3' ) .replace('_4' , '.4' ) .replace('_5' , '.5' ) .replace('_6' , '.6' ) .replace('_7' , '.7' ) .replace('_8' , '.8' ) .replace('_9' , '.9' ) ) SCREAMING_SNAKE_CASE_ : int = '.'.join(lowerCamelCase_ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict SCREAMING_SNAKE_CASE_ : int = np.asarray(lowerCamelCase_ ) if not isinstance(lowerCamelCase_ , np.ndarray ) else flax_tensor SCREAMING_SNAKE_CASE_ : List[Any] = torch.from_numpy(lowerCamelCase_ ) # remove from missing keys missing_keys.remove(lowerCamelCase_ ) else: # weight is not expected by PyTorch model unexpected_keys.append(lowerCamelCase_ ) pt_model.load_state_dict(lowerCamelCase_ ) # re-transform missing_keys to list SCREAMING_SNAKE_CASE_ : Dict = list(lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: logger.warning( 'Some weights of the Flax model were not used when initializing the PyTorch model' F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' ' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This' F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' ' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a' ' FlaxBertForSequenceClassification model).' ) if len(lowerCamelCase_ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' ' use it for predictions and inference.' ) return pt_model
685
import qiskit def __UpperCAmelCase ( lowerCamelCase_ : int = 2 ) -> qiskit.result.counts.Counts: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = qubits # Using Aer's simulator SCREAMING_SNAKE_CASE_ : Optional[int] = qiskit.Aer.get_backend('aer_simulator' ) # Creating a Quantum Circuit acting on the q register SCREAMING_SNAKE_CASE_ : str = qiskit.QuantumCircuit(lowerCamelCase_ , lowerCamelCase_ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , lowerCamelCase_ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , lowerCamelCase_ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(lowerCamelCase_ ) ) , list(range(lowerCamelCase_ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator SCREAMING_SNAKE_CASE_ : Tuple = qiskit.execute(lowerCamelCase_ , lowerCamelCase_ , shots=10_00 ) return job.result().get_counts(lowerCamelCase_ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
685
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: UpperCamelCase__ : Tuple = None UpperCamelCase__ : Any = logging.get_logger(__name__) UpperCamelCase__ : Optional[int] = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase__ : Optional[Any] = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json''', }, } UpperCamelCase__ : Optional[Any] = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } UpperCamelCase__ : Union[str, Any] = '''▁''' # Segments (not really needed) UpperCamelCase__ : Optional[Any] = 0 UpperCamelCase__ : Optional[Any] = 1 UpperCamelCase__ : List[Any] = 2 UpperCamelCase__ : Optional[int] = 3 UpperCamelCase__ : str = 4 class lowerCAmelCase_ ( lowerCamelCase_ ): __a : str = VOCAB_FILES_NAMES __a : str = PRETRAINED_VOCAB_FILES_MAP __a : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __a : Optional[Any] = "left" __a : Tuple = XLNetTokenizer def __init__( self ,snake_case__=None ,snake_case__=None ,snake_case__=False ,snake_case__=True ,snake_case__=False ,snake_case__="<s>" ,snake_case__="</s>" ,snake_case__="<unk>" ,snake_case__="<sep>" ,snake_case__="<pad>" ,snake_case__="<cls>" ,snake_case__="<mask>" ,snake_case__=["<eop>", "<eod>"] ,**snake_case__ ,): # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_ : List[Any] = AddedToken(snake_case__ ,lstrip=snake_case__ ,rstrip=snake_case__ ) if isinstance(snake_case__ ,snake_case__ ) else mask_token super().__init__( vocab_file=snake_case__ ,tokenizer_file=snake_case__ ,do_lower_case=snake_case__ ,remove_space=snake_case__ ,keep_accents=snake_case__ ,bos_token=snake_case__ ,eos_token=snake_case__ ,unk_token=snake_case__ ,sep_token=snake_case__ ,pad_token=snake_case__ ,cls_token=snake_case__ ,mask_token=snake_case__ ,additional_special_tokens=snake_case__ ,**snake_case__ ,) SCREAMING_SNAKE_CASE_ : Optional[int] = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = do_lower_case SCREAMING_SNAKE_CASE_ : Optional[Any] = remove_space SCREAMING_SNAKE_CASE_ : List[Any] = keep_accents SCREAMING_SNAKE_CASE_ : List[str] = vocab_file SCREAMING_SNAKE_CASE_ : Tuple = False if not self.vocab_file else True def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : int = [self.sep_token_id] SCREAMING_SNAKE_CASE_ : Tuple = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def snake_case ( self ,snake_case__ ,snake_case__ = None ): SCREAMING_SNAKE_CASE_ : str = [self.sep_token_id] SCREAMING_SNAKE_CASE_ : Tuple = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def snake_case ( self ,snake_case__ ,snake_case__ = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(snake_case__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return SCREAMING_SNAKE_CASE_ : Union[str, Any] = os.path.join( snake_case__ ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ): copyfile(self.vocab_file ,snake_case__ ) return (out_vocab_file,)
685
def __UpperCAmelCase ( lowerCamelCase_ : int ) -> bool: """simple docstring""" if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise ValueError('check_bouncy() accepts only integer arguments' ) SCREAMING_SNAKE_CASE_ : Optional[int] = str(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ : str = ''.join(sorted(lowerCamelCase_ ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __UpperCAmelCase ( lowerCamelCase_ : float = 99 ) -> int: """simple docstring""" if not 0 < percent < 1_00: raise ValueError('solution() only accepts values from 0 to 100' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = 0 SCREAMING_SNAKE_CASE_ : Dict = 1 while True: if check_bouncy(lowerCamelCase_ ): bouncy_num += 1 if (bouncy_num / num) * 1_00 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(99)}""")
685
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase__ : Optional[int] = { '''configuration_vision_text_dual_encoder''': ['''VisionTextDualEncoderConfig'''], '''processing_vision_text_dual_encoder''': ['''VisionTextDualEncoderProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Tuple = ['''VisionTextDualEncoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[str] = ['''FlaxVisionTextDualEncoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : str = ['''TFVisionTextDualEncoderModel'''] if TYPE_CHECKING: from .configuration_vision_text_dual_encoder import VisionTextDualEncoderConfig from .processing_vision_text_dual_encoder import VisionTextDualEncoderProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_text_dual_encoder import VisionTextDualEncoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_text_dual_encoder import FlaxVisionTextDualEncoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_text_dual_encoder import TFVisionTextDualEncoderModel else: import sys UpperCamelCase__ : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
685
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase__ : Dict = { '''configuration_chinese_clip''': [ '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ChineseCLIPConfig''', '''ChineseCLIPOnnxConfig''', '''ChineseCLIPTextConfig''', '''ChineseCLIPVisionConfig''', ], '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Any = ['''ChineseCLIPFeatureExtractor'''] UpperCamelCase__ : Optional[int] = ['''ChineseCLIPImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : List[Any] = [ '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ChineseCLIPModel''', '''ChineseCLIPPreTrainedModel''', '''ChineseCLIPTextModel''', '''ChineseCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_chinese_clip import ( CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, ChineseCLIPConfig, ChineseCLIPOnnxConfig, ChineseCLIPTextConfig, ChineseCLIPVisionConfig, ) from .processing_chinese_clip import ChineseCLIPProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_chinese_clip import ( CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST, ChineseCLIPModel, ChineseCLIPPreTrainedModel, ChineseCLIPTextModel, ChineseCLIPVisionModel, ) else: import sys UpperCamelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
685
1