code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class __A ( unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Tuple = JukeboxTokenizer __lowerCamelCase : Dict = { 'artist': 'Zac Brown Band', 'genres': 'Country', 'lyrics': 'I met a traveller from an antique land,\n Who said "Two vast and trunkless legs of stone\n Stand in the desert. . . . Near them, on the sand,\n Half sunk a shattered visage lies, whose frown,\n And wrinkled lip, and sneer of cold command,\n Tell that its sculptor well those passions read\n Which yet survive, stamped on these lifeless things,\n The hand that mocked them, and the heart that fed;\n And on the pedestal, these words appear:\n My name is Ozymandias, King of Kings;\n Look on my Works, ye Mighty, and despair!\n Nothing beside remains. Round the decay\n Of that colossal Wreck, boundless and bare\n The lone and level sands stretch far away\n ', } @require_torch def a__ (self ) -> Dict: """simple docstring""" import torch _a = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) _a = tokenizer(**self.metas )['''input_ids'''] # fmt: off _a = [ torch.tensor([[ 0, 0, 0, 7_169, 507, 9, 76, 39, 31, 46, 76, 27, 76, 46, 44, 27, 48, 31, 38, 38, 31, 44, 76, 32, 44, 41, 39, 76, 27, 40, 76, 27, 40, 46, 35, 43, 47, 31, 76, 38, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 41, 76, 45, 27, 35, 30, 76, 71, 20, 49, 41, 76, 48, 27, 45, 46, 76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33, 45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 19, 46, 27, 40, 30, 76, 35, 40, 76, 46, 34, 31, 76, 30, 31, 45, 31, 44, 46, 63, 76, 63, 76, 63, 76, 63, 76, 14, 31, 27, 44, 76, 46, 34, 31, 39, 64, 76, 41, 40, 76, 46, 34, 31, 76, 45, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 8, 27, 38, 32, 76, 45, 47, 40, 37, 76, 27, 76, 45, 34, 27, 46, 46, 31, 44, 31, 30, 76, 48, 35, 45, 27, 33, 31, 76, 38, 35, 31, 45, 64, 76, 49, 34, 41, 45, 31, 76, 32, 44, 41, 49, 40, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 49, 44, 35, 40, 37, 38, 31, 30, 76, 38, 35, 42, 64, 76, 27, 40, 30, 76, 45, 40, 31, 31, 44, 76, 41, 32, 76, 29, 41, 38, 30, 76, 29, 41, 39, 39, 27, 40, 30, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 31, 38, 38, 76, 46, 34, 27, 46, 76, 35, 46, 45, 76, 45, 29, 47, 38, 42, 46, 41, 44, 76, 49, 31, 38, 38, 76, 46, 34, 41, 45, 31, 76, 42, 27, 45, 45, 35, 41, 40, 45, 76, 44, 31, 27, 30, 78, 76, 76, 76, 76, 76, 76, 76, 76, 23, 34, 35, 29, 34, 76, 51, 31, 46, 76, 45, 47, 44, 48, 35, 48, 31, 64, 76, 45, 46, 27, 39, 42, 31, 30, 76, 41, 40, 76, 46, 34, 31, 45, 31, 76, 38, 35, 32, 31, 38, 31, 45, 45, 76, 46, 34, 35, 40, 33, 45, 64, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 34, 27, 40, 30, 76, 46, 34, 27, 46, 76, 39, 41, 29, 37, 31, 30, 76, 46, 34, 31, 39, 64, 76, 27, 40, 30, 76, 46, 34, 31, 76, 34, 31, 27, 44, 46, 76, 46, 34, 27, 46, 76, 32, 31, 30, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 1, 40, 30, 76, 41, 40, 76, 46, 34, 31, 76, 42, 31, 30, 31, 45, 46, 27, 38, 64, 76, 46, 34, 31, 45, 31, 76, 49, 41, 44, 30, 45, 76, 27, 42, 42, 31, 27, 44, 65, 78, 76, 76, 76, 76, 76, 76, 76, 76, 13, 51, 76, 40, 27, 39, 31, 76, 35, 45, 76, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 76, 11, 35, 40, 33, 76, 41, 32, 76, 11, 35, 40, 33, 45, 66, 78, 76, 76, 76, 76, 76, 76, 76, 76, 12, 41, 41, 37, 76, 41, 40, 76, 39, 51, 76, 23, 41, 44, 37, 45, 64, 76, 51, 31, 76, 13, 35, 33, 34, 46, 51, 64, 76, 27, 40, 30, 76, 30, 31, 45, 42, 27, 35, 44, 67, 78, 76, 76, 76, 76, 76, 76, 76, 76, 14, 41, 46, 34, 35, 40, 33, 76, 28, 31, 45, 35, 30, 31, 76, 44, 31, 39, 27, 35, 40, 45, 63, 76, 18, 41, 47, 40, 30, 76, 46, 34, 31, 76, 30, 31, 29, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76, 15, 32, 76, 46, 34, 27, 46, 76, 29, 41, 38, 41, 45, 45, 27, 38, 76, 23, 44, 31, 29, 37, 64, 76, 28, 41, 47, 40, 30, 38, 31, 45, 45, 76, 27, 40, 30, 76, 28, 27, 44, 31, 78, 76, 76, 76, 76, 76, 76, 76, 76, 20, 34, 31, 76, 38, 41, 40, 31, 76, 27, 40, 30, 76, 38, 31, 48, 31, 38, 76, 45, 27, 40, 30, 45, 76, 45, 46, 44, 31, 46, 29, 34, 76, 32, 27, 44, 76, 27, 49, 27, 51, 78, 76, 76, 76, 76, 76, 76, 76, 76]] ), torch.tensor([[0, 0, 0, 1_069, 11]] ), torch.tensor([[0, 0, 0, 1_069, 11]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def a__ (self ) -> Optional[int]: """simple docstring""" import torch _a = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) _a = tokenizer(**self.metas )['''input_ids'''] # fmt: off _a = [ torch.tensor([[ 0, 0, 0, 1_069, 11, -1, -1, -1, -1, 9, 77, 39, 31, 46, 77, 27, 77, 46, 44, 27, 48, 31, 38, 38, 31, 44, 77, 32, 44, 41, 39, 77, 27, 40, 77, 27, 40, 46, 35, 43, 47, 31, 77, 38, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 41, 77, 45, 27, 35, 30, 77, 72, 20, 49, 41, 77, 48, 27, 45, 46, 77, 27, 40, 30, 77, 46, 44, 47, 40, 37, 38, 31, 45, 45, 77, 38, 31, 33, 45, 77, 41, 32, 77, 45, 46, 41, 40, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 19, 46, 27, 40, 30, 77, 35, 40, 77, 46, 34, 31, 77, 30, 31, 45, 31, 44, 46, 63, 77, 63, 77, 63, 77, 63, 77, 14, 31, 27, 44, 77, 46, 34, 31, 39, 64, 77, 41, 40, 77, 46, 34, 31, 77, 45, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 8, 27, 38, 32, 77, 45, 47, 40, 37, 77, 27, 77, 45, 34, 27, 46, 46, 31, 44, 31, 30, 77, 48, 35, 45, 27, 33, 31, 77, 38, 35, 31, 45, 64, 77, 49, 34, 41, 45, 31, 77, 32, 44, 41, 49, 40, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 49, 44, 35, 40, 37, 38, 31, 30, 77, 38, 35, 42, 64, 77, 27, 40, 30, 77, 45, 40, 31, 31, 44, 77, 41, 32, 77, 29, 41, 38, 30, 77, 29, 41, 39, 39, 27, 40, 30, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 31, 38, 38, 77, 46, 34, 27, 46, 77, 35, 46, 45, 77, 45, 29, 47, 38, 42, 46, 41, 44, 77, 49, 31, 38, 38, 77, 46, 34, 41, 45, 31, 77, 42, 27, 45, 45, 35, 41, 40, 45, 77, 44, 31, 27, 30, 79, 77, 77, 77, 77, 77, 77, 77, 77, 23, 34, 35, 29, 34, 77, 51, 31, 46, 77, 45, 47, 44, 48, 35, 48, 31, 64, 77, 45, 46, 27, 39, 42, 31, 30, 77, 41, 40, 77, 46, 34, 31, 45, 31, 77, 38, 35, 32, 31, 38, 31, 45, 45, 77, 46, 34, 35, 40, 33, 45, 64, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 34, 27, 40, 30, 77, 46, 34, 27, 46, 77, 39, 41, 29, 37, 31, 30, 77, 46, 34, 31, 39, 64, 77, 27, 40, 30, 77, 46, 34, 31, 77, 34, 31, 27, 44, 46, 77, 46, 34, 27, 46, 77, 32, 31, 30, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 1, 40, 30, 77, 41, 40, 77, 46, 34, 31, 77, 42, 31, 30, 31, 45, 46, 27, 38, 64, 77, 46, 34, 31, 45, 31, 77, 49, 41, 44, 30, 45, 77, 27, 42, 42, 31, 27, 44, 65, 79, 77, 77, 77, 77, 77, 77, 77, 77, 13, 51, 77, 40, 27, 39, 31, 77, 35, 45, 77, 15, 52, 51, 39, 27, 40, 30, 35, 27, 45, 64, 77, 11, 35, 40, 33, 77, 41, 32, 77, 11, 35, 40, 33, 45, 66, 79, 77, 77, 77, 77, 77, 77, 77, 77, 12, 41, 41, 37, 77, 41, 40, 77, 39, 51, 77, 23, 41, 44, 37, 45, 64, 77, 51, 31, 77, 13, 35, 33, 34, 46, 51, 64, 77, 27, 40, 30, 77, 30, 31, 45, 42, 27, 35, 44, 67, 79, 77, 77, 77, 77, 77, 77, 77, 77, 14, 41, 46, 34, 35, 40, 33, 77, 28, 31, 45, 35, 30, 31, 77, 44, 31, 39, 27, 35, 40, 45, 63, 77, 18, 41, 47, 40, 30, 77, 46, 34, 31, 77, 30, 31, 29, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77, 15, 32, 77, 46, 34, 27, 46, 77, 29, 41, 38, 41, 45, 45, 27, 38, 77, 23, 44, 31, 29, 37, 64, 77, 28, 41, 47, 40, 30, 38, 31, 45, 45, 77, 27, 40, 30, 77, 28, 27, 44, 31, 79, 77, 77, 77, 77, 77, 77, 77, 77, 20, 34, 31, 77, 38, 41, 40, 31, 77, 27, 40, 30, 77, 38, 31, 48, 31, 38, 77, 45, 27, 40, 30, 45, 77, 45, 46, 44, 31, 46, 29, 34, 77, 32, 27, 44, 77, 27, 49, 27, 51, 79, 77, 77, 77, 77, 77, 77, 77, 77]] ), torch.tensor([[0, 0, 0, 1_069, 11, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1_069, 11, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' import unittest from typing import Dict, List, Optional, Union 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 BridgeTowerImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A = True , A = None , A = 32 , A = True , A = 1 / 255 , A = True , A = True , A = [0.48145466, 0.4578275, 0.40821073] , A = [0.26862954, 0.26130258, 0.27577711] , A = True , A=7 , A=30 , A=400 , A=3 , ) -> Optional[int]: """simple docstring""" _a = parent _a = do_resize _a = size if size is not None else {'''shortest_edge''': 288} _a = size_divisor _a = do_rescale _a = rescale_factor _a = do_normalize _a = do_center_crop _a = image_mean _a = image_std _a = do_pad _a = batch_size _a = num_channels _a = min_resolution _a = max_resolution def a__ (self ) -> Optional[int]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def a__ (self , A , A=False ) -> Optional[Any]: """simple docstring""" if not batched: _a = self.size['''shortest_edge'''] _a = image_inputs[0] if isinstance(A , Image.Image ): _a , _a = image.size else: _a , _a = image.shape[1], image.shape[2] _a = size / min(A , A ) if h < w: _a , _a = size, scale * w else: _a , _a = scale * h, size _a = int((1_333 / 800) * size ) if max(A , A ) > max_size: _a = max_size / max(A , A ) _a = newh * scale _a = neww * scale _a , _a = int(newh + 0.5 ), int(neww + 0.5 ) _a , _a = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: _a = [] for image in image_inputs: _a , _a = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) _a = max(A , key=lambda A : item[0] )[0] _a = max(A , key=lambda A : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = BridgeTowerImageProcessor if is_vision_available() else None def a__ (self ) -> List[str]: """simple docstring""" _a = BridgeTowerImageProcessingTester(self ) @property def a__ (self ) -> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Tuple: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) self.assertTrue(hasattr(A , '''size_divisor''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" pass def a__ (self ) -> Tuple: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A , batched=A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def a__ (self ) -> List[str]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A , batched=A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched _a = image_processing(A , return_tensors='''pt''' ).pixel_values _a , _a = self.image_processor_tester.get_expected_values(A , batched=A ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { "configuration_nllb_moe": [ "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP", "NllbMoeConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST", "NllbMoeForConditionalGeneration", "NllbMoeModel", "NllbMoePreTrainedModel", "NllbMoeTop2Router", "NllbMoeSparseMLP", ] if TYPE_CHECKING: from .configuration_nllb_moe import ( NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP, NllbMoeConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nllb_moe import ( NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST, NllbMoeForConditionalGeneration, NllbMoeModel, NllbMoePreTrainedModel, NllbMoeSparseMLP, NllbMoeTopaRouter, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import contextlib import copy import random from typing import Any, Dict, Iterable, Optional, Union import numpy as np import torch from .utils import deprecate, is_transformers_available if is_transformers_available(): import transformers def lowerCAmelCase (__A): """simple docstring""" random.seed(__A) np.random.seed(__A) torch.manual_seed(__A) torch.cuda.manual_seed_all(__A) # ^^ safe to call this function even if cuda is not available class __A : '''simple docstring''' def __init__(self , A , A = 0.9999 , A = 0.0 , A = 0 , A = False , A = 1.0 , A = 2 / 3 , A = None , A = None , **A , ) -> List[str]: """simple docstring""" if isinstance(A , torch.nn.Module ): _a = ( '''Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. ''' '''Please pass the parameters of the module instead.''' ) deprecate( '''passing a `torch.nn.Module` to `ExponentialMovingAverage`''' , '''1.0.0''' , A , standard_warn=A , ) _a = parameters.parameters() # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility _a = True if kwargs.get('''max_value''' , A ) is not None: _a = '''The `max_value` argument is deprecated. Please use `decay` instead.''' deprecate('''max_value''' , '''1.0.0''' , A , standard_warn=A ) _a = kwargs['''max_value'''] if kwargs.get('''min_value''' , A ) is not None: _a = '''The `min_value` argument is deprecated. Please use `min_decay` instead.''' deprecate('''min_value''' , '''1.0.0''' , A , standard_warn=A ) _a = kwargs['''min_value'''] _a = list(A ) _a = [p.clone().detach() for p in parameters] if kwargs.get('''device''' , A ) is not None: _a = '''The `device` argument is deprecated. Please use `to` instead.''' deprecate('''device''' , '''1.0.0''' , A , standard_warn=A ) self.to(device=kwargs['''device'''] ) _a = None _a = decay _a = min_decay _a = update_after_step _a = use_ema_warmup _a = inv_gamma _a = power _a = 0 _a = None # set in `step()` _a = model_cls _a = model_config @classmethod def a__ (cls , A , A ) -> "EMAModel": """simple docstring""" _a , _a = model_cls.load_config(A , return_unused_kwargs=A ) _a = model_cls.from_pretrained(A ) _a = cls(model.parameters() , model_cls=A , model_config=model.config ) ema_model.load_state_dict(A ) return ema_model def a__ (self , A ) -> Tuple: """simple docstring""" if self.model_cls is None: raise ValueError('''`save_pretrained` can only be used if `model_cls` was defined at __init__.''' ) if self.model_config is None: raise ValueError('''`save_pretrained` can only be used if `model_config` was defined at __init__.''' ) _a = self.model_cls.from_config(self.model_config ) _a = self.state_dict() state_dict.pop('''shadow_params''' , A ) model.register_to_config(**A ) self.copy_to(model.parameters() ) model.save_pretrained(A ) def a__ (self , A ) -> float: """simple docstring""" _a = max(0 , optimization_step - self.update_after_step - 1 ) if step <= 0: return 0.0 if self.use_ema_warmup: _a = 1 - (1 + step / self.inv_gamma) ** -self.power else: _a = (1 + step) / (10 + step) _a = min(A , self.decay ) # make sure decay is not smaller than min_decay _a = max(A , self.min_decay ) return cur_decay_value @torch.no_grad() def a__ (self , A ) -> Optional[Any]: """simple docstring""" if isinstance(A , torch.nn.Module ): _a = ( '''Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. ''' '''Please pass the parameters of the module instead.''' ) deprecate( '''passing a `torch.nn.Module` to `ExponentialMovingAverage.step`''' , '''1.0.0''' , A , standard_warn=A , ) _a = parameters.parameters() _a = list(A ) self.optimization_step += 1 # Compute the decay factor for the exponential moving average. _a = self.get_decay(self.optimization_step ) _a = decay _a = 1 - decay _a = contextlib.nullcontext if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): import deepspeed for s_param, param in zip(self.shadow_params , A ): if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): _a = deepspeed.zero.GatheredParameters(A , modifier_rank=A ) with context_manager(): if param.requires_grad: s_param.sub_(one_minus_decay * (s_param - param) ) else: s_param.copy_(A ) def a__ (self , A ) -> None: """simple docstring""" _a = list(A ) for s_param, param in zip(self.shadow_params , A ): param.data.copy_(s_param.to(param.device ).data ) def a__ (self , A=None , A=None ) -> None: """simple docstring""" _a = [ p.to(device=A , dtype=A ) if p.is_floating_point() else p.to(device=A ) for p in self.shadow_params ] def a__ (self ) -> dict: """simple docstring""" return { "decay": self.decay, "min_decay": self.min_decay, "optimization_step": self.optimization_step, "update_after_step": self.update_after_step, "use_ema_warmup": self.use_ema_warmup, "inv_gamma": self.inv_gamma, "power": self.power, "shadow_params": self.shadow_params, } def a__ (self , A ) -> None: """simple docstring""" _a = [param.detach().cpu().clone() for param in parameters] def a__ (self , A ) -> None: """simple docstring""" if self.temp_stored_params is None: raise RuntimeError('''This ExponentialMovingAverage has no `store()`ed weights ''' '''to `restore()`''' ) for c_param, param in zip(self.temp_stored_params , A ): param.data.copy_(c_param.data ) # Better memory-wise. _a = None def a__ (self , A ) -> None: """simple docstring""" _a = copy.deepcopy(A ) _a = state_dict.get('''decay''' , self.decay ) if self.decay < 0.0 or self.decay > 1.0: raise ValueError('''Decay must be between 0 and 1''' ) _a = state_dict.get('''min_decay''' , self.min_decay ) if not isinstance(self.min_decay , A ): raise ValueError('''Invalid min_decay''' ) _a = state_dict.get('''optimization_step''' , self.optimization_step ) if not isinstance(self.optimization_step , A ): raise ValueError('''Invalid optimization_step''' ) _a = state_dict.get('''update_after_step''' , self.update_after_step ) if not isinstance(self.update_after_step , A ): raise ValueError('''Invalid update_after_step''' ) _a = state_dict.get('''use_ema_warmup''' , self.use_ema_warmup ) if not isinstance(self.use_ema_warmup , A ): raise ValueError('''Invalid use_ema_warmup''' ) _a = state_dict.get('''inv_gamma''' , self.inv_gamma ) if not isinstance(self.inv_gamma , (float, int) ): raise ValueError('''Invalid inv_gamma''' ) _a = state_dict.get('''power''' , self.power ) if not isinstance(self.power , (float, int) ): raise ValueError('''Invalid power''' ) _a = state_dict.get('''shadow_params''' , A ) if shadow_params is not None: _a = shadow_params if not isinstance(self.shadow_params , A ): raise ValueError('''shadow_params must be a list''' ) if not all(isinstance(A , torch.Tensor ) for p in self.shadow_params ): raise ValueError('''shadow_params must all be Tensors''' )
11
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowercase_ = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 2048-bit 14: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 3072-bit 15: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 4096-bit 16: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 6144-bit 17: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 8192-bit 18: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, } class __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from __future__ import annotations lowercase_ = tuple[int, int, int] lowercase_ = tuple[str, str, str] # used alphabet -------------------------- # from string.ascii_uppercase lowercase_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # -------------------------- default selection -------------------------- # rotors -------------------------- lowercase_ = "EGZWVONAHDCLFQMSIPJBYUKXTR" lowercase_ = "FOBHMDKEXQNRAULPGSJVTYICZW" lowercase_ = "ZJXESIUQLHAVRMDOYGTNFWPBKC" # reflector -------------------------- lowercase_ = { "A": "N", "N": "A", "B": "O", "O": "B", "C": "P", "P": "C", "D": "Q", "Q": "D", "E": "R", "R": "E", "F": "S", "S": "F", "G": "T", "T": "G", "H": "U", "U": "H", "I": "V", "V": "I", "J": "W", "W": "J", "K": "X", "X": "K", "L": "Y", "Y": "L", "M": "Z", "Z": "M", } # -------------------------- extra rotors -------------------------- lowercase_ = "RMDJXFUWGISLHVTCQNKYPBEZOA" lowercase_ = "SGLCPQWZHKXAREONTFBVIYJUDM" lowercase_ = "HVSICLTYKQUBXDWAJZOMFGPREN" lowercase_ = "RZWQHFMVDBKICJLNTUXAGYPSOE" lowercase_ = "LFKIJODBEGAMQPXVUHYSTCZRWN" lowercase_ = "KOAEGVDHXPQZMLFTYWJNBRCIUS" def lowerCAmelCase (__A , __A , __A): """simple docstring""" if (unique_rotsel := len(set(__A))) < 3: _a = F'''Please use 3 unique rotors (not {unique_rotsel})''' raise Exception(__A) # Checks if rotor positions are valid _a , _a , _a = rotpos if not 0 < rotorposa <= len(__A): _a = F'''First rotor position is not within range of 1..26 ({rotorposa}''' raise ValueError(__A) if not 0 < rotorposa <= len(__A): _a = F'''Second rotor position is not within range of 1..26 ({rotorposa})''' raise ValueError(__A) if not 0 < rotorposa <= len(__A): _a = F'''Third rotor position is not within range of 1..26 ({rotorposa})''' raise ValueError(__A) # Validates string and returns dict _a = _plugboard(__A) return rotpos, rotsel, pbdict def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): _a = F'''Plugboard setting isn\'t type string ({type(__A)})''' raise TypeError(__A) elif len(__A) % 2 != 0: _a = F'''Odd number of symbols ({len(__A)})''' raise Exception(__A) elif pbstring == "": return {} pbstring.replace(''' ''' , '''''') # Checks if all characters are unique _a = set() for i in pbstring: if i not in abc: _a = F'''\'{i}\' not in list of symbols''' raise Exception(__A) elif i in tmppbl: _a = F'''Duplicate symbol ({i})''' raise Exception(__A) else: tmppbl.add(__A) del tmppbl # Created the dictionary _a = {} for j in range(0 , len(__A) - 1 , 2): _a = pbstring[j + 1] _a = pbstring[j] return pb def lowerCAmelCase (__A , __A , __A = (rotora, rotora, rotora) , __A = "" , ): """simple docstring""" _a = text.upper() _a , _a , _a = _validator( __A , __A , plugb.upper()) _a , _a , _a = rotor_position _a , _a , _a = rotor_selection rotorposa -= 1 rotorposa -= 1 rotorposa -= 1 _a = [] # encryption/decryption process -------------------------- for symbol in text: if symbol in abc: # 1st plugboard -------------------------- if symbol in plugboard: _a = plugboard[symbol] # rotor ra -------------------------- _a = abc.index(__A) + rotorposa _a = rotora[index % len(__A)] # rotor rb -------------------------- _a = abc.index(__A) + rotorposa _a = rotora[index % len(__A)] # rotor rc -------------------------- _a = abc.index(__A) + rotorposa _a = rotora[index % len(__A)] # reflector -------------------------- # this is the reason you don't need another machine to decipher _a = reflector[symbol] # 2nd rotors _a = abc[rotora.index(__A) - rotorposa] _a = abc[rotora.index(__A) - rotorposa] _a = abc[rotora.index(__A) - rotorposa] # 2nd plugboard if symbol in plugboard: _a = plugboard[symbol] # moves/resets rotor positions rotorposa += 1 if rotorposa >= len(__A): _a = 0 rotorposa += 1 if rotorposa >= len(__A): _a = 0 rotorposa += 1 if rotorposa >= len(__A): _a = 0 # else: # pass # Error could be also raised # raise ValueError( # 'Invalid symbol('+repr(symbol)+')') result.append(__A) return "".join(__A) if __name__ == "__main__": lowercase_ = "This is my Python script that emulates the Enigma machine from WWII." lowercase_ = (1, 1, 1) lowercase_ = "pictures" lowercase_ = (rotora, rotora, rotora) lowercase_ = enigma(message, rotor_pos, rotor_sel, pb) print("Encrypted message:", en) print("Decrypted message:", enigma(en, rotor_pos, rotor_sel, pb))
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" return 1 if input_a == input_a else 0 def lowerCAmelCase (): """simple docstring""" assert xnor_gate(0 , 0) == 1 assert xnor_gate(0 , 1) == 0 assert xnor_gate(1 , 0) == 0 assert xnor_gate(1 , 1) == 1 if __name__ == "__main__": print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) print(xnor_gate(1, 1))
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowercase_ = sys.version_info >= (3, 10) def lowerCAmelCase (__A=None , __A=None): """simple docstring""" return field(default_factory=lambda: default , metadata=__A) @dataclass class __A : '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : float __lowerCamelCase : str __lowerCamelCase : bool @dataclass class __A : '''simple docstring''' __lowerCamelCase : int = 42 __lowerCamelCase : str = field(default='toto' , metadata={'help': 'help message'} ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : bool = False __lowerCamelCase : bool = True __lowerCamelCase : Optional[bool] = None class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = 'titi' __lowerCamelCase : Union[str, Any] = 'toto' class __A ( A ): '''simple docstring''' __lowerCamelCase : Tuple = 'titi' __lowerCamelCase : Union[str, Any] = 'toto' __lowerCamelCase : Optional[int] = 42 @dataclass class __A : '''simple docstring''' __lowerCamelCase : BasicEnum = "toto" def a__ (self ) -> Dict: """simple docstring""" _a = BasicEnum(self.foo ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : MixedTypeEnum = "toto" def a__ (self ) -> Optional[Any]: """simple docstring""" _a = MixedTypeEnum(self.foo ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : Optional[int] = None __lowerCamelCase : Optional[float] = field(default=A , metadata={'help': 'help message'} ) __lowerCamelCase : Optional[str] = None __lowerCamelCase : Optional[List[str]] = list_field(default=[] ) __lowerCamelCase : Optional[List[int]] = list_field(default=[] ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : List[int] = list_field(default=[] ) __lowerCamelCase : List[int] = list_field(default=[1, 2, 3] ) __lowerCamelCase : List[str] = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) __lowerCamelCase : List[float] = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : List[int] = field() __lowerCamelCase : str = field() __lowerCamelCase : BasicEnum = field() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = BasicEnum(self.required_enum ) @dataclass class __A : '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : "BasicEnum" = field() __lowerCamelCase : "Optional[bool]" = None __lowerCamelCase : "str" = field(default='toto' , metadata={'help': 'help message'} ) __lowerCamelCase : "List[str]" = list_field(default=['Hallo', 'Bonjour', 'Hello'] ) if is_python_no_less_than_3_10: @dataclass class __A : '''simple docstring''' __lowerCamelCase : bool = False __lowerCamelCase : bool = True __lowerCamelCase : bool | None = None @dataclass class __A : '''simple docstring''' __lowerCamelCase : int | None = None __lowerCamelCase : float | None = field(default=A , metadata={'help': 'help message'} ) __lowerCamelCase : str | None = None __lowerCamelCase : list[str] | None = list_field(default=[] ) __lowerCamelCase : list[int] | None = list_field(default=[] ) class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self , A , A ) -> List[Any]: """simple docstring""" self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): _a = {k: v for k, v in vars(A ).items() if k != '''container'''} _a = {k: v for k, v in vars(A ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('''choices''' , A ) and yy.get('''choices''' , A ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['''type'''](A ) , yy['''type'''](A ) ) del xx["type"], yy["type"] self.assertEqual(A , A ) def a__ (self ) -> Tuple: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=A , required=A ) expected.add_argument('''--bar''' , type=A , required=A ) expected.add_argument('''--baz''' , type=A , required=A ) expected.add_argument('''--flag''' , type=A , default=A , const=A , nargs='''?''' ) self.argparsersEqual(A , A ) _a = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] ((_a) , ) = parser.parse_args_into_dataclasses(A , look_for_args_file=A ) self.assertFalse(example.flag ) def a__ (self ) -> Any: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=42 , type=A ) expected.add_argument('''--baz''' , default='''toto''' , type=A , help='''help message''' ) self.argparsersEqual(A , A ) def a__ (self ) -> List[Any]: """simple docstring""" _a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=A , default=A , const=A , nargs='''?''' ) expected.add_argument('''--baz''' , type=A , default=A , const=A , nargs='''?''' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('''--no_baz''' , action='''store_false''' , default=A , dest='''baz''' ) expected.add_argument('''--opt''' , type=A , default=A ) _a = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(A ) for dataclass_type in dataclass_types: _a = HfArgumentParser(A ) self.argparsersEqual(A , A ) _a = parser.parse_args([] ) self.assertEqual(A , Namespace(foo=A , baz=A , opt=A ) ) _a = parser.parse_args(['''--foo''', '''--no_baz'''] ) self.assertEqual(A , Namespace(foo=A , baz=A , opt=A ) ) _a = parser.parse_args(['''--foo''', '''--baz'''] ) self.assertEqual(A , Namespace(foo=A , baz=A , opt=A ) ) _a = parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True'''] ) self.assertEqual(A , Namespace(foo=A , baz=A , opt=A ) ) _a = parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False'''] ) self.assertEqual(A , Namespace(foo=A , baz=A , opt=A ) ) def a__ (self ) -> Dict: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=['''titi''', '''toto''', 42] , type=make_choice_type_function(['''titi''', '''toto''', 42] ) , ) self.argparsersEqual(A , A ) _a = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) _a = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) _a = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) _a = parser.parse_args_into_dataclasses(['''--foo''', '''titi'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) _a = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 42 ) _a = parser.parse_args_into_dataclasses(['''--foo''', '''42'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def a__ (self ) -> Optional[Any]: """simple docstring""" @dataclass class __A : '''simple docstring''' __lowerCamelCase : Literal["titi", "toto", 42] = "toto" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=('''titi''', '''toto''', 42) , type=make_choice_type_function(['''titi''', '''toto''', 42] ) , ) self.argparsersEqual(A , A ) _a = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) _a = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) _a = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 42 ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument('''--foo_int''' , nargs='''+''' , default=[] , type=A ) expected.add_argument('''--bar_int''' , nargs='''+''' , default=[1, 2, 3] , type=A ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=A ) expected.add_argument('''--foo_float''' , nargs='''+''' , default=[0.1, 0.2, 0.3] , type=A ) self.argparsersEqual(A , A ) _a = parser.parse_args([] ) self.assertEqual( A , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['''Hallo''', '''Bonjour''', '''Hello'''] , foo_float=[0.1, 0.2, 0.3] ) , ) _a = parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split() ) self.assertEqual(A , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['''a''', '''b''', '''c'''] , foo_float=[0.1, 0.7] ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=A , type=A ) expected.add_argument('''--bar''' , default=A , type=A , help='''help message''' ) expected.add_argument('''--baz''' , default=A , type=A ) expected.add_argument('''--ces''' , nargs='''+''' , default=[] , type=A ) expected.add_argument('''--des''' , nargs='''+''' , default=[] , type=A ) _a = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(A ) for dataclass_type in dataclass_types: _a = HfArgumentParser(A ) self.argparsersEqual(A , A ) _a = parser.parse_args([] ) self.assertEqual(A , Namespace(foo=A , bar=A , baz=A , ces=[] , des=[] ) ) _a = parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split() ) self.assertEqual(A , Namespace(foo=12 , bar=3.14 , baz='''42''' , ces=['''a''', '''b''', '''c'''] , des=[1, 2, 3] ) ) def a__ (self ) -> Dict: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument('''--required_list''' , nargs='''+''' , type=A , required=A ) expected.add_argument('''--required_str''' , type=A , required=A ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=A , ) self.argparsersEqual(A , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = HfArgumentParser(A ) _a = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=A , required=A ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=A , ) expected.add_argument('''--opt''' , type=A , default=A ) expected.add_argument('''--baz''' , default='''toto''' , type=A , help='''help message''' ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=A ) self.argparsersEqual(A , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = HfArgumentParser(A ) _a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } _a = parser.parse_dict(A )[0] _a = BasicExample(**A ) self.assertEqual(A , A ) def a__ (self ) -> str: """simple docstring""" _a = HfArgumentParser(A ) _a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, '''extra''': 42, } self.assertRaises(A , parser.parse_dict , A , allow_extra_keys=A ) def a__ (self ) -> Any: """simple docstring""" _a = HfArgumentParser(A ) _a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: _a = os.path.join(A , '''temp_json''' ) os.mkdir(A ) with open(temp_local_path + '''.json''' , '''w+''' ) as f: json.dump(A , A ) _a = parser.parse_yaml_file(Path(temp_local_path + '''.json''' ) )[0] _a = BasicExample(**A ) self.assertEqual(A , A ) def a__ (self ) -> Tuple: """simple docstring""" _a = HfArgumentParser(A ) _a = { '''foo''': 12, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: _a = os.path.join(A , '''temp_yaml''' ) os.mkdir(A ) with open(temp_local_path + '''.yaml''' , '''w+''' ) as f: yaml.dump(A , A ) _a = parser.parse_yaml_file(Path(temp_local_path + '''.yaml''' ) )[0] _a = BasicExample(**A ) self.assertEqual(A , A ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = HfArgumentParser(A ) self.assertIsNotNone(A )
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = 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 _a = image_processing(A , 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'''], ) , )
11
1
'''simple docstring''' from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class __A ( nn.Module ): '''simple docstring''' def __init__(self , A = 16 , A = 88 , A = None , A = 1 , A = 0.0 , A = 32 , A = None , A = False , A = None , A = None , A = "geglu" , A = None , ) -> Union[str, Any]: """simple docstring""" super().__init__() _a = nn.ModuleList( [ TransformeraDModel( num_attention_heads=A , attention_head_dim=A , in_channels=A , num_layers=A , dropout=A , norm_num_groups=A , cross_attention_dim=A , attention_bias=A , sample_size=A , num_vector_embeds=A , activation_fn=A , num_embeds_ada_norm=A , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _a = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _a = [77, 257] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _a = [1, 0] def a__ (self , A , A , A=None , A=None , A=None , A = True , ) -> int: """simple docstring""" _a = hidden_states _a = [] _a = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _a = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _a = self.transformer_index_for_condition[i] _a = self.transformers[transformer_index]( A , encoder_hidden_states=A , timestep=A , cross_attention_kwargs=A , return_dict=A , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _a = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _a = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=A )
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase_ = logging.get_logger(__name__) @add_end_docstrings( A , R'\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n ' , ) class __A ( A ): '''simple docstring''' def a__ (self , A ) -> np.ndarray: """simple docstring""" if self.framework == "tf": _a = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": _a = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=A ) else: raise ValueError('''Unsupported framework''' ) return masked_index def a__ (self , A ) -> np.ndarray: """simple docstring""" _a = self.get_masked_index(A ) _a = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , f'''No mask_token ({self.tokenizer.mask_token}) found on the input''' , ) def a__ (self , A ) -> Tuple: """simple docstring""" if isinstance(A , A ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(A ) def a__ (self , A , A=None , **A ) -> Dict[str, GenericTensor]: """simple docstring""" if return_tensors is None: _a = self.framework _a = self.tokenizer(A , return_tensors=A ) self.ensure_exactly_one_mask_token(A ) return model_inputs def a__ (self , A ) -> List[Any]: """simple docstring""" _a = self.model(**A ) _a = model_inputs['''input_ids'''] return model_outputs def a__ (self , A , A=5 , A=None ) -> Union[str, Any]: """simple docstring""" if target_ids is not None and target_ids.shape[0] < top_k: _a = target_ids.shape[0] _a = model_outputs['''input_ids'''][0] _a = model_outputs['''logits'''] if self.framework == "tf": _a = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] _a = outputs.numpy() _a = outputs[0, masked_index, :] _a = stable_softmax(A , axis=-1 ) if target_ids is not None: _a = tf.gather_nd(tf.squeeze(A , 0 ) , target_ids.reshape(-1 , 1 ) ) _a = tf.expand_dims(A , 0 ) _a = tf.math.top_k(A , k=A ) _a , _a = topk.values.numpy(), topk.indices.numpy() else: _a = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=A ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample _a = outputs[0, masked_index, :] _a = logits.softmax(dim=-1 ) if target_ids is not None: _a = probs[..., target_ids] _a , _a = probs.topk(A ) _a = [] _a = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): _a = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place _a = input_ids.numpy().copy() if target_ids is not None: _a = target_ids[p].tolist() _a = p # Filter padding out: _a = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back _a = self.tokenizer.decode(A , skip_special_tokens=A ) _a = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(A ) result.append(A ) if single_mask: return result[0] return result def a__ (self , A , A=None ) -> List[str]: """simple docstring""" if isinstance(A , A ): _a = [targets] try: _a = self.tokenizer.get_vocab() except Exception: _a = {} _a = [] for target in targets: _a = vocab.get(A , A ) if id_ is None: _a = self.tokenizer( A , add_special_tokens=A , return_attention_mask=A , return_token_type_ids=A , max_length=1 , truncation=A , )['''input_ids'''] if len(A ) == 0: logger.warning( f'''The specified target token `{target}` does not exist in the model vocabulary. ''' '''We cannot replace it with anything meaningful, ignoring it''' ) continue _a = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f'''The specified target token `{target}` does not exist in the model vocabulary. ''' f'''Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.''' ) target_ids.append(id_ ) _a = list(set(A ) ) if len(A ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) _a = np.array(A ) return target_ids def a__ (self , A=None , A=None ) -> Dict: """simple docstring""" _a = {} if targets is not None: _a = self.get_target_ids(A , A ) _a = target_ids if top_k is not None: _a = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__(self , A , *A , **A ) -> Optional[Any]: """simple docstring""" _a = super().__call__(A , **A ) if isinstance(A , A ) and len(A ) == 1: return outputs[0] return outputs
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
1
'''simple docstring''' import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, 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.models.esm.modeling_esmfold import EsmForProteinFolding class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=False , A=True , A=False , A=False , A=19 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Tuple: """simple docstring""" _a = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , 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 , initializer_range=self.initializer_range , is_folding_model=A , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def a__ (self , A , A , A , A , A , A ) -> Dict: """simple docstring""" _a = EsmForProteinFolding(config=A ).float() model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) _a = model(A ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def a__ (self ) -> int: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Tuple = False __lowerCamelCase : Union[str, Any] = (EsmForProteinFolding,) if is_torch_available() else () __lowerCamelCase : Union[str, Any] = () __lowerCamelCase : Union[str, Any] = {} if is_torch_available() else {} __lowerCamelCase : List[str] = False def a__ (self ) -> List[str]: """simple docstring""" _a = EsmFoldModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> int: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> int: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @unittest.skip('''Does not support attention outputs''' ) def a__ (self ) -> Tuple: """simple docstring""" pass @unittest.skip def a__ (self ) -> Tuple: """simple docstring""" pass @unittest.skip('''Esm does not support embedding resizing''' ) def a__ (self ) -> Optional[int]: """simple docstring""" pass @unittest.skip('''Esm does not support embedding resizing''' ) def a__ (self ) -> List[str]: """simple docstring""" pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def a__ (self ) -> Tuple: """simple docstring""" pass @unittest.skip('''ESMFold does not support head pruning.''' ) def a__ (self ) -> int: """simple docstring""" pass @unittest.skip('''ESMFold does not support head pruning.''' ) def a__ (self ) -> str: """simple docstring""" pass @unittest.skip('''ESMFold does not support head pruning.''' ) def a__ (self ) -> Optional[int]: """simple docstring""" pass @unittest.skip('''ESMFold does not support head pruning.''' ) def a__ (self ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip('''ESMFold does not support head pruning.''' ) def a__ (self ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def a__ (self ) -> Tuple: """simple docstring""" pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def a__ (self ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip('''ESMFold only has one output format.''' ) def a__ (self ) -> Dict: """simple docstring""" pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def a__ (self ) -> List[str]: """simple docstring""" pass @unittest.skip('''ESMFold does not support input chunking.''' ) def a__ (self ) -> str: """simple docstring""" pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def a__ (self ) -> List[str]: """simple docstring""" pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def a__ (self ) -> Union[str, Any]: """simple docstring""" pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def a__ (self ) -> str: """simple docstring""" pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def a__ (self ) -> List[Any]: """simple docstring""" pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @require_torch class __A ( A ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() _a = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) _a = model(A )['''positions'''] _a = torch.tensor([2.5828, 0.7993, -10.9334] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , A , atol=1E-4 ) )
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
1
'''simple docstring''' import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration lowercase_ = [ # tf -> hf ("/", "."), ("layer_", "layers."), ("kernel", "weight"), ("beta", "bias"), ("gamma", "weight"), ("pegasus", "model"), ] lowercase_ = [ (".output.dense", ".fc2"), ("intermediate.LayerNorm", "final_layer_norm"), ("intermediate.dense", "fc1"), ] lowercase_ = ( INIT_COMMON + [ ("attention.self.LayerNorm", "self_attn_layer_norm"), ("attention.output.dense", "self_attn.out_proj"), ("attention.self", "self_attn"), ("attention.encdec.LayerNorm", "encoder_attn_layer_norm"), ("attention.encdec_output.dense", "encoder_attn.out_proj"), ("attention.encdec", "encoder_attn"), ("key", "k_proj"), ("value", "v_proj"), ("query", "q_proj"), ("decoder.LayerNorm", "decoder.layernorm_embedding"), ] + END_COMMON ) lowercase_ = ( INIT_COMMON + [ ("embeddings.word_embeddings", "shared.weight"), ("embeddings.position_embeddings", "embed_positions.weight"), ("attention.self.LayerNorm", "self_attn_layer_norm"), ("attention.output.dense", "self_attn.output"), ("attention.self", "self_attn.self"), ("encoder.LayerNorm", "encoder.layernorm_embedding"), ] + END_COMMON ) lowercase_ = [ "encdec/key/bias", "encdec/query/bias", "encdec/value/bias", "self/key/bias", "self/query/bias", "self/value/bias", "encdec_output/dense/bias", "attention/output/dense/bias", ] def lowerCAmelCase (__A , __A): """simple docstring""" for tf_name, hf_name in patterns: _a = k.replace(__A , __A) return k def lowerCAmelCase (__A , __A): """simple docstring""" _a = BigBirdPegasusConfig(**__A) _a = BigBirdPegasusForConditionalGeneration(__A) _a = torch_model.state_dict() _a = {} # separating decoder weights _a = {k: tf_weights[k] for k in tf_weights if k.startswith('''pegasus/decoder''')} _a = {k: tf_weights[k] for k in tf_weights if not k.startswith('''pegasus/decoder''')} for k, v in tqdm(decoder_weights.items() , '''tf -> hf conversion'''): _a = [k.endswith(__A) for ending in KEYS_TO_IGNORE] if any(__A): continue _a = DECODER_PATTERNS _a = rename_state_dict_key(__A , __A) if new_k not in state_dict: raise ValueError(F'''could not find new key {new_k} in state dict. (converted from {k})''') if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']): _a = v.T _a = torch.from_numpy(__A) assert v.shape == state_dict[new_k].shape, F'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' for k, v in tqdm(remaining_weights.items() , '''tf -> hf conversion'''): _a = [k.endswith(__A) for ending in KEYS_TO_IGNORE] if any(__A): continue _a = REMAINING_PATTERNS _a = rename_state_dict_key(__A , __A) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F'''could not find new key {new_k} in state dict. (converted from {k})''') if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']): _a = v.T _a = torch.from_numpy(__A) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' _a = mapping['''model.embed_positions.weight'''] _a = mapping.pop('''model.embed_positions.weight''') _a , _a = torch_model.load_state_dict(__A , strict=__A) _a = [ k for k in missing if k not in [ '''final_logits_bias''', '''model.encoder.embed_tokens.weight''', '''model.decoder.embed_tokens.weight''', '''lm_head.weight''', ] ] assert unexpected_missing == [], F'''no matches found for the following torch keys {unexpected_missing}''' assert extra == [], F'''no matches found for the following tf keys {extra}''' return torch_model def lowerCAmelCase (__A): """simple docstring""" _a = tf.train.list_variables(__A) _a = {} _a = ['''global_step'''] for name, shape in tqdm(__A , desc='''converting tf checkpoint to dict'''): _a = any(pat in name for pat in ignore_name) if skip_key: continue _a = tf.train.load_variable(__A , __A) _a = array return tf_weights def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a = get_tf_weights_as_numpy(__A) _a = convert_bigbird_pegasus(__A , __A) torch_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--tf_ckpt_path", type=str, help="passed to tf.train.list_variables") parser.add_argument("--save_dir", default=None, type=str, help="Path to the output PyTorch model.") lowercase_ = parser.parse_args() lowercase_ = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
11
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
1
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
1
'''simple docstring''' import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets lowercase_ = "\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n" lowercase_ = "\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting \"1/2\" to \"\\frac{1}{2}\") and then computes accuracy.\n" lowercase_ = R"\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting \"1/2\" to \"\\frac{1}{2}\")\n\nExamples:\n >>> metric = datasets.load_metric(\"competition_math\")\n >>> results = metric.compute(references=[\"\\frac{1}{2}\"], predictions=[\"1/2\"])\n >>> print(results)\n {'accuracy': 1.0}\n" @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def a__ (self ) -> List[str]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' ), '''references''': datasets.Value('''string''' ), } ) , homepage='''https://github.com/hendrycks/math''' , codebase_urls=['''https://github.com/hendrycks/math'''] , ) def a__ (self , A , A ) -> Dict: """simple docstring""" _a = 0.0 for i, j in zip(A , A ): n_correct += 1.0 if math_equivalence.is_equiv(A , A ) else 0.0 _a = n_correct / len(A ) return { "accuracy": accuracy, }
11
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """simple docstring""" super().__init__() self.register_modules( vae=A , text_encoder=A , clip_model=A , tokenizer=A , unet=A , scheduler=A , feature_extractor=A , coca_model=A , coca_tokenizer=A , coca_transform=A , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , A ) set_requires_grad(self.clip_model , A ) def a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = latents - sigma * noise_pred else: raise ValueError(f'''scheduler type {type(self.scheduler )} not supported''' ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """simple docstring""" if isinstance(A , A ) and len(A ) != batch_size: raise ValueError(f'''You have passed {batch_size} batch_size, but only {len(A )} generators.''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if isinstance(A , torch.Generator ) and batch_size > 1: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.join(A ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(A ): raise ValueError( f'''Content prompt is None and CoCa [{coca_is_none_str}] is None.''' f'''Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) if style_prompt is None: if len(A ): raise ValueError( f'''Style prompt is None and CoCa [{coca_is_none_str}] is None.''' f''' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 1 self.scheduler.set_timesteps(A , **A ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = slerp( A , A , A ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = uncond_embeddings.repeat_interleave(A , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _a = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = torch.randn(A , generator=A , device=self.device , dtype=A ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
1
'''simple docstring''' from sklearn.metrics import mean_squared_error import datasets lowercase_ = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" lowercase_ = "\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n" lowercase_ = "\nArgs:\n predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Estimated target values.\n references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Ground truth (correct) target values.\n sample_weight: array-like of shape (n_samples,), default=None\n Sample weights.\n multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"\n Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n \"raw_values\" : Returns a full set of errors in case of multioutput input.\n\n \"uniform_average\" : Errors of all outputs are averaged with uniform weight.\n\n squared : bool, default=True\n If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n mse : mean squared error.\nExamples:\n\n >>> mse_metric = datasets.load_metric(\"mse\")\n >>> predictions = [2.5, 0.0, 2, 8]\n >>> references = [3, -0.5, 2, 7]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'mse': 0.375}\n >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n >>> print(rmse_result)\n {'mse': 0.6123724356957945}\n\n If you're using multi-dimensional lists, then set the config as follows :\n\n >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")\n >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n >>> references = [[0, 2], [-1, 2], [8, -5]]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'mse': 0.7083333333333334}\n >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput='raw_values')\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {'mse': array([0.41666667, 1. ])}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def a__ (self ) -> str: """simple docstring""" 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 a__ (self ) -> int: """simple docstring""" 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 a__ (self , A , A , A=None , A="uniform_average" , A=True ) -> str: """simple docstring""" _a = mean_squared_error( A , A , sample_weight=A , multioutput=A , squared=A ) return {"mse": mse}
11
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = 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(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
1
'''simple docstring''' 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 from .tokenization_gpta import GPTaTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} lowercase_ = { "vocab_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/vocab.json", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/vocab.json", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/vocab.json", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/vocab.json", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/vocab.json", }, "merges_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/merges.txt", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/merges.txt", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/merges.txt", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/merges.txt", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/merges.txt", }, "tokenizer_file": { "gpt2": "https://huggingface.co/gpt2/resolve/main/tokenizer.json", "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json", "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/tokenizer.json", "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json", "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/tokenizer.json", }, } lowercase_ = { "gpt2": 1_024, "gpt2-medium": 1_024, "gpt2-large": 1_024, "gpt2-xl": 1_024, "distilgpt2": 1_024, } class __A ( A ): '''simple docstring''' __lowerCamelCase : Optional[int] = VOCAB_FILES_NAMES __lowerCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Dict = ['input_ids', 'attention_mask'] __lowerCamelCase : int = GPTaTokenizer def __init__(self , A=None , A=None , A=None , A="<|endoftext|>" , A="<|endoftext|>" , A="<|endoftext|>" , A=False , **A , ) -> Dict: """simple docstring""" super().__init__( A , A , tokenizer_file=A , unk_token=A , bos_token=A , eos_token=A , add_prefix_space=A , **A , ) _a = kwargs.pop('''add_bos_token''' , A ) _a = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , A ) != add_prefix_space: _a = getattr(A , pre_tok_state.pop('''type''' ) ) _a = add_prefix_space _a = pre_tok_class(**A ) _a = add_prefix_space def a__ (self , *A , **A ) -> BatchEncoding: """simple docstring""" _a = kwargs.get('''is_split_into_words''' , A ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*A , **A ) def a__ (self , *A , **A ) -> BatchEncoding: """simple docstring""" _a = kwargs.get('''is_split_into_words''' , A ) assert self.add_prefix_space or not is_split_into_words, ( f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True ''' "to use it with pretokenized inputs." ) return super()._encode_plus(*A , **A ) def a__ (self , A , A = None ) -> Tuple[str]: """simple docstring""" _a = self._tokenizer.model.save(A , name=A ) return tuple(A ) def a__ (self , A ) -> List[int]: """simple docstring""" _a = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(A , add_special_tokens=A ) + [self.eos_token_id] ) if len(A ) > self.model_max_length: _a = input_ids[-self.model_max_length :] return input_ids
11
'''simple docstring''' 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 lowercase_ = { "/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 lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = 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: _a = 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: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "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 lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = 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") lowercase_ = 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, )
11
1
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json", } class __A ( A ): '''simple docstring''' __lowerCamelCase : Any = 'deta' __lowerCamelCase : Tuple = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', } def __init__(self , A=None , A=900 , A=2_048 , A=6 , A=2_048 , A=8 , A=6 , A=1_024 , A=8 , A=0.0 , A=True , A="relu" , A=256 , A=0.1 , A=0.0 , A=0.0 , A=0.02 , A=1.0 , A=True , A=False , A="sine" , A=5 , A=4 , A=4 , A=True , A=300 , A=True , A=True , A=1 , A=5 , A=2 , A=1 , A=1 , A=5 , A=2 , A=0.1 , A=0.25 , **A , ) -> Optional[int]: """simple docstring""" if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) _a = CONFIG_MAPPING['''resnet'''](out_features=['''stage2''', '''stage3''', '''stage4'''] ) else: if isinstance(A , A ): _a = backbone_config.pop('''model_type''' ) _a = CONFIG_MAPPING[backbone_model_type] _a = config_class.from_dict(A ) _a = backbone_config _a = num_queries _a = max_position_embeddings _a = d_model _a = encoder_ffn_dim _a = encoder_layers _a = encoder_attention_heads _a = decoder_ffn_dim _a = decoder_layers _a = decoder_attention_heads _a = dropout _a = attention_dropout _a = activation_dropout _a = activation_function _a = init_std _a = init_xavier_std _a = encoder_layerdrop _a = auxiliary_loss _a = position_embedding_type # deformable attributes _a = num_feature_levels _a = encoder_n_points _a = decoder_n_points _a = two_stage _a = two_stage_num_proposals _a = with_box_refine _a = assign_first_stage if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher _a = class_cost _a = bbox_cost _a = giou_cost # Loss coefficients _a = mask_loss_coefficient _a = dice_loss_coefficient _a = bbox_loss_coefficient _a = giou_loss_coefficient _a = eos_coefficient _a = focal_alpha super().__init__(is_encoder_decoder=A , **A ) @property def a__ (self ) -> int: """simple docstring""" return self.encoder_attention_heads @property def a__ (self ) -> int: """simple docstring""" return self.d_model def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = copy.deepcopy(self.__dict__ ) _a = self.backbone_config.to_dict() _a = self.__class__.model_type return output
11
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "facebook/levit-128S": "https://huggingface.co/facebook/levit-128S/resolve/main/config.json", # See all LeViT models at https://huggingface.co/models?filter=levit } class __A ( A ): '''simple docstring''' __lowerCamelCase : Tuple = 'levit' def __init__(self , A=224 , A=3 , A=3 , A=2 , A=1 , A=16 , A=[128, 256, 384] , A=[4, 8, 12] , A=[4, 4, 4] , A=[16, 16, 16] , A=0 , A=[2, 2, 2] , A=[2, 2, 2] , A=0.02 , **A , ) -> Any: """simple docstring""" super().__init__(**A ) _a = image_size _a = num_channels _a = kernel_size _a = stride _a = padding _a = hidden_sizes _a = num_attention_heads _a = depths _a = key_dim _a = drop_path_rate _a = patch_size _a = attention_ratio _a = mlp_ratio _a = initializer_range _a = [ ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2], ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2], ] class __A ( A ): '''simple docstring''' __lowerCamelCase : str = version.parse('1.11' ) @property def a__ (self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def a__ (self ) -> float: """simple docstring""" return 1E-4
11
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
1
'''simple docstring''' from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass lowercase_ = (3, 9, -11, 0, 7, 5, 1, -1) lowercase_ = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class __A : '''simple docstring''' __lowerCamelCase : int __lowerCamelCase : Node | None class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = None for i in sorted(A , reverse=A ): _a = Node(A , self.head ) def __iter__(self ) -> Iterator[int]: """simple docstring""" _a = self.head while node: yield node.data _a = node.next_node def __len__(self ) -> int: """simple docstring""" return sum(1 for _ in self ) def __str__(self ) -> str: """simple docstring""" return " -> ".join([str(A ) for node in self] ) def lowerCAmelCase (__A , __A): """simple docstring""" return SortedLinkedList(list(__A) + list(__A)) if __name__ == "__main__": import doctest doctest.testmod() lowercase_ = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
11
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
1
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return sum(i for i in range(1 , number // 2 + 1) if number % i == 0) == number if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not...") lowercase_ = int(input("Enter number: ").strip()) print(F"""{number} is {'' if perfect(number) else 'not '}a Perfect Number.""")
11
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
1
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
1
'''simple docstring''' import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class __A : '''simple docstring''' __lowerCamelCase : Optional[Union[str, Path]] = None __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : Optional[Dict] = None __lowerCamelCase : Optional[str] = None __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = False __lowerCamelCase : bool = True __lowerCamelCase : Optional[int] = None __lowerCamelCase : int = 1 __lowerCamelCase : Optional[Union[str, bool]] = None __lowerCamelCase : bool = False __lowerCamelCase : Optional[Dict] = None __lowerCamelCase : Optional[str] = None def a__ (self ) -> "DownloadConfig": """simple docstring""" return self.__class__(**{k: copy.deepcopy(A ) for k, v in self.__dict__.items()} )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowercase_ = logging.get_logger(__name__) lowercase_ = { "Salesforce/instruct-blip-flan-t5": "https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json", } class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = 'instructblip_vision_model' def __init__(self , A=1_408 , A=6_144 , A=39 , A=16 , A=224 , A=14 , A="gelu" , A=1E-6 , A=0.0 , A=1E-10 , A=True , **A , ) -> Union[str, Any]: """simple docstring""" super().__init__(**A ) _a = hidden_size _a = intermediate_size _a = num_hidden_layers _a = num_attention_heads _a = patch_size _a = image_size _a = initializer_range _a = attention_dropout _a = layer_norm_eps _a = hidden_act _a = qkv_bias @classmethod def a__ (cls , A , **A ) -> "PretrainedConfig": """simple docstring""" cls._set_token_in_kwargs(A ) _a , _a = cls.get_config_dict(A , **A ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": _a = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(A , **A ) class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = 'instructblip_qformer' def __init__(self , A=30_522 , A=768 , A=12 , A=12 , A=3_072 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=0.02 , A=1E-12 , A=0 , A="absolute" , A=2 , A=1_408 , **A , ) -> Any: """simple docstring""" super().__init__(pad_token_id=A , **A ) _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = hidden_act _a = intermediate_size _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = initializer_range _a = layer_norm_eps _a = position_embedding_type _a = cross_attention_frequency _a = encoder_hidden_size @classmethod def a__ (cls , A , **A ) -> "PretrainedConfig": """simple docstring""" cls._set_token_in_kwargs(A ) _a , _a = cls.get_config_dict(A , **A ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('''model_type''' ) == "instructblip": _a = config_dict['''qformer_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type ''' f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(A , **A ) class __A ( A ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = 'instructblip' __lowerCamelCase : str = True def __init__(self , A=None , A=None , A=None , A=32 , **A ) -> Dict: """simple docstring""" super().__init__(**A ) if vision_config is None: _a = {} logger.info('''vision_config is None. initializing the InstructBlipVisionConfig with default values.''' ) if qformer_config is None: _a = {} logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.''' ) if text_config is None: _a = {} logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' ) _a = InstructBlipVisionConfig(**A ) _a = InstructBlipQFormerConfig(**A ) _a = text_config['''model_type'''] if '''model_type''' in text_config else '''opt''' _a = CONFIG_MAPPING[text_model_type](**A ) _a = self.text_config.tie_word_embeddings _a = self.text_config.is_encoder_decoder _a = num_query_tokens _a = self.vision_config.hidden_size _a = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES _a = 1.0 _a = 0.02 @classmethod def a__ (cls , A , A , A , **A , ) -> List[str]: """simple docstring""" return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **A , ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = copy.deepcopy(self.__dict__ ) _a = self.vision_config.to_dict() _a = self.qformer_config.to_dict() _a = self.text_config.to_dict() _a = self.__class__.model_type return output
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = ['image_processor', 'tokenizer'] __lowerCamelCase : Union[str, Any] = 'OwlViTImageProcessor' __lowerCamelCase : Optional[int] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__(self , A=None , A=None , **A ) -> str: """simple docstring""" _a = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , A , ) _a = kwargs.pop('''feature_extractor''' ) _a = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(A , A ) def __call__(self , A=None , A=None , A=None , A="max_length" , A="np" , **A ) -> int: """simple docstring""" if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(A , A ) or (isinstance(A , A ) and not isinstance(text[0] , A )): _a = [self.tokenizer(A , padding=A , return_tensors=A , **A )] elif isinstance(A , A ) and isinstance(text[0] , A ): _a = [] # Maximum number of queries across batch _a = max([len(A ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(A ) != max_num_queries: _a = t + [''' '''] * (max_num_queries - len(A )) _a = self.tokenizer(A , padding=A , return_tensors=A , **A ) encodings.append(A ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": _a = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) _a = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp _a = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) _a = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch _a = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) _a = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf _a = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) _a = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) _a = BatchEncoding() _a = input_ids _a = attention_mask if query_images is not None: _a = BatchEncoding() _a = self.image_processor( A , return_tensors=A , **A ).pixel_values _a = query_pixel_values if images is not None: _a = self.image_processor(A , return_tensors=A , **A ) if text is not None and images is not None: _a = image_features.pixel_values return encoding elif query_images is not None and images is not None: _a = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**A ) , tensor_type=A ) def a__ (self , *A , **A ) -> Any: """simple docstring""" return self.image_processor.post_process(*A , **A ) def a__ (self , *A , **A ) -> Union[str, Any]: """simple docstring""" return self.image_processor.post_process_object_detection(*A , **A ) def a__ (self , *A , **A ) -> Optional[Any]: """simple docstring""" return self.image_processor.post_process_image_guided_detection(*A , **A ) def a__ (self , *A , **A ) -> Tuple: """simple docstring""" return self.tokenizer.batch_decode(*A , **A ) def a__ (self , *A , **A ) -> str: """simple docstring""" return self.tokenizer.decode(*A , **A ) @property def a__ (self ) -> Dict: """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , A , ) return self.image_processor_class @property def a__ (self ) -> List[Any]: """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , A , ) return self.image_processor
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () lowercase_ = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). lowercase_ = [0, 25, 50] lowercase_ = [25, 50, 75] lowercase_ = fuzz.membership.trimf(X, abca) lowercase_ = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. lowercase_ = np.ones(75) lowercase_ = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) lowercase_ = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) lowercase_ = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) lowercase_ = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) lowercase_ = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] lowercase_ = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) lowercase_ = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] lowercase_ = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] lowercase_ = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title("Young") plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title("Middle aged") plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title("union") plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title("intersection") plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title("complement_a") plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title("difference a/b") plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title("alg_sum") plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title("alg_product") plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title("bdd_sum") plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title("bdd_difference") plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
11
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowercase_ = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 2048-bit 14: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 3072-bit 15: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 4096-bit 16: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 6144-bit 17: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 8192-bit 18: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, } class __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase_ = { "configuration_biogpt": ["BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BioGptConfig"], "tokenization_biogpt": ["BioGptTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST", "BioGptForCausalLM", "BioGptForTokenClassification", "BioGptForSequenceClassification", "BioGptModel", "BioGptPreTrainedModel", ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf lowercase_ = logging.get_logger(__name__) @dataclass class __A ( A ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = [ 'no_inference', 'no_cuda', 'no_tpu', 'no_speed', 'no_memory', 'no_env_print', 'no_multi_process', ] def __init__(self , **A ) -> int: """simple docstring""" for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: _a = deprecated_arg[3:] _a = not kwargs.pop(A ) logger.warning( f'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' f''' {positive_arg}={kwargs[positive_arg]}''' ) _a = kwargs.pop('''tpu_name''' , self.tpu_name ) _a = kwargs.pop('''device_idx''' , self.device_idx ) _a = kwargs.pop('''eager_mode''' , self.eager_mode ) _a = kwargs.pop('''use_xla''' , self.use_xla ) super().__init__(**A ) __lowerCamelCase : str = field( default=A , metadata={'help': 'Name of TPU'} , ) __lowerCamelCase : int = field( default=0 , metadata={'help': 'CPU / GPU device index. Defaults to 0.'} , ) __lowerCamelCase : bool = field(default=A , metadata={'help': 'Benchmark models in eager model.'} ) __lowerCamelCase : bool = field( default=A , metadata={ 'help': 'Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.' } , ) @cached_property def a__ (self ) -> Tuple["tf.distribute.cluster_resolver.TPUClusterResolver"]: """simple docstring""" requires_backends(self , ['''tf'''] ) _a = None if self.tpu: try: if self.tpu_name: _a = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: _a = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: _a = None return tpu @cached_property def a__ (self ) -> Tuple["tf.distribute.Strategy", "tf.distribute.cluster_resolver.TPUClusterResolver"]: """simple docstring""" requires_backends(self , ['''tf'''] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) _a = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] , '''GPU''' ) _a = tf.distribute.OneDeviceStrategy(device=f'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] , '''GPU''' ) # disable GPU _a = tf.distribute.OneDeviceStrategy(device=f'''/cpu:{self.device_idx}''' ) return strategy @property def a__ (self ) -> bool: """simple docstring""" requires_backends(self , ['''tf'''] ) return self._setup_tpu is not None @property def a__ (self ) -> "tf.distribute.Strategy": """simple docstring""" requires_backends(self , ['''tf'''] ) return self._setup_strategy @property def a__ (self ) -> Optional[int]: """simple docstring""" requires_backends(self , ['''tf'''] ) return tf.config.list_physical_devices('''GPU''' ) @property def a__ (self ) -> int: """simple docstring""" requires_backends(self , ['''tf'''] ) if self.cuda: return len(self.gpu_list ) return 0 @property def a__ (self ) -> bool: """simple docstring""" return self.n_gpu > 0
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = 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 _a = image_processing(A , 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'''], ) , )
11
1
'''simple docstring''' from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class __A ( A ): '''simple docstring''' __lowerCamelCase : int = DistilBertTokenizer __lowerCamelCase : Union[str, Any] = DistilBertTokenizerFast __lowerCamelCase : Dict = True @slow def a__ (self ) -> str: """simple docstring""" _a = DistilBertTokenizer.from_pretrained('''distilbert-base-uncased''' ) _a = tokenizer.encode('''sequence builders''' , add_special_tokens=A ) _a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=A ) _a = tokenizer.build_inputs_with_special_tokens(A ) _a = tokenizer.build_inputs_with_special_tokens(A , A ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' import argparse import collections import torch from flax import traverse_util from tax import checkpoints from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase (__A , __A , __A , __A="attention"): """simple docstring""" _a = params[F'''{prefix}/layers_{i}/{layer_name}/key/kernel'''] _a = params[F'''{prefix}/layers_{i}/{layer_name}/out/kernel'''] _a = params[F'''{prefix}/layers_{i}/{layer_name}/query/kernel'''] _a = params[F'''{prefix}/layers_{i}/{layer_name}/value/kernel'''] return k, o, q, v def lowerCAmelCase (__A , __A , __A , __A=False): """simple docstring""" if split_mlp_wi: _a = params[F'''{prefix}/layers_{i}/mlp/wi_0/kernel'''] _a = params[F'''{prefix}/layers_{i}/mlp/wi_1/kernel'''] _a = (wi_a, wi_a) else: _a = params[F'''{prefix}/layers_{i}/mlp/wi/kernel'''] _a = params[F'''{prefix}/layers_{i}/mlp/wo/kernel'''] return wi, wo def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" return params[F'''{prefix}/layers_{i}/{layer_name}/scale'''] def lowerCAmelCase (__A , *, __A , __A): """simple docstring""" _a = traverse_util.flatten_dict(variables['''target''']) _a = {'''/'''.join(__A): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi _a = '''encoder/layers_0/mlp/wi_0/kernel''' in old print('''Split MLP:''' , __A) _a = collections.OrderedDict() # Shared embeddings. _a = old['''token_embedder/embedding'''] # Encoder. for i in range(__A): # Block i, layer 0 (Self Attention). _a = tax_layer_norm_lookup(__A , __A , '''encoder''' , '''pre_attention_layer_norm''') _a , _a , _a , _a = tax_attention_lookup(__A , __A , '''encoder''' , '''attention''') _a = layer_norm _a = k.T _a = o.T _a = q.T _a = v.T # Block i, layer 1 (MLP). _a = tax_layer_norm_lookup(__A , __A , '''encoder''' , '''pre_mlp_layer_norm''') _a , _a = tax_mlp_lookup(__A , __A , '''encoder''' , __A) _a = layer_norm if split_mlp_wi: _a = wi[0].T _a = wi[1].T else: _a = wi.T _a = wo.T _a = old[ '''encoder/relpos_bias/rel_embedding''' ].T _a = old['''encoder/encoder_norm/scale'''] if not is_encoder_only: # Decoder. for i in range(__A): # Block i, layer 0 (Self Attention). _a = tax_layer_norm_lookup(__A , __A , '''decoder''' , '''pre_self_attention_layer_norm''') _a , _a , _a , _a = tax_attention_lookup(__A , __A , '''decoder''' , '''self_attention''') _a = layer_norm _a = k.T _a = o.T _a = q.T _a = v.T # Block i, layer 1 (Cross Attention). _a = tax_layer_norm_lookup(__A , __A , '''decoder''' , '''pre_cross_attention_layer_norm''') _a , _a , _a , _a = tax_attention_lookup(__A , __A , '''decoder''' , '''encoder_decoder_attention''') _a = layer_norm _a = k.T _a = o.T _a = q.T _a = v.T # Block i, layer 2 (MLP). _a = tax_layer_norm_lookup(__A , __A , '''decoder''' , '''pre_mlp_layer_norm''') _a , _a = tax_mlp_lookup(__A , __A , '''decoder''' , __A) _a = layer_norm if split_mlp_wi: _a = wi[0].T _a = wi[1].T else: _a = wi.T _a = wo.T _a = old['''decoder/decoder_norm/scale'''] _a = old[ '''decoder/relpos_bias/rel_embedding''' ].T # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: _a = old['''decoder/logits_dense/kernel'''].T return new def lowerCAmelCase (__A , __A): """simple docstring""" _a = collections.OrderedDict([(k, torch.from_numpy(v.copy())) for (k, v) in converted_params.items()]) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: _a = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: _a = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''') _a = state_dict['''shared.weight'''] return state_dict def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = checkpoints.load_tax_checkpoint(__A) _a = convert_tax_to_pytorch(__A , num_layers=config.num_layers , is_encoder_only=__A) _a = make_state_dict(__A , __A) model.load_state_dict(__A , strict=__A) def lowerCAmelCase (__A , __A , __A , __A = False): """simple docstring""" _a = TaConfig.from_json_file(__A) print(F'''Building PyTorch model from configuration: {config}''') # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: _a = TaEncoderModel(__A) else: _a = TaForConditionalGeneration(__A) # Load weights from tf checkpoint load_tax_weights_in_ta(__A , __A , __A , __A) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''') model.save_pretrained(__A) # Verify that we can load the checkpoint. model.from_pretrained(__A) print('''Done''') if __name__ == "__main__": lowercase_ = argparse.ArgumentParser(description="Converts a native T5X checkpoint into a PyTorch checkpoint.") # Required parameters parser.add_argument( "--t5x_checkpoint_path", default=None, type=str, required=True, help="Path to the T5X checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--is_encoder_only", action="store_true", help="Check if the model is encoder-decoder model", default=False ) lowercase_ = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only )
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1
'''simple docstring''' # Function to print upper half of diamond (pyramid) def lowerCAmelCase (__A): """simple docstring""" for i in range(0 , __A): for _ in range(0 , n - i - 1): # printing spaces print(''' ''' , end='''''') for _ in range(0 , i + 1): # printing stars print('''* ''' , end='''''') print() def lowerCAmelCase (__A): """simple docstring""" for i in range(__A , 0 , -1): for _ in range(__A , 0 , -1): # printing stars print('''* ''' , end='''''') print() for _ in range(n - i + 1 , 0 , -1): # printing spaces print(''' ''' , end='''''') def lowerCAmelCase (__A): """simple docstring""" if n <= 0: print(''' ... .... nothing printing :(''') return floyd(__A) # upper half reverse_floyd(__A) # lower half if __name__ == "__main__": print(R"| /\ | |- | |- |--| |\ /| |-") print(R"|/ \| |- |_ |_ |__| | \/ | |_") lowercase_ = 1 while K: lowercase_ = int(input("enter the number and , and see the magic : ")) print() pretty_print(user_number) lowercase_ = int(input("press 0 to exit... and 1 to continue...")) print("Good Bye...")
11
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
1
'''simple docstring''' import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42 , dataset_name='''my_dataset''')}), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_337 , num_examples=42)}), SplitDict({'''train''': SplitInfo()}), ] , ) def lowerCAmelCase (__A): """simple docstring""" _a = split_dict._to_yaml_list() assert len(__A) == len(__A) _a = SplitDict._from_yaml_list(__A) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump _a = None # the split name of split_dict takes over the name of the split info object _a = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=__A), SplitInfo(dataset_name='''my_dataset''')]) def lowerCAmelCase (__A): """simple docstring""" _a = asdict(SplitDict({'''train''': split_info})) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
1
'''simple docstring''' from __future__ import annotations from collections.abc import Callable from typing import Generic, TypeVar lowercase_ = TypeVar("T") lowercase_ = TypeVar("U") class __A ( Generic[T, U] ): '''simple docstring''' def __init__(self , A , A ) -> Optional[int]: """simple docstring""" _a = key _a = val _a = None _a = None def __repr__(self ) -> str: """simple docstring""" return ( f'''Node: key: {self.key}, val: {self.val}, ''' f'''has next: {bool(self.next )}, has prev: {bool(self.prev )}''' ) class __A ( Generic[T, U] ): '''simple docstring''' def __init__(self ) -> None: """simple docstring""" _a = DoubleLinkedListNode(A , A ) _a = DoubleLinkedListNode(A , A ) _a , _a = self.rear, self.head def __repr__(self ) -> str: """simple docstring""" _a = ['''DoubleLinkedList'''] _a = self.head while node.next is not None: rep.append(str(A ) ) _a = node.next rep.append(str(self.rear ) ) return ",\n ".join(A ) def a__ (self , A ) -> None: """simple docstring""" _a = self.rear.prev # All nodes other than self.head are guaranteed to have non-None previous assert previous is not None _a = node _a = previous _a = node _a = self.rear def a__ (self , A ) -> DoubleLinkedListNode[T, U] | None: """simple docstring""" if node.prev is None or node.next is None: return None _a = node.next _a = node.prev _a = None _a = None return node class __A ( Generic[T, U] ): '''simple docstring''' __lowerCamelCase : dict[Callable[[T], U], LRUCache[T, U]] = {} def __init__(self , A ) -> List[Any]: """simple docstring""" _a = DoubleLinkedList() _a = capacity _a = 0 _a = 0 _a = 0 _a = {} def __repr__(self ) -> str: """simple docstring""" return ( f'''CacheInfo(hits={self.hits}, misses={self.miss}, ''' f'''capacity={self.capacity}, current size={self.num_keys})''' ) def __contains__(self , A ) -> bool: """simple docstring""" return key in self.cache def a__ (self , A ) -> U | None: """simple docstring""" if key in self.cache: self.hits += 1 _a = self.cache[key] _a = self.list.remove(self.cache[key] ) assert node == value_node # node is guaranteed not None because it is in self.cache assert node is not None self.list.add(A ) return node.val self.miss += 1 return None def a__ (self , A , A ) -> None: """simple docstring""" if key not in self.cache: if self.num_keys >= self.capacity: # delete first node (oldest) when over capacity _a = self.list.head.next # guaranteed to have a non-None first node when num_keys > 0 # explain to type checker via assertions assert first_node is not None assert first_node.key is not None assert ( self.list.remove(A ) is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] self.num_keys -= 1 _a = DoubleLinkedListNode(A , A ) self.list.add(self.cache[key] ) self.num_keys += 1 else: # bump node to the end of the list, update value _a = self.list.remove(self.cache[key] ) assert node is not None # node guaranteed to be in list _a = value self.list.add(A ) @classmethod def a__ (cls , A = 128 ) -> Callable[[Callable[[T], U]], Callable[..., U]]: """simple docstring""" def cache_decorator_inner(A ) -> Callable[..., U]: def cache_decorator_wrapper(*A ) -> U: if func not in cls.decorator_function_to_instance_map: _a = LRUCache(A ) _a = cls.decorator_function_to_instance_map[func].get(args[0] ) if result is None: _a = func(*A ) cls.decorator_function_to_instance_map[func].put(args[0] , A ) return result def cache_info() -> LRUCache[T, U]: return cls.decorator_function_to_instance_map[func] setattr(A , '''cache_info''' , A ) # noqa: B010 return cache_decorator_wrapper return cache_decorator_inner if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
1
'''simple docstring''' import unittest from transformers import GPTSwaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin lowercase_ = get_tests_dir("fixtures/test_sentencepiece_with_bytefallback.model") @require_sentencepiece @require_tokenizers class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = GPTSwaTokenizer __lowerCamelCase : Optional[Any] = False __lowerCamelCase : List[Any] = True __lowerCamelCase : Union[str, Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing _a = GPTSwaTokenizer(A , eos_token='''<unk>''' , bos_token='''<unk>''' , pad_token='''<unk>''' ) tokenizer.save_pretrained(self.tmpdirname ) def a__ (self , A ) -> str: """simple docstring""" _a = '''This is a test''' _a = '''This is a test''' return input_text, output_text def a__ (self ) -> List[str]: """simple docstring""" _a = '''<s>''' _a = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) , A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) , A ) def a__ (self ) -> Dict: """simple docstring""" _a = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<unk>''' ) self.assertEqual(vocab_keys[1] , '''<s>''' ) self.assertEqual(vocab_keys[-1] , '''j''' ) self.assertEqual(len(A ) , 2_000 ) def a__ (self ) -> List[str]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 2_000 ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = GPTSwaTokenizer(A ) _a = tokenizer.tokenize('''This is a test''' ) self.assertListEqual(A , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , [465, 287, 265, 631, 842] ) _a = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) # fmt: off self.assertListEqual( A , ['''▁I''', '''▁was''', '''▁bor''', '''n''', '''▁in''', '''▁''', '''<0x39>''', '''2''', '''0''', '''0''', '''0''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁f''', '''al''', '''s''', '''<0xC3>''', '''<0xA9>''', '''.'''] , ) # fmt: on _a = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A , [262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260] , ) _a = tokenizer.convert_ids_to_tokens(A ) # fmt: off self.assertListEqual( A , ['''▁I''', '''▁was''', '''▁bor''', '''n''', '''▁in''', '''▁''', '''<0x39>''', '''2''', '''0''', '''0''', '''0''', ''',''', '''▁and''', '''▁this''', '''▁is''', '''▁f''', '''al''', '''s''', '''<0xC3>''', '''<0xA9>''', '''.'''] ) # fmt: on def a__ (self ) -> Optional[int]: """simple docstring""" _a = GPTSwaTokenizer(A ) _a = ['''This is a test''', '''I was born in 92000, and this is falsé.'''] _a = [ [465, 287, 265, 631, 842], [262, 272, 1_525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260], ] # Test that encode_fast returns the same as tokenize + convert_tokens_to_ids for text, expected_ids in zip(A , A ): self.assertListEqual(tokenizer.encode_fast(A ) , A ) # Test that decode_fast returns the input text for text, token_ids in zip(A , A ): self.assertEqual(tokenizer.decode_fast(A ) , A ) @slow def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = [ '''<|python|>def fibonacci(n)\n if n < 0:\n print(\'Incorrect input\')''', '''Hey there, how are you doing this fine day?''', '''This is a text with a trailing spaces followed by a dot .''', '''Häj sväjs lillebrör! =)''', '''Det är inget fel på Mr. Cool''', ] # fmt: off _a = {'''input_ids''': [[63_423, 5, 6_811, 14_954, 282, 816, 3_821, 63_466, 63_425, 63_462, 18, 63_978, 678, 301, 1_320, 63_423, 63_455, 63_458, 18, 63_982, 4_246, 3_940, 1_901, 47_789, 5_547, 18_994], [19_630, 1_100, 63_446, 1_342, 633, 544, 4_488, 593, 5_102, 2_416, 63_495, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1_652, 428, 268, 1_936, 515, 268, 58_593, 22_413, 9_106, 546, 268, 33_213, 63_979, 698, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [55_130, 63_450, 924, 63_449, 2_249, 4_062, 1_558, 318, 63_504, 21_498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [509, 377, 2_827, 2_559, 332, 6_575, 63_443, 26_801, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '''token_type_ids''': [[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, 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, 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, 1, 1, 1, 1, 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], [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]]} # fmt: on self.tokenizer_integration_test_util( expected_encoding=A , model_name='''AI-Sweden/gpt-sw3-126m''' , sequences=A , )
11
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
1
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for part_id in partition_order: _a = df.where(F'''SPARK_PARTITION_ID() = {part_id}''').collect() for row_idx, row in enumerate(__A): expected_row_ids_and_row_dicts.append((F'''{part_id}_{row_idx}''', row.asDict())) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(100).repartition(1) _a = Spark(__A) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(10).repartition(2) _a = [1, 0] _a = _generate_iterable_examples(__A , __A) # Reverse the partitions. _a = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A) for i, (row_id, row_dict) in enumerate(generate_fn()): _a , _a = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(10).repartition(1) _a = SparkExamplesIterable(__A) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A): assert row_id == F'''0_{i}''' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(30).repartition(3) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''') as generator_mock: _a = lambda __A: x.reverse() _a = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0]) _a = SparkExamplesIterable(__A).shuffle_data_sources(__A) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A): _a , _a = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(20).repartition(4) # Partitions 0 and 2 _a = SparkExamplesIterable(__A).shard_data_sources(worker_id=0 , num_workers=2) assert shard_it_a.n_shards == 2 _a = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2]) for i, (row_id, row_dict) in enumerate(__A): _a , _a = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 _a = SparkExamplesIterable(__A).shard_data_sources(worker_id=1 , num_workers=2) assert shard_it_a.n_shards == 2 _a = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3]) for i, (row_id, row_dict) in enumerate(__A): _a , _a = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowerCAmelCase (): """simple docstring""" _a = pyspark.sql.SparkSession.builder.master('''local[*]''').appName('''pyspark''').getOrCreate() _a = spark.range(100).repartition(1) _a = Spark(__A) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 100
11
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """simple docstring""" super().__init__() self.register_modules( vae=A , text_encoder=A , clip_model=A , tokenizer=A , unet=A , scheduler=A , feature_extractor=A , coca_model=A , coca_tokenizer=A , coca_transform=A , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , A ) set_requires_grad(self.clip_model , A ) def a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = latents - sigma * noise_pred else: raise ValueError(f'''scheduler type {type(self.scheduler )} not supported''' ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """simple docstring""" if isinstance(A , A ) and len(A ) != batch_size: raise ValueError(f'''You have passed {batch_size} batch_size, but only {len(A )} generators.''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if isinstance(A , torch.Generator ) and batch_size > 1: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.join(A ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(A ): raise ValueError( f'''Content prompt is None and CoCa [{coca_is_none_str}] is None.''' f'''Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) if style_prompt is None: if len(A ): raise ValueError( f'''Style prompt is None and CoCa [{coca_is_none_str}] is None.''' f''' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 1 self.scheduler.set_timesteps(A , **A ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = slerp( A , A , A ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = uncond_embeddings.repeat_interleave(A , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _a = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = torch.randn(A , generator=A , device=self.device , dtype=A ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
1
'''simple docstring''' import argparse from pathlib import Path import fairseq import torch from fairseq.models.xmod import XMODModel as FairseqXmodModel from packaging import version from transformers import XmodConfig, XmodForMaskedLM, XmodForSequenceClassification from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("0.12.2"): raise Exception("requires fairseq >= 0.12.2") if version.parse(fairseq.__version__) > version.parse("2"): raise Exception("requires fairseq < v2") logging.set_verbosity_info() lowercase_ = logging.get_logger(__name__) lowercase_ = "Hello, World!" lowercase_ = "en_XX" def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a = Path('''data_bin''') _a = FairseqXmodModel.from_pretrained( model_name_or_path=str(Path(__A).parent) , checkpoint_file=Path(__A).name , _name='''xmod_base''' , arch='''xmod_base''' , task='''multilingual_masked_lm''' , data_name_or_path=str(__A) , bpe='''sentencepiece''' , sentencepiece_model=str(Path(__A).parent / '''sentencepiece.bpe.model''') , src_dict=str(data_dir / '''dict.txt''') , ) xmod.eval() # disable dropout print(__A) _a = xmod.model.encoder.sentence_encoder _a = XmodConfig( vocab_size=xmod_sent_encoder.embed_tokens.num_embeddings , hidden_size=xmod.cfg.model.encoder_embed_dim , num_hidden_layers=xmod.cfg.model.encoder_layers , num_attention_heads=xmod.cfg.model.encoder_attention_heads , intermediate_size=xmod.cfg.model.encoder_ffn_embed_dim , max_position_embeddings=514 , type_vocab_size=1 , layer_norm_eps=1e-5 , pre_norm=xmod.cfg.model.encoder_normalize_before , adapter_reduction_factor=getattr(xmod.cfg.model , '''bottleneck''' , 2) , adapter_layer_norm=xmod.cfg.model.adapter_layer_norm , adapter_reuse_layer_norm=xmod.cfg.model.adapter_reuse_layer_norm , ln_before_adapter=xmod.cfg.model.ln_before_adapter , languages=xmod.cfg.model.languages , ) if classification_head: _a = xmod.model.classification_heads['''mnli'''].out_proj.weight.shape[0] print('''Our X-MOD config:''' , __A) _a = XmodForSequenceClassification(__A) if classification_head else XmodForMaskedLM(__A) model.eval() # Now let's copy all the weights. # Embeddings _a = xmod_sent_encoder.embed_tokens.weight _a = xmod_sent_encoder.embed_positions.weight _a = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight) # just zero them out b/c xmod doesn't use them. _a = xmod_sent_encoder.layernorm_embedding.weight _a = xmod_sent_encoder.layernorm_embedding.bias for i in range(config.num_hidden_layers): # Encoder: start of layer _a = model.roberta.encoder.layer[i] _a = xmod_sent_encoder.layers[i] # self attention _a = layer.attention.self if not ( xmod_layer.self_attn.k_proj.weight.data.shape == xmod_layer.self_attn.q_proj.weight.data.shape == xmod_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size)) ): raise AssertionError('''Dimensions of self-attention weights do not match.''') _a = xmod_layer.self_attn.q_proj.weight _a = xmod_layer.self_attn.q_proj.bias _a = xmod_layer.self_attn.k_proj.weight _a = xmod_layer.self_attn.k_proj.bias _a = xmod_layer.self_attn.v_proj.weight _a = xmod_layer.self_attn.v_proj.bias # self-attention output _a = layer.attention.output if self_output.dense.weight.shape != xmod_layer.self_attn.out_proj.weight.shape: raise AssertionError('''Dimensions of self-attention output weights do not match.''') _a = xmod_layer.self_attn.out_proj.weight _a = xmod_layer.self_attn.out_proj.bias _a = xmod_layer.self_attn_layer_norm.weight _a = xmod_layer.self_attn_layer_norm.bias # intermediate _a = layer.intermediate if intermediate.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of intermediate weights do not match.''') _a = xmod_layer.fca.weight _a = xmod_layer.fca.bias # output _a = layer.output if bert_output.dense.weight.shape != xmod_layer.fca.weight.shape: raise AssertionError('''Dimensions of feed-forward weights do not match.''') _a = xmod_layer.fca.weight _a = xmod_layer.fca.bias _a = xmod_layer.final_layer_norm.weight _a = xmod_layer.final_layer_norm.bias if bert_output.adapter_layer_norm is not None: _a = xmod_layer.adapter_layer_norm.weight _a = xmod_layer.adapter_layer_norm.bias if sorted(bert_output.adapter_modules.keys()) != sorted(xmod_layer.adapter_modules.keys()): raise AssertionError('''Lists of language adapters do not match.''') for lang_code, adapter in xmod_layer.adapter_modules.items(): _a = bert_output.adapter_modules[lang_code] _a = xmod_layer.adapter_modules[lang_code] _a = from_adapter.fca.weight _a = from_adapter.fca.bias _a = from_adapter.fca.weight _a = from_adapter.fca.bias # end of layer if xmod_sent_encoder.layer_norm is not None: _a = xmod_sent_encoder.layer_norm.weight _a = xmod_sent_encoder.layer_norm.bias if classification_head: _a = xmod.model.classification_heads['''mnli'''].dense.weight _a = xmod.model.classification_heads['''mnli'''].dense.bias _a = xmod.model.classification_heads['''mnli'''].out_proj.weight _a = xmod.model.classification_heads['''mnli'''].out_proj.bias else: # LM Head _a = xmod.model.encoder.lm_head.dense.weight _a = xmod.model.encoder.lm_head.dense.bias _a = xmod.model.encoder.lm_head.layer_norm.weight _a = xmod.model.encoder.lm_head.layer_norm.bias _a = xmod.model.encoder.lm_head.weight _a = xmod.model.encoder.lm_head.bias # Let's check that we get the same results. _a = xmod.encode(__A).unsqueeze(0) # batch of size 1 model.roberta.set_default_language(__A) _a = model(__A)[0] if classification_head: _a = xmod.model.classification_heads['''mnli'''](xmod.extract_features(__A)) else: _a = xmod.model(__A , lang_id=[SAMPLE_LANGUAGE])[0] print(our_output.shape , their_output.shape) _a = torch.max(torch.abs(our_output - their_output)).item() print(F'''max_absolute_diff = {max_absolute_diff}''') # ~ 1e-7 _a = torch.allclose(__A , __A , atol=1e-3) print('''Do both models output the same tensors?''' , '''🔥''' if success else '''💩''') if not success: raise Exception('''Something went wRoNg''') Path(__A).mkdir(parents=__A , exist_ok=__A) print(F'''Saving model to {pytorch_dump_folder_path}''') model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--xmod_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--classification_head", action="store_true", help="Whether to convert a final classification head." ) lowercase_ = parser.parse_args() convert_xmod_checkpoint_to_pytorch( args.xmod_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
11
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = 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(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
1
'''simple docstring''' import collections import os import re from pathlib import Path lowercase_ = "src/transformers" # Matches is_xxx_available() lowercase_ = re.compile(R"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} lowercase_ = re.compile(R"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] lowercase_ = re.compile(R"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available lowercase_ = re.compile(R"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") lowercase_ = re.compile(R"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] lowercase_ = re.compile(R"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", lowercase_ = re.compile(R"^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], lowercase_ = re.compile(R"^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo lowercase_ = re.compile(R"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: lowercase_ = re.compile(R"^\s*try:") # Catches a line with else: lowercase_ = re.compile(R"^\s*else:") def lowerCAmelCase (__A): """simple docstring""" if _re_test_backend.search(__A) is None: return None _a = [b[0] for b in _re_backend.findall(__A)] backends.sort() return "_and_".join(__A) def lowerCAmelCase (__A): """simple docstring""" with open(__A , '''r''' , encoding='''utf-8''' , newline='''\n''') as f: _a = f.readlines() _a = 0 while line_index < len(__A) and not lines[line_index].startswith('''_import_structure = {'''): line_index += 1 # If this is a traditional init, just return. if line_index >= len(__A): return None # First grab the objects without a specific backend in _import_structure _a = [] while not lines[line_index].startswith('''if TYPE_CHECKING''') and find_backend(lines[line_index]) is None: _a = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(__A): _a = _re_one_line_import_struct.search(__A).groups()[0] _a = re.findall(r'''\[([^\]]+)\]''' , __A) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''')]) line_index += 1 continue _a = _re_import_struct_key_value.search(__A) if single_line_import_search is not None: _a = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''') if len(__A) > 0] objects.extend(__A) elif line.startswith(''' ''' * 8 + '''"'''): objects.append(line[9:-3]) line_index += 1 _a = {'''none''': objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith('''if TYPE_CHECKING'''): # If the line is an if not is_backend_available, we grab all objects associated. _a = find_backend(lines[line_index]) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1]) is None: _a = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index]) is None: line_index += 1 line_index += 1 _a = [] # Until we unindent, add backend objects to the list while len(lines[line_index]) <= 1 or lines[line_index].startswith(''' ''' * 4): _a = lines[line_index] if _re_import_struct_add_one.search(__A) is not None: objects.append(_re_import_struct_add_one.search(__A).groups()[0]) elif _re_import_struct_add_many.search(__A) is not None: _a = _re_import_struct_add_many.search(__A).groups()[0].split(''', ''') _a = [obj[1:-1] for obj in imports if len(__A) > 0] objects.extend(__A) elif _re_between_brackets.search(__A) is not None: _a = _re_between_brackets.search(__A).groups()[0].split(''', ''') _a = [obj[1:-1] for obj in imports if len(__A) > 0] objects.extend(__A) elif _re_quote_object.search(__A) is not None: objects.append(_re_quote_object.search(__A).groups()[0]) elif line.startswith(''' ''' * 8 + '''"'''): objects.append(line[9:-3]) elif line.startswith(''' ''' * 12 + '''"'''): objects.append(line[13:-3]) line_index += 1 _a = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend _a = [] while ( line_index < len(__A) and find_backend(lines[line_index]) is None and not lines[line_index].startswith('''else''') ): _a = lines[line_index] _a = _re_import.search(__A) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''')) elif line.startswith(''' ''' * 8): objects.append(line[8:-2]) line_index += 1 _a = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(__A): # If the line is an if is_backend_available, we grab all objects associated. _a = find_backend(lines[line_index]) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1]) is None: _a = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index]) is None: line_index += 1 line_index += 1 _a = [] # Until we unindent, add backend objects to the list while len(lines[line_index]) <= 1 or lines[line_index].startswith(''' ''' * 8): _a = lines[line_index] _a = _re_import.search(__A) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''')) elif line.startswith(''' ''' * 12): objects.append(line[12:-2]) line_index += 1 _a = objects else: line_index += 1 return import_dict_objects, type_hint_objects def lowerCAmelCase (__A , __A): """simple docstring""" def find_duplicates(__A): return [k for k, v in collections.Counter(__A).items() if v > 1] if list(import_dict_objects.keys()) != list(type_hint_objects.keys()): return ["Both sides of the init do not have the same backends!"] _a = [] for key in import_dict_objects.keys(): _a = find_duplicates(import_dict_objects[key]) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''') _a = find_duplicates(type_hint_objects[key]) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''') if sorted(set(import_dict_objects[key])) != sorted(set(type_hint_objects[key])): _a = '''base imports''' if key == '''none''' else F'''{key} backend''' errors.append(F'''Differences for {name}:''') for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''') for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''') return errors def lowerCAmelCase (): """simple docstring""" _a = [] for root, _, files in os.walk(__A): if "__init__.py" in files: _a = os.path.join(__A , '''__init__.py''') _a = parse_init(__A) if objects is not None: _a = analyze_results(*__A) if len(__A) > 0: _a = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append('''\n'''.join(__A)) if len(__A) > 0: raise ValueError('''\n\n'''.join(__A)) def lowerCAmelCase (): """simple docstring""" _a = [] for path, directories, files in os.walk(__A): for folder in directories: # Ignore private modules if folder.startswith('''_'''): directories.remove(__A) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(__A) / folder).glob('''*.py'''))) == 0: continue _a = str((Path(__A) / folder).relative_to(__A)) _a = short_path.replace(os.path.sep , '''.''') submodules.append(__A) for fname in files: if fname == "__init__.py": continue _a = str((Path(__A) / fname).relative_to(__A)) _a = short_path.replace('''.py''' , '''''').replace(os.path.sep , '''.''') if len(submodule.split('''.''')) == 1: submodules.append(__A) return submodules lowercase_ = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", "models.esm.openfold_utils", ] def lowerCAmelCase (): """simple docstring""" from transformers.utils import direct_transformers_import _a = direct_transformers_import(__A) _a = set(transformers._import_structure.keys()) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(__A , '''__init__.py''') , '''r''') as f: _a = f.read() import_structure_keys.update(set(re.findall(r'''import_structure\[\"([^\"]*)\"\]''' , __A))) _a = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(__A) > 0: _a = '''\n'''.join(F'''- {module}''' for module in module_not_registered) raise ValueError( '''The following submodules are not properly registed in the main init of Transformers:\n''' F'''{list_of_modules}\n''' '''Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.''') if __name__ == "__main__": check_all_inits() check_submodules()
11
'''simple docstring''' 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 lowercase_ = { "/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 lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = 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: _a = 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: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "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 lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = 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") lowercase_ = 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, )
11
1
'''simple docstring''' import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = {"vocab_file": "spiece.model"} lowercase_ = { "vocab_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model", } } lowercase_ = { "albert-base-v1": 512, "albert-large-v1": 512, "albert-xlarge-v1": 512, "albert-xxlarge-v1": 512, "albert-base-v2": 512, "albert-large-v2": 512, "albert-xlarge-v2": 512, "albert-xxlarge-v2": 512, } lowercase_ = "▁" class __A ( A ): '''simple docstring''' __lowerCamelCase : List[Any] = VOCAB_FILES_NAMES __lowerCamelCase : str = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__(self , A , A=True , A=True , A=False , A="[CLS]" , A="[SEP]" , A="<unk>" , A="[SEP]" , A="<pad>" , A="[CLS]" , A="[MASK]" , A = None , **A , ) -> None: """simple docstring""" _a = ( AddedToken(A , lstrip=A , rstrip=A , normalized=A ) if isinstance(A , A ) else mask_token ) _a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=A , remove_space=A , keep_accents=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , sp_model_kwargs=self.sp_model_kwargs , **A , ) _a = do_lower_case _a = remove_space _a = keep_accents _a = vocab_file _a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def a__ (self ) -> Tuple: """simple docstring""" return len(self.sp_model ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__(self ) -> Optional[Any]: """simple docstring""" _a = self.__dict__.copy() _a = None return state def __setstate__(self , A ) -> Tuple: """simple docstring""" _a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _a = {} _a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def a__ (self , A ) -> Tuple: """simple docstring""" if self.remove_space: _a = ''' '''.join(inputs.strip().split() ) else: _a = inputs _a = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: _a = unicodedata.normalize('''NFKD''' , A ) _a = ''''''.join([c for c in outputs if not unicodedata.combining(A )] ) if self.do_lower_case: _a = outputs.lower() return outputs def a__ (self , A ) -> List[str]: """simple docstring""" _a = self.preprocess_text(A ) _a = self.sp_model.encode(A , out_type=A ) _a = [] for piece in pieces: if len(A ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): _a = self.sp_model.EncodeAsPieces(piece[:-1].replace(A , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: _a = cur_pieces[1:] else: _a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(A ) else: new_pieces.append(A ) return new_pieces def a__ (self , A ) -> Dict: """simple docstring""" return self.sp_model.PieceToId(A ) def a__ (self , A ) -> Tuple: """simple docstring""" return self.sp_model.IdToPiece(A ) def a__ (self , A ) -> Union[str, Any]: """simple docstring""" _a = [] _a = '''''' _a = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(A ) + token _a = True _a = [] else: current_sub_tokens.append(A ) _a = False out_string += self.sp_model.decode(A ) return out_string.strip() def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a__ (self , A , A = None , A = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A , token_ids_a=A , already_has_special_tokens=A ) if token_ids_a is not None: return [1] + ([0] * len(A )) + [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1] def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a__ (self , A , A = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _a = os.path.join( A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A ) elif not os.path.isfile(self.vocab_file ): with open(A , '''wb''' ) as fi: _a = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
11
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
1
'''simple docstring''' from collections.abc import Sequence def lowerCAmelCase (__A = None): """simple docstring""" if nums is None or not nums: raise ValueError('''Input sequence should not be empty''') _a = nums[0] for i in range(1 , len(__A)): _a = nums[i] _a = max(__A , ans + num , __A) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user lowercase_ = int(input("Enter number of elements : ").strip()) lowercase_ = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] print(max_subsequence_sum(array))
11
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
1
'''simple docstring''' import os import re import shutil import sys import tempfile import unittest import black lowercase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. lowercase_ = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) _a = self.transformer_dir shutil.copy( os.path.join(A , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def a__ (self , A , A , A , A=None ) -> Dict: """simple docstring""" _a = comment + f'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _a = comment + f'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _a = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _a = black.format_str(A , mode=A ) _a = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(A , '''w''' , newline='''\n''' ) as f: f.write(A ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(A ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=A ) with open(A , '''r''' ) as f: self.assertTrue(f.read() , A ) def a__ (self ) -> List[str]: """simple docstring""" _a = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(A , A ) def a__ (self ) -> Any: """simple docstring""" self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , A , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , A ) , ) # Copy consistency with a really long name _a = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( f'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , f'''{long_class_name}LMPredictionHead''' , re.sub('''Bert''' , A , A ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , A , overwrite_result=re.sub('''Bert''' , '''TestModel''' , A ) , ) def a__ (self ) -> int: """simple docstring""" _a = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) _a , _a = check_copies.convert_to_localized_md( A , A , localized_readme['''format_model_list'''] ) self.assertFalse(A ) self.assertEqual(A , A ) _a , _a = check_copies.convert_to_localized_md( A , A , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(A ) _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) _a = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) _a , _a = check_copies.convert_to_localized_md( A , A , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(A , A )
11
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
1
'''simple docstring''' import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class __A ( A ): '''simple docstring''' def a__ (self , A ) -> Union[str, Any]: """simple docstring""" with open(A , encoding='''utf-8''' ) as input_file: _a = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) _a = input_file.read() _a = regexp.search(A ) return match def a__ (self , A ) -> Optional[Any]: """simple docstring""" with open(A , encoding='''utf-8''' ) as input_file: _a = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) _a = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` _a = regexp.finditer(A ) _a = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = Path('''./datasets''' ) _a = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(A ) ): raise AssertionError(f'''open(...) must use utf-8 encoding in {dataset}''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = Path('''./datasets''' ) _a = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(A ) ): raise AssertionError(f'''print statement found in {dataset}. Use datasets.logger/logging instead.''' )
11
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
1
'''simple docstring''' import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(A ) , 'Tatoeba directory does not exist.' ) class __A ( unittest.TestCase ): '''simple docstring''' @cached_property def a__ (self ) -> List[str]: """simple docstring""" _a = tempfile.mkdtemp() return TatoebaConverter(save_dir=A ) @slow def a__ (self ) -> Dict: """simple docstring""" self.resolver.convert_models(['''heb-eng'''] ) @slow def a__ (self ) -> Tuple: """simple docstring""" _a , _a = self.resolver.write_model_card('''opus-mt-he-en''' , dry_run=A ) assert mmeta["long_pair"] == "heb-eng"
11
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
1
'''simple docstring''' from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer lowercase_ = logging.get_logger(__name__) lowercase_ = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } lowercase_ = { "vocab_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json" }, "merges_file": { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt" }, "tokenizer_config_file": { "facebook/blenderbot_small-90M": ( "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json" ) }, } lowercase_ = { "facebook/blenderbot_small-90M": 512, } class __A ( A ): '''simple docstring''' __lowerCamelCase : Dict = VOCAB_FILES_NAMES __lowerCamelCase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : int = BlenderbotSmallTokenizer def __init__(self , A=None , A=None , A="<|endoftext|>" , A="<|endoftext|>" , A="<|endoftext|>" , A=False , A=True , **A , ) -> Optional[Any]: """simple docstring""" super().__init__( ByteLevelBPETokenizer( vocab=A , merges=A , add_prefix_space=A , trim_offsets=A , ) , bos_token=A , eos_token=A , unk_token=A , **A , ) _a = add_prefix_space def a__ (self , A , A=None ) -> List[Any]: """simple docstring""" _a = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def a__ (self , A , A = None ) -> List[int]: """simple docstring""" _a = [self.sep_token_id] _a = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' 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 __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = FunnelTokenizer __lowerCamelCase : Optional[Any] = FunnelTokenizerFast __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Optional[int] = True def a__ (self ) -> Optional[Any]: """simple docstring""" super().setUp() _a = [ '''<unk>''', '''<cls>''', '''<sep>''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _a = 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 a__ (self , **A ) -> Union[str, Any]: """simple docstring""" return FunnelTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , **A ) -> Optional[Any]: """simple docstring""" return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> List[str]: """simple docstring""" _a = '''UNwant\u00E9d,running''' _a = '''unwanted, running''' return input_text, output_text def a__ (self ) -> Tuple: """simple docstring""" _a = self.tokenizer_class(self.vocab_file ) _a = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(A , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , [7, 4, 5, 10, 8, 9] ) def a__ (self ) -> List[Any]: """simple docstring""" _a = self.get_tokenizers(do_lower_case=A ) for tokenizer in tokenizers: _a = tokenizer('''UNwant\u00E9d,running''' ) _a = len(inputs['''input_ids'''] ) - 1 self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len ) _a = tokenizer('''UNwant\u00E9d,running''' , '''UNwant\u00E9d,running''' ) self.assertListEqual(inputs['''token_type_ids'''] , [2] + [0] * sentence_len + [1] * sentence_len )
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
1
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if isinstance(__A , __A): raise TypeError('''\'float\' object cannot be interpreted as an integer''') if isinstance(__A , __A): raise TypeError('''\'str\' object cannot be interpreted as an integer''') if num == 0: return "0b0" _a = False if num < 0: _a = True _a = -num _a = [] while num > 0: binary.insert(0 , num % 2) num >>= 1 if negative: return "-0b" + "".join(str(__A) for e in binary) return "0b" + "".join(str(__A) for e in binary) if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import math def lowerCAmelCase (__A): """simple docstring""" _a = math.loga(math.sqrt(4 * positive_integer + 1) / 2 + 1 / 2) return exponent == int(__A) def lowerCAmelCase (__A = 1 / 12_345): """simple docstring""" _a = 0 _a = 0 _a = 3 while True: _a = (integer**2 - 1) / 4 # if candidate is an integer, then there is a partition for k if partition_candidate == int(__A): _a = int(__A) total_partitions += 1 if check_partition_perfect(__A): perfect_partitions += 1 if perfect_partitions > 0: if perfect_partitions / total_partitions < max_proportion: return int(__A) integer += 1 if __name__ == "__main__": print(F"""{solution() = }""")
11
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowercase_ = logging.get_logger(__name__) lowercase_ = { "vocab_file": "vocab.json", "tokenizer_config_file": "tokenizer_config.json", "merges_file": "merges.txt", } lowercase_ = { "vocab_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json" ), }, "tokenizer_config_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json" ), }, "merges_file": { "facebook/s2t-wav2vec2-large-en-de": ( "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt" ), }, } lowercase_ = "</w>" lowercase_ = "@@ " def lowerCAmelCase (__A): """simple docstring""" _a = set() _a = word[0] for char in word[1:]: pairs.add((prev_char, char)) _a = char return pairs # Speech2Text2 has no max input length lowercase_ = {"facebook/s2t-wav2vec2-large-en-de": 1_024} class __A ( A ): '''simple docstring''' __lowerCamelCase : Any = VOCAB_FILES_NAMES __lowerCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : int = ['input_ids', 'attention_mask'] def __init__(self , A , A="<s>" , A="<pad>" , A="</s>" , A="<unk>" , A=False , A=None , **A , ) -> Tuple: """simple docstring""" super().__init__( unk_token=A , bos_token=A , eos_token=A , pad_token=A , do_lower_case=A , **A , ) _a = do_lower_case with open(A , encoding='''utf-8''' ) as vocab_handle: _a = json.load(A ) _a = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(f'''No merges files provided. {self.__class__.__name__} can only be used for decoding.''' ) _a = None _a = None else: with open(A , encoding='''utf-8''' ) as merges_handle: _a = merges_handle.read().split('''\n''' )[:-1] _a = [tuple(merge.split()[:2] ) for merge in merges] _a = dict(zip(A , range(len(A ) ) ) ) _a = {} @property def a__ (self ) -> int: """simple docstring""" return len(self.decoder ) def a__ (self ) -> Dict: """simple docstring""" return dict(self.encoder , **self.added_tokens_encoder ) def a__ (self , A ) -> Any: """simple docstring""" _a = tuple(token[:-1] ) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] _a = get_pairs(A ) if not pairs: return token while True: _a = min(A , key=lambda A : self.bpe_ranks.get(A , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break _a , _a = bigram _a = [] _a = 0 while i < len(A ): try: _a = word.index(A , A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _a = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _a = tuple(A ) _a = new_word if len(A ) == 1: break else: _a = get_pairs(A ) _a = ''' '''.join(A ) if word == "\n " + BPE_TOKEN_MERGES: _a = '''\n''' + BPE_TOKEN_MERGES if word.endswith(A ): _a = word.replace(A , '''''' ) _a = word.replace(''' ''' , A ) _a = word return word def a__ (self , A ) -> List[str]: """simple docstring""" if self.bpe_ranks is None: raise ValueError( '''This tokenizer was instantiated without a `merges.txt` file, so''' ''' that it can only be used for decoding, not for encoding.''' '''Make sure to provide `merges.txt` file at instantiation to enable ''' '''encoding.''' ) if self.do_lower_case: _a = text.lower() _a = text.split() _a = [] for token in text: if token: split_tokens.extend(list(self.bpe(A ).split(''' ''' ) ) ) return split_tokens def a__ (self , A ) -> int: """simple docstring""" return self.encoder.get(A , self.encoder.get(self.unk_token ) ) def a__ (self , A ) -> str: """simple docstring""" _a = self.decoder.get(A , self.unk_token ) return result def a__ (self , A ) -> str: """simple docstring""" _a = ''' '''.join(A ) # make sure @@ tokens are concatenated _a = ''''''.join(string.split(A ) ) return string def a__ (self , A , A = None ) -> Tuple[str]: """simple docstring""" if not os.path.isdir(A ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return _a = os.path.join( A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) _a = os.path.join( A , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file'''] ) with open(A , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A , ensure_ascii=A ) + '''\n''' ) _a = 0 if self.bpe_ranks is None: return (vocab_file,) with open(A , '''w''' , encoding='''utf-8''' ) as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda A : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.''' ''' Please check that the tokenizer is not corrupted!''' ) _a = token_index writer.write(''' '''.join(A ) + '''\n''' ) index += 1 return (vocab_file, merges_file)
11
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowercase_ = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 2048-bit 14: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 3072-bit 15: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 4096-bit 16: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 6144-bit 17: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 8192-bit 18: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, } class __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __A ( A ): '''simple docstring''' __lowerCamelCase : List[Any] = (DDPMScheduler,) def a__ (self , **A ) -> Optional[int]: """simple docstring""" _a = { '''num_train_timesteps''': 1_000, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**A ) return config def a__ (self ) -> Union[str, Any]: """simple docstring""" for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=A , beta_end=A ) def a__ (self ) -> int: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=A ) def a__ (self ) -> str: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=A ) def a__ (self ) -> Any: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=A ) def a__ (self ) -> str: """simple docstring""" self.check_over_configs(thresholding=A ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=A , prediction_type=A , sample_max_value=A , ) def a__ (self ) -> str: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=A ) def a__ (self ) -> int: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=A ) def a__ (self ) -> List[str]: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1E-5 def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) _a = len(A ) _a = self.dummy_model() _a = self.dummy_sample_deter _a = torch.manual_seed(0 ) for t in reversed(range(A ) ): # 1. predict noise residual _a = model(A , A ) # 2. predict previous mean of sample x_t-1 _a = scheduler.step(A , A , A , generator=A ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance _a = pred_prev_sample _a = torch.sum(torch.abs(A ) ) _a = torch.mean(torch.abs(A ) ) assert abs(result_sum.item() - 258.9606 ) < 1E-2 assert abs(result_mean.item() - 0.3372 ) < 1E-3 def a__ (self ) -> Dict: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config(prediction_type='''v_prediction''' ) _a = scheduler_class(**A ) _a = len(A ) _a = self.dummy_model() _a = self.dummy_sample_deter _a = torch.manual_seed(0 ) for t in reversed(range(A ) ): # 1. predict noise residual _a = model(A , A ) # 2. predict previous mean of sample x_t-1 _a = scheduler.step(A , A , A , generator=A ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance _a = pred_prev_sample _a = torch.sum(torch.abs(A ) ) _a = torch.mean(torch.abs(A ) ) assert abs(result_sum.item() - 202.0296 ) < 1E-2 assert abs(result_mean.item() - 0.2631 ) < 1E-3 def a__ (self ) -> Optional[int]: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) _a = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=A ) _a = scheduler.timesteps for i, timestep in enumerate(A ): if i == len(A ) - 1: _a = -1 else: _a = timesteps[i + 1] _a = scheduler.previous_timestep(A ) _a = prev_t.item() self.assertEqual(A , A ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) _a = [100, 87, 50, 51, 0] with self.assertRaises(A , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=A ) def a__ (self ) -> Tuple: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) _a = [100, 87, 50, 1, 0] _a = len(A ) with self.assertRaises(A , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=A , timesteps=A ) def a__ (self ) -> str: """simple docstring""" _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**A ) _a = [scheduler.config.num_train_timesteps] with self.assertRaises( A , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=A )
11
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
1
'''simple docstring''' import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase_ = logging.get_logger(__name__) class __A ( A ): '''simple docstring''' __lowerCamelCase : int = 'AutoTokenizer' __lowerCamelCase : Tuple = ['tokenizer'] __lowerCamelCase : List[str] = { 'semantic_prompt': 1, 'coarse_prompt': 2, 'fine_prompt': 2, } def __init__(self , A , A=None ) -> str: """simple docstring""" super().__init__(A ) _a = speaker_embeddings @classmethod def a__ (cls , A , A="speaker_embeddings_path.json" , **A ) -> Optional[int]: """simple docstring""" if speaker_embeddings_dict_path is not None: _a = get_file_from_repo( A , A , subfolder=kwargs.pop('''subfolder''' , A ) , cache_dir=kwargs.pop('''cache_dir''' , A ) , force_download=kwargs.pop('''force_download''' , A ) , proxies=kwargs.pop('''proxies''' , A ) , resume_download=kwargs.pop('''resume_download''' , A ) , local_files_only=kwargs.pop('''local_files_only''' , A ) , use_auth_token=kwargs.pop('''use_auth_token''' , A ) , revision=kwargs.pop('''revision''' , A ) , ) if speaker_embeddings_path is None: logger.warning( f'''`{os.path.join(A , A )}` does not exists , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.''' ) _a = None else: with open(A ) as speaker_embeddings_json: _a = json.load(A ) else: _a = None _a = AutoTokenizer.from_pretrained(A , **A ) return cls(tokenizer=A , speaker_embeddings=A ) def a__ (self , A , A="speaker_embeddings_path.json" , A="speaker_embeddings" , A = False , **A , ) -> int: """simple docstring""" if self.speaker_embeddings is not None: os.makedirs(os.path.join(A , A , '''v2''' ) , exist_ok=A ) _a = {} _a = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": _a = self._load_voice_preset(A ) _a = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['''repo_or_path'''] , A , f'''{prompt_key}_{key}''' ) , voice_preset[key] , allow_pickle=A , ) _a = os.path.join(A , f'''{prompt_key}_{key}.npy''' ) _a = tmp_dict with open(os.path.join(A , A ) , '''w''' ) as fp: json.dump(A , A ) super().save_pretrained(A , A , **A ) def a__ (self , A = None , **A ) -> Dict: """simple docstring""" _a = self.speaker_embeddings[voice_preset] _a = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( f'''Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].''' ) _a = get_file_from_repo( self.speaker_embeddings.get('''repo_or_path''' , '''/''' ) , voice_preset_paths[key] , subfolder=kwargs.pop('''subfolder''' , A ) , cache_dir=kwargs.pop('''cache_dir''' , A ) , force_download=kwargs.pop('''force_download''' , A ) , proxies=kwargs.pop('''proxies''' , A ) , resume_download=kwargs.pop('''resume_download''' , A ) , local_files_only=kwargs.pop('''local_files_only''' , A ) , use_auth_token=kwargs.pop('''use_auth_token''' , A ) , revision=kwargs.pop('''revision''' , A ) , ) if path is None: raise ValueError( f'''`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset} embeddings.''' ) _a = np.load(A ) return voice_preset_dict def a__ (self , A = None ) -> int: """simple docstring""" for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(f'''Voice preset unrecognized, missing {key} as a key.''' ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(f'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(f'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) def __call__(self , A=None , A=None , A="pt" , A=256 , A=False , A=True , A=False , **A , ) -> Optional[int]: """simple docstring""" if voice_preset is not None and not isinstance(A , A ): if ( isinstance(A , A ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): _a = self._load_voice_preset(A ) else: if isinstance(A , A ) and not voice_preset.endswith('''.npz''' ): _a = voice_preset + '''.npz''' _a = np.load(A ) if voice_preset is not None: self._validate_voice_preset_dict(A , **A ) _a = BatchFeature(data=A , tensor_type=A ) _a = self.tokenizer( A , return_tensors=A , padding='''max_length''' , max_length=A , return_attention_mask=A , return_token_type_ids=A , add_special_tokens=A , **A , ) if voice_preset is not None: _a = voice_preset return encoded_text
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
1
'''simple docstring''' from __future__ import annotations from fractions import Fraction def lowerCAmelCase (__A , __A): """simple docstring""" return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def lowerCAmelCase (__A): """simple docstring""" _a = [] _a = 11 _a = int('''1''' + '''0''' * digit_len) for num in range(__A , __A): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(__A , __A): solutions.append(F'''{num}/{den}''') den += 1 num += 1 _a = 10 return solutions def lowerCAmelCase (__A = 2): """simple docstring""" _a = 1.0 for fraction in fraction_list(__A): _a = Fraction(__A) result *= frac.denominator / frac.numerator return int(__A) if __name__ == "__main__": print(solution())
11
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = 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 _a = image_processing(A , 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'''], ) , )
11
1
'''simple docstring''' def lowerCAmelCase (__A = 1 , __A = 1_000): """simple docstring""" _a = 1 _a = 0 for divide_by_number in range(__A , digit + 1): _a = [] _a = numerator for _ in range(1 , digit + 1): if now_divide in has_been_divided: if longest_list_length < len(__A): _a = len(__A) _a = divide_by_number else: has_been_divided.append(__A) _a = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
1
'''simple docstring''' from __future__ import annotations from typing import Any class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = num_of_nodes _a = [] _a = {} def a__ (self , A , A , A ) -> None: """simple docstring""" self.m_edges.append([u_node, v_node, weight] ) def a__ (self , A ) -> int: """simple docstring""" if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def a__ (self , A ) -> None: """simple docstring""" if self.m_component[u_node] != u_node: for k in self.m_component: _a = self.find_component(A ) def a__ (self , A , A , A ) -> None: """simple docstring""" if component_size[u_node] <= component_size[v_node]: _a = v_node component_size[v_node] += component_size[u_node] self.set_component(A ) elif component_size[u_node] >= component_size[v_node]: _a = self.find_component(A ) component_size[u_node] += component_size[v_node] self.set_component(A ) def a__ (self ) -> None: """simple docstring""" _a = [] _a = 0 _a = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) _a = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: _a , _a , _a = edge _a = self.m_component[u] _a = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): _a = [u, v, w] for edge in minimum_weight_edge: if isinstance(A , A ): _a , _a , _a = edge _a = self.m_component[u] _a = self.m_component[v] if u_component != v_component: mst_weight += w self.union(A , A , A ) print(f'''Added edge [{u} - {v}]\nAdded weight: {w}\n''' ) num_of_components -= 1 _a = [-1] * self.m_num_of_nodes print(f'''The total weight of the minimal spanning tree is: {mst_weight}''' ) def lowerCAmelCase (): """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase_ = {"tokenization_wav2vec2_phoneme": ["Wav2Vec2PhonemeCTCTokenizer"]} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
1
'''simple docstring''' from math import sqrt def lowerCAmelCase (__A = 1_000_000): """simple docstring""" _a = 0 _a = 0 _a = 42 while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2 , 2 * max_cuboid_size + 1): if sqrt(sum_shortest_sides**2 + max_cuboid_size**2).is_integer(): num_cuboids += ( min(__A , sum_shortest_sides // 2) - max(1 , sum_shortest_sides - max_cuboid_size) + 1 ) return max_cuboid_size if __name__ == "__main__": print(F"""{solution() = }""")
11
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
1
'''simple docstring''' import random def lowerCAmelCase (__A , __A , __A = False): """simple docstring""" _a = {i: [] for i in range(__A)} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(__A) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(__A): for j in range(i + 1 , __A): if random.random() < probability: graph[i].append(__A) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(__A) return graph def lowerCAmelCase (__A): """simple docstring""" return { i: [j for j in range(__A) if i != j] for i in range(__A) } if __name__ == "__main__": import doctest doctest.testmod()
11
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
1
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" if len(__A) == 0: return [] _a , _a = min(__A), max(__A) _a = int(max_value - min_value) + 1 _a = [[] for _ in range(__A)] for i in my_list: buckets[int(i - min_value)].append(__A) return [v for bucket in buckets for v in sorted(__A)] if __name__ == "__main__": from doctest import testmod testmod() assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
11
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
1
'''simple docstring''' import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures lowercase_ = logging.get_logger(__name__) @dataclass class __A : '''simple docstring''' __lowerCamelCase : str = field(metadata={'help': 'The name of the task to train on: ' + ', '.join(glue_processors.keys() )} ) __lowerCamelCase : str = field( metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'} ) __lowerCamelCase : int = field( default=128 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) __lowerCamelCase : bool = field( default=A , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.task_name.lower() class __A ( A ): '''simple docstring''' __lowerCamelCase : List[Any] = 'train' __lowerCamelCase : Tuple = 'dev' __lowerCamelCase : Tuple = 'test' class __A ( A ): '''simple docstring''' __lowerCamelCase : GlueDataTrainingArguments __lowerCamelCase : str __lowerCamelCase : List[InputFeatures] def __init__(self , A , A , A = None , A = Split.train , A = None , ) -> Optional[Any]: """simple docstring""" warnings.warn( '''This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ''' '''library. You can have a look at this example script for pointers: ''' '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py''' , A , ) _a = args _a = glue_processors[args.task_name]() _a = glue_output_modes[args.task_name] if isinstance(A , A ): try: _a = Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) # Load data features from cache or dataset file _a = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}''' , ) _a = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) _a , _a = label_list[2], label_list[1] _a = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _a = cached_features_file + '''.lock''' with FileLock(A ): if os.path.exists(A ) and not args.overwrite_cache: _a = time.time() _a = torch.load(A ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''' , time.time() - start ) else: logger.info(f'''Creating features from dataset file at {args.data_dir}''' ) if mode == Split.dev: _a = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: _a = self.processor.get_test_examples(args.data_dir ) else: _a = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: _a = examples[:limit_length] _a = glue_convert_examples_to_features( A , A , max_length=args.max_seq_length , label_list=A , output_mode=self.output_mode , ) _a = time.time() torch.save(self.features , A ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__(self ) -> Any: """simple docstring""" return len(self.features ) def __getitem__(self , A ) -> InputFeatures: """simple docstring""" return self.features[i] def a__ (self ) -> int: """simple docstring""" return self.label_list
11
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """simple docstring""" super().__init__() self.register_modules( vae=A , text_encoder=A , clip_model=A , tokenizer=A , unet=A , scheduler=A , feature_extractor=A , coca_model=A , coca_tokenizer=A , coca_transform=A , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , A ) set_requires_grad(self.clip_model , A ) def a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = latents - sigma * noise_pred else: raise ValueError(f'''scheduler type {type(self.scheduler )} not supported''' ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """simple docstring""" if isinstance(A , A ) and len(A ) != batch_size: raise ValueError(f'''You have passed {batch_size} batch_size, but only {len(A )} generators.''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if isinstance(A , torch.Generator ) and batch_size > 1: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.join(A ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(A ): raise ValueError( f'''Content prompt is None and CoCa [{coca_is_none_str}] is None.''' f'''Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) if style_prompt is None: if len(A ): raise ValueError( f'''Style prompt is None and CoCa [{coca_is_none_str}] is None.''' f''' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 1 self.scheduler.set_timesteps(A , **A ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = slerp( A , A , A ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = uncond_embeddings.repeat_interleave(A , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _a = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = torch.randn(A , generator=A , device=self.device , dtype=A ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowercase_ = { "configuration_conditional_detr": [ "CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConditionalDetrConfig", "ConditionalDetrOnnxConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["ConditionalDetrFeatureExtractor"] lowercase_ = ["ConditionalDetrImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST", "ConditionalDetrForObjectDetection", "ConditionalDetrForSegmentation", "ConditionalDetrModel", "ConditionalDetrPreTrainedModel", ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = 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(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
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 , __lowerCAmelCase , __lowerCAmelCase=1_3 , __lowerCAmelCase=7 , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=False , __lowerCAmelCase=False , __lowerCAmelCase=False , __lowerCAmelCase=2 , __lowerCAmelCase=9_9 , __lowerCAmelCase=0 , __lowerCAmelCase=3_2 , __lowerCAmelCase=5 , __lowerCAmelCase=4 , __lowerCAmelCase=0.1 , __lowerCAmelCase=0.1 , __lowerCAmelCase=5_1_2 , __lowerCAmelCase=1_2 , __lowerCAmelCase=2 , __lowerCAmelCase=0.02 , __lowerCAmelCase=3 , __lowerCAmelCase=4 , __lowerCAmelCase="last" , __lowerCAmelCase=None , __lowerCAmelCase=None , ): """simple docstring""" __magic_name__ :Dict = parent __magic_name__ :List[str] = batch_size __magic_name__ :Optional[Any] = seq_length __magic_name__ :Union[str, Any] = is_training __magic_name__ :Dict = use_input_lengths __magic_name__ :Tuple = use_token_type_ids __magic_name__ :Dict = use_labels __magic_name__ :List[str] = gelu_activation __magic_name__ :Dict = sinusoidal_embeddings __magic_name__ :List[Any] = causal __magic_name__ :Dict = asm __magic_name__ :Union[str, Any] = n_langs __magic_name__ :List[Any] = vocab_size __magic_name__ :int = n_special __magic_name__ :Tuple = hidden_size __magic_name__ :Optional[int] = num_hidden_layers __magic_name__ :Optional[Any] = num_attention_heads __magic_name__ :Any = hidden_dropout_prob __magic_name__ :List[str] = attention_probs_dropout_prob __magic_name__ :Any = max_position_embeddings __magic_name__ :Tuple = type_vocab_size __magic_name__ :Optional[int] = type_sequence_label_size __magic_name__ :Tuple = initializer_range __magic_name__ :Dict = num_labels __magic_name__ :Any = num_choices __magic_name__ :Optional[Any] = summary_type __magic_name__ :Dict = use_proj __magic_name__ :Tuple = scope def A ( self ): """simple docstring""" __magic_name__ :List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ :Dict = random_attention_mask([self.batch_size, self.seq_length] ) __magic_name__ :int = None if self.use_input_lengths: __magic_name__ :List[Any] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length __magic_name__ :Dict = None if self.use_token_type_ids: __magic_name__ :Dict = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) __magic_name__ :str = None __magic_name__ :Union[str, Any] = None __magic_name__ :int = None if self.use_labels: __magic_name__ :Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __magic_name__ :List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __magic_name__ :Optional[int] = ids_tensor([self.batch_size] , 2 ).float() __magic_name__ :Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) __magic_name__ :int = self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def A ( self ): """simple docstring""" 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 A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :Union[str, Any] = FlaubertModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :str = model(__lowerCAmelCase , lengths=__lowerCAmelCase , langs=__lowerCAmelCase ) __magic_name__ :Union[str, Any] = model(__lowerCAmelCase , langs=__lowerCAmelCase ) __magic_name__ :List[Any] = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :str = FlaubertWithLMHeadModel(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :List[str] = model(__lowerCAmelCase , token_type_ids=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :Union[str, Any] = FlaubertForQuestionAnsweringSimple(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :int = model(__lowerCAmelCase ) __magic_name__ :Optional[int] = model(__lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase ) 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 A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :Tuple = FlaubertForQuestionAnswering(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :Optional[Any] = model(__lowerCAmelCase ) __magic_name__ :int = model( __lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase , cls_index=__lowerCAmelCase , is_impossible=__lowerCAmelCase , p_mask=__lowerCAmelCase , ) __magic_name__ :Optional[Any] = model( __lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase , cls_index=__lowerCAmelCase , is_impossible=__lowerCAmelCase , ) ((__magic_name__) , ) :Any = result_with_labels.to_tuple() __magic_name__ :str = model(__lowerCAmelCase , start_positions=__lowerCAmelCase , end_positions=__lowerCAmelCase ) ((__magic_name__) , ) :List[str] = 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 A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :Union[str, Any] = FlaubertForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :int = model(__lowerCAmelCase ) __magic_name__ :List[str] = model(__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :List[str] = self.num_labels __magic_name__ :Any = FlaubertForTokenClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :List[Any] = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): """simple docstring""" __magic_name__ :Dict = self.num_choices __magic_name__ :Any = FlaubertForMultipleChoice(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() __magic_name__ :int = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __magic_name__ :Any = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __magic_name__ :List[str] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() __magic_name__ :Any = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , token_type_ids=__lowerCAmelCase , labels=__lowerCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def A ( self ): """simple docstring""" __magic_name__ :Dict = self.prepare_config_and_inputs() ( ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ) :int = config_and_inputs __magic_name__ :str = { '''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__ = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) a__ = ( { '''feature-extraction''': FlaubertModel, '''fill-mask''': FlaubertWithLMHeadModel, '''question-answering''': FlaubertForQuestionAnsweringSimple, '''text-classification''': FlaubertForSequenceClassification, '''token-classification''': FlaubertForTokenClassification, '''zero-shot''': FlaubertForSequenceClassification, } if is_torch_available() else {} ) def A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): """simple docstring""" 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 A ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False ): """simple docstring""" __magic_name__ :int = super()._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase , return_labels=__lowerCAmelCase ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": __magic_name__ :Union[str, Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__lowerCAmelCase ) __magic_name__ :Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__lowerCAmelCase ) return inputs_dict def A ( self ): """simple docstring""" __magic_name__ :Optional[Any] = FlaubertModelTester(self ) __magic_name__ :List[str] = ConfigTester(self , config_class=__lowerCAmelCase , emb_dim=3_7 ) def A ( self ): """simple docstring""" self.config_tester.run_common_tests() def A ( self ): """simple docstring""" __magic_name__ :Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*__lowerCAmelCase ) def A ( self ): """simple docstring""" __magic_name__ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCAmelCase ) @slow def A ( self ): """simple docstring""" for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __magic_name__ :List[str] = FlaubertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @slow @require_torch_gpu def A ( self ): """simple docstring""" __magic_name__ , __magic_name__ :List[Any] = 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 __magic_name__ :List[str] = True __magic_name__ :Union[str, Any] = model_class(config=__lowerCAmelCase ) __magic_name__ :Union[str, Any] = self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) __magic_name__ :Union[str, Any] = torch.jit.trace( __lowerCAmelCase , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(__lowerCAmelCase , os.path.join(__lowerCAmelCase , '''traced_model.pt''' ) ) __magic_name__ :Any = torch.jit.load(os.path.join(__lowerCAmelCase , '''traced_model.pt''' ) , map_location=__lowerCAmelCase ) loaded(inputs_dict['''input_ids'''].to(__lowerCAmelCase ) , inputs_dict['''attention_mask'''].to(__lowerCAmelCase ) ) @require_torch class lowerCamelCase_ ( unittest.TestCase ): @slow def A ( self ): """simple docstring""" __magic_name__ :int = FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) __magic_name__ :int = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) with torch.no_grad(): __magic_name__ :List[str] = model(__lowerCAmelCase )[0] __magic_name__ :List[Any] = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , __lowerCAmelCase ) __magic_name__ :Optional[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] , __lowerCAmelCase , atol=1E-4 ) )
0
'''simple docstring''' 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 lowercase_ = { "/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 lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = 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: _a = 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: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "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 lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = 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") lowercase_ = 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, )
11
0
def _A ( _lowercase , _lowercase ) -> float: """simple docstring""" if density <= 0: raise ValueError('Impossible fluid density' ) if bulk_modulus <= 0: raise ValueError('Impossible bulk modulus' ) return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
1
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
0
from __future__ import annotations def SCREAMING_SNAKE_CASE_ ( _snake_case :int = 4 ) -> list[list[int]]: _A = abs(_snake_case ) or 4 return [[1 + x + y * row_size for x in range(_snake_case )] for y in range(_snake_case )] def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: return reverse_row(transpose(_snake_case ) ) # OR.. transpose(reverse_column(matrix)) def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: return reverse_row(reverse_column(_snake_case ) ) # OR.. reverse_column(reverse_row(matrix)) def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: return reverse_column(transpose(_snake_case ) ) # OR.. transpose(reverse_row(matrix)) def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: _A = [list(_snake_case ) for x in zip(*_snake_case )] return matrix def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: _A = matrix[::-1] return matrix def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> list[list[int]]: _A = [x[::-1] for x in matrix] return matrix def SCREAMING_SNAKE_CASE_ ( _snake_case :list[list[int]] ) -> None: for i in matrix: print(*_snake_case ) if __name__ == "__main__": UpperCAmelCase_ = make_matrix() print("""\norigin:\n""") print_matrix(matrix) print("""\nrotate 90 counterclockwise:\n""") print_matrix(rotate_aa(matrix)) UpperCAmelCase_ = make_matrix() print("""\norigin:\n""") print_matrix(matrix) print("""\nrotate 180:\n""") print_matrix(rotate_aaa(matrix)) UpperCAmelCase_ = make_matrix() print("""\norigin:\n""") print_matrix(matrix) print("""\nrotate 270 counterclockwise:\n""") print_matrix(rotate_aaa(matrix))
2
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
0
'''simple docstring''' import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py lowerCAmelCase : List[str] = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase : Union[str, Any] = direct_transformers_import(PATH_TO_TRANSFORMERS) lowerCAmelCase : str = transformers.models.auto.configuration_auto.CONFIG_MAPPING lowerCAmelCase : Any = { # used to compute the property `self.chunk_length` 'EncodecConfig': ['overlap'], # used as `self.bert_model = BertModel(config, ...)` 'DPRConfig': True, # not used in modeling files, but it's an important information 'FSMTConfig': ['langs'], # used internally in the configuration class file 'GPTNeoConfig': ['attention_types'], # used internally in the configuration class file 'EsmConfig': ['is_folding_model'], # used during training (despite we don't have training script for these models yet) 'Mask2FormerConfig': ['ignore_value'], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) 'OneFormerConfig': ['ignore_value', 'norm'], # used during preprocessing and collation, see `collating_graphormer.py` 'GraphormerConfig': ['spatial_pos_max'], # used internally in the configuration class file 'T5Config': ['feed_forward_proj'], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally 'MT5Config': ['feed_forward_proj', 'tokenizer_class'], 'UMT5Config': ['feed_forward_proj', 'tokenizer_class'], # used internally in the configuration class file 'LongT5Config': ['feed_forward_proj'], # used internally in the configuration class file 'SwitchTransformersConfig': ['feed_forward_proj'], # having default values other than `1e-5` - we can't fix them without breaking 'BioGptConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'GLPNConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'SegformerConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'CvtConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'PerceiverConfig': ['layer_norm_eps'], # used internally to calculate the feature size 'InformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'TimeSeriesTransformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'AutoformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate `mlp_dim` 'SamVisionConfig': ['mlp_ratio'], # For (head) training, but so far not implemented 'ClapAudioConfig': ['num_classes'], # Not used, but providing useful information to users 'SpeechT5HifiGanConfig': ['sampling_rate'], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { 'CLIPSegConfig': True, 'DeformableDetrConfig': True, 'DetaConfig': True, 'DinatConfig': True, 'DonutSwinConfig': True, 'EfficientFormerConfig': True, 'FSMTConfig': True, 'JukeboxConfig': True, 'LayoutLMv2Config': True, 'MaskFormerSwinConfig': True, 'MT5Config': True, 'NatConfig': True, 'OneFormerConfig': True, 'PerceiverConfig': True, 'RagConfig': True, 'SpeechT5Config': True, 'SwinConfig': True, 'Swin2SRConfig': True, 'Swinv2Config': True, 'SwitchTransformersConfig': True, 'TableTransformerConfig': True, 'TapasConfig': True, 'TransfoXLConfig': True, 'UniSpeechConfig': True, 'UniSpeechSatConfig': True, 'WavLMConfig': True, 'WhisperConfig': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) 'JukeboxPriorConfig': True, # TODO: @Younes (for `is_decoder`) 'Pix2StructTextConfig': True, } ) def A_( A : Optional[int] , A : List[str] , A : Tuple , A : Tuple): UpperCamelCase = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( f'''config.{attribute}''' in modeling_source or f'''getattr(config, "{attribute}"''' in modeling_source or f'''getattr(self.config, "{attribute}"''' in modeling_source ): UpperCamelCase = True # Deal with multi-line cases elif ( re.search( rf'''getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*"{attribute}"''' , A , ) is not None ): UpperCamelCase = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: UpperCamelCase = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files UpperCamelCase = [ 'bos_index', 'eos_index', 'pad_index', 'unk_index', 'mask_index', 'image_size', 'use_cache', 'out_features', 'out_indices', ] UpperCamelCase = ['encoder_no_repeat_ngram_size'] # Special cases to be allowed UpperCamelCase = True if not attribute_used: UpperCamelCase = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: UpperCamelCase = True elif attribute in ["tie_word_embeddings"] and default_value is False: UpperCamelCase = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: UpperCamelCase = True elif attribute.endswith('_token_id'): UpperCamelCase = True # configuration class specific cases if not case_allowed: UpperCamelCase = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , []) UpperCamelCase = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def A_( A : Optional[Any]): UpperCamelCase = dict(inspect.signature(config_class.__init__).parameters) UpperCamelCase = [x for x in list(signature.keys()) if x not in ['self', 'kwargs']] UpperCamelCase = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass UpperCamelCase = {} if len(config_class.attribute_map) > 0: UpperCamelCase = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files UpperCamelCase = inspect.getsourcefile(A) UpperCamelCase = os.path.dirname(A) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. UpperCamelCase = [os.path.join(A , A) for fn in os.listdir(A) if fn.startswith('modeling_')] # Get the source code strings UpperCamelCase = [] for path in modeling_paths: if os.path.isfile(A): with open(A) as fp: modeling_sources.append(fp.read()) UpperCamelCase = [] for config_param, default_value in zip(A , A): # `attributes` here is all the variant names for `config_param` UpperCamelCase = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param]) if not check_attribute_being_used(A , A , A , A): unused_attributes.append(attributes[0]) return sorted(A) def A_( ): UpperCamelCase = {} for _config_class in list(CONFIG_MAPPING.values()): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) UpperCamelCase = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class) , lambda A: inspect.isclass(A) and issubclass(A , A) and inspect.getmodule(A) == inspect.getmodule(_config_class) , ) ] for config_class in config_classes_in_module: UpperCamelCase = check_config_attributes_being_used(A) if len(A) > 0: UpperCamelCase = unused_attributes if len(A) > 0: UpperCamelCase = 'The following configuration classes contain unused attributes in the corresponding modeling files:\n' for name, attributes in configs_with_unused_attributes.items(): error += f'''{name}: {attributes}\n''' raise ValueError(A) if __name__ == "__main__": check_config_attributes()
3
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
0
"""simple docstring""" def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : float , _UpperCAmelCase : float ): if mass < 0: raise ValueError('The mass of a body cannot be negative' ) return 0.5 * mass * abs(_UpperCAmelCase ) * abs(_UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
4
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
0
'''simple docstring''' from typing import List, Optional, Tuple, Union import PIL import torch from torchvision import transforms from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput from diffusers.schedulers import DDIMScheduler from diffusers.utils import randn_tensor _lowercase = transforms.Compose( [ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize([0.5], [0.5]), ] ) def A (__lowerCamelCase :List[str] ): if isinstance(__lowerCamelCase , torch.Tensor ): return image elif isinstance(__lowerCamelCase , PIL.Image.Image ): _lowerCAmelCase = [image] _lowerCAmelCase = [trans(img.convert("""RGB""" ) ) for img in image] _lowerCAmelCase = torch.stack(__lowerCamelCase ) return image class UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _lowercase , _lowercase ): """simple docstring""" super().__init__() # make sure scheduler can always be converted to DDIM _lowerCAmelCase = DDIMScheduler.from_config(scheduler.config ) self.register_modules(unet=_lowercase , scheduler=_lowercase ) def _lowercase ( self , _lowercase ): """simple docstring""" if strength < 0 or strength > 1: raise ValueError(F'The value of strength should in [0.0, 1.0] but is {strength}' ) def _lowercase ( self , _lowercase , _lowercase , _lowercase ): """simple docstring""" _lowerCAmelCase = min(int(num_inference_steps * strength ) , _lowercase ) _lowerCAmelCase = max(num_inference_steps - init_timestep , 0 ) _lowerCAmelCase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def _lowercase ( self , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase=None ): """simple docstring""" if not isinstance(_lowercase , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( F'`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(_lowercase )}' ) _lowerCAmelCase = image.to(device=_lowercase , dtype=_lowercase ) if isinstance(_lowercase , _lowercase ) and len(_lowercase ) != batch_size: raise ValueError( F'You have passed a list of generators of length {len(_lowercase )}, but requested an effective batch' F' size of {batch_size}. Make sure the batch size matches the length of the generators.' ) _lowerCAmelCase = init_latents.shape _lowerCAmelCase = randn_tensor(_lowercase , generator=_lowercase , device=_lowercase , dtype=_lowercase ) # get latents print("""add noise to latents at timestep""" , _lowercase ) _lowerCAmelCase = self.scheduler.add_noise(_lowercase , _lowercase , _lowercase ) _lowerCAmelCase = init_latents return latents @torch.no_grad() def __call__( self , _lowercase = None , _lowercase = 0.8 , _lowercase = 1 , _lowercase = None , _lowercase = 0.0 , _lowercase = 50 , _lowercase = None , _lowercase = "pil" , _lowercase = True , ): """simple docstring""" self.check_inputs(_lowercase ) # 2. Preprocess image _lowerCAmelCase = preprocess(_lowercase ) # 3. set timesteps self.scheduler.set_timesteps(_lowercase , device=self.device ) _lowerCAmelCase , _lowerCAmelCase = self.get_timesteps(_lowercase , _lowercase , self.device ) _lowerCAmelCase = timesteps[:1].repeat(_lowercase ) # 4. Prepare latent variables _lowerCAmelCase = self.prepare_latents(_lowercase , _lowercase , _lowercase , self.unet.dtype , self.device , _lowercase ) _lowerCAmelCase = latents # 5. Denoising loop for t in self.progress_bar(_lowercase ): # 1. predict noise model_output _lowerCAmelCase = self.unet(_lowercase , _lowercase ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 _lowerCAmelCase = self.scheduler.step( _lowercase , _lowercase , _lowercase , eta=_lowercase , use_clipped_model_output=_lowercase , generator=_lowercase , ).prev_sample _lowerCAmelCase = (image / 2 + 0.5).clamp(0 , 1 ) _lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _lowerCAmelCase = self.numpy_to_pil(_lowercase ) if not return_dict: return (image, latent_timestep.item()) return ImagePipelineOutput(images=_lowercase )
5
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
0
def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: float , UpperCamelCase__: float ): if mass < 0: raise ValueError("""The mass of a body cannot be negative""" ) return 0.5 * mass * abs(UpperCamelCase__ ) * abs(UpperCamelCase__ ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
6
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
0
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging a = logging.get_logger(__name__) class lowercase_ ( __lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Tuple = ['''input_features''', '''attention_mask'''] def __init__( self : Optional[Any] , _UpperCAmelCase : int=80 , _UpperCAmelCase : Any=16_000 , _UpperCAmelCase : Dict=0.0 , _UpperCAmelCase : Optional[Any]=10 , _UpperCAmelCase : Optional[int]=25 , _UpperCAmelCase : List[str]="hamming_window" , _UpperCAmelCase : Dict=3_2768.0 , _UpperCAmelCase : int=0.97 , _UpperCAmelCase : List[str]=1.0 , _UpperCAmelCase : Tuple=True , _UpperCAmelCase : Union[str, Any]=True , _UpperCAmelCase : int=False , **_UpperCAmelCase : List[Any] , ): super().__init__(feature_size=_UpperCAmelCase , sampling_rate=_UpperCAmelCase , padding_value=_UpperCAmelCase , **_UpperCAmelCase ) _A = feature_size _A = sampling_rate _A = padding_value _A = hop_length _A = win_length _A = frame_signal_scale _A = preemphasis_coeff _A = mel_floor _A = normalize_means _A = normalize_vars _A = win_function _A = return_attention_mask _A = win_length * sampling_rate // 1_000 _A = hop_length * sampling_rate // 1_000 _A = optimal_fft_length(self.sample_size ) _A = (self.n_fft // 2) + 1 def lowerCAmelCase_ ( self : List[Any] , _UpperCAmelCase : np.array ): if self.win_function == "hamming_window": _A = window_function(window_length=self.sample_size , name=self.win_function , periodic=_UpperCAmelCase ) else: _A = window_function(window_length=self.sample_size , name=self.win_function ) _A = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) _A = spectrogram( one_waveform * self.frame_signal_scale , window=_UpperCAmelCase , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=_UpperCAmelCase , preemphasis=self.preemphasis_coeff , mel_filters=_UpperCAmelCase , mel_floor=self.mel_floor , log_mel='log' , ) return msfc_features.T def lowerCAmelCase_ ( self : Optional[int] , _UpperCAmelCase : Dict , _UpperCAmelCase : List[str] , _UpperCAmelCase : List[Any] ): # make sure we normalize float32 arrays if self.normalize_means: _A = x[:input_length].mean(axis=0 ) _A = np.subtract(_UpperCAmelCase , _UpperCAmelCase ) if self.normalize_vars: _A = x[:input_length].std(axis=0 ) _A = np.divide(_UpperCAmelCase , _UpperCAmelCase ) if input_length < x.shape[0]: _A = padding_value # make sure array is in float32 _A = x.astype(np.floataa ) return x def lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : List[np.ndarray] , _UpperCAmelCase : Optional[np.ndarray] = None ): _A = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(_UpperCAmelCase , _UpperCAmelCase , self.padding_value ) for x, n in zip(_UpperCAmelCase , _UpperCAmelCase )] def __call__( self : Dict , _UpperCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , _UpperCAmelCase : Union[bool, str, PaddingStrategy] = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : bool = False , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[bool] = None , _UpperCAmelCase : Optional[Union[str, TensorType]] = None , _UpperCAmelCase : Optional[int] = None , **_UpperCAmelCase : Optional[int] , ): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of''' F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with''' F''' {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) _A = isinstance(_UpperCAmelCase , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) _A = is_batched_numpy or ( isinstance(_UpperCAmelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _A = [np.asarray(_UpperCAmelCase , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(_UpperCAmelCase , np.ndarray ): _A = np.asarray(_UpperCAmelCase , dtype=np.floataa ) elif isinstance(_UpperCAmelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): _A = raw_speech.astype(np.floataa ) # always return batch if not is_batched: _A = [raw_speech] # extract fbank features _A = [self._extract_mfsc_features(_UpperCAmelCase ) for one_waveform in raw_speech] # convert into correct format for padding _A = BatchFeature({'input_features': features} ) _A = self.pad( _UpperCAmelCase , padding=_UpperCAmelCase , max_length=_UpperCAmelCase , truncation=_UpperCAmelCase , pad_to_multiple_of=_UpperCAmelCase , return_attention_mask=_UpperCAmelCase , **_UpperCAmelCase , ) # make sure list is in array format _A = padded_inputs.get('input_features' ) if isinstance(input_features[0] , _UpperCAmelCase ): _A = [np.asarray(_UpperCAmelCase , dtype=np.floataa ) for feature in input_features] _A = padded_inputs.get('attention_mask' ) if attention_mask is not None: _A = [np.asarray(_UpperCAmelCase , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: _A = ( np.array(_UpperCAmelCase , dtype=np.intaa ) if self._get_padding_strategies(_UpperCAmelCase , max_length=_UpperCAmelCase ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) _A = self.normalize( padded_inputs['input_features'] , attention_mask=_UpperCAmelCase ) if return_tensors is not None: _A = padded_inputs.convert_to_tensors(_UpperCAmelCase ) return padded_inputs
7
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
0
'''simple docstring''' import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model lowercase__ : int = '''0.12''' # assumed parallelism: 8 if is_torch_available(): import torch def _lowerCAmelCase ( __snake_case : List[str] , __snake_case : Dict , __snake_case : str=None ) -> Tuple: if rng is None: __A : str = random.Random() __A : Optional[Any] = 1 for dim in shape: total_dims *= dim __A : str = [] for _ in range(__snake_case ): values.append(rng.randint(0 , vocab_size - 1 ) ) __A : List[Any] = np.array(__snake_case , dtype=jnp.intaa ).reshape(__snake_case ) return output def _lowerCAmelCase ( __snake_case : Optional[Any] , __snake_case : Optional[int]=None ) -> str: __A : Optional[int] = ids_tensor(__snake_case , vocab_size=2 , rng=__snake_case ) # make sure that at least one token is attended to for each batch __A : Any = 1 return attn_mask @require_flax class SCREAMING_SNAKE_CASE : lowerCAmelCase = None lowerCAmelCase = () def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A : List[str] = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 __A : str = 2 __A : Dict = inputs['input_ids'].shape[-1] // 2 __A : Union[str, Any] = inputs['input_ids'][:max_batch_size, :sequence_length] __A : Dict = jnp.ones_like(_UpperCAmelCase) __A : List[str] = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens __A : Optional[Any] = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` __A : Dict = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : List[Any] = self._get_input_ids_and_config() __A : Optional[int] = False __A : List[Any] = max_length __A : Union[str, Any] = 0 for model_class in self.all_generative_model_classes: __A : List[Any] = model_class(_UpperCAmelCase) __A : Optional[int] = model_class.__name__[4:] # Skip the "Flax" at the beginning __A : List[Any] = getattr(_UpperCAmelCase , _UpperCAmelCase) __A : str = pt_model_class(_UpperCAmelCase).eval() __A : int = load_flax_weights_in_pytorch_model(_UpperCAmelCase , flax_model.params) __A : Dict = flax_model.generate(_UpperCAmelCase).sequences __A : Optional[Any] = pt_model.generate(torch.tensor(_UpperCAmelCase , dtype=torch.long)) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: __A : Optional[int] = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : List[Any] = self._get_input_ids_and_config() __A : Optional[Any] = False __A : str = max_length for model_class in self.all_generative_model_classes: __A : Union[str, Any] = model_class(_UpperCAmelCase) __A : int = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Dict = jit(model.generate) __A : Any = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : int = self._get_input_ids_and_config() __A : Any = True __A : int = max_length for model_class in self.all_generative_model_classes: __A : Union[str, Any] = model_class(_UpperCAmelCase) __A : Tuple = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Dict = jit(model.generate) __A : List[Any] = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Union[str, Any] = self._get_input_ids_and_config() __A : int = False __A : int = max_length __A : str = 2 for model_class in self.all_generative_model_classes: __A : Optional[int] = model_class(_UpperCAmelCase) __A : Tuple = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Dict = jit(model.generate) __A : str = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : str = self._get_input_ids_and_config() __A : Optional[Any] = False __A : Optional[Any] = max_length __A : Any = 2 __A : Dict = 2 for model_class in self.all_generative_model_classes: __A : Dict = model_class(_UpperCAmelCase) __A : Any = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Any = self._get_input_ids_and_config() __A : Tuple = True __A : Any = max_length __A : Optional[Any] = 0.8 __A : Any = 10 __A : Optional[int] = 0.3 __A : str = 1 __A : Dict = 8 __A : str = 9 for model_class in self.all_generative_model_classes: __A : Dict = model_class(_UpperCAmelCase) __A : Tuple = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : List[Any] = jit(model.generate) __A : List[Any] = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Optional[Any] = self._get_input_ids_and_config() __A : Union[str, Any] = max_length __A : Any = 1 __A : str = 8 __A : Dict = 9 for model_class in self.all_generative_model_classes: __A : int = model_class(_UpperCAmelCase) __A : Any = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : str = jit(model.generate) __A : int = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Any = self._get_input_ids_and_config() __A : str = max_length __A : Tuple = 2 __A : Optional[Any] = 1 __A : List[Any] = 8 __A : str = 9 for model_class in self.all_generative_model_classes: __A : Tuple = model_class(_UpperCAmelCase) __A : Optional[int] = model.generate(_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Union[str, Any] = jit(model.generate) __A : str = jit_generate(_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : List[Any] = self._get_input_ids_and_config() # pad attention mask on the left __A : Any = attention_mask.at[(0, 0)].set(0) __A : Optional[Any] = False __A : Union[str, Any] = max_length for model_class in self.all_generative_model_classes: __A : Any = model_class(_UpperCAmelCase) __A : Any = model.generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Optional[int] = jit(model.generate) __A : Union[str, Any] = jit_generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Optional[Any] = self._get_input_ids_and_config() # pad attention mask on the left __A : Dict = attention_mask.at[(0, 0)].set(0) __A : str = True __A : Dict = max_length for model_class in self.all_generative_model_classes: __A : Any = model_class(_UpperCAmelCase) __A : List[str] = model.generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : Optional[Any] = jit(model.generate) __A : Union[str, Any] = jit_generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A ,__A ,__A : Optional[int] = self._get_input_ids_and_config() # pad attention mask on the left __A : List[str] = attention_mask.at[(0, 0)].set(0) __A : int = 2 __A : str = max_length for model_class in self.all_generative_model_classes: __A : str = model_class(_UpperCAmelCase) __A : Optional[Any] = model.generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertEqual(generation_outputs.shape[-1] , _UpperCAmelCase) __A : str = jit(model.generate) __A : List[Any] = jit_generate(_UpperCAmelCase , attention_mask=_UpperCAmelCase).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist()) @require_flax class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[Any] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert') __A : List[str] = FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only') __A : List[Any] = 'Hello world' __A : Union[str, Any] = tokenizer(_UpperCAmelCase , return_tensors='np').input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(_UpperCAmelCase , 'do_samples'): model.generate(_UpperCAmelCase , do_samples=_UpperCAmelCase) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(_UpperCAmelCase , 'foo'): __A : List[str] = {'foo': 'bar'} model.generate(_UpperCAmelCase , **_UpperCAmelCase)
8
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
0
import heapq as hq import math from collections.abc import Iterator class __lowerCAmelCase : """simple docstring""" def __init__( self : List[Any] , _snake_case : Tuple ): """simple docstring""" A__ = str(id_ ) A__ = None A__ = None A__ = [] A__ = {} # {vertex:distance} def __lt__( self : List[str] , _snake_case : Tuple ): """simple docstring""" return self.key < other.key def __repr__( self : int ): """simple docstring""" return self.id def _a ( self : str , _snake_case : str ): """simple docstring""" self.neighbors.append(_snake_case ) def _a ( self : Tuple , _snake_case : str , _snake_case : Optional[Any] ): """simple docstring""" A__ = weight def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: # add the neighbors: graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __UpperCamelCase ) graph[b - 1].add_edge(graph[a - 1] , __UpperCamelCase ) def A ( __UpperCamelCase , __UpperCamelCase ) -> list: A__ = [] for u in graph: A__ = math.inf A__ = None A__ = 0 A__ = graph[:] while q: A__ = min(__UpperCamelCase ) q.remove(__UpperCamelCase ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): A__ = u A__ = u.edges[v.id] for i in range(1 , len(__UpperCamelCase ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def A ( __UpperCamelCase , __UpperCamelCase ) -> Iterator[tuple]: for u in graph: A__ = math.inf A__ = None A__ = 0 A__ = list(__UpperCamelCase ) hq.heapify(__UpperCamelCase ) while h: A__ = hq.heappop(__UpperCamelCase ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): A__ = u A__ = u.edges[v.id] hq.heapify(__UpperCamelCase ) for i in range(1 , len(__UpperCamelCase ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def A ( ) -> None: pass if __name__ == "__main__": import doctest doctest.testmod()
9
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
0
import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup _lowerCAmelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( __lowercase ): def __init__( self : Any , **_A : Optional[int] ): requires_backends(self , ['''bs4'''] ) super().__init__(**_A ) def UpperCamelCase_ ( self : Dict , _A : List[str] ): _UpperCamelCase = [] _UpperCamelCase = [] _UpperCamelCase = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag _UpperCamelCase = parent.find_all(child.name , recursive=_A ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_A ) else next(i for i, s in enumerate(_A , 1 ) if s is child ) ) _UpperCamelCase = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def UpperCamelCase_ ( self : List[Any] , _A : int ): _UpperCamelCase = BeautifulSoup(_A , '''html.parser''' ) _UpperCamelCase = [] _UpperCamelCase = [] _UpperCamelCase = [] for element in html_code.descendants: if type(_A ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue _UpperCamelCase = html.unescape(_A ).strip() if not text_in_this_tag: continue all_doc_strings.append(_A ) _UpperCamelCase , _UpperCamelCase = self.xpath_soup(_A ) stringaxtag_seq.append(_A ) stringaxsubs_seq.append(_A ) if len(_A ) != len(_A ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(_A ) != len(_A ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def UpperCamelCase_ ( self : int , _A : int , _A : int ): _UpperCamelCase = '''''' for tagname, subs in zip(_A , _A ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self : str , _A : Optional[Any] ): _UpperCamelCase = False # Check that strings has a valid type if isinstance(_A , _A ): _UpperCamelCase = True elif isinstance(_A , (list, tuple) ): if len(_A ) == 0 or isinstance(html_strings[0] , _A ): _UpperCamelCase = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F"""but is of type {type(_A )}.""" ) _UpperCamelCase = bool(isinstance(_A , (list, tuple) ) and (isinstance(html_strings[0] , _A )) ) if not is_batched: _UpperCamelCase = [html_strings] # Get nodes + xpaths _UpperCamelCase = [] _UpperCamelCase = [] for html_string in html_strings: _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = self.get_three_from_single(_A ) nodes.append(_A ) _UpperCamelCase = [] for node, tag_list, sub_list in zip(_A , _A , _A ): _UpperCamelCase = self.construct_xpath(_A , _A ) xpath_strings.append(_A ) xpaths.append(_A ) # return as Dict _UpperCamelCase = {'''nodes''': nodes, '''xpaths''': xpaths} _UpperCamelCase = BatchFeature(data=_A , tensor_type=_A ) return encoded_inputs
10
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowercase_ = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 2048-bit 14: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 3072-bit 15: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 4096-bit 16: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 6144-bit 17: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 8192-bit 18: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, } class __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
0
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ , lowercase_ ) -> Tuple: '''simple docstring''' lowercase__ : Dict = multiprocessing.Manager() lowercase__ : Optional[int] = manager.list() lowercase__ : str = multiprocessing.Process(target=lowercase_ , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def UpperCamelCase ( lowercase_ , lowercase_ , lowercase_ ) -> Tuple: '''simple docstring''' with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil lowercase__ : Optional[int] = shutil.rmtree lowercase__ : Dict = os.rmdir lowercase__ : int = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: lowercase__ : Any = {} with swallow_io(): with time_limit(lowercase_ ): exec(lowercase_ , lowercase_ ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. lowercase__ : List[Any] = rmtree lowercase__ : int = rmdir lowercase__ : int = chdir @contextlib.contextmanager def UpperCamelCase ( lowercase_ ) -> str: '''simple docstring''' def signal_handler(lowercase_ , lowercase_ ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , lowercase_ ) signal.signal(signal.SIGALRM , lowercase_ ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def UpperCamelCase ( ) -> Dict: '''simple docstring''' lowercase__ : Union[str, Any] = WriteOnlyStringIO() with contextlib.redirect_stdout(lowercase_ ): with contextlib.redirect_stderr(lowercase_ ): with redirect_stdin(lowercase_ ): yield @contextlib.contextmanager def UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' with tempfile.TemporaryDirectory() as dirname: with chdir(lowercase_ ): yield dirname class _snake_case ( UpperCAmelCase_ ): pass class _snake_case ( io.StringIO ): def lowercase__ ( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_): '''simple docstring''' raise OSError def lowercase__ ( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_): '''simple docstring''' raise OSError def lowercase__ ( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_): '''simple docstring''' raise OSError def lowercase__ ( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_): '''simple docstring''' return False class _snake_case ( contextlib._RedirectStream ): # type: ignore __lowerCAmelCase : Any = 'stdin' @contextlib.contextmanager def UpperCamelCase ( lowercase_ ) -> Optional[int]: '''simple docstring''' if root == ".": yield return lowercase__ : Any = os.getcwd() os.chdir(lowercase_ ) try: yield except BaseException as exc: raise exc finally: os.chdir(lowercase_ ) def UpperCamelCase ( lowercase_=None ) -> Union[str, Any]: '''simple docstring''' if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins lowercase__ : Union[str, Any] = None lowercase__ : int = None import os lowercase__ : Union[str, Any] = """1""" lowercase__ : Union[str, Any] = None lowercase__ : List[Any] = None lowercase__ : List[Any] = None lowercase__ : Tuple = None lowercase__ : List[Any] = None lowercase__ : List[Any] = None lowercase__ : str = None lowercase__ : int = None lowercase__ : Optional[Any] = None lowercase__ : str = None lowercase__ : List[str] = None lowercase__ : Union[str, Any] = None lowercase__ : List[Any] = None lowercase__ : str = None lowercase__ : Tuple = None lowercase__ : int = None lowercase__ : Optional[int] = None lowercase__ : Optional[Any] = None lowercase__ : int = None lowercase__ : str = None lowercase__ : str = None lowercase__ : Optional[int] = None lowercase__ : List[str] = None lowercase__ : List[str] = None lowercase__ : Union[str, Any] = None lowercase__ : Union[str, Any] = None lowercase__ : List[Any] = None import shutil lowercase__ : Any = None lowercase__ : Optional[int] = None lowercase__ : Any = None import subprocess lowercase__ : Union[str, Any] = None # type: ignore lowercase__ : Optional[int] = None import sys lowercase__ : Dict = None lowercase__ : Dict = None lowercase__ : Tuple = None lowercase__ : Optional[Any] = None lowercase__ : Any = None
12
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
0
'''simple docstring''' from ..utils import DummyObject, requires_backends class UpperCAmelCase_ (metaclass=_UpperCAmelCase ): """simple docstring""" lowerCamelCase : Optional[int] = ['note_seq'] def __init__( self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> str: requires_backends(self , ['note_seq'] ) @classmethod def lowercase_ ( cls , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Optional[int]: requires_backends(cls , ['note_seq'] ) @classmethod def lowercase_ ( cls , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: requires_backends(cls , ['note_seq'] )
13
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
0
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : List[Any] = ["image_processor", "tokenizer"] UpperCAmelCase__ : List[str] = "LayoutLMv2ImageProcessor" UpperCAmelCase__ : Any = ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self , _a=None , _a=None , **_a ) -> str: if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _a , ) _a : Union[str, Any] = kwargs.pop('''feature_extractor''' ) _a : str = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(_a , _a ) def __call__( self , _a , _a = None , _a = None , _a = None , _a = None , _a = True , _a = False , _a = None , _a = None , _a = 0 , _a = None , _a = None , _a = None , _a = False , _a = False , _a = False , _a = False , _a = True , _a = None , **_a , ) -> BatchEncoding: # verify input if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor _a : List[Any] = self.image_processor(images=_a , return_tensors=_a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(_a , _a ): _a : Dict = [text] # add batch dimension (as the image processor always adds a batch dimension) _a : List[str] = features['''words'''] _a : int = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=_a , add_special_tokens=_a , padding=_a , truncation=_a , max_length=_a , stride=_a , pad_to_multiple_of=_a , return_token_type_ids=_a , return_attention_mask=_a , return_overflowing_tokens=_a , return_special_tokens_mask=_a , return_offsets_mapping=_a , return_length=_a , verbose=_a , return_tensors=_a , **_a , ) # add pixel values _a : int = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: _a : int = self.get_overflowing_images(_a , encoded_inputs['''overflow_to_sample_mapping'''] ) _a : Optional[int] = images return encoded_inputs def __lowercase ( self , _a , _a ) -> Any: # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image _a : Optional[int] = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(_a ) != len(_a ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' F""" {len(_a )} and {len(_a )}""" ) return images_with_overflow def __lowercase ( self , *_a , **_a ) -> List[str]: return self.tokenizer.batch_decode(*_a , **_a ) def __lowercase ( self , *_a , **_a ) -> Union[str, Any]: return self.tokenizer.decode(*_a , **_a ) @property def __lowercase ( self ) -> List[Any]: return ["input_ids", "bbox", "attention_mask", "image"] @property def __lowercase ( self ) -> str: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _a , ) return self.image_processor_class @property def __lowercase ( self ) -> Union[str, Any]: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _a , ) return self.image_processor
14
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = 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 _a = image_processing(A , 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'''], ) , )
11
0
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def UpperCamelCase ( __magic_name__ : List[Any] ) -> Optional[Any]: """simple docstring""" lowercase__ = 384 if "tiny" in model_name: lowercase__ = [3, 3, 9, 3] lowercase__ = [96, 192, 384, 768] if "small" in model_name: lowercase__ = [3, 3, 27, 3] lowercase__ = [96, 192, 384, 768] if "base" in model_name: lowercase__ = [3, 3, 27, 3] lowercase__ = [128, 256, 512, 1024] lowercase__ = 512 if "large" in model_name: lowercase__ = [3, 3, 27, 3] lowercase__ = [192, 384, 768, 1536] lowercase__ = 768 if "xlarge" in model_name: lowercase__ = [3, 3, 27, 3] lowercase__ = [256, 512, 1024, 2048] lowercase__ = 1024 # set label information lowercase__ = 150 lowercase__ = """huggingface/label-files""" lowercase__ = """ade20k-id2label.json""" lowercase__ = json.load(open(hf_hub_download(__magic_name__ , __magic_name__ , repo_type="""dataset""" ) , """r""" ) ) lowercase__ = {int(__magic_name__ ): v for k, v in idalabel.items()} lowercase__ = {v: k for k, v in idalabel.items()} lowercase__ = ConvNextConfig( depths=__magic_name__ , hidden_sizes=__magic_name__ , out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ) lowercase__ = UperNetConfig( backbone_config=__magic_name__ , auxiliary_in_channels=__magic_name__ , num_labels=__magic_name__ , idalabel=__magic_name__ , labelaid=__magic_name__ , ) return config def UpperCamelCase ( __magic_name__ : Optional[Any] ) -> str: """simple docstring""" lowercase__ = [] # fmt: off # stem rename_keys.append(("""backbone.downsample_layers.0.0.weight""", """backbone.embeddings.patch_embeddings.weight""") ) rename_keys.append(("""backbone.downsample_layers.0.0.bias""", """backbone.embeddings.patch_embeddings.bias""") ) rename_keys.append(("""backbone.downsample_layers.0.1.weight""", """backbone.embeddings.layernorm.weight""") ) rename_keys.append(("""backbone.downsample_layers.0.1.bias""", """backbone.embeddings.layernorm.bias""") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'''backbone.stages.{i}.{j}.gamma''', f'''backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.depthwise_conv.weight''', f'''backbone.encoder.stages.{i}.layers.{j}.dwconv.weight''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.depthwise_conv.bias''', f'''backbone.encoder.stages.{i}.layers.{j}.dwconv.bias''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.norm.weight''', f'''backbone.encoder.stages.{i}.layers.{j}.layernorm.weight''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.norm.bias''', f'''backbone.encoder.stages.{i}.layers.{j}.layernorm.bias''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.pointwise_conv1.weight''', f'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.pointwise_conv1.bias''', f'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.pointwise_conv2.weight''', f'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight''') ) rename_keys.append((f'''backbone.stages.{i}.{j}.pointwise_conv2.bias''', f'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias''') ) if i > 0: rename_keys.append((f'''backbone.downsample_layers.{i}.0.weight''', f'''backbone.encoder.stages.{i}.downsampling_layer.0.weight''') ) rename_keys.append((f'''backbone.downsample_layers.{i}.0.bias''', f'''backbone.encoder.stages.{i}.downsampling_layer.0.bias''') ) rename_keys.append((f'''backbone.downsample_layers.{i}.1.weight''', f'''backbone.encoder.stages.{i}.downsampling_layer.1.weight''') ) rename_keys.append((f'''backbone.downsample_layers.{i}.1.bias''', f'''backbone.encoder.stages.{i}.downsampling_layer.1.bias''') ) rename_keys.append((f'''backbone.norm{i}.weight''', f'''backbone.hidden_states_norms.stage{i+1}.weight''') ) rename_keys.append((f'''backbone.norm{i}.bias''', f'''backbone.hidden_states_norms.stage{i+1}.bias''') ) # decode head rename_keys.extend( [ ("""decode_head.conv_seg.weight""", """decode_head.classifier.weight"""), ("""decode_head.conv_seg.bias""", """decode_head.classifier.bias"""), ("""auxiliary_head.conv_seg.weight""", """auxiliary_head.classifier.weight"""), ("""auxiliary_head.conv_seg.bias""", """auxiliary_head.classifier.bias"""), ] ) # fmt: on return rename_keys def UpperCamelCase ( __magic_name__ : Optional[int] , __magic_name__ : str , __magic_name__ : List[str] ) -> Union[str, Any]: """simple docstring""" lowercase__ = dct.pop(__magic_name__ ) lowercase__ = val def UpperCamelCase ( __magic_name__ : Optional[int] , __magic_name__ : Optional[Any] , __magic_name__ : Union[str, Any] ) -> str: """simple docstring""" lowercase__ = { """upernet-convnext-tiny""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth""", """upernet-convnext-small""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth""", """upernet-convnext-base""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth""", """upernet-convnext-large""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth""", """upernet-convnext-xlarge""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth""", } lowercase__ = model_name_to_url[model_name] lowercase__ = torch.hub.load_state_dict_from_url(__magic_name__ , map_location="""cpu""" )["""state_dict"""] lowercase__ = get_upernet_config(__magic_name__ ) lowercase__ = UperNetForSemanticSegmentation(__magic_name__ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): lowercase__ = state_dict.pop(__magic_name__ ) if "bn" in key: lowercase__ = key.replace("""bn""" , """batch_norm""" ) lowercase__ = val # rename keys lowercase__ = create_rename_keys(__magic_name__ ) for src, dest in rename_keys: rename_key(__magic_name__ , __magic_name__ , __magic_name__ ) model.load_state_dict(__magic_name__ ) # verify on image lowercase__ = """https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg""" lowercase__ = Image.open(requests.get(__magic_name__ , stream=__magic_name__ ).raw ).convert("""RGB""" ) lowercase__ = SegformerImageProcessor() lowercase__ = processor(__magic_name__ , return_tensors="""pt""" ).pixel_values with torch.no_grad(): lowercase__ = model(__magic_name__ ) if model_name == "upernet-convnext-tiny": lowercase__ = torch.tensor( [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ) elif model_name == "upernet-convnext-small": lowercase__ = torch.tensor( [[-8.8_2_3_6, -8.8_2_3_6, -8.6_7_7_1], [-8.8_2_3_6, -8.8_2_3_6, -8.6_7_7_1], [-8.7_6_3_8, -8.7_6_3_8, -8.6_2_4_0]] ) elif model_name == "upernet-convnext-base": lowercase__ = torch.tensor( [[-8.8_5_5_8, -8.8_5_5_8, -8.6_9_0_5], [-8.8_5_5_8, -8.8_5_5_8, -8.6_9_0_5], [-8.7_6_6_9, -8.7_6_6_9, -8.6_0_2_1]] ) elif model_name == "upernet-convnext-large": lowercase__ = torch.tensor( [[-8.6_6_6_0, -8.6_6_6_0, -8.6_2_1_0], [-8.6_6_6_0, -8.6_6_6_0, -8.6_2_1_0], [-8.6_3_1_0, -8.6_3_1_0, -8.5_9_6_4]] ) elif model_name == "upernet-convnext-xlarge": lowercase__ = torch.tensor( [[-8.4_9_8_0, -8.4_9_8_0, -8.3_9_7_7], [-8.4_9_8_0, -8.4_9_8_0, -8.3_9_7_7], [-8.4_3_7_9, -8.4_3_7_9, -8.3_4_1_2]] ) print("""Logits:""" , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , __magic_name__ , atol=1E-4 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__magic_name__ ) print(f'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(__magic_name__ ) if push_to_hub: print(f'''Pushing model and processor for {model_name} to hub''' ) model.push_to_hub(f'''openmmlab/{model_name}''' ) processor.push_to_hub(f'''openmmlab/{model_name}''' ) if __name__ == "__main__": A : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='upernet-convnext-tiny', type=str, choices=[F'upernet-convnext-{size}' for size in ['tiny', 'small', 'base', 'large', 'xlarge']], help='Name of the ConvNext UperNet model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) A : Union[str, Any] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
15
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
0
from ...configuration_utils import PretrainedConfig from ...utils import logging __A : Dict = logging.get_logger(__name__) __A : Union[str, Any] = { 'MIT/ast-finetuned-audioset-10-10-0.4593': ( 'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json' ), } class _SCREAMING_SNAKE_CASE ( __snake_case ): '''simple docstring''' lowerCamelCase__ = "audio-spectrogram-transformer" def __init__( self : int , __lowerCamelCase : str=768 , __lowerCamelCase : int=12 , __lowerCamelCase : Optional[Any]=12 , __lowerCamelCase : Union[str, Any]=3072 , __lowerCamelCase : List[Any]="gelu" , __lowerCamelCase : Tuple=0.0 , __lowerCamelCase : Any=0.0 , __lowerCamelCase : Any=0.02 , __lowerCamelCase : str=1e-12 , __lowerCamelCase : Any=16 , __lowerCamelCase : str=True , __lowerCamelCase : List[str]=10 , __lowerCamelCase : Dict=10 , __lowerCamelCase : List[str]=1024 , __lowerCamelCase : List[Any]=128 , **__lowerCamelCase : Optional[int] , ): super().__init__(**__lowerCamelCase ) SCREAMING_SNAKE_CASE = hidden_size SCREAMING_SNAKE_CASE = num_hidden_layers SCREAMING_SNAKE_CASE = num_attention_heads SCREAMING_SNAKE_CASE = intermediate_size SCREAMING_SNAKE_CASE = hidden_act SCREAMING_SNAKE_CASE = hidden_dropout_prob SCREAMING_SNAKE_CASE = attention_probs_dropout_prob SCREAMING_SNAKE_CASE = initializer_range SCREAMING_SNAKE_CASE = layer_norm_eps SCREAMING_SNAKE_CASE = patch_size SCREAMING_SNAKE_CASE = qkv_bias SCREAMING_SNAKE_CASE = frequency_stride SCREAMING_SNAKE_CASE = time_stride SCREAMING_SNAKE_CASE = max_length SCREAMING_SNAKE_CASE = num_mel_bins
16
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : Optional[int] = logging.get_logger(__name__) UpperCAmelCase_ : Optional[int] = { '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/config.json''', '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/config.json''', '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/config.json''', '''funnel-transformer/medium-base''': '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/config.json''', '''funnel-transformer/intermediate''': ( '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/config.json''' ), '''funnel-transformer/intermediate-base''': ( '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/config.json''' ), '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/config.json''', '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/config.json''', '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/config.json''', '''funnel-transformer/xlarge-base''': '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/config.json''', } class lowerCamelCase_ ( _lowercase ): _lowercase : Optional[Any] = '''funnel''' _lowercase : Tuple = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''n_head''', } def __init__( self : Union[str, Any] , __A : List[Any]=3_0522 , __A : List[str]=[4, 4, 4] , __A : Optional[int]=None , __A : List[str]=2 , __A : Optional[Any]=768 , __A : int=12 , __A : List[Any]=64 , __A : Optional[Any]=3072 , __A : str="gelu_new" , __A : Optional[Any]=0.1 , __A : Optional[int]=0.1 , __A : List[Any]=0.0 , __A : List[str]=0.1 , __A : Union[str, Any]=None , __A : Tuple=1e-9 , __A : Optional[Any]="mean" , __A : Optional[int]="relative_shift" , __A : List[str]=True , __A : Union[str, Any]=True , __A : Union[str, Any]=True , **__A : Tuple , ): __A : Tuple = vocab_size __A : Tuple = block_sizes __A : Optional[int] = [1] * len(__A ) if block_repeats is None else block_repeats assert len(__A ) == len( self.block_repeats ), "`block_sizes` and `block_repeats` should have the same length." __A : Tuple = num_decoder_layers __A : Optional[int] = d_model __A : Union[str, Any] = n_head __A : Dict = d_head __A : List[str] = d_inner __A : Optional[Any] = hidden_act __A : Union[str, Any] = hidden_dropout __A : Tuple = attention_dropout __A : Optional[int] = activation_dropout __A : Optional[Any] = initializer_range __A : int = initializer_std __A : List[str] = layer_norm_eps assert pooling_type in [ "mean", "max", ], F"""Got {pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.""" __A : List[str] = pooling_type assert attention_type in [ "relative_shift", "factorized", ], F"""Got {attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported.""" __A : int = attention_type __A : str = separate_cls __A : List[Any] = truncate_seq __A : List[str] = pool_q_only super().__init__(**__A ) @property def lowerCAmelCase_ ( self : Dict ): return sum(self.block_sizes ) @num_hidden_layers.setter def lowerCAmelCase_ ( self : Optional[Any] , __A : str ): raise NotImplementedError( """This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`.""" ) @property def lowerCAmelCase_ ( self : Optional[Any] ): return len(self.block_sizes ) @num_blocks.setter def lowerCAmelCase_ ( self : List[Any] , __A : List[Any] ): raise NotImplementedError("""This model does not support the setting of `num_blocks`. Please set `block_sizes`.""" )
17
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) class lowerCAmelCase_ ( __magic_name__ ): def __init__( self , *_lowerCAmelCase , **_lowerCAmelCase ) -> None: warnings.warn( "The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use GLPNImageProcessor instead." , _lowerCAmelCase , ) super().__init__(*_lowerCAmelCase , **_lowerCAmelCase )
18
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
0
"""simple docstring""" import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def lowerCamelCase__ ( __snake_case ) -> Union[str, Any]: """simple docstring""" return x + 2 class _UpperCAmelCase( unittest.TestCase ): def UpperCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = '''x = 3''' _UpperCamelCase = {} _UpperCamelCase = evaluate(__a , {} , state=__a) assert result == 3 self.assertDictEqual(__a , {'''x''': 3}) _UpperCamelCase = '''x = y''' _UpperCamelCase = {'''y''': 5} _UpperCamelCase = evaluate(__a , {} , state=__a) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__a , {'''x''': 5, '''y''': 5}) def UpperCAmelCase ( self) -> int: '''simple docstring''' _UpperCamelCase = '''y = add_two(x)''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {'''add_two''': add_two} , state=__a) assert result == 5 self.assertDictEqual(__a , {'''x''': 3, '''y''': 5}) # Won't work without the tool with CaptureStdout() as out: _UpperCamelCase = evaluate(__a , {} , state=__a) assert result is None assert "tried to execute add_two" in out.out def UpperCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' _UpperCamelCase = '''x = 3''' _UpperCamelCase = {} _UpperCamelCase = evaluate(__a , {} , state=__a) assert result == 3 self.assertDictEqual(__a , {'''x''': 3}) def UpperCAmelCase ( self) -> Any: '''simple docstring''' _UpperCamelCase = '''test_dict = {\'x\': x, \'y\': add_two(x)}''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {'''add_two''': add_two} , state=__a) self.assertDictEqual(__a , {'''x''': 3, '''y''': 5}) self.assertDictEqual(__a , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}) def UpperCAmelCase ( self) -> Optional[int]: '''simple docstring''' _UpperCamelCase = '''x = 3\ny = 5''' _UpperCamelCase = {} _UpperCamelCase = evaluate(__a , {} , state=__a) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__a , {'''x''': 3, '''y''': 5}) def UpperCAmelCase ( self) -> List[str]: '''simple docstring''' _UpperCamelCase = '''text = f\'This is x: {x}.\'''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {} , state=__a) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__a , {'''x''': 3, '''text''': '''This is x: 3.'''}) def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = '''if x <= 3:\n y = 2\nelse:\n y = 5''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {} , state=__a) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__a , {'''x''': 3, '''y''': 2}) _UpperCamelCase = {'''x''': 8} _UpperCamelCase = evaluate(__a , {} , state=__a) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__a , {'''x''': 8, '''y''': 5}) def UpperCAmelCase ( self) -> Dict: '''simple docstring''' _UpperCamelCase = '''test_list = [x, add_two(x)]''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {'''add_two''': add_two} , state=__a) self.assertListEqual(__a , [3, 5]) self.assertDictEqual(__a , {'''x''': 3, '''test_list''': [3, 5]}) def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = '''y = x''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {} , state=__a) assert result == 3 self.assertDictEqual(__a , {'''x''': 3, '''y''': 3}) def UpperCAmelCase ( self) -> List[Any]: '''simple docstring''' _UpperCamelCase = '''test_list = [x, add_two(x)]\ntest_list[1]''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {'''add_two''': add_two} , state=__a) assert result == 5 self.assertDictEqual(__a , {'''x''': 3, '''test_list''': [3, 5]}) _UpperCamelCase = '''test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']''' _UpperCamelCase = {'''x''': 3} _UpperCamelCase = evaluate(__a , {'''add_two''': add_two} , state=__a) assert result == 5 self.assertDictEqual(__a , {'''x''': 3, '''test_dict''': {'''x''': 3, '''y''': 5}}) def UpperCAmelCase ( self) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = '''x = 0\nfor i in range(3):\n x = i''' _UpperCamelCase = {} _UpperCamelCase = evaluate(__a , {'''range''': range} , state=__a) assert result == 2 self.assertDictEqual(__a , {'''x''': 2, '''i''': 2})
19
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowercase_ = { "configuration_blip": [ "BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlipConfig", "BlipTextConfig", "BlipVisionConfig", ], "processing_blip": ["BlipProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["BlipImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "BlipModel", "BlipPreTrainedModel", "BlipForConditionalGeneration", "BlipForQuestionAnswering", "BlipVisionModel", "BlipTextModel", "BlipForImageTextRetrieval", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "TFBlipModel", "TFBlipPreTrainedModel", "TFBlipForConditionalGeneration", "TFBlipForQuestionAnswering", "TFBlipVisionModel", "TFBlipTextModel", "TFBlipForImageTextRetrieval", ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
11
0
def _lowercase( __a : int ): a__ =(1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def _lowercase( __a : int = 5000 ): a__ =[(i * (3 * i - 1)) // 2 for i in range(1 , __a )] for i, pentagonal_i in enumerate(__a ): for j in range(__a , len(__a ) ): a__ =pentagonal_nums[j] a__ =pentagonal_i + pentagonal_j a__ =pentagonal_j - pentagonal_i if is_pentagonal(__a ) and is_pentagonal(__a ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
20
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def lowerCAmelCase (__A = "laptop"): """simple docstring""" _a = F'''https://www.amazon.in/laptop/s?k={product}''' _a = { '''User-Agent''': '''Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36''', '''Accept-Language''': '''en-US, en;q=0.5''', } _a = BeautifulSoup(requests.get(__A , headers=__A).text) # Initialize a Pandas dataframe with the column titles _a = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ]) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' , attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} , ) , soup.find_all('''div''' , attrs={'''class''': '''a-row a-size-base a-color-base'''}) , ): try: _a = item.ha.text _a = '''https://www.amazon.in/''' + item.ha.a['''href'''] _a = item.find('''span''' , attrs={'''class''': '''a-offscreen'''}).text try: _a = item.find('''span''' , attrs={'''class''': '''a-icon-alt'''}).text except AttributeError: _a = '''Not available''' try: _a = ( '''₹''' + item.find( '''span''' , attrs={'''class''': '''a-price a-text-price'''}).text.split('''₹''')[1] ) except AttributeError: _a = '''''' try: _a = float( ( ( float(product_mrp.strip('''₹''').replace(''',''' , '''''')) - float(product_price.strip('''₹''').replace(''',''' , '''''')) ) / float(product_mrp.strip('''₹''').replace(''',''' , '''''')) ) * 100) except ValueError: _a = float('''nan''') except AttributeError: pass _a = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] _a = ''' ''' _a = ''' ''' data_frame.index += 1 return data_frame if __name__ == "__main__": lowercase_ = "headphones" get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
11
0
import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : Any = logging.get_logger(__name__) UpperCAmelCase_ : List[Any] = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class __A ( UpperCamelCase__ ): UpperCamelCase = """encodec""" def __init__( self :str , __snake_case :Any=[1.5, 3.0, 6.0, 12.0, 24.0] , __snake_case :Dict=2_40_00 , __snake_case :Optional[int]=1 , __snake_case :str=False , __snake_case :Optional[int]=None , __snake_case :int=None , __snake_case :Any=1_28 , __snake_case :Dict=32 , __snake_case :Optional[int]=1 , __snake_case :Union[str, Any]=[8, 5, 4, 2] , __snake_case :Optional[int]="weight_norm" , __snake_case :Dict=7 , __snake_case :str=7 , __snake_case :int=3 , __snake_case :Union[str, Any]=2 , __snake_case :Optional[int]=True , __snake_case :Any="reflect" , __snake_case :List[Any]=2 , __snake_case :Any=2 , __snake_case :Tuple=1.0 , __snake_case :int=10_24 , __snake_case :Optional[int]=None , __snake_case :str=True , **__snake_case :Optional[int] , ): '''simple docstring''' __magic_name__ : Any =target_bandwidths __magic_name__ : Optional[Any] =sampling_rate __magic_name__ : Any =audio_channels __magic_name__ : List[Any] =normalize __magic_name__ : List[str] =chunk_length_s __magic_name__ : Optional[int] =overlap __magic_name__ : Any =hidden_size __magic_name__ : List[Any] =num_filters __magic_name__ : List[str] =num_residual_layers __magic_name__ : Optional[int] =upsampling_ratios __magic_name__ : str =norm_type __magic_name__ : Optional[Any] =kernel_size __magic_name__ : List[str] =last_kernel_size __magic_name__ : List[str] =residual_kernel_size __magic_name__ : str =dilation_growth_rate __magic_name__ : Optional[Any] =use_causal_conv __magic_name__ : int =pad_mode __magic_name__ : Optional[int] =compress __magic_name__ : Any =num_lstm_layers __magic_name__ : Optional[Any] =trim_right_ratio __magic_name__ : Any =codebook_size __magic_name__ : Tuple =codebook_dim if codebook_dim is not None else hidden_size __magic_name__ : List[str] =use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**__snake_case ) @property def A__ ( self :List[str] ): '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def A__ ( self :List[str] ): '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def A__ ( self :int ): '''simple docstring''' __magic_name__ : Any =np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def A__ ( self :Tuple ): '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
21
'''simple docstring''' import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def lowerCAmelCase (__A , __A , __A): """simple docstring""" if isinstance(__A , torch.Tensor): return image elif isinstance(__A , PIL.Image.Image): _a = [image] if isinstance(image[0] , PIL.Image.Image): _a = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos''']))[None, :] for i in image] _a = np.concatenate(__A , axis=0) _a = np.array(__A).astype(np.floataa) / 2_55.0 _a = image.transpose(0 , 3 , 1 , 2) _a = 2.0 * image - 1.0 _a = torch.from_numpy(__A) elif isinstance(image[0] , torch.Tensor): _a = torch.cat(__A , dim=0) return image def lowerCAmelCase (__A , __A , __A , __A=0.99_95): """simple docstring""" if not isinstance(__A , np.ndarray): _a = True _a = va.device _a = va.cpu().numpy() _a = va.cpu().numpy() _a = np.sum(va * va / (np.linalg.norm(__A) * np.linalg.norm(__A))) if np.abs(__A) > DOT_THRESHOLD: _a = (1 - t) * va + t * va else: _a = np.arccos(__A) _a = np.sin(__A) _a = theta_a * t _a = np.sin(__A) _a = np.sin(theta_a - theta_t) / sin_theta_a _a = sin_theta_t / sin_theta_a _a = sa * va + sa * va if inputs_are_torch: _a = torch.from_numpy(__A).to(__A) return va def lowerCAmelCase (__A , __A): """simple docstring""" _a = F.normalize(__A , dim=-1) _a = F.normalize(__A , dim=-1) return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2) def lowerCAmelCase (__A , __A): """simple docstring""" for param in model.parameters(): _a = value class __A ( A ): '''simple docstring''' def __init__(self , A , A , A , A , A , A , A , A=None , A=None , A=None , ) -> str: """simple docstring""" super().__init__() self.register_modules( vae=A , text_encoder=A , clip_model=A , tokenizer=A , unet=A , scheduler=A , feature_extractor=A , coca_model=A , coca_tokenizer=A , coca_transform=A , ) _a = ( feature_extractor.size if isinstance(feature_extractor.size , A ) else feature_extractor.size['''shortest_edge'''] ) _a = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , A ) set_requires_grad(self.clip_model , A ) def a__ (self , A = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A ) def a__ (self ) -> Optional[Any]: """simple docstring""" self.enable_attention_slicing(A ) def a__ (self ) -> int: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" set_requires_grad(self.vae , A ) def a__ (self ) -> Dict: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self ) -> str: """simple docstring""" set_requires_grad(self.unet , A ) def a__ (self , A , A , A ) -> Optional[Any]: """simple docstring""" _a = min(int(num_inference_steps * strength ) , A ) _a = max(num_inference_steps - init_timestep , 0 ) _a = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def a__ (self , A , A , A , A , A , A=None ) -> List[str]: """simple docstring""" if not isinstance(A , torch.Tensor ): raise ValueError(f'''`image` has to be of type `torch.Tensor` but is {type(A )}''' ) _a = image.to(device=A , dtype=A ) if isinstance(A , A ): _a = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(A ) ] _a = torch.cat(A , dim=0 ) else: _a = self.vae.encode(A ).latent_dist.sample(A ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 0.18215 * init_latents _a = init_latents.repeat_interleave(A , dim=0 ) _a = randn_tensor(init_latents.shape , generator=A , device=A , dtype=A ) # get latents _a = self.scheduler.add_noise(A , A , A ) _a = init_latents return latents def a__ (self , A ) -> Tuple: """simple docstring""" _a = self.coca_transform(A ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): _a = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) _a = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def a__ (self , A , A ) -> List[Any]: """simple docstring""" _a = self.feature_extractor.preprocess(A ) _a = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = image_embeddings_clip.repeat_interleave(A , dim=0 ) return image_embeddings_clip @torch.enable_grad() def a__ (self , A , A , A , A , A , A , A , ) -> Union[str, Any]: """simple docstring""" _a = latents.detach().requires_grad_() _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): _a = self.scheduler.alphas_cumprod[timestep] _a = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf _a = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 _a = torch.sqrt(A ) _a = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , A ): _a = self.scheduler.sigmas[index] _a = latents - sigma * noise_pred else: raise ValueError(f'''scheduler type {type(self.scheduler )} not supported''' ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * sample _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = transforms.Resize(self.feature_extractor_size )(A ) _a = self.normalize(A ).to(latents.dtype ) _a = self.clip_model.get_image_features(A ) _a = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=A ) _a = spherical_dist_loss(A , A ).mean() * clip_guidance_scale _a = -torch.autograd.grad(A , A )[0] if isinstance(self.scheduler , A ): _a = latents.detach() + grads * (sigma**2) _a = noise_pred_original else: _a = noise_pred_original - torch.sqrt(A ) * grads return noise_pred, latents @torch.no_grad() def __call__(self , A , A , A = None , A = None , A = 512 , A = 512 , A = 0.6 , A = 50 , A = 7.5 , A = 1 , A = 0.0 , A = 100 , A = None , A = "pil" , A = True , A = 0.8 , A = 0.1 , A = 0.1 , ) -> str: """simple docstring""" if isinstance(A , A ) and len(A ) != batch_size: raise ValueError(f'''You have passed {batch_size} batch_size, but only {len(A )} generators.''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if isinstance(A , torch.Generator ) and batch_size > 1: _a = [generator] + [None] * (batch_size - 1) _a = [ ('''model''', self.coca_model is None), ('''tokenizer''', self.coca_tokenizer is None), ('''transform''', self.coca_transform is None), ] _a = [x[0] for x in coca_is_none if x[1]] _a = ''', '''.join(A ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(A ): raise ValueError( f'''Content prompt is None and CoCa [{coca_is_none_str}] is None.''' f'''Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) if style_prompt is None: if len(A ): raise ValueError( f'''Style prompt is None and CoCa [{coca_is_none_str}] is None.''' f''' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.''' ) _a = self.get_image_description(A ) # get prompt text embeddings for content and style _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] _a = self.tokenizer( A , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=A , return_tensors='''pt''' , ) _a = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] _a = slerp(A , A , A ) # duplicate text embeddings for each generation per prompt _a = text_embeddings.repeat_interleave(A , dim=0 ) # set timesteps _a = '''offset''' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) _a = {} if accepts_offset: _a = 1 self.scheduler.set_timesteps(A , **A ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) _a , _a = self.get_timesteps(A , A , self.device ) _a = timesteps[:1].repeat(A ) # Preprocess image _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = preprocess(A , A , A ) _a = self.prepare_latents( A , A , A , text_embeddings.dtype , self.device , A ) _a = slerp(A , A , A ) if clip_guidance_scale > 0: _a = self.get_clip_image_embeddings(A , A ) _a = self.get_clip_image_embeddings(A , A ) _a = slerp( A , A , A ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _a = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _a = content_text_input.input_ids.shape[-1] _a = self.tokenizer([''''''] , padding='''max_length''' , max_length=A , return_tensors='''pt''' ) _a = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt _a = uncond_embeddings.repeat_interleave(A , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _a = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _a = (batch_size, self.unet.config.in_channels, height // 8, width // 8) _a = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps _a = torch.randn(A , generator=A , device='''cpu''' , dtype=A ).to( self.device ) else: _a = torch.randn(A , generator=A , device=self.device , dtype=A ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) _a = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _a = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _a = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _a = {} if accepts_eta: _a = eta # check if the scheduler accepts generator _a = '''generator''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: _a = generator with self.progress_bar(total=A ): for i, t in enumerate(A ): # expand the latents if we are doing classifier free guidance _a = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _a = self.scheduler.scale_model_input(A , A ) # predict the noise residual _a = self.unet(A , A , encoder_hidden_states=A ).sample # perform classifier free guidance if do_classifier_free_guidance: _a , _a = noise_pred.chunk(2 ) _a = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: _a = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) _a , _a = self.cond_fn( A , A , A , A , A , A , A , ) # compute the previous noisy sample x_t -> x_t-1 _a = self.scheduler.step(A , A , A , **A ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor _a = 1 / 0.18215 * latents _a = self.vae.decode(A ).sample _a = (image / 2 + 0.5).clamp(0 , 1 ) _a = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _a = self.numpy_to_pil(A ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=A , nsfw_content_detected=A )
11
0
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=_a ) class A ( _a ): lowercase_ = field(default='language-modeling' ,metadata={'include_in_asdict_even_if_is_default': True} ) lowercase_ = Features({'text': Value('string' )} ) lowercase_ = Features({} ) lowercase_ = "text" @property def __lowerCAmelCase ( self : List[str] ) -> Dict[str, str]: """simple docstring""" return {self.text_column: "text"}
22
'''simple docstring''' import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = CTRLTokenizer __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Any = False def a__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _a = dict(zip(A , range(len(A ) ) ) ) _a = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _a = {'''unk_token''': '''<unk>'''} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _a = 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(A ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(A ) ) def a__ (self , **A ) -> int: """simple docstring""" kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **A ) def a__ (self , A ) -> Tuple: """simple docstring""" _a = '''adapt react readapt apt''' _a = '''adapt react readapt apt''' return input_text, output_text def a__ (self ) -> List[Any]: """simple docstring""" _a = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = '''adapt react readapt apt''' _a = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _a = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _a = tokens + [tokenizer.unk_token] _a = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A )
11
0
from __future__ import annotations def _snake_case (__lowercase , __lowercase , __lowercase): if (voltage, current, resistance).count(0) != 1: raise ValueError('One and only one argument must be 0') if resistance < 0: raise ValueError('Resistance cannot be negative') if voltage == 0: return {"voltage": float(current * resistance)} elif current == 0: return {"current": voltage / resistance} elif resistance == 0: return {"resistance": voltage / current} else: raise ValueError('Exactly one argument must be 0') if __name__ == "__main__": import doctest doctest.testmod()
23
'''simple docstring''' 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 lowercase_ = { "/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 lowerCAmelCase (__A): """simple docstring""" _a = list(s_dict.keys()) for key in keys: _a = r'''.*/layers_(\d+)''' _a = key if re.match(__A , __A): _a = re.sub(r'''layers_(\d+)''' , r'''block/\1/layer''' , __A) _a = r'''(encoder|decoder)\/''' if re.match(__A , __A): _a = re.match(__A , __A).groups() if groups[0] == "encoder": _a = re.sub(r'''/mlp/''' , r'''/1/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/1/layer_norm/''' , __A) elif groups[0] == "decoder": _a = re.sub(r'''/mlp/''' , r'''/2/mlp/''' , __A) _a = re.sub(r'''/pre_mlp_layer_norm/''' , r'''/2/layer_norm/''' , __A) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: _a = new_key.replace(__A , __A) print(F'''{key} -> {new_key}''') _a = s_dict.pop(__A) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: _a = 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: _a = 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: _a = s_dict[key].shape[0] _a = s_dict[key] for idx in range(__A): _a = expert_weihts[idx] print(F'''{key} -> {key.replace('expert/' , 'nested fstring')}''') s_dict.pop(__A) return s_dict lowercase_ = { "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 lowerCAmelCase (__A , __A): """simple docstring""" import regex as re with open(__A , '''r''') as f: _a = f.read() _a = re.findall(r'''(.*) = ([0-9.]*)''' , __A) _a = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": _a = float(__A) if '''.''' in value else int(__A) _a = re.findall(r'''(.*activations) = \(\'(.*)\',\)''' , __A)[0] _a = str(activation[1]) _a = num_experts _a = SwitchTransformersConfig(**__A) return config def lowerCAmelCase (__A , __A , __A=None , __A="./" , __A=8): """simple docstring""" print(F'''Loading flax weights from : {flax_checkpoint_path}''') _a = checkpoints.load_tax_checkpoint(__A) if gin_file is not None: _a = convert_gin_to_config(__A , __A) else: _a = SwitchTransformersConfig.from_pretrained(__A) _a = SwitchTransformersForConditionalGeneration(__A) _a = flax_params['''target'''] _a = flatten_dict(__A , sep='''/''') _a = rename_keys(__A) _a = unflatten_dict(__A , sep='''/''') # Load the flax params in the PT model load_flax_weights_in_pytorch_model(__A , __A) print(F'''Save PyTorch model to {pytorch_dump_path}''') pt_model.save_pretrained(__A) if __name__ == "__main__": lowercase_ = 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") lowercase_ = 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, )
11
0
'''simple docstring''' UpperCAmelCase_ : List[Any] = { 0: '''0''', 1: '''1''', 2: '''2''', 3: '''3''', 4: '''4''', 5: '''5''', 6: '''6''', 7: '''7''', 8: '''8''', 9: '''9''', 1_0: '''a''', 1_1: '''b''', 1_2: '''c''', 1_3: '''d''', 1_4: '''e''', 1_5: '''f''', } def _UpperCamelCase (_lowerCamelCase : float )-> str: '''simple docstring''' assert type(_lowerCamelCase ) in (int, float) and decimal == int(_lowerCamelCase ) __snake_case = int(_lowerCamelCase ) __snake_case = '''''' __snake_case = False if decimal < 0: __snake_case = True decimal *= -1 while decimal > 0: __snake_case , __snake_case = divmod(_lowerCamelCase , 16 ) __snake_case = values[remainder] + hexadecimal __snake_case = '''0x''' + hexadecimal if negative: __snake_case = '''-''' + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
24
'''simple docstring''' def lowerCAmelCase (__A , __A): """simple docstring""" if digit_amount > 0: return round(number - int(__A) , __A) return number - int(__A) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
11
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ = { 'configuration_lilt': ['LILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LiltConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'LILT_PRETRAINED_MODEL_ARCHIVE_LIST', 'LiltForQuestionAnswering', 'LiltForSequenceClassification', 'LiltForTokenClassification', 'LiltModel', 'LiltPreTrainedModel', ] if TYPE_CHECKING: from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lilt import ( LILT_PRETRAINED_MODEL_ARCHIVE_LIST, LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, LiltPreTrainedModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
25
'''simple docstring''' import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm lowercase_ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex lowercase_ = 10 lowercase_ = 256 def lowerCAmelCase (__A): """simple docstring""" if len(__A) < MIN_NUM_TOKENS: return None _a = MinHash(num_perm=__A) for token in set(__A): min_hash.update(token.encode()) return min_hash def lowerCAmelCase (__A): """simple docstring""" return {t for t in NON_ALPHA.split(__A) if len(t.strip()) > 0} class __A : '''simple docstring''' def __init__(self , *, A = 0.85 , ) -> Optional[int]: """simple docstring""" _a = duplication_jaccard_threshold _a = NUM_PERM _a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) _a = defaultdict(A ) def a__ (self , A , A ) -> None: """simple docstring""" _a = self._index.query(A ) if code_key in self._index.keys: print(f'''Duplicate key {code_key}''' ) return self._index.insert(A , A ) if len(A ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(A ) break else: self._duplicate_clusters[close_duplicates[0]].add(A ) def a__ (self ) -> List[List[Dict]]: """simple docstring""" _a = [] for base, duplicates in self._duplicate_clusters.items(): _a = [base] + list(A ) # reformat the cluster to be a list of dict _a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(A ) return duplicate_clusters def a__ (self , A ) -> None: """simple docstring""" _a = self.get_duplicate_clusters() with open(A , '''w''' ) as f: json.dump(A , A ) def lowerCAmelCase (__A): """simple docstring""" _a , _a = element _a = get_min_hash([t for t in NON_ALPHA.split(data['''content''']) if len(t.strip()) > 0]) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def lowerCAmelCase (__A): """simple docstring""" with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(__A , max_queue_size=10_000) , chunksize=100 , ): if data is not None: yield data def lowerCAmelCase (__A , __A): """simple docstring""" _a = DuplicationIndex(duplication_jaccard_threshold=__A) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(__A)) , max_queue_size=100)): di.add(__A , __A) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def lowerCAmelCase (__A , __A): """simple docstring""" _a = get_tokens(__A) _a = get_tokens(__A) return len(tokensa & tokensa) / len(tokensa | tokensa) lowercase_ = None def lowerCAmelCase (__A , __A): """simple docstring""" _a = [] for elementa in cluster: _a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: _a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(__A , __A) >= jaccard_threshold: elementa["copies"] += 1 break else: _a = 1 extremes.append(__A) return extremes def lowerCAmelCase (__A , __A , __A): """simple docstring""" global _shared_dataset _a = dataset _a = [] _a = partial(_find_cluster_extremes_shared , jaccard_threshold=__A) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( __A , __A , ) , total=len(__A) , ): extremes_list.append(__A) return extremes_list def lowerCAmelCase (__A , __A = 0.85): """simple docstring""" _a = make_duplicate_clusters(__A , __A) _a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} _a = {} _a = find_extremes(__A , __A , __A) for extremes in extremes_clusters: for element in extremes: _a = element _a = duplicate_indices - set(extreme_dict.keys()) _a = dataset.filter(lambda __A , __A: idx not in remove_indices , with_indices=__A) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: _a = element['''base_index'''] in extreme_dict if element["is_extreme"]: _a = extreme_dict[element['''base_index''']]['''copies'''] print(F'''Original dataset size: {len(__A)}''') print(F'''Number of duplicate clusters: {len(__A)}''') print(F'''Files in duplicate cluster: {len(__A)}''') print(F'''Unique files in duplicate cluster: {len(__A)}''') print(F'''Filtered dataset size: {len(__A)}''') return ds_filter, duplicate_clusters
11
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __UpperCamelCase = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys __UpperCamelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class __A ( nn.Module ): '''simple docstring''' def __init__(self ) -> Dict: """simple docstring""" super().__init__() _a = nn.Linear(3 , 4 ) _a = nn.BatchNormad(4 ) _a = nn.Linear(4 , 5 ) def a__ (self , A ) -> Dict: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(A ) ) ) class __A ( A ): '''simple docstring''' def a__ (self , A , *A , **A ) -> Optional[Any]: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class __A ( A ): '''simple docstring''' def a__ (self , A , A ) -> int: """simple docstring""" return output + 1 class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) self.assertEqual(test_model._hf_hook , A ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() _a = ModelHook() add_hook_to_module(A , A ) add_hook_to_module(A , A , append=A ) self.assertEqual(isinstance(test_model._hf_hook , A ) , A ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(A , '''_old_forward''' ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , '''forward''' ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ['''x'''] ) remove_hook_from_module(A ) self.assertFalse(hasattr(A , '''_hf_hook''' ) ) self.assertFalse(hasattr(A , '''_old_forward''' ) ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(x + 1 ) _a = test_model(x + 2 ) _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PreForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , A , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , A , atol=1E-5 ) def a__ (self ) -> str: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks _a = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(A , A ) _a = test_model(A ) assert torch.allclose(A , output + 2 , atol=1E-5 ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() _a = torch.randn(2 , 3 ) _a = test_model(A ) _a = PostForwardHook() add_hook_to_module(A , A ) _a = test_model(A ) self.assertTrue(torch.allclose(A , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _a = True _a = test_model(A ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def a__ (self ) -> List[Any]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(A , AlignDevicesHook(io_same_device=A ) ) _a = torch.randn(2 , 3 ).to(0 ) _a = model(A ) self.assertEqual(output.device , torch.device(0 ) ) def a__ (self ) -> List[str]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = {'''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True} add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(hook_kwargs['''execution_device'''] ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload _a = { '''execution_device''': 0 if torch.cuda.is_available() else '''cpu''', '''offload''': True, '''offload_buffers''': True, } add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**A ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**A ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Optional[int]: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook(A , execution_device=A , offload=A ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook(A , execution_device=A , offload=A , offload_buffers=A ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # This will move each submodule on different devices _a = 0 if torch.cuda.is_available() else '''cpu''' attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) # Buffers are not included in the offload by default, so are on the execution device _a = torch.device(A ) self.assertEqual(model.batchnorm.running_mean.device , A ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) # Now test with buffers included in the offload attach_align_device_hook( A , execution_device=A , offload=A , weights_map=model.state_dict() , offload_buffers=A , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''meta''' ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device('''meta''' ) ) _a = torch.randn(2 , 3 ) _a = model(A ) self.assertEqual(output.device , A ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(A ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.batchnorm.weight.device , torch.device('''cpu''' ) ) self.assertEqual(model.lineara.weight.device , torch.device('''cpu''' ) )
11
0
from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" _A = int(number**0.5 ) return number == sq * sq def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> tuple[int, int]: """simple docstring""" _A = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _A = x_den * y_den * z_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) top //= hcf bottom //= hcf return top, bottom def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = 35 ) -> int: """simple docstring""" _A = set() _A = 42 _A = Fraction(0 ) _A = 42 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 _A = x_num * y_den + x_den * y_num _A = x_den * y_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=2 _A = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _A = x_den * x_den * y_den * y_den if is_sq(_SCREAMING_SNAKE_CASE ) and is_sq(_SCREAMING_SNAKE_CASE ): _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=-1 _A = x_num * y_num _A = x_den * y_num + x_num * y_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=2 _A = x_num * x_num * y_num * y_num _A = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(_SCREAMING_SNAKE_CASE ) and is_sq(_SCREAMING_SNAKE_CASE ): _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) for num, den in unique_s: total += Fraction(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return total.denominator + total.numerator if __name__ == "__main__": print(f"{solution() = }")
27
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : List[Any] = IFInpaintingSuperResolutionPipeline __lowerCamelCase : Tuple = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __lowerCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __lowerCamelCase : str = PipelineTesterMixin.required_optional_params - {'latents'} def a__ (self ) -> List[Any]: """simple docstring""" return self._get_superresolution_dummy_components() def a__ (self , A , A=0 ) -> List[Any]: """simple docstring""" if str(A ).startswith('''mps''' ): _a = torch.manual_seed(A ) else: _a = torch.Generator(device=A ).manual_seed(A ) _a = floats_tensor((1, 3, 16, 16) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = floats_tensor((1, 3, 32, 32) , rng=random.Random(A ) ).to(A ) _a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def a__ (self ) -> Optional[int]: """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def a__ (self ) -> str: """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def a__ (self ) -> str: """simple docstring""" super().test_save_load_floataa(expected_max_diff=1E-1 ) def a__ (self ) -> Tuple: """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self._test_save_load_local() def a__ (self ) -> Any: """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
11
0
'''simple docstring''' import argparse import logging import os import time import timeit import datasets import numpy as np import pycuda.autoinit # noqa: F401 import pycuda.driver as cuda import tensorrt as trt import torch from absl import logging as absl_logging from accelerate import Accelerator from datasets import load_dataset, load_metric from torch.utils.data import DataLoader from utils_qa import postprocess_qa_predictions import transformers from transformers import AutoTokenizer, EvalPrediction, default_data_collator, set_seed from transformers.trainer_pt_utils import nested_concat, nested_truncate UpperCamelCase_ = trt.Logger(trt.Logger.WARNING) UpperCamelCase_ = absl_logging.get_absl_logger() absl_logger.setLevel(logging.WARNING) UpperCamelCase_ = logging.getLogger(__name__) UpperCamelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--onnx_model_path", default=None, type=str, required=True, help="Path to ONNX model: ", ) parser.add_argument( "--output_dir", default=None, type=str, required=True, help="The output directory where the model checkpoints and predictions will be written.", ) # Other parameters parser.add_argument( "--tokenizer_name", default="", type=str, required=True, help="Pretrained tokenizer name or path if not the same as model_name", ) parser.add_argument( "--version_2_with_negative", action="store_true", help="If true, the SQuAD examples contain some that do not have an answer.", ) parser.add_argument( "--null_score_diff_threshold", type=float, default=0.0, help="If null_score - best_non_null is greater than the threshold predict null.", ) parser.add_argument( "--max_seq_length", default=3_8_4, type=int, help=( "The maximum total input sequence length after WordPiece tokenization. Sequences " "longer than this will be truncated, and sequences shorter than this will be padded." ), ) parser.add_argument( "--doc_stride", default=1_2_8, type=int, help="When splitting up a long document into chunks, how much stride to take between chunks.", ) parser.add_argument("--per_device_eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument( "--n_best_size", default=2_0, type=int, help="The total number of n-best predictions to generate in the nbest_predictions.json output file.", ) parser.add_argument( "--max_answer_length", default=3_0, type=int, help=( "The maximum length of an answer that can be generated. This is needed because the start " "and end predictions are not conditioned on one another." ), ) parser.add_argument("--seed", type=int, default=4_2, help="random seed for initialization") parser.add_argument( "--dataset_name", type=str, default=None, required=True, help="The name of the dataset to use (via the datasets library).", ) parser.add_argument( "--dataset_config_name", type=str, default=None, help="The configuration name of the dataset to use (via the datasets library).", ) parser.add_argument( "--preprocessing_num_workers", type=int, default=4, help="A csv or a json file containing the training data." ) parser.add_argument("--overwrite_cache", action="store_true", help="Overwrite the cached training and evaluation sets") parser.add_argument( "--fp16", action="store_true", help="Whether to use 16-bit (mixed) precision instead of 32-bit", ) parser.add_argument( "--int8", action="store_true", help="Whether to use INT8", ) UpperCamelCase_ = parser.parse_args() if args.tokenizer_name: UpperCamelCase_ = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=True) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." "You can do it from another script, save it, and load it from here, using --tokenizer_name." ) logger.info("Training/evaluation parameters %s", args) UpperCamelCase_ = args.per_device_eval_batch_size UpperCamelCase_ = (args.eval_batch_size, args.max_seq_length) # TRT Engine properties UpperCamelCase_ = True UpperCamelCase_ = "temp_engine/bert-fp32.engine" if args.fpaa: UpperCamelCase_ = "temp_engine/bert-fp16.engine" if args.inta: UpperCamelCase_ = "temp_engine/bert-int8.engine" # import ONNX file if not os.path.exists("temp_engine"): os.makedirs("temp_engine") UpperCamelCase_ = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) with trt.Builder(TRT_LOGGER) as builder, builder.create_network(EXPLICIT_BATCH) as network, trt.OnnxParser( network, TRT_LOGGER ) as parser: with open(args.onnx_model_path, "rb") as model: if not parser.parse(model.read()): for error in range(parser.num_errors): print(parser.get_error(error)) # Query input names and shapes from parsed TensorRT network UpperCamelCase_ = [network.get_input(i) for i in range(network.num_inputs)] UpperCamelCase_ = [_input.name for _input in network_inputs] # ex: ["actual_input1"] with builder.create_builder_config() as config: UpperCamelCase_ = 1 << 5_0 if STRICT_TYPES: config.set_flag(trt.BuilderFlag.STRICT_TYPES) if args.fpaa: config.set_flag(trt.BuilderFlag.FPaa) if args.inta: config.set_flag(trt.BuilderFlag.INTa) UpperCamelCase_ = builder.create_optimization_profile() config.add_optimization_profile(profile) for i in range(len(input_names)): profile.set_shape(input_names[i], INPUT_SHAPE, INPUT_SHAPE, INPUT_SHAPE) UpperCamelCase_ = builder.build_engine(network, config) # serialize_engine and store in file (can be directly loaded and deserialized): with open(engine_name, "wb") as f: f.write(engine.serialize()) def lowercase__( __UpperCamelCase: Optional[Any] ,__UpperCamelCase: List[Any] ,__UpperCamelCase: List[Any] ,__UpperCamelCase: Any ,__UpperCamelCase: Tuple ,__UpperCamelCase: List[str] ,__UpperCamelCase: List[str] ,__UpperCamelCase: Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = np.asarray(inputs['input_ids'] ,dtype=np.intaa ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.asarray(inputs['attention_mask'] ,dtype=np.intaa ) SCREAMING_SNAKE_CASE : Dict = np.asarray(inputs['token_type_ids'] ,dtype=np.intaa ) # Copy inputs cuda.memcpy_htod_async(d_inputs[0] ,input_ids.ravel() ,__UpperCamelCase ) cuda.memcpy_htod_async(d_inputs[1] ,attention_mask.ravel() ,__UpperCamelCase ) cuda.memcpy_htod_async(d_inputs[2] ,token_type_ids.ravel() ,__UpperCamelCase ) # start time SCREAMING_SNAKE_CASE : Optional[int] = time.time() # Run inference context.execute_async( bindings=[int(__UpperCamelCase ) for d_inp in d_inputs] + [int(__UpperCamelCase ), int(__UpperCamelCase )] ,stream_handle=stream.handle ) # Transfer predictions back from GPU cuda.memcpy_dtoh_async(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) cuda.memcpy_dtoh_async(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) # Synchronize the stream and take time stream.synchronize() # end time SCREAMING_SNAKE_CASE : List[Any] = time.time() SCREAMING_SNAKE_CASE : Tuple = end_time - start_time SCREAMING_SNAKE_CASE : Optional[int] = (h_outputa, h_outputa) # print(outputs) return outputs, infer_time # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. UpperCamelCase_ = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) # Setup logging, we only want one process per machine to log things on the screen. # accelerator.is_local_main_process is only True for one process per machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). if args.dataset_name is not None: # Downloading and loading a dataset from the hub. UpperCamelCase_ = load_dataset(args.dataset_name, args.dataset_config_name) else: raise ValueError("Evaluation requires a dataset name") # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. UpperCamelCase_ = raw_datasets["validation"].column_names UpperCamelCase_ = "question" if "question" in column_names else column_names[0] UpperCamelCase_ = "context" if "context" in column_names else column_names[1] UpperCamelCase_ = "answers" if "answers" in column_names else column_names[2] # Padding side determines if we do (question|context) or (context|question). UpperCamelCase_ = tokenizer.padding_side == "right" if args.max_seq_length > tokenizer.model_max_length: logger.warning( F"""The max_seq_length passed ({args.max_seq_length}) is larger than the maximum length for the""" F"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) UpperCamelCase_ = min(args.max_seq_length, tokenizer.model_max_length) def lowercase__( __UpperCamelCase: List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = [q.lstrip() for q in examples[question_column_name]] # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results # in one example possible giving several features when a context is long, each of those features having a # context that overlaps a bit the context of the previous feature. SCREAMING_SNAKE_CASE : List[str] = tokenizer( examples[question_column_name if pad_on_right else context_column_name] ,examples[context_column_name if pad_on_right else question_column_name] ,truncation='only_second' if pad_on_right else 'only_first' ,max_length=__UpperCamelCase ,stride=args.doc_stride ,return_overflowing_tokens=__UpperCamelCase ,return_offsets_mapping=__UpperCamelCase ,padding='max_length' ,) # Since one example might give us several features if it has a long context, we need a map from a feature to # its corresponding example. This key gives us just that. SCREAMING_SNAKE_CASE : Tuple = tokenized_examples.pop('overflow_to_sample_mapping' ) # For evaluation, we will need to convert our predictions to substrings of the context, so we keep the # corresponding example_id and we will store the offset mappings. SCREAMING_SNAKE_CASE : List[str] = [] for i in range(len(tokenized_examples['input_ids'] ) ): # Grab the sequence corresponding to that example (to know what is the context and what is the question). SCREAMING_SNAKE_CASE : Optional[int] = tokenized_examples.sequence_ids(__UpperCamelCase ) SCREAMING_SNAKE_CASE : List[Any] = 1 if pad_on_right else 0 # One example can give several spans, this is the index of the example containing this span of text. SCREAMING_SNAKE_CASE : List[str] = sample_mapping[i] tokenized_examples["example_id"].append(examples['id'][sample_index] ) # Set to None the offset_mapping that are not part of the context so it's easy to determine if a token # position is part of the context or not. SCREAMING_SNAKE_CASE : List[str] = [ (o if sequence_ids[k] == context_index else None) for k, o in enumerate(tokenized_examples['offset_mapping'][i] ) ] return tokenized_examples UpperCamelCase_ = raw_datasets["validation"] # Validation Feature Creation UpperCamelCase_ = eval_examples.map( prepare_validation_features, batched=True, num_proc=args.preprocessing_num_workers, remove_columns=column_names, load_from_cache_file=not args.overwrite_cache, desc="Running tokenizer on validation dataset", ) UpperCamelCase_ = default_data_collator UpperCamelCase_ = eval_dataset.remove_columns(["example_id", "offset_mapping"]) UpperCamelCase_ = DataLoader( eval_dataset_for_model, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size ) def lowercase__( __UpperCamelCase: Union[str, Any] ,__UpperCamelCase: Dict ,__UpperCamelCase: Any ,__UpperCamelCase: Dict="eval" ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = postprocess_qa_predictions( examples=__UpperCamelCase ,features=__UpperCamelCase ,predictions=__UpperCamelCase ,version_2_with_negative=args.version_2_with_negative ,n_best_size=args.n_best_size ,max_answer_length=args.max_answer_length ,null_score_diff_threshold=args.null_score_diff_threshold ,output_dir=args.output_dir ,prefix=__UpperCamelCase ,) # Format the result to the format the metric expects. if args.version_2_with_negative: SCREAMING_SNAKE_CASE : List[Any] = [ {'id': k, 'prediction_text': v, 'no_answer_probability': 0.0} for k, v in predictions.items() ] else: SCREAMING_SNAKE_CASE : Optional[int] = [{'id': k, 'prediction_text': v} for k, v in predictions.items()] SCREAMING_SNAKE_CASE : Any = [{'id': ex['id'], 'answers': ex[answer_column_name]} for ex in examples] return EvalPrediction(predictions=__UpperCamelCase ,label_ids=__UpperCamelCase ) UpperCamelCase_ = load_metric("squad_v2" if args.version_2_with_negative else "squad") # Evaluation! logger.info("Loading ONNX model %s for evaluation", args.onnx_model_path) with open(engine_name, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime, runtime.deserialize_cuda_engine( f.read() ) as engine, engine.create_execution_context() as context: # setup for TRT inferrence for i in range(len(input_names)): context.set_binding_shape(i, INPUT_SHAPE) assert context.all_binding_shapes_specified def lowercase__( __UpperCamelCase: Any ): """simple docstring""" return trt.volume(engine.get_binding_shape(__UpperCamelCase ) ) * engine.get_binding_dtype(__UpperCamelCase ).itemsize # Allocate device memory for inputs and outputs. UpperCamelCase_ = [cuda.mem_alloc(binding_nbytes(binding)) for binding in engine if engine.binding_is_input(binding)] # Allocate output buffer UpperCamelCase_ = cuda.pagelocked_empty(tuple(context.get_binding_shape(3)), dtype=np.floataa) UpperCamelCase_ = cuda.pagelocked_empty(tuple(context.get_binding_shape(4)), dtype=np.floataa) UpperCamelCase_ = cuda.mem_alloc(h_outputa.nbytes) UpperCamelCase_ = cuda.mem_alloc(h_outputa.nbytes) # Create a stream in which to copy inputs/outputs and run inference. UpperCamelCase_ = cuda.Stream() # Evaluation logger.info("***** Running Evaluation *****") logger.info(F""" Num examples = {len(eval_dataset)}""") logger.info(F""" Batch size = {args.per_device_eval_batch_size}""") UpperCamelCase_ = 0.0 UpperCamelCase_ = 0 UpperCamelCase_ = timeit.default_timer() UpperCamelCase_ = None for step, batch in enumerate(eval_dataloader): UpperCamelCase_ , UpperCamelCase_ = model_infer(batch, context, d_inputs, h_outputa, h_outputa, d_outputa, d_outputa, stream) total_time += infer_time niter += 1 UpperCamelCase_ , UpperCamelCase_ = outputs UpperCamelCase_ = torch.tensor(start_logits) UpperCamelCase_ = torch.tensor(end_logits) # necessary to pad predictions and labels for being gathered UpperCamelCase_ = accelerator.pad_across_processes(start_logits, dim=1, pad_index=-1_0_0) UpperCamelCase_ = accelerator.pad_across_processes(end_logits, dim=1, pad_index=-1_0_0) UpperCamelCase_ = (accelerator.gather(start_logits).cpu().numpy(), accelerator.gather(end_logits).cpu().numpy()) UpperCamelCase_ = logits if all_preds is None else nested_concat(all_preds, logits, padding_index=-1_0_0) if all_preds is not None: UpperCamelCase_ = nested_truncate(all_preds, len(eval_dataset)) UpperCamelCase_ = timeit.default_timer() - start_time logger.info(" Evaluation done in total %f secs (%f sec per example)", evalTime, evalTime / len(eval_dataset)) # Inference time from TRT logger.info("Average Inference Time = {:.3f} ms".format(total_time * 1_0_0_0 / niter)) logger.info("Total Inference Time = {:.3f} ms".format(total_time * 1_0_0_0)) logger.info("Total Number of Inference = %d", niter) UpperCamelCase_ = post_processing_function(eval_examples, eval_dataset, all_preds) UpperCamelCase_ = metric.compute(predictions=prediction.predictions, references=prediction.label_ids) logger.info(F"""Evaluation metrics: {eval_metric}""")
28
'''simple docstring''' import inspect import unittest from transformers import DecisionTransformerConfig, is_torch_available 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, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import DecisionTransformerModel from transformers.models.decision_transformer.modeling_decision_transformer import ( DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=6 , A=17 , A=23 , A=11 , A=True , ) -> Tuple: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = act_dim _a = state_dim _a = hidden_size _a = max_length _a = is_training def a__ (self ) -> Optional[int]: """simple docstring""" _a = floats_tensor((self.batch_size, self.seq_length, self.state_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, self.act_dim) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = floats_tensor((self.batch_size, self.seq_length, 1) ) _a = ids_tensor((self.batch_size, self.seq_length) , vocab_size=1_000 ) _a = random_attention_mask((self.batch_size, self.seq_length) ) _a = self.get_config() return ( config, states, actions, rewards, returns_to_go, timesteps, attention_mask, ) def a__ (self ) -> str: """simple docstring""" return DecisionTransformerConfig( batch_size=self.batch_size , seq_length=self.seq_length , act_dim=self.act_dim , state_dim=self.state_dim , hidden_size=self.hidden_size , max_length=self.max_length , ) def a__ (self , A , A , A , A , A , A , A , ) -> List[Any]: """simple docstring""" _a = DecisionTransformerModel(config=A ) model.to(A ) model.eval() _a = model(A , A , A , A , A , A ) self.parent.assertEqual(result.state_preds.shape , states.shape ) self.parent.assertEqual(result.action_preds.shape , actions.shape ) self.parent.assertEqual(result.return_preds.shape , returns_to_go.shape ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.seq_length * 3, self.hidden_size) ) # seq length *3 as there are 3 modelities: states, returns and actions def a__ (self ) -> Dict: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = { '''states''': states, '''actions''': actions, '''rewards''': rewards, '''returns_to_go''': returns_to_go, '''timesteps''': timesteps, '''attention_mask''': attention_mask, } return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = (DecisionTransformerModel,) if is_torch_available() else () __lowerCamelCase : List[str] = () __lowerCamelCase : Tuple = {'feature-extraction': DecisionTransformerModel} if is_torch_available() else {} # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids __lowerCamelCase : str = False # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False __lowerCamelCase : Tuple = False __lowerCamelCase : str = False __lowerCamelCase : Dict = False __lowerCamelCase : Tuple = False __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : List[str] = False def a__ (self ) -> Optional[int]: """simple docstring""" _a = DecisionTransformerModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> List[Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) @slow def a__ (self ) -> Optional[Any]: """simple docstring""" for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = DecisionTransformerModel.from_pretrained(A ) self.assertIsNotNone(A ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''states''', '''actions''', '''rewards''', '''returns_to_go''', '''timesteps''', '''attention_mask''', ] self.assertListEqual(arg_names[: len(A )] , A ) @require_torch class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Optional[Any]: """simple docstring""" _a = 2 # number of steps of autoregressive prediction we will perform _a = 10 # defined by the RL environment, may be normalized _a = DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert''' ) _a = model.to(A ) _a = model.config torch.manual_seed(0 ) _a = torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ) # env.reset() _a = torch.tensor( [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]] , device=A ) _a = torch.tensor(A , device=A , dtype=torch.floataa ).reshape(1 , 1 , 1 ) _a = state _a = torch.zeros(1 , 0 , config.act_dim , device=A , dtype=torch.floataa ) _a = torch.zeros(1 , 0 , device=A , dtype=torch.floataa ) _a = torch.tensor(0 , device=A , dtype=torch.long ).reshape(1 , 1 ) for step in range(A ): _a = torch.cat([actions, torch.zeros(1 , 1 , config.act_dim , device=A )] , dim=1 ) _a = torch.cat([rewards, torch.zeros(1 , 1 , device=A )] , dim=1 ) _a = torch.ones(1 , states.shape[1] ).to(dtype=torch.long , device=states.device ) with torch.no_grad(): _a , _a , _a = model( states=A , actions=A , rewards=A , returns_to_go=A , timesteps=A , attention_mask=A , return_dict=A , ) self.assertEqual(action_pred.shape , actions.shape ) self.assertTrue(torch.allclose(action_pred[0, -1] , expected_outputs[step] , atol=1E-4 ) ) _a , _a , _a , _a = ( # env.step(action) torch.randn(1 , 1 , config.state_dim ).to(device=A , dtype=torch.floataa ), 1.0, False, {}, ) _a = action_pred[0, -1] _a = torch.cat([states, state] , dim=1 ) _a = returns_to_go[0, -1] - reward _a = torch.cat([returns_to_go, pred_return.reshape(1 , 1 , 1 )] , dim=1 ) _a = torch.cat( [timesteps, torch.ones((1, 1) , device=A , dtype=torch.long ) * (step + 1)] , dim=1 )
11
0
"""simple docstring""" from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def lowercase ( lowerCAmelCase__ ,lowerCAmelCase__ ): lowerCamelCase_ = [] for part_id in partition_order: lowerCamelCase_ = df.where(f"SPARK_PARTITION_ID() = {part_id}" ).collect() for row_idx, row in enumerate(lowerCAmelCase__ ): expected_row_ids_and_row_dicts.append((f"{part_id}_{row_idx}", row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(100 ).repartition(1 ) lowerCamelCase_ = Spark(lowerCAmelCase__ ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(10 ).repartition(2 ) lowerCamelCase_ = [1, 0] lowerCamelCase_ = _generate_iterable_examples(lowerCAmelCase__ ,lowerCAmelCase__ ) # Reverse the partitions. lowerCamelCase_ = _get_expected_row_ids_and_row_dicts_for_partition_order(lowerCAmelCase__ ,lowerCAmelCase__ ) for i, (row_id, row_dict) in enumerate(generate_fn() ): lowerCamelCase_ , lowerCamelCase_ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(10 ).repartition(1 ) lowerCamelCase_ = SparkExamplesIterable(lowerCAmelCase__ ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(lowerCAmelCase__ ): assert row_id == f"0_{i}" assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: lowerCamelCase_ = lambda lowerCAmelCase__ : x.reverse() lowerCamelCase_ = _get_expected_row_ids_and_row_dicts_for_partition_order(lowerCAmelCase__ ,[2, 1, 0] ) lowerCamelCase_ = SparkExamplesIterable(lowerCAmelCase__ ).shuffle_data_sources(lowerCAmelCase__ ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(lowerCAmelCase__ ): lowerCamelCase_ , lowerCamelCase_ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 lowerCamelCase_ = SparkExamplesIterable(lowerCAmelCase__ ).shard_data_sources(worker_id=0 ,num_workers=2 ) assert shard_it_a.n_shards == 2 lowerCamelCase_ = _get_expected_row_ids_and_row_dicts_for_partition_order(lowerCAmelCase__ ,[0, 2] ) for i, (row_id, row_dict) in enumerate(lowerCAmelCase__ ): lowerCamelCase_ , lowerCamelCase_ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 lowerCamelCase_ = SparkExamplesIterable(lowerCAmelCase__ ).shard_data_sources(worker_id=1 ,num_workers=2 ) assert shard_it_a.n_shards == 2 lowerCamelCase_ = _get_expected_row_ids_and_row_dicts_for_partition_order(lowerCAmelCase__ ,[1, 3] ) for i, (row_id, row_dict) in enumerate(lowerCAmelCase__ ): lowerCamelCase_ , lowerCamelCase_ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def lowercase ( ): lowerCamelCase_ = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() lowerCamelCase_ = spark.range(100 ).repartition(1 ) lowerCamelCase_ = Spark(lowerCAmelCase__ ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 100
29
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" return len(set(__A)) == len(__A) if __name__ == "__main__": import doctest doctest.testmod()
11
0
__a = 'Input must be a string of 8 numbers plus letter' __a = 'TRWAGMYFPDXBNJZSQVHLCKE' def lowerCamelCase__ ( _lowercase ): '''simple docstring''' if not isinstance(_lowercase , _lowercase ): UpperCAmelCase_ : List[Any] = f'''Expected string as input, found {type(_lowercase ).__name__}''' raise TypeError(_lowercase ) UpperCAmelCase_ : Tuple = spanish_id.replace('''-''' , '''''' ).upper() if len(_lowercase ) != 9: raise ValueError(_lowercase ) try: UpperCAmelCase_ : str = int(spanish_id_clean[0:8] ) UpperCAmelCase_ : Optional[int] = spanish_id_clean[8] except ValueError as ex: raise ValueError(_lowercase ) from ex if letter.isdigit(): raise ValueError(_lowercase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
30
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A , __A): """simple docstring""" if len(__A) == 0: return False _a = len(__A) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , __A) else: return binary_search(a_list[midpoint + 1 :] , __A) if __name__ == "__main__": lowercase_ = input("Enter numbers separated by comma:\n").strip() lowercase_ = [int(item.strip()) for item in user_input.split(",")] lowercase_ = int(input("Enter the number to be found in the list:\n").strip()) lowercase_ = "" if binary_search(sequence, target) else "not " print(F"""{target} was {not_str}found in {sequence}""")
11
0
from __future__ import annotations from functools import lru_cache from math import ceil lowerCamelCase__ : Any = 100 lowerCamelCase__ : Optional[Any] = set(range(3, NUM_PRIMES, 2)) primes.add(2) lowerCamelCase__ : 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_ ( __UpperCAmelCase : int ) -> set[int]: if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} SCREAMING_SNAKE_CASE_ = set() SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 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_ ( __UpperCAmelCase : int = 50_00 ) -> int | None: for number_to_partition in range(1 , __UpperCAmelCase ): if len(partition(__UpperCAmelCase ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(f'''{solution() = }''')
31
'''simple docstring''' class __A : '''simple docstring''' def __init__(self , A ) -> None: """simple docstring""" _a = len(A ) _a = [0] * len_array if len_array > 0: _a = array[0] for i in range(1 , A ): _a = self.prefix_sum[i - 1] + array[i] def a__ (self , A , A ) -> int: """simple docstring""" if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def a__ (self , A ) -> bool: """simple docstring""" _a = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(A ) return False if __name__ == "__main__": import doctest doctest.testmod()
11
0
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert import BertTokenizer UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCAmelCase_ = { "vocab_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCAmelCase_ = { "vocab_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCAmelCase_ = { "vocab_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json" ), }, } UpperCAmelCase_ = { "facebook/dpr-ctx_encoder-single-nq-base": 5_12, "facebook/dpr-ctx_encoder-multiset-base": 5_12, } UpperCAmelCase_ = { "facebook/dpr-question_encoder-single-nq-base": 5_12, "facebook/dpr-question_encoder-multiset-base": 5_12, } UpperCAmelCase_ = { "facebook/dpr-reader-single-nq-base": 5_12, "facebook/dpr-reader-multiset-base": 5_12, } UpperCAmelCase_ = { "facebook/dpr-ctx_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-ctx_encoder-multiset-base": {"do_lower_case": True}, } UpperCAmelCase_ = { "facebook/dpr-question_encoder-single-nq-base": {"do_lower_case": True}, "facebook/dpr-question_encoder-multiset-base": {"do_lower_case": True}, } UpperCAmelCase_ = { "facebook/dpr-reader-single-nq-base": {"do_lower_case": True}, "facebook/dpr-reader-multiset-base": {"do_lower_case": True}, } class __UpperCamelCase ( A__ ): __A : Optional[int] = VOCAB_FILES_NAMES __A : int = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __A : List[str] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __A : Optional[int] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION class __UpperCamelCase ( A__ ): __A : Dict = VOCAB_FILES_NAMES __A : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __A : Any = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __A : str = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCAmelCase_ = collections.namedtuple( "DPRSpanPrediction", ["span_score", "relevance_score", "doc_id", "start_index", "end_index", "text"] ) UpperCAmelCase_ = collections.namedtuple("DPRReaderOutput", ["start_logits", "end_logits", "relevance_logits"]) UpperCAmelCase_ = r"\n Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n with the format:\n\n ```\n [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n ```\n\n Args:\n questions (`str` or `List[str]`):\n The questions to be encoded. You can specify one question for many passages. In this case, the question\n will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n `titles` or `texts`.\n titles (`str` or `List[str]`):\n The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n texts (`str` or `List[str]`):\n The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n Activates and controls padding. Accepts the following values:\n\n - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n if provided).\n - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided.\n - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different\n lengths).\n truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n Activates and controls truncation. Accepts the following values:\n\n - `True` or `'longest_first'`: Truncate to a maximum length specified with the argument `max_length` or to\n the maximum acceptable input length for the model if that argument is not provided. This will truncate\n token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n of pairs) is provided.\n - `'only_first'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the first\n sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `'only_second'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n acceptable input length for the model if that argument is not provided. This will only truncate the\n second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths\n greater than the model maximum admissible input size).\n max_length (`int`, *optional*):\n Controls the maximum length to use by one of the truncation/padding parameters.\n\n If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n is required by one of the truncation/padding parameters. If the model has no specific maximum input\n length (like XLNet) truncation/padding to a maximum length will be deactivated.\n return_tensors (`str` or [`~utils.TensorType`], *optional*):\n If set, will return tensors instead of list of python integers. Acceptable values are:\n\n - `'tf'`: Return TensorFlow `tf.constant` objects.\n - `'pt'`: Return PyTorch `torch.Tensor` objects.\n - `'np'`: Return Numpy `np.ndarray` objects.\n return_attention_mask (`bool`, *optional*):\n Whether or not to return the attention mask. If not set, will return the attention mask according to the\n specific tokenizer's default, defined by the `return_outputs` attribute.\n\n [What are attention masks?](../glossary#attention-mask)\n\n Returns:\n `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n - `input_ids`: List of token ids to be fed to a model.\n - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n " @add_start_docstrings(A__ ) class __UpperCamelCase : def __call__( self , _UpperCamelCase , _UpperCamelCase = None , _UpperCamelCase = None , _UpperCamelCase = False , _UpperCamelCase = False , _UpperCamelCase = None , _UpperCamelCase = None , _UpperCamelCase = None , **_UpperCamelCase , ): if titles is None and texts is None: return super().__call__( _UpperCamelCase , padding=_UpperCamelCase , truncation=_UpperCamelCase , max_length=_UpperCamelCase , return_tensors=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) elif titles is None or texts is None: _UpperCAmelCase = titles if texts is None else texts return super().__call__( _UpperCamelCase , _UpperCamelCase , padding=_UpperCamelCase , truncation=_UpperCamelCase , max_length=_UpperCamelCase , return_tensors=_UpperCamelCase , return_attention_mask=_UpperCamelCase , **_UpperCamelCase , ) _UpperCAmelCase = titles if not isinstance(_UpperCamelCase , _UpperCamelCase ) else [titles] _UpperCAmelCase = texts if not isinstance(_UpperCamelCase , _UpperCamelCase ) else [texts] _UpperCAmelCase = len(_UpperCamelCase ) _UpperCAmelCase = questions if not isinstance(_UpperCamelCase , _UpperCamelCase ) else [questions] * n_passages if len(_UpperCamelCase ) != len(_UpperCamelCase ): raise ValueError( f'''There should be as many titles than texts but got {len(_UpperCamelCase )} titles and {len(_UpperCamelCase )} texts.''' ) _UpperCAmelCase = super().__call__(_UpperCamelCase , _UpperCamelCase , padding=_UpperCamelCase , truncation=_UpperCamelCase )['''input_ids'''] _UpperCAmelCase = super().__call__(_UpperCamelCase , add_special_tokens=_UpperCamelCase , padding=_UpperCamelCase , truncation=_UpperCamelCase )['''input_ids'''] _UpperCAmelCase = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(_UpperCamelCase , _UpperCamelCase ) ] } if return_attention_mask is not False: _UpperCAmelCase = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) _UpperCAmelCase = attention_mask return self.pad(_UpperCamelCase , padding=_UpperCamelCase , max_length=_UpperCamelCase , return_tensors=_UpperCamelCase ) def UpperCamelCase( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = 16 , _UpperCamelCase = 64 , _UpperCamelCase = 4 , ): _UpperCAmelCase = reader_input['''input_ids'''] _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = reader_output[:3] _UpperCAmelCase = len(_UpperCamelCase ) _UpperCAmelCase = sorted(range(_UpperCamelCase ) , reverse=_UpperCamelCase , key=relevance_logits.__getitem__ ) _UpperCAmelCase = [] for doc_id in sorted_docs: _UpperCAmelCase = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence _UpperCAmelCase = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: _UpperCAmelCase = sequence_ids.index(self.pad_token_id ) else: _UpperCAmelCase = len(_UpperCamelCase ) _UpperCAmelCase = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=_UpperCamelCase , top_spans=_UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=_UpperCamelCase , start_index=_UpperCamelCase , end_index=_UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(_UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def UpperCamelCase( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , ): _UpperCAmelCase = [] for start_index, start_score in enumerate(_UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) _UpperCAmelCase = sorted(_UpperCamelCase , key=lambda _UpperCamelCase : x[1] , reverse=_UpperCamelCase ) _UpperCAmelCase = [] for (start_index, end_index), score in scores: if start_index > end_index: raise ValueError(f'''Wrong span indices: [{start_index}:{end_index}]''' ) _UpperCAmelCase = end_index - start_index + 1 if length > max_answer_length: raise ValueError(f'''Span is too long: {length} > {max_answer_length}''' ) if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(_UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(A__ ) class __UpperCamelCase ( A__ , A__ ): __A : Dict = VOCAB_FILES_NAMES __A : Dict = READER_PRETRAINED_VOCAB_FILES_MAP __A : int = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __A : Optional[Any] = READER_PRETRAINED_INIT_CONFIGURATION __A : Tuple = ["""input_ids""", """attention_mask"""]
32
'''simple docstring''' from __future__ import annotations def lowerCAmelCase (__A): """simple docstring""" _a = 2 _a = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(__A) if n > 1: factors.append(__A) return factors if __name__ == "__main__": import doctest doctest.testmod()
11
0
import argparse import os import re lowerCamelCase__ : Optional[int] = """src/transformers/models/auto""" # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict lowerCamelCase__ : List[Any] = re.compile(r"""[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict""") # re pattern that matches identifiers in mappings lowerCamelCase__ : Dict = re.compile(r"""\s*\(\s*\"(\S[^\"]+)\"""") def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase = False ) -> Union[str, Any]: with open(__lowerCAmelCase , '''r''' , encoding='''utf-8''' ) as f: snake_case__ = f.read() snake_case__ = content.split('''\n''' ) snake_case__ = [] snake_case__ = 0 while line_idx < len(__lowerCAmelCase ): if _re_intro_mapping.search(lines[line_idx] ) is not None: snake_case__ = len(re.search(r'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 snake_case__ = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": snake_case__ = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers snake_case__ = sorted(__lowerCAmelCase , key=lambda __lowerCAmelCase : _re_identifier.search(__lowerCAmelCase ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(__lowerCAmelCase , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(__lowerCAmelCase ) ) elif "\n".join(__lowerCAmelCase ) != content: return True def SCREAMING_SNAKE_CASE ( __lowerCAmelCase = False ) -> Tuple: snake_case__ = [os.path.join(__lowerCAmelCase , __lowerCAmelCase ) for f in os.listdir(__lowerCAmelCase ) if f.endswith('''.py''' )] snake_case__ = [sort_auto_mapping(__lowerCAmelCase , overwrite=__lowerCAmelCase ) for fname in fnames] if not overwrite and any(__lowerCAmelCase ): snake_case__ = [f for f, d in zip(__lowerCAmelCase , __lowerCAmelCase ) if d] raise ValueError( F"""The following files have auto mappings that need sorting: {', '.join(__lowerCAmelCase )}. Run `make style` to fix""" ''' this.''' ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("""--check_only""", action="""store_true""", help="""Whether to only check or fix style.""") lowerCamelCase__ : Union[str, Any] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
33
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 lowercase_ = { # 1536-bit 5: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 2048-bit 14: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 3072-bit 15: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 4096-bit 16: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 6144-bit 17: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" + "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" + "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9" + "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6" + "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8" + "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C" + "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" + "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D" + "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D" + "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226" + "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC" + "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26" + "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB" + "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2" + "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" + "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406" + "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918" + "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151" + "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03" + "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F" + "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B" + "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632" + "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E" + "6DCC4024FFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, # 8192-bit 18: { "prime": int( "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF", base=16, ), "generator": 2, }, } class __A : '''simple docstring''' def __init__(self , A = 14 ) -> None: """simple docstring""" if group not in primes: raise ValueError('''Unsupported Group''' ) _a = primes[group]['''prime'''] _a = primes[group]['''generator'''] _a = int(hexlify(urandom(32 ) ) , base=16 ) def a__ (self ) -> str: """simple docstring""" return hex(self.__private_key )[2:] def a__ (self ) -> str: """simple docstring""" _a = pow(self.generator , self.__private_key , self.prime ) return hex(A )[2:] def a__ (self , A ) -> bool: """simple docstring""" return ( 2 <= key <= self.prime - 2 and pow(A , (self.prime - 1) // 2 , self.prime ) == 1 ) def a__ (self , A ) -> str: """simple docstring""" _a = int(A , base=16 ) if not self.is_valid_public_key(A ): raise ValueError('''Invalid public key''' ) _a = pow(A , self.__private_key , self.prime ) return shaaaa(str(A ).encode() ).hexdigest() @staticmethod def a__ (A , A ) -> bool: """simple docstring""" return ( 2 <= remote_public_key_str <= prime - 2 and pow(A , (prime - 1) // 2 , A ) == 1 ) @staticmethod def a__ (A , A , A = 14 ) -> str: """simple docstring""" _a = int(A , base=16 ) _a = int(A , base=16 ) _a = primes[group]['''prime'''] if not DiffieHellman.is_valid_public_key_static(A , A ): raise ValueError('''Invalid public key''' ) _a = pow(A , A , A ) return shaaaa(str(A ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
11
0
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available SCREAMING_SNAKE_CASE_ = {'configuration_focalnet': ['FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FocalNetConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE_ = [ 'FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST', 'FocalNetForImageClassification', 'FocalNetForMaskedImageModeling', 'FocalNetBackbone', 'FocalNetModel', 'FocalNetPreTrainedModel', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
34
'''simple docstring''' import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel lowercase_ = logging.getLogger(__name__) def lowerCAmelCase (__A , __A): """simple docstring""" if os.path.exists(__A): if os.path.exists(os.path.join(__A , '''config.json''')) and os.path.isfile( os.path.join(__A , '''config.json''')): os.remove(os.path.join(__A , '''config.json''')) if os.path.exists(os.path.join(__A , '''pytorch_model.bin''')) and os.path.isfile( os.path.join(__A , '''pytorch_model.bin''')): os.remove(os.path.join(__A , '''pytorch_model.bin''')) else: os.makedirs(__A) model.save_pretrained(__A) def lowerCAmelCase (__A , __A=False): """simple docstring""" _a = 2 if unlogit: _a = torch.pow(__A , __A) _a = p * torch.log(__A) _a = 0 return -plogp.sum(dim=-1) def lowerCAmelCase (__A): """simple docstring""" logger.info('''lv, h >\t''' + '''\t'''.join(F'''{x + 1}''' for x in range(len(__A)))) for row in range(len(__A)): if tensor.dtype != torch.long: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:.5f}''' for x in tensor[row].cpu().data)) else: logger.info(F'''layer {row + 1}:\t''' + '''\t'''.join(F'''{x:d}''' for x in tensor[row].cpu().data)) def lowerCAmelCase (__A , __A , __A , __A=True , __A=True , __A=None , __A=False): """simple docstring""" _a , _a = model.config.num_hidden_layers, model.config.num_attention_heads _a = torch.zeros(__A , __A).to(args.device) _a = torch.zeros(__A , __A).to(args.device) if head_mask is None: _a = torch.ones(__A , __A).to(args.device) head_mask.requires_grad_(requires_grad=__A) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: _a = None _a = 0.0 _a = 0.0 for step, inputs in enumerate(tqdm(__A , desc='''Iteration''' , disable=args.local_rank not in [-1, 0])): _a = tuple(t.to(args.device) for t in inputs) ((_a) , ) = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) _a = model(__A , labels=__A , head_mask=__A) # (loss), lm_logits, presents, (all hidden_states), (attentions) _a , _a , _a = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__A): _a = entropy(attn.detach() , __A) attn_entropy[layer] += masked_entropy.sum(-1).sum(0).sum(0).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__A).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: _a = 2 _a = torch.pow(torch.pow(__A , __A).sum(-1) , 1 / exponent) head_importance /= norm_by_layer.unsqueeze(-1) + 1e-20 if not args.dont_normalize_global_importance: _a = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info('''Attention entropies''') print_ad_tensor(__A) if compute_importance: logger.info('''Head importance scores''') print_ad_tensor(__A) logger.info('''Head ranked by importance scores''') _a = torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device) _a = torch.arange( head_importance.numel() , device=args.device) _a = head_ranks.view_as(__A) print_ad_tensor(__A) return attn_entropy, head_importance, total_loss def lowerCAmelCase (__A , __A , __A): """simple docstring""" _a , _a , _a = compute_heads_importance(__A , __A , __A , compute_entropy=__A) _a = 1 / loss # instead of downsteam score use the LM loss logger.info('''Pruning: original score: %f, threshold: %f''' , __A , original_score * args.masking_threshold) _a = torch.ones_like(__A) _a = max(1 , int(new_head_mask.numel() * args.masking_amount)) _a = original_score while current_score >= original_score * args.masking_threshold: _a = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads _a = float('''Inf''') _a = head_importance.view(-1).sort()[1] if len(__A) <= num_to_mask: print('''BREAK BY num_to_mask''') break # mask heads _a = current_heads_to_mask[:num_to_mask] logger.info('''Heads to mask: %s''' , str(current_heads_to_mask.tolist())) _a = new_head_mask.view(-1) _a = 0.0 _a = new_head_mask.view_as(__A) _a = new_head_mask.clone().detach() print_ad_tensor(__A) # Compute metric and head importance again _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , head_mask=__A) _a = 1 / loss logger.info( '''Masking: current score: %f, remaining heads %d (%.1f percents)''' , __A , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info('''Final head mask''') print_ad_tensor(__A) np.save(os.path.join(args.output_dir , '''head_mask.npy''') , head_mask.detach().cpu().numpy()) return head_mask def lowerCAmelCase (__A , __A , __A , __A): """simple docstring""" _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A) _a = 1 / loss _a = datetime.now() - before_time _a = sum(p.numel() for p in model.parameters()) _a = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__A)) } for k, v in heads_to_prune.items(): if isinstance(__A , __A): _a = [ v, ] assert sum(len(__A) for h in heads_to_prune.values()) == (1 - head_mask.long()).sum().item() model.prune_heads(__A) _a = sum(p.numel() for p in model.parameters()) _a = datetime.now() _a , _a , _a = compute_heads_importance( __A , __A , __A , compute_entropy=__A , compute_importance=__A , head_mask=__A , actually_pruned=__A , ) _a = 1 / loss _a = datetime.now() - before_time logger.info( '''Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)''' , __A , __A , pruned_num_params / original_num_params * 100 , ) logger.info('''Pruning: score with masking: %f score with pruning: %f''' , __A , __A) logger.info('''Pruning: speed ratio (original timing / new timing): %f percents''' , original_time / new_time * 100) save_model(__A , args.output_dir) def lowerCAmelCase (): """simple docstring""" _a = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--data_dir''' , default=__A , type=__A , required=__A , help='''The input data dir. Should contain the .tsv files (or other data files) for the task.''' , ) parser.add_argument( '''--model_name_or_path''' , default=__A , type=__A , required=__A , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--output_dir''' , default=__A , type=__A , required=__A , help='''The output directory where the model predictions and checkpoints will be written.''' , ) # Other parameters parser.add_argument( '''--config_name''' , default='''''' , type=__A , help='''Pretrained config name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--tokenizer_name''' , default='''''' , type=__A , help='''Pretrained tokenizer name or path if not the same as model_name_or_path''' , ) parser.add_argument( '''--cache_dir''' , default=__A , type=__A , help='''Where do you want to store the pre-trained models downloaded from s3''' , ) parser.add_argument( '''--data_subset''' , type=__A , default=-1 , help='''If > 0: limit the data to a subset of data_subset instances.''') parser.add_argument( '''--overwrite_output_dir''' , action='''store_true''' , help='''Whether to overwrite data in output directory''') parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''') parser.add_argument( '''--dont_normalize_importance_by_layer''' , action='''store_true''' , help='''Don\'t normalize importance score by layers''') parser.add_argument( '''--dont_normalize_global_importance''' , action='''store_true''' , help='''Don\'t normalize all importance scores between 0 and 1''' , ) parser.add_argument( '''--try_masking''' , action='''store_true''' , help='''Whether to try to mask head until a threshold of accuracy.''') parser.add_argument( '''--masking_threshold''' , default=0.9 , type=__A , help='''masking threshold in term of metrics (stop masking when metric < threshold * original metric value).''' , ) parser.add_argument( '''--masking_amount''' , default=0.1 , type=__A , help='''Amount to heads to masking at each masking step.''') parser.add_argument('''--metric_name''' , default='''acc''' , type=__A , help='''Metric to use for head masking.''') parser.add_argument( '''--max_seq_length''' , default=128 , type=__A , help=( '''The maximum total input sequence length after WordPiece tokenization. \n''' '''Sequences longer than this will be truncated, sequences shorter padded.''' ) , ) parser.add_argument('''--batch_size''' , default=1 , type=__A , help='''Batch size.''') parser.add_argument('''--seed''' , type=__A , default=42) parser.add_argument('''--local_rank''' , type=__A , default=-1 , help='''local_rank for distributed training on gpus''') parser.add_argument('''--no_cuda''' , action='''store_true''' , help='''Whether not to use CUDA when available''') parser.add_argument('''--server_ip''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') parser.add_argument('''--server_port''' , type=__A , default='''''' , help='''Can be used for distant debugging.''') _a = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('''Waiting for debugger attach''') ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__A) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: _a = torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu''') _a = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank) _a = torch.device('''cuda''' , args.local_rank) _a = 1 torch.distributed.init_process_group(backend='''nccl''') # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN) logger.info('''device: {} n_gpu: {}, distributed: {}'''.format(args.device , args.n_gpu , bool(args.local_rank != -1))) _a = GPTaLMHeadModel.from_pretrained(args.model_name_or_path) # Distributed and parallel training model.to(args.device) if args.local_rank != -1: _a = nn.parallel.DistributedDataParallel( __A , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__A) elif args.n_gpu > 1: _a = nn.DataParallel(__A) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__A) torch.save(__A , os.path.join(args.output_dir , '''run_args.bin''')) logger.info('''Training/evaluation parameters %s''' , __A) # Prepare dataset _a = np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa), ]) _a = (torch.from_numpy(__A),) _a = TensorDataset(*__A) _a = RandomSampler(__A) _a = DataLoader(__A , sampler=__A , batch_size=args.batch_size) # Compute head entropy and importance score compute_heads_importance(__A , __A , __A) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: _a = mask_heads(__A , __A , __A) prune_heads(__A , __A , __A , __A) if __name__ == "__main__": main()
11
0
import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, 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 lowercase ( unittest.TestCase ): def lowercase__ ( self : Any ): # clean up the VRAM after each test super().tearDown() gc.collect() def lowercase__ ( self : List[Any] ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[int] = FlaxStableDiffusionPipeline.from_pretrained( '''stabilityai/stable-diffusion-2''' , revision='''bf16''' , dtype=jnp.bfloataa , ) SCREAMING_SNAKE_CASE__ : Dict = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE__ : Union[str, Any] = jax.device_count() SCREAMING_SNAKE_CASE__ : Optional[int] = num_samples * [prompt] SCREAMING_SNAKE_CASE__ : Optional[int] = sd_pipe.prepare_inputs(_lowercase ) SCREAMING_SNAKE_CASE__ : int = replicate(_lowercase ) SCREAMING_SNAKE_CASE__ : str = shard(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE__ : Any = jax.random.split(_lowercase , jax.device_count() ) SCREAMING_SNAKE_CASE__ : List[str] = sd_pipe(_lowercase , _lowercase , _lowercase , num_inference_steps=25 , jit=_lowercase )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) SCREAMING_SNAKE_CASE__ : Optional[int] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = images[0, 2_53:2_56, 2_53:2_56, -1] SCREAMING_SNAKE_CASE__ : Tuple = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE__ : List[str] = jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512] ) print(f"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def lowercase__ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ : str = '''stabilityai/stable-diffusion-2''' SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = FlaxDPMSolverMultistepScheduler.from_pretrained(_lowercase , subfolder='''scheduler''' ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[Any] = FlaxStableDiffusionPipeline.from_pretrained( _lowercase , scheduler=_lowercase , revision='''bf16''' , dtype=jnp.bfloataa , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = scheduler_params SCREAMING_SNAKE_CASE__ : List[Any] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE__ : List[Any] = jax.device_count() SCREAMING_SNAKE_CASE__ : str = num_samples * [prompt] SCREAMING_SNAKE_CASE__ : List[Any] = sd_pipe.prepare_inputs(_lowercase ) SCREAMING_SNAKE_CASE__ : str = replicate(_lowercase ) SCREAMING_SNAKE_CASE__ : List[Any] = shard(_lowercase ) SCREAMING_SNAKE_CASE__ : Any = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE__ : int = jax.random.split(_lowercase , jax.device_count() ) SCREAMING_SNAKE_CASE__ : int = sd_pipe(_lowercase , _lowercase , _lowercase , num_inference_steps=25 , jit=_lowercase )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) SCREAMING_SNAKE_CASE__ : str = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE__ : str = images[0, 2_53:2_56, 2_53:2_56, -1] SCREAMING_SNAKE_CASE__ : List[Any] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE__ : List[Any] = jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297] ) print(f"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
35
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''multiplicative_persistence() only accepts integral values''') if num < 0: raise ValueError('''multiplicative_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 1 for i in range(0 , len(__A)): total *= numbers[i] _a = str(__A) steps += 1 return steps def lowerCAmelCase (__A): """simple docstring""" if not isinstance(__A , __A): raise ValueError('''additive_persistence() only accepts integral values''') if num < 0: raise ValueError('''additive_persistence() does not accept negative values''') _a = 0 _a = str(__A) while len(__A) != 1: _a = [int(__A) for i in num_string] _a = 0 for i in range(0 , len(__A)): total += numbers[i] _a = str(__A) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
11
0
import unittest import numpy as np import torch from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _A ( unittest.TestCase ): '''simple docstring''' @property def snake_case_ ( self ): '''simple docstring''' torch.manual_seed(0 ) snake_case : Union[str, Any] = UNetaDModel( block_out_channels=(32, 64) ,layers_per_block=2 ,sample_size=32 ,in_channels=3 ,out_channels=3 ,down_block_types=("""DownBlock2D""", """AttnDownBlock2D""") ,up_block_types=("""AttnUpBlock2D""", """UpBlock2D""") ,) return model def snake_case_ ( self ): '''simple docstring''' snake_case : str = self.dummy_uncond_unet snake_case : Union[str, Any] = ScoreSdeVeScheduler() snake_case : int = ScoreSdeVePipeline(unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ) sde_ve.to(SCREAMING_SNAKE_CASE_ ) sde_ve.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = torch.manual_seed(0 ) snake_case : Dict = sde_ve(num_inference_steps=2 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ).images snake_case : Tuple = torch.manual_seed(0 ) snake_case : str = sde_ve(num_inference_steps=2 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ,return_dict=SCREAMING_SNAKE_CASE_ )[ 0 ] snake_case : Union[str, Any] = image[0, -3:, -3:, -1] snake_case : Tuple = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case : Tuple = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class _A ( unittest.TestCase ): '''simple docstring''' def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = """google/ncsnpp-church-256""" snake_case : Any = UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = ScoreSdeVeScheduler.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case : Tuple = ScoreSdeVePipeline(unet=SCREAMING_SNAKE_CASE_ ,scheduler=SCREAMING_SNAKE_CASE_ ) sde_ve.to(SCREAMING_SNAKE_CASE_ ) sde_ve.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) snake_case : Union[str, Any] = torch.manual_seed(0 ) snake_case : str = sde_ve(num_inference_steps=10 ,output_type="""numpy""" ,generator=SCREAMING_SNAKE_CASE_ ).images snake_case : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) snake_case : Tuple = np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
36
'''simple docstring''' import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=7 , A=3 , A=18 , A=30 , A=400 , A=True , A=None , A=True , A=[0.5, 0.5, 0.5] , A=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" _a = size if size is not None else {'''height''': 18, '''width''': 18} _a = parent _a = batch_size _a = num_channels _a = image_size _a = min_resolution _a = max_resolution _a = do_resize _a = size _a = do_normalize _a = image_mean _a = image_std def a__ (self ) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = DPTImageProcessor if is_vision_available() else None def a__ (self ) -> Optional[Any]: """simple docstring""" _a = DPTImageProcessingTester(self ) @property def a__ (self ) -> int: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def a__ (self ) -> Dict: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , '''image_mean''' ) ) self.assertTrue(hasattr(A , '''image_std''' ) ) self.assertTrue(hasattr(A , '''do_normalize''' ) ) self.assertTrue(hasattr(A , '''do_resize''' ) ) self.assertTrue(hasattr(A , '''size''' ) ) def a__ (self ) -> Any: """simple docstring""" _a = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) _a = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> str: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _a = 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 _a = image_processing(A , 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 a__ (self ) -> Optional[int]: """simple docstring""" _a = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _a = 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 _a = image_processing(A , 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'''], ) , )
11
0
from __future__ import annotations from typing import Generic, TypeVar UpperCamelCase : Optional[Any] = TypeVar("""T""") class A__ ( Generic[T] ): """simple docstring""" def __init__( self : Tuple , lowerCamelCase__ : T ): a__ : int = data a__ : List[Any] = self a__ : Optional[Any] = 0 class A__ ( Generic[T] ): """simple docstring""" def __init__( self : Union[str, Any] ): # map from node name to the node object a__ : dict[T, DisjointSetTreeNode[T]] = {} def _UpperCamelCase( self : List[Any] , lowerCamelCase__ : T ): # create a new set with x as its member a__ : str = DisjointSetTreeNode(lowerCamelCase__ ) def _UpperCamelCase( self : int , lowerCamelCase__ : T ): # find the set x belongs to (with path-compression) a__ : Optional[Any] = self.map[data] if elem_ref != elem_ref.parent: a__ : Optional[int] = self.find_set(elem_ref.parent.data ) return elem_ref.parent def _UpperCamelCase( self : Optional[int] , lowerCamelCase__ : DisjointSetTreeNode[T] , lowerCamelCase__ : DisjointSetTreeNode[T] ): # helper function for union operation if nodea.rank > nodea.rank: a__ : Tuple = nodea else: a__ : Any = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def _UpperCamelCase( self : List[str] , lowerCamelCase__ : T , lowerCamelCase__ : T ): # merge 2 disjoint sets self.link(self.find_set(lowerCamelCase__ ) , self.find_set(lowerCamelCase__ ) ) class A__ ( Generic[T] ): """simple docstring""" def __init__( self : List[Any] ): # connections: map from the node to the neighbouring nodes (with weights) a__ : dict[T, dict[T, int]] = {} def _UpperCamelCase( self : Union[str, Any] , lowerCamelCase__ : T ): # add a node ONLY if its not present in the graph if node not in self.connections: a__ : List[str] = {} def _UpperCamelCase( self : Tuple , lowerCamelCase__ : T , lowerCamelCase__ : T , lowerCamelCase__ : int ): # add an edge with the given weight self.add_node(lowerCamelCase__ ) self.add_node(lowerCamelCase__ ) a__ : Tuple = weight a__ : Union[str, Any] = weight def _UpperCamelCase( self : Union[str, Any] ): a__ : List[Any] = [] a__ : Dict = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda lowerCamelCase__ : x[2] ) # creating the disjoint set a__ : Optional[Any] = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(lowerCamelCase__ ) # MST generation a__ : List[Any] = 0 a__ : Union[str, Any] = 0 a__ : Dict = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: a__, a__, a__ : str = edges[index] index += 1 a__ : str = disjoint_set.find_set(lowerCamelCase__ ) a__ : List[str] = disjoint_set.find_set(lowerCamelCase__ ) if parent_u != parent_v: num_edges += 1 graph.add_edge(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) disjoint_set.union(lowerCamelCase__ , lowerCamelCase__ ) return graph
37
'''simple docstring''' import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin lowercase_ = 1e-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __A : '''simple docstring''' def __init__(self , A , A=16 , A=13 , A=7 , A=14 , A=10 , A=19 , A=5 , A=4 , A=True , A=16 , A=2 , A=4 , A=4 , A="gelu" , A=0.1 , A=0.1 , A=[1, 2, 3, 4, 5] , A=25 , A=5 , ) -> List[str]: """simple docstring""" _a = d_model _a = parent _a = batch_size _a = prediction_length _a = context_length _a = cardinality _a = num_time_features _a = lags_sequence _a = embedding_dimension _a = is_training _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = context_length _a = prediction_length + label_length _a = label_length _a = moving_average _a = autocorrelation_factor def a__ (self ) -> Any: """simple docstring""" return AutoformerConfig( d_model=self.d_model , 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 , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def a__ (self , A ) -> List[Any]: """simple docstring""" _a = config.context_length + max(config.lags_sequence ) _a = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) _a = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, _past_length] ) _a = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs _a = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) _a = floats_tensor([self.batch_size, config.prediction_length] ) _a = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def a__ (self ) -> Any: """simple docstring""" _a = self.get_config() _a = self.prepare_autoformer_inputs_dict(A ) return config, inputs_dict def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.prepare_config_and_inputs() return config, inputs_dict def a__ (self , A , A ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModel(config=A ).to(A ).eval() _a = model(**A ) _a = outputs.encoder_last_hidden_state _a = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_encoder() encoder.save_pretrained(A ) _a = AutoformerEncoder.from_pretrained(A ).to(A ) _a , _a , _a , _a , _a = model.create_network_inputs(**A ) _a , _a = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) _a = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) _a = encoder(inputs_embeds=A )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) _a = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) _a = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) _a = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) _a = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: _a = model.get_decoder() decoder.save_pretrained(A ) _a = AutoformerDecoder.from_pretrained(A ).to(A ) _a = decoder( trend=A , inputs_embeds=A , encoder_hidden_states=A , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __A ( A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () __lowerCamelCase : Optional[Any] = (AutoformerForPrediction,) if is_torch_available() else () __lowerCamelCase : Tuple = {'feature-extraction': AutoformerModel} if is_torch_available() else {} __lowerCamelCase : Tuple = False __lowerCamelCase : Dict = False __lowerCamelCase : int = False __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Optional[int] = False __lowerCamelCase : List[Any] = False def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = AutoformerModelTester(self ) _a = ConfigTester(self , config_class=A , has_text_modality=A ) def a__ (self ) -> Dict: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: _a = model_class(A ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(A ) _a , _a = model_class.from_pretrained(A , output_loading_info=A ) self.assertEqual(info['''missing_keys'''] , [] ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*A ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def a__ (self ) -> Tuple: """simple docstring""" pass def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = inspect.signature(getattr(A , '''forward''' ) ) # The main input is the name of the argument after `self` _a = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(A ) _a = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a = [*signature.parameters.keys()] _a = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(A )] , A ) def a__ (self ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = True _a = getattr(self.model_tester , '''seq_length''' , A ) _a = getattr(self.model_tester , '''decoder_seq_length''' , A ) _a = getattr(self.model_tester , '''encoder_seq_length''' , A ) _a = getattr(self.model_tester , '''d_model''' , A ) _a = getattr(self.model_tester , '''num_attention_heads''' , A ) _a = d_model // num_attention_heads for model_class in self.all_model_classes: _a = True _a = False _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) _a = outputs.encoder_attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) _a = len(A ) _a = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(A , A ) # decoder attentions _a = outputs.decoder_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions _a = outputs.cross_attentions self.assertIsInstance(A , (list, tuple) ) self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine _a = True _a = True _a = model_class(A ) model.to(A ) model.eval() with torch.no_grad(): _a = model(**self._prepare_for_class(A , A ) ) self.assertEqual(out_len + 2 , len(A ) ) _a = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def a__ (self ) -> Optional[Any]: """simple docstring""" super().test_retain_grad_hidden_states_attentions() def lowerCAmelCase (__A="train-batch.pt"): """simple docstring""" _a = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=__A , repo_type='''dataset''') _a = torch.load(__A , map_location=__A) return batch @require_torch @slow class __A ( unittest.TestCase ): '''simple docstring''' def a__ (self ) -> Optional[int]: """simple docstring""" _a = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch() with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] _a = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Any: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state _a = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , A ) _a = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=A ) self.assertTrue(torch.allclose(output[0, :3, :3] , A , atol=A ) ) def a__ (self ) -> Tuple: """simple docstring""" _a = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(A ) _a = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): _a = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) _a = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , A ) _a = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=A ) _a = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , A , rtol=1E-1 ) )
11
0
'''simple docstring''' from __future__ import annotations def UpperCamelCase__ ( __magic_name__ : str , __magic_name__ : str ) -> bool: '''simple docstring''' snake_case__ : Union[str, Any] = get_failure_array(__magic_name__ ) # 2) Step through text searching for pattern snake_case__ , snake_case__ : List[str] = 0, 0 # index into text, pattern while i < len(__magic_name__ ): if pattern[j] == text[i]: if j == (len(__magic_name__ ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: snake_case__ : Dict = failure[j - 1] continue i += 1 return False def UpperCamelCase__ ( __magic_name__ : str ) -> list[int]: '''simple docstring''' snake_case__ : Union[str, Any] = [0] snake_case__ : int = 0 snake_case__ : Any = 1 while j < len(__magic_name__ ): if pattern[i] == pattern[j]: i += 1 elif i > 0: snake_case__ : List[str] = failure[i - 1] continue j += 1 failure.append(__magic_name__ ) return failure if __name__ == "__main__": # Test 1) A_ : Optional[Any] = "abc1abc12" A_ : List[str] = "alskfjaldsabc1abc1abc12k23adsfabcabc" A_ : Union[str, Any] = "alskfjaldsk23adsfabcabc" assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) A_ : Dict = "ABABX" A_ : int = "ABABZABABYABABX" assert kmp(pattern, text) # Test 3) A_ : List[Any] = "AAAB" A_ : List[str] = "ABAAAAAB" assert kmp(pattern, text) # Test 4) A_ : Optional[int] = "abcdabcy" A_ : List[str] = "abcxabcdabxabcdabcdabcy" assert kmp(pattern, text) # Test 5) A_ : Tuple = "aabaabaaa" assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
38
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=False , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=3 , A=4 , A=None , ) -> str: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_labels _a = num_choices _a = scope def a__ (self ) -> List[str]: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = None _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _a = ids_tensor([self.batch_size] , self.num_choices ) _a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ (self ) -> Optional[int]: """simple docstring""" return OpenLlamaConfig( 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=A , initializer_range=self.initializer_range , use_stable_embedding=A , ) def a__ (self , A , A , A , A , A , A , A ) -> Any: """simple docstring""" _a = OpenLlamaModel(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A ) _a = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Any: """simple docstring""" _a = True _a = OpenLlamaModel(A ) model.to(A ) model.eval() _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , ) _a = model( A , attention_mask=A , encoder_hidden_states=A , ) _a = model(A , attention_mask=A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Tuple: """simple docstring""" _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ (self , A , A , A , A , A , A , A , A , A , ) -> Dict: """simple docstring""" _a = True _a = True _a = OpenLlamaForCausalLM(config=A ) model.to(A ) model.eval() # first forward pass _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , use_cache=A , ) _a = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids _a = ids_tensor((self.batch_size, 3) , config.vocab_size ) _a = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and _a = torch.cat([input_ids, next_tokens] , dim=-1 ) _a = torch.cat([input_mask, next_mask] , dim=-1 ) _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , output_hidden_states=A , )['''hidden_states'''][0] _a = model( A , attention_mask=A , encoder_hidden_states=A , encoder_attention_mask=A , past_key_values=A , output_hidden_states=A , )['''hidden_states'''][0] # select random slice _a = ids_tensor((1,) , output_from_past.shape[-1] ).item() _a = output_from_no_past[:, -3:, random_slice_idx].detach() _a = 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(A , A , atol=1E-3 ) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a = self.prepare_config_and_inputs() ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = config_and_inputs _a = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( A , A , A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowerCamelCase : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[Any] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[str] = False __lowerCamelCase : List[str] = False def a__ (self ) -> Tuple: """simple docstring""" _a = OpenLlamaModelTester(self ) _a = ConfigTester(self , config_class=A , hidden_size=37 ) def a__ (self ) -> List[str]: """simple docstring""" self.config_tester.run_common_tests() def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def a__ (self ) -> str: """simple docstring""" _a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(*A ) def a__ (self ) -> Any: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Dict: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''single_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a__ (self ) -> Optional[Any]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = 3 _a = '''multi_label_classification''' _a = input_dict['''input_ids'''] _a = input_ids.ne(1 ).to(A ) _a = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _a = OpenLlamaForSequenceClassification(A ) model.to(A ) model.eval() _a = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def a__ (self ) -> Optional[Any]: """simple docstring""" pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def a__ (self , A ) -> Optional[int]: """simple docstring""" _a , _a = self.model_tester.prepare_config_and_inputs_for_common() _a = ids_tensor([1, 10] , config.vocab_size ) _a = 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 _a = OpenLlamaModel(A ) original_model.to(A ) original_model.eval() _a = original_model(A ).last_hidden_state _a = original_model(A ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights _a = {'''type''': scaling_type, '''factor''': 10.0} _a = OpenLlamaModel(A ) scaled_model.to(A ) scaled_model.eval() _a = scaled_model(A ).last_hidden_state _a = scaled_model(A ).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(A , A , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(A , A , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(A , A , atol=1E-5 ) )
11
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase_ = { '''configuration_mobilenet_v2''': [ '''MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileNetV2Config''', '''MobileNetV2OnnxConfig''', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''MobileNetV2FeatureExtractor'''] lowerCAmelCase_ = ['''MobileNetV2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MobileNetV2ForImageClassification''', '''MobileNetV2ForSemanticSegmentation''', '''MobileNetV2Model''', '''MobileNetV2PreTrainedModel''', '''load_tf_weights_in_mobilenet_v2''', ] if TYPE_CHECKING: from .configuration_mobilenet_va import ( MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileNetVaConfig, MobileNetVaOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor from .image_processing_mobilenet_va import MobileNetVaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilenet_va import ( MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel, MobileNetVaPreTrainedModel, load_tf_weights_in_mobilenet_va, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
'''simple docstring''' import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __A ( unittest.TestCase ): '''simple docstring''' def __init__(self , A , A=13 , A=7 , A=True , A=True , A=True , A=True , A=99 , A=32 , A=5 , A=4 , A=37 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=16 , A=2 , A=0.02 , A=4 , ) -> List[str]: """simple docstring""" _a = parent _a = batch_size _a = seq_length _a = is_training _a = use_attention_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = num_choices def a__ (self ) -> str: """simple docstring""" _a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _a = None if self.use_attention_mask: _a = random_attention_mask([self.batch_size, self.seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _a = AlbertConfig( 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=A , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a__ (self ) -> List[str]: """simple docstring""" _a = self.prepare_config_and_inputs() _a , _a , _a , _a = config_and_inputs _a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __A ( A , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a__ (self ) -> Union[str, Any]: """simple docstring""" _a = FlaxAlbertModelTester(self ) @slow def a__ (self ) -> str: """simple docstring""" for model_class_name in self.all_model_classes: _a = model_class_name.from_pretrained('''albert-base-v2''' ) _a = model(np.ones((1, 1) ) ) self.assertIsNotNone(A ) @require_flax class __A ( unittest.TestCase ): '''simple docstring''' @slow def a__ (self ) -> Dict: """simple docstring""" _a = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) _a = np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) _a = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) _a = model(A , attention_mask=A )[0] _a = (1, 11, 768) self.assertEqual(output.shape , A ) _a = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , A , atol=1E-4 ) )
11
0
from collections import Counter from timeit import timeit def UpperCamelCase ( snake_case__ : str = "" , ) -> bool: return sum(c % 2 for c in Counter(input_str.replace(' ' , '' ).lower() ).values() ) < 2 def UpperCamelCase ( snake_case__ : str = "" ) -> bool: if len(snake_case__ ) == 0: return True UpperCamelCase : Union[str, Any] = input_str.replace(' ' , '' ).lower() # character_freq_dict: Stores the frequency of every character in the input string UpperCamelCase : dict[str, int] = {} for character in lower_case_input_str: UpperCamelCase : List[Any] = character_freq_dict.get(snake_case__ , 0 ) + 1 UpperCamelCase : int = 0 for character_count in character_freq_dict.values(): if character_count % 2: odd_char += 1 if odd_char > 1: return False return True def UpperCamelCase ( snake_case__ : str = "" ) -> None: print('\nFor string = ' , snake_case__ , ':' ) print( '> can_string_be_rearranged_as_palindrome_counter()' , '\tans =' , can_string_be_rearranged_as_palindrome_counter(snake_case__ ) , '\ttime =' , timeit( 'z.can_string_be_rearranged_as_palindrome_counter(z.check_str)' , setup='import __main__ as z' , ) , 'seconds' , ) print( '> can_string_be_rearranged_as_palindrome()' , '\tans =' , can_string_be_rearranged_as_palindrome(snake_case__ ) , '\ttime =' , timeit( 'z.can_string_be_rearranged_as_palindrome(z.check_str)' , setup='import __main__ as z' , ) , 'seconds' , ) if __name__ == "__main__": __UpperCAmelCase = input( '''Enter string to determine if it can be rearranged as a palindrome or not: ''' ).strip() benchmark(check_str) __UpperCAmelCase = can_string_be_rearranged_as_palindrome_counter(check_str) print(F"""{check_str} can {"" if status else "not "}be rearranged as a palindrome""")
40
'''simple docstring''' def lowerCAmelCase (__A): """simple docstring""" return credit_card_number.startswith(('''34''', '''35''', '''37''', '''4''', '''5''', '''6''')) def lowerCAmelCase (__A): """simple docstring""" _a = credit_card_number _a = 0 _a = len(__A) - 2 for i in range(__A , -1 , -2): # double the value of every second digit _a = int(cc_number[i]) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _a = cc_number[:i] + str(__A) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(__A) - 1 , -1 , -2): total += int(cc_number[i]) return total % 10 == 0 def lowerCAmelCase (__A): """simple docstring""" _a = F'''{credit_card_number} is an invalid credit card number because''' if not credit_card_number.isdigit(): print(F'''{error_message} it has nonnumerical characters.''') return False if not 13 <= len(__A) <= 16: print(F'''{error_message} of its length.''') return False if not validate_initial_digits(__A): print(F'''{error_message} of its first two digits.''') return False if not luhn_validation(__A): print(F'''{error_message} it fails the Luhn check.''') return False print(F'''{credit_card_number} is a valid credit card number.''') return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("4111111111111111") validate_credit_card_number("32323")
11
0