code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin _a : Any = False @skip_mps class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = StableDiffusionAttendAndExcitePipeline _UpperCamelCase : Optional[Any] = False _UpperCamelCase : List[Any] = TEXT_TO_IMAGE_PARAMS _UpperCamelCase : str = TEXT_TO_IMAGE_BATCH_PARAMS.union({"token_indices"} ) _UpperCamelCase : int = TEXT_TO_IMAGE_IMAGE_PARAMS _UpperCamelCase : List[str] = TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def __A ( cls ): super().setUpClass() torch.use_deterministic_algorithms(a__ ) @classmethod def __A ( cls ): super().tearDownClass() torch.use_deterministic_algorithms(a__ ) def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Optional[Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=a__ , ) _lowerCAmelCase : Any = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="""scaled_linear""" , clip_sample=a__ , set_alpha_to_one=a__ , ) torch.manual_seed(0 ) _lowerCAmelCase : Union[str, Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) _lowerCAmelCase : Dict = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="""gelu""" , projection_dim=512 , ) _lowerCAmelCase : List[Any] = CLIPTextModel(a__ ) _lowerCAmelCase : int = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) _lowerCAmelCase : Dict = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Optional[int] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Any = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Tuple = { """prompt""": """a cat and a frog""", """token_indices""": [2, 5], """generator""": generator, """num_inference_steps""": 1, """guidance_scale""": 6.0, """output_type""": """numpy""", """max_iter_to_alter""": 2, """thresholds""": {0: 0.7}, } return inputs def __A ( self ): _lowerCAmelCase : int = """cpu""" _lowerCAmelCase : Any = self.get_dummy_components() _lowerCAmelCase : Any = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : List[str] = self.get_dummy_inputs(a__ ) _lowerCAmelCase : List[str] = pipe(**a__ ).images _lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 64, 64, 3) ) _lowerCAmelCase : Dict = np.array( [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6] ) _lowerCAmelCase : Dict = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1e-3 ) def __A ( self ): super().test_cpu_offload_forward_pass(expected_max_diff=5e-4 ) def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): self._test_inference_batch_single_identical(batch_size=2 , expected_max_diff=7e-4 ) def __A ( self ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def __A ( self ): super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4 ) def __A ( self ): super().test_save_load_local(expected_max_difference=5e-4 ) def __A ( self ): super().test_save_load_optional_components(expected_max_difference=4e-4 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): @classmethod def __A ( cls ): super().setUpClass() torch.use_deterministic_algorithms(a__ ) @classmethod def __A ( cls ): super().tearDownClass() torch.use_deterministic_algorithms(a__ ) def __A ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : int = torch.manual_seed(51 ) _lowerCAmelCase : Tuple = StableDiffusionAttendAndExcitePipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""" , safety_checker=a__ , torch_dtype=torch.floataa ) pipe.to("""cuda""" ) _lowerCAmelCase : List[Any] = """a painting of an elephant with glasses""" _lowerCAmelCase : Any = [5, 7] _lowerCAmelCase : List[str] = pipe( prompt=a__ , token_indices=a__ , guidance_scale=7.5 , generator=a__ , num_inference_steps=5 , max_iter_to_alter=5 , output_type="""numpy""" , ).images[0] _lowerCAmelCase : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy""" ) assert np.abs((expected_image - image).max() ) < 5e-1
663
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
1
"""simple docstring""" import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[Any] = (DPMSolverSDEScheduler,) _UpperCamelCase : List[str] = 10 def __A ( self , **a__ ): _lowerCAmelCase : Dict = { """num_train_timesteps""": 1100, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**a__ ) return config def __A ( self ): for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=a__ ) def __A ( self ): for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1] , [0.0_0_0_2, 0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a__ , beta_end=a__ ) def __A ( self ): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=a__ ) def __A ( self ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a__ ) def __A ( self ): _lowerCAmelCase : int = self.scheduler_classes[0] _lowerCAmelCase : List[str] = self.get_scheduler_config() _lowerCAmelCase : str = scheduler_class(**a__ ) scheduler.set_timesteps(self.num_inference_steps ) _lowerCAmelCase : Optional[int] = self.dummy_model() _lowerCAmelCase : str = self.dummy_sample_deter * scheduler.init_noise_sigma _lowerCAmelCase : List[str] = sample.to(a__ ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase : List[str] = scheduler.scale_model_input(a__ , a__ ) _lowerCAmelCase : Optional[int] = model(a__ , a__ ) _lowerCAmelCase : Optional[int] = scheduler.step(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = output.prev_sample _lowerCAmelCase : int = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : int = torch.mean(torch.abs(a__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2 ) < 1e-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1e-3 def __A ( self ): _lowerCAmelCase : List[str] = self.scheduler_classes[0] _lowerCAmelCase : List[Any] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _lowerCAmelCase : Tuple = scheduler_class(**a__ ) scheduler.set_timesteps(self.num_inference_steps ) _lowerCAmelCase : Union[str, Any] = self.dummy_model() _lowerCAmelCase : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma _lowerCAmelCase : int = sample.to(a__ ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase : Optional[Any] = scheduler.scale_model_input(a__ , a__ ) _lowerCAmelCase : List[Any] = model(a__ , a__ ) _lowerCAmelCase : List[str] = scheduler.step(a__ , a__ , a__ ) _lowerCAmelCase : List[Any] = output.prev_sample _lowerCAmelCase : List[str] = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : Any = torch.mean(torch.abs(a__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3 ) < 1e-2 assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3 ) < 1e-2 assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7 ) < 1e-3 else: assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1 ) < 1e-3 def __A ( self ): _lowerCAmelCase : List[Any] = self.scheduler_classes[0] _lowerCAmelCase : str = self.get_scheduler_config() _lowerCAmelCase : Dict = scheduler_class(**a__ ) scheduler.set_timesteps(self.num_inference_steps , device=a__ ) _lowerCAmelCase : Dict = self.dummy_model() _lowerCAmelCase : Optional[Any] = self.dummy_sample_deter.to(a__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _lowerCAmelCase : Optional[Any] = scheduler.scale_model_input(a__ , a__ ) _lowerCAmelCase : Dict = model(a__ , a__ ) _lowerCAmelCase : int = scheduler.step(a__ , a__ , a__ ) _lowerCAmelCase : str = output.prev_sample _lowerCAmelCase : Union[str, Any] = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : Any = torch.mean(torch.abs(a__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1 ) < 1e-3 else: assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6 ) < 1e-3 def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.scheduler_classes[0] _lowerCAmelCase : List[Any] = self.get_scheduler_config() _lowerCAmelCase : Union[str, Any] = scheduler_class(**a__ , use_karras_sigmas=a__ ) scheduler.set_timesteps(self.num_inference_steps , device=a__ ) _lowerCAmelCase : Optional[int] = self.dummy_model() _lowerCAmelCase : Union[str, Any] = self.dummy_sample_deter.to(a__ ) * scheduler.init_noise_sigma _lowerCAmelCase : List[str] = sample.to(a__ ) for t in scheduler.timesteps: _lowerCAmelCase : Any = scheduler.scale_model_input(a__ , a__ ) _lowerCAmelCase : str = model(a__ , a__ ) _lowerCAmelCase : str = scheduler.step(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = output.prev_sample _lowerCAmelCase : int = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : Dict = torch.mean(torch.abs(a__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2 else: assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1 ) < 1e-2
663
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : str = CpmAntTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """<d>""", """</d>""", """<s>""", """</s>""", """</_>""", """<unk>""", """<pad>""", """</n>""", """我""", """是""", """C""", """P""", """M""", """A""", """n""", """t""", ] _lowerCAmelCase : int = 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] ) ) @tooslow def __A ( self ): _lowerCAmelCase : Tuple = CpmAntTokenizer.from_pretrained("""openbmb/cpm-ant-10b""" ) _lowerCAmelCase : Optional[Any] = """今天天气真好!""" _lowerCAmelCase : Any = ["""今天""", """天气""", """真""", """好""", """!"""] _lowerCAmelCase : str = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = """今天天气真好!""" _lowerCAmelCase : Optional[Any] = [tokenizer.bos_token] + tokens _lowerCAmelCase : Optional[int] = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) _lowerCAmelCase : Tuple = tokenizer.decode(a__ ) self.assertEqual(a__ , a__ )
663
1
"""simple docstring""" import math _a : Union[str, Any] = 10 _a : List[Any] = 7 _a : Optional[Any] = BALLS_PER_COLOUR * NUM_COLOURS def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 20 ) -> str: _lowerCAmelCase : int = math.comb(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : List[str] = math.comb(NUM_BALLS - BALLS_PER_COLOUR ,_lowerCamelCase ) _lowerCAmelCase : Tuple = NUM_COLOURS * (1 - missing_colour / total) return f"{result:.9f}" if __name__ == "__main__": print(solution(20))
663
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
1
"""simple docstring""" from ....utils import logging _a : Any = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__=None , a__=2048 ): _lowerCAmelCase : List[str] = config.__dict__ _lowerCAmelCase : Optional[Any] = modal_hidden_size if num_labels: _lowerCAmelCase : Optional[Any] = num_labels
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : str = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Union[str, Any] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> bool: 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...') _a : int = int(input('Enter number: ').strip()) print(F"""{number} is {"" if perfect(number) else "not "}a Perfect Number.""")
663
1
"""simple docstring""" import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ) -> Any: _lowerCAmelCase : Optional[int] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(_lowerCamelCase ,_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ) -> Union[str, Any]: _lowerCAmelCase , _lowerCAmelCase : int = emb.weight.shape _lowerCAmelCase : Any = nn.Linear(_lowerCamelCase ,_lowerCamelCase ,bias=_lowerCamelCase ) _lowerCAmelCase : int = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Tuple=None ) -> Tuple: _lowerCAmelCase : Any = {} for old_key in state_dict.keys(): _lowerCAmelCase : str = old_key if "moe_layer.experts." in key: if expert_idx is not None: _lowerCAmelCase : Union[str, Any] = key.replace("""moe_layer.experts.0""" ,f"ffn.experts.expert_{expert_idx}" ) else: _lowerCAmelCase : Optional[Any] = key.replace("""moe_layer.experts.""" ,"""ffn.experts.expert_""" ) if "gate" in key: _lowerCAmelCase : Optional[int] = key.replace(""".moe_layer.gate.wg""" ,""".ffn.router.classifier""" ) if "fc2" and "experts" not in key: _lowerCAmelCase : str = key.replace(""".fc2.""" ,""".ffn.fc2.""" ) if "fc1" and "experts" not in key: _lowerCAmelCase : List[str] = key.replace(""".fc1.""" ,""".ffn.fc1.""" ) if ".encoder_attn." in key: _lowerCAmelCase : str = key.replace(""".encoder_attn.""" ,""".cross_attention.""" ) if "encoder_attn_layer_norm" in key: _lowerCAmelCase : str = key.replace("""encoder_attn_layer_norm""" ,"""cross_attention_layer_norm""" ) if "final_layer_norm" in key: _lowerCAmelCase : Any = key.replace("""final_layer_norm""" ,"""ff_layer_norm""" ) _lowerCAmelCase : Dict = state_dict[old_key] return new_dict def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ,_lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ,_lowerCamelCase : Optional[int] ,_lowerCamelCase : str = WEIGHTS_NAME ) -> Dict: _lowerCAmelCase : Optional[Any] = [] _lowerCAmelCase : List[str] = 0 os.makedirs(_lowerCamelCase ,exist_ok=_lowerCamelCase ) for expert in range(_lowerCamelCase ): _lowerCAmelCase : str = switch_checkpoint_path + f"-rank-{expert}.pt" if os.path.isfile(_lowerCamelCase ): _lowerCAmelCase : int = torch.load(_lowerCamelCase )["""model"""] remove_ignore_keys_(_lowerCamelCase ) _lowerCAmelCase : Optional[int] = rename_fairseq_keys(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : Dict = os.path.join( _lowerCamelCase ,weights_name.replace(""".bin""" ,f"-{len(_lowerCamelCase )+1:05d}-of-???.bin" ) ) torch.save(_lowerCamelCase ,_lowerCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCamelCase )[0]].dtype ) # Add the last block _lowerCAmelCase : Any = os.path.join(_lowerCamelCase ,weights_name.replace(""".bin""" ,f"-{len(_lowerCamelCase )+1:05d}-of-???.bin" ) ) _lowerCAmelCase : Tuple = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = rename_fairseq_keys(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : Optional[int] = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCamelCase ) == 1: _lowerCAmelCase : Optional[int] = os.path.join(_lowerCamelCase ,_lowerCamelCase ) torch.save(_lowerCamelCase ,_lowerCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCamelCase ,_lowerCamelCase ) # Otherwise, let's build the index _lowerCAmelCase : int = {} for idx, shard in enumerate(_lowerCamelCase ): _lowerCAmelCase : Union[str, Any] = weights_name.replace(""".bin""" ,f"-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin" ) _lowerCAmelCase : int = os.path.join(_lowerCamelCase ,weights_name.replace(""".bin""" ,f"-{idx+1:05d}-of-???.bin" ) ) os.rename(_lowerCamelCase ,os.path.join(_lowerCamelCase ,_lowerCamelCase ) ) for key in shard: _lowerCAmelCase : Dict = shard_file # Add the metadata _lowerCAmelCase : List[Any] = {"""total_size""": total_size} _lowerCAmelCase : Tuple = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(_lowerCamelCase ,_lowerCamelCase ) ,"""w""" ,encoding="""utf-8""" ) as f: _lowerCAmelCase : List[Any] = json.dumps(_lowerCamelCase ,indent=2 ,sort_keys=_lowerCamelCase ) + """\n""" f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": _a : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '--nllb_moe_checkpoint_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000', type=str, required=False, help='Path to a directory containing a folder per layer. Follows the original Google format.', ) parser.add_argument('--dtype', default='float32', type=str, required=False, help='dtype of the saved model') parser.add_argument( '--pytorch_dump_folder_path', default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b', type=str, required=False, help='Path to the output pytorch model.', ) _a : str = parser.parse_args() _a , _a : Union[str, Any] = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) _a : str = NllbMoeConfig.from_pretrained( 'facebook/nllb-200-3.3B', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) _a : Dict = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print('Done') model.save_pretrained(args.pytorch_dump_folder_path)
663
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
1
"""simple docstring""" from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class __A ( nn.Module ): 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 , ): super().__init__() _lowerCAmelCase : Optional[int] = 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 _lowerCAmelCase : List[Any] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowerCAmelCase : List[str] = [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])` _lowerCAmelCase : str = [1, 0] def __A ( self , a__ , a__ , a__=None , a__=None , a__=None , a__ = True , ): _lowerCAmelCase : Any = hidden_states _lowerCAmelCase : Optional[Any] = [] _lowerCAmelCase : Dict = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowerCAmelCase : List[str] = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowerCAmelCase : Union[str, Any] = self.transformer_index_for_condition[i] _lowerCAmelCase : str = 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] _lowerCAmelCase : Tuple = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowerCAmelCase : str = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=a__ )
663
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
1
"""simple docstring""" _a : Optional[Any] = 'Tobias Carryer' from time import time class __A : def __init__( self , a__ , a__ , a__ , a__=int(time() ) ): # noqa: B008 _lowerCAmelCase : Any = multiplier _lowerCAmelCase : str = increment _lowerCAmelCase : Union[str, Any] = modulo _lowerCAmelCase : str = seed def __A ( self ): _lowerCAmelCase : List[Any] = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed if __name__ == "__main__": # Show the LCG in action. _a : Optional[Any] = LinearCongruentialGenerator(1_664_525, 1_013_904_223, 2 << 31) while True: print(lcg.next_number())
663
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
1
"""simple docstring""" import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): @register_to_config def __init__( self , a__ = 128 , a__ = 256 , a__ = 2_0_0_0.0 , a__ = 768 , a__ = 12 , a__ = 12 , a__ = 64 , a__ = 2048 , a__ = 0.1 , ): super().__init__() _lowerCAmelCase : str = nn.Sequential( nn.Linear(a__ , d_model * 4 , bias=a__ ) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=a__ ) , nn.SiLU() , ) _lowerCAmelCase : Optional[Any] = nn.Embedding(a__ , a__ ) _lowerCAmelCase : str = False _lowerCAmelCase : Optional[int] = nn.Linear(a__ , a__ , bias=a__ ) _lowerCAmelCase : int = nn.Dropout(p=a__ ) _lowerCAmelCase : str = nn.ModuleList() for lyr_num in range(a__ ): # FiLM conditional T5 decoder _lowerCAmelCase : Optional[int] = DecoderLayer(d_model=a__ , d_kv=a__ , num_heads=a__ , d_ff=a__ , dropout_rate=a__ ) self.decoders.append(a__ ) _lowerCAmelCase : Union[str, Any] = TaLayerNorm(a__ ) _lowerCAmelCase : List[str] = nn.Dropout(p=a__ ) _lowerCAmelCase : Dict = nn.Linear(a__ , a__ , bias=a__ ) def __A ( self , a__ , a__ ): _lowerCAmelCase : int = torch.mul(query_input.unsqueeze(-1 ) , key_input.unsqueeze(-2 ) ) return mask.unsqueeze(-3 ) def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Optional[int] = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. _lowerCAmelCase : Tuple = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype ) _lowerCAmelCase : List[Any] = self.conditioning_emb(a__ ).unsqueeze(1 ) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) _lowerCAmelCase : Tuple = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. _lowerCAmelCase : str = torch.broadcast_to( torch.arange(a__ , device=decoder_input_tokens.device ) , (batch, seq_length) , ) _lowerCAmelCase : List[Any] = self.position_encoding(a__ ) _lowerCAmelCase : Dict = self.continuous_inputs_projection(a__ ) inputs += position_encodings _lowerCAmelCase : int = self.dropout(a__ ) # decoder: No padding present. _lowerCAmelCase : List[Any] = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype ) # Translate encoding masks to encoder-decoder masks. _lowerCAmelCase : Any = [(x, self.encoder_decoder_mask(a__ , a__ )) for x, y in encodings_and_masks] # cross attend style: concat encodings _lowerCAmelCase : Dict = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1 ) _lowerCAmelCase : Any = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1 ) for lyr in self.decoders: _lowerCAmelCase : Optional[int] = lyr( a__ , conditioning_emb=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , )[0] _lowerCAmelCase : List[str] = self.decoder_norm(a__ ) _lowerCAmelCase : Optional[int] = self.post_dropout(a__ ) _lowerCAmelCase : int = self.spec_out(a__ ) return spec_out class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__ , a__ , a__=1e-6 ): super().__init__() _lowerCAmelCase : Any = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=a__ , d_kv=a__ , num_heads=a__ , dropout_rate=a__ ) ) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=a__ , d_kv=a__ , num_heads=a__ , dropout_rate=a__ , layer_norm_epsilon=a__ , ) ) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=a__ , d_ff=a__ , dropout_rate=a__ , layer_norm_epsilon=a__ ) ) def __A ( self , a__ , a__=None , a__=None , a__=None , a__=None , a__=None , ): _lowerCAmelCase : List[Any] = self.layer[0]( a__ , conditioning_emb=a__ , attention_mask=a__ , ) if encoder_hidden_states is not None: _lowerCAmelCase : Dict = torch.where(encoder_attention_mask > 0 , 0 , -1e10 ).to( encoder_hidden_states.dtype ) _lowerCAmelCase : str = self.layer[1]( a__ , key_value_states=a__ , attention_mask=a__ , ) # Apply Film Conditional Feed Forward layer _lowerCAmelCase : Optional[Any] = self.layer[-1](a__ , a__ ) return (hidden_states,) class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__ ): super().__init__() _lowerCAmelCase : Tuple = TaLayerNorm(a__ ) _lowerCAmelCase : Optional[Any] = TaFiLMLayer(in_features=d_model * 4 , out_features=a__ ) _lowerCAmelCase : Tuple = Attention(query_dim=a__ , heads=a__ , dim_head=a__ , out_bias=a__ , scale_qk=a__ ) _lowerCAmelCase : Union[str, Any] = nn.Dropout(a__ ) def __A ( self , a__ , a__=None , a__=None , ): # pre_self_attention_layer_norm _lowerCAmelCase : List[str] = self.layer_norm(a__ ) if conditioning_emb is not None: _lowerCAmelCase : Tuple = self.FiLMLayer(a__ , a__ ) # Self-attention block _lowerCAmelCase : List[str] = self.attention(a__ ) _lowerCAmelCase : str = hidden_states + self.dropout(a__ ) return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__ , a__ ): super().__init__() _lowerCAmelCase : Tuple = Attention(query_dim=a__ , heads=a__ , dim_head=a__ , out_bias=a__ , scale_qk=a__ ) _lowerCAmelCase : Any = TaLayerNorm(a__ , eps=a__ ) _lowerCAmelCase : Tuple = nn.Dropout(a__ ) def __A ( self , a__ , a__=None , a__=None , ): _lowerCAmelCase : int = self.layer_norm(a__ ) _lowerCAmelCase : List[str] = self.attention( a__ , encoder_hidden_states=a__ , attention_mask=attention_mask.squeeze(1 ) , ) _lowerCAmelCase : Dict = hidden_states + self.dropout(a__ ) return layer_output class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__ ): super().__init__() _lowerCAmelCase : Optional[int] = TaDenseGatedActDense(d_model=a__ , d_ff=a__ , dropout_rate=a__ ) _lowerCAmelCase : str = TaFiLMLayer(in_features=d_model * 4 , out_features=a__ ) _lowerCAmelCase : Dict = TaLayerNorm(a__ , eps=a__ ) _lowerCAmelCase : int = nn.Dropout(a__ ) def __A ( self , a__ , a__=None ): _lowerCAmelCase : Optional[int] = self.layer_norm(a__ ) if conditioning_emb is not None: _lowerCAmelCase : Any = self.film(a__ , a__ ) _lowerCAmelCase : Optional[int] = self.DenseReluDense(a__ ) _lowerCAmelCase : Tuple = hidden_states + self.dropout(a__ ) return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ ): super().__init__() _lowerCAmelCase : int = nn.Linear(a__ , a__ , bias=a__ ) _lowerCAmelCase : List[str] = nn.Linear(a__ , a__ , bias=a__ ) _lowerCAmelCase : Any = nn.Linear(a__ , a__ , bias=a__ ) _lowerCAmelCase : Dict = nn.Dropout(a__ ) _lowerCAmelCase : Any = NewGELUActivation() def __A ( self , a__ ): _lowerCAmelCase : Tuple = self.act(self.wi_a(a__ ) ) _lowerCAmelCase : Dict = self.wi_a(a__ ) _lowerCAmelCase : Any = hidden_gelu * hidden_linear _lowerCAmelCase : Optional[Any] = self.dropout(a__ ) _lowerCAmelCase : List[str] = self.wo(a__ ) return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__=1e-6 ): super().__init__() _lowerCAmelCase : Optional[Any] = nn.Parameter(torch.ones(a__ ) ) _lowerCAmelCase : str = eps def __A ( self , a__ ): # T5 uses a layer_norm which only scales and doesn't shift, which is also known as Root Mean # Square Layer Normalization https://arxiv.org/abs/1910.07467 thus variance is calculated # w/o mean and there is no bias. Additionally we want to make sure that the accumulation for # half-precision inputs is done in fp32 _lowerCAmelCase : List[str] = hidden_states.to(torch.floataa ).pow(2 ).mean(-1 , keepdim=a__ ) _lowerCAmelCase : Optional[Any] = hidden_states * torch.rsqrt(variance + self.variance_epsilon ) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: _lowerCAmelCase : Optional[int] = hidden_states.to(self.weight.dtype ) return self.weight * hidden_states class __A ( nn.Module ): def __A ( self , a__ ): return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.0_4_4_7_1_5 * torch.pow(a__ , 3.0 )) )) class __A ( nn.Module ): def __init__( self , a__ , a__ ): super().__init__() _lowerCAmelCase : List[Any] = nn.Linear(a__ , out_features * 2 , bias=a__ ) def __A ( self , a__ , a__ ): _lowerCAmelCase : Optional[int] = self.scale_bias(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = torch.chunk(a__ , 2 , -1 ) _lowerCAmelCase : str = x * (1 + scale) + shift return x
663
"""simple docstring""" _a : Optional[Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[str] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : str = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
663
1
"""simple docstring""" import argparse import torch from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ,_lowerCamelCase : int ,_lowerCamelCase : Optional[int] ,_lowerCamelCase : Dict ) -> int: # Initialise PyTorch model _lowerCAmelCase : Any = FunnelConfig.from_json_file(_lowerCamelCase ) print(f"Building PyTorch model from configuration: {config}" ) _lowerCAmelCase : Optional[int] = FunnelBaseModel(_lowerCamelCase ) if base_model else FunnelModel(_lowerCamelCase ) # Load weights from tf checkpoint load_tf_weights_in_funnel(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() ,_lowerCamelCase ) if __name__ == "__main__": _a : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained 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( '--base_model', action='store_true', help='Whether you want just the base model (no decoder) or not.' ) _a : Any = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model )
663
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Tuple = 13 _lowerCAmelCase : Tuple = 7 _lowerCAmelCase : Any = 30 _lowerCAmelCase : Optional[int] = self.seq_length + self.mem_len _lowerCAmelCase : Dict = 15 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : Any = True _lowerCAmelCase : List[str] = 99 _lowerCAmelCase : List[Any] = [10, 50, 80] _lowerCAmelCase : Tuple = 32 _lowerCAmelCase : int = 32 _lowerCAmelCase : Dict = 4 _lowerCAmelCase : List[str] = 8 _lowerCAmelCase : Tuple = 128 _lowerCAmelCase : Any = 2 _lowerCAmelCase : List[Any] = 2 _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Optional[Any] = 1 _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : List[Any] = 3 _lowerCAmelCase : Optional[int] = self.vocab_size - 1 _lowerCAmelCase : Dict = 0.0_1 def __A ( self ): _lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Union[str, Any] = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[int] = TFTransfoXLModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() _lowerCAmelCase : Optional[Any] = {"""input_ids""": input_ids_a, """mems""": mems_a} _lowerCAmelCase , _lowerCAmelCase : List[Any] = model(a__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : int = TFTransfoXLLMHeadModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase : Dict = {"""input_ids""": input_ids_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = model([input_ids_a, mems_a] ).to_tuple() _lowerCAmelCase : Any = {"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFTransfoXLForSequenceClassification(a__ ) _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : Dict = config_and_inputs _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids_a} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : Any = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : str = False _UpperCamelCase : str = False _UpperCamelCase : Tuple = False _UpperCamelCase : Any = False def __A ( self , a__ , a__ , a__ , a__ , a__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLModelTester(self ) _lowerCAmelCase : List[Any] = ConfigTester(self , config_class=a__ , d_embed=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a__ ) def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a__ ) def __A ( self ): _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase : List[Any] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(a__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _lowerCAmelCase : str = model.get_output_embeddings() assert isinstance(a__ , tf.keras.layers.Layer ) _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None else: _lowerCAmelCase : Union[str, Any] = model.get_output_embeddings() assert x is None _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None def __A ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Union[str, Any] = TFTransfoXLModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" ) def __A ( self ): pass @require_tf class __A ( unittest.TestCase ): @unittest.skip("""Skip test until #12651 is resolved.""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" ) # fmt: off _lowerCAmelCase : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _lowerCAmelCase : List[Any] = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _lowerCAmelCase : Tuple = model.generate(a__ , max_length=200 , do_sample=a__ ) self.assertListEqual(output_ids[0].numpy().tolist() , a__ )
663
1
"""simple docstring""" import os import pytest import yaml from datasets.features.features import Features, Value from datasets.info import DatasetInfo, DatasetInfosDict @pytest.mark.parametrize( """files""" ,[ ["""full:README.md""", """dataset_infos.json"""], ["""empty:README.md""", """dataset_infos.json"""], ["""dataset_infos.json"""], ["""full:README.md"""], ] ,) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : List[Any] ) -> List[Any]: _lowerCAmelCase : Optional[int] = tmp_path_factory.mktemp("""dset_infos_dir""" ) if "full:README.md" in files: with open(dataset_infos_dir / """README.md""" ,"""w""" ) as f: f.write("""---\ndataset_info:\n dataset_size: 42\n---""" ) if "empty:README.md" in files: with open(dataset_infos_dir / """README.md""" ,"""w""" ) as f: f.write("""""" ) # we want to support dataset_infos.json for backward compatibility if "dataset_infos.json" in files: with open(dataset_infos_dir / """dataset_infos.json""" ,"""w""" ) as f: f.write("""{\"default\": {\"dataset_size\": 42}}""" ) _lowerCAmelCase : Any = DatasetInfosDict.from_directory(_lowerCamelCase ) assert dataset_infos assert dataset_infos["default"].dataset_size == 42 @pytest.mark.parametrize( """dataset_info""" ,[ DatasetInfo(), DatasetInfo( description="""foo""" ,features=Features({"""a""": Value("""int32""" )} ) ,builder_name="""builder""" ,config_name="""config""" ,version="""1.0.0""" ,splits=[{"""name""": """train"""}] ,download_size=42 ,), ] ,) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ,_lowerCamelCase : DatasetInfo ) -> Any: _lowerCAmelCase : List[str] = str(_lowerCamelCase ) dataset_info.write_to_directory(_lowerCamelCase ) _lowerCAmelCase : Any = DatasetInfo.from_directory(_lowerCamelCase ) assert dataset_info == reloaded assert os.path.exists(os.path.join(_lowerCamelCase ,"""dataset_info.json""" ) ) def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: _lowerCAmelCase : Union[str, Any] = DatasetInfo( description="""foo""" ,citation="""bar""" ,homepage="""https://foo.bar""" ,license="""CC0""" ,features=Features({"""a""": Value("""int32""" )} ) ,post_processed={} ,supervised_keys=() ,task_templates=[] ,builder_name="""builder""" ,config_name="""config""" ,version="""1.0.0""" ,splits=[{"""name""": """train""", """num_examples""": 42}] ,download_checksums={} ,download_size=1337 ,post_processing_size=442 ,dataset_size=1234 ,size_in_bytes=1337 + 442 + 1234 ,) _lowerCAmelCase : Dict = dataset_info._to_yaml_dict() assert sorted(_lowerCamelCase ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML ) for key in DatasetInfo._INCLUDED_INFO_IN_YAML: assert key in dataset_info_yaml_dict assert isinstance(dataset_info_yaml_dict[key] ,(list, dict, int, str) ) _lowerCAmelCase : List[str] = yaml.safe_dump(_lowerCamelCase ) _lowerCAmelCase : Optional[Any] = yaml.safe_load(_lowerCamelCase ) assert dataset_info_yaml_dict == reloaded def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: _lowerCAmelCase : Tuple = DatasetInfo() _lowerCAmelCase : int = dataset_info._to_yaml_dict() assert dataset_info_yaml_dict == {} @pytest.mark.parametrize( """dataset_infos_dict""" ,[ DatasetInfosDict(), DatasetInfosDict({"""default""": DatasetInfo()} ), DatasetInfosDict({"""my_config_name""": DatasetInfo()} ), DatasetInfosDict( { """default""": DatasetInfo( description="""foo""" ,features=Features({"""a""": Value("""int32""" )} ) ,builder_name="""builder""" ,config_name="""config""" ,version="""1.0.0""" ,splits=[{"""name""": """train"""}] ,download_size=42 ,) } ), DatasetInfosDict( { """v1""": DatasetInfo(dataset_size=42 ), """v2""": DatasetInfo(dataset_size=1337 ), } ), ] ,) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ,_lowerCamelCase : DatasetInfosDict ) -> List[str]: _lowerCAmelCase : Dict = str(_lowerCamelCase ) dataset_infos_dict.write_to_directory(_lowerCamelCase ) _lowerCAmelCase : Optional[int] = DatasetInfosDict.from_directory(_lowerCamelCase ) # the config_name of the dataset_infos_dict take over the attribute for config_name, dataset_info in dataset_infos_dict.items(): _lowerCAmelCase : List[Any] = config_name # the yaml representation doesn't include fields like description or citation # so we just test that we can recover what we can from the yaml _lowerCAmelCase : Union[str, Any] = DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict() ) assert dataset_infos_dict == reloaded if dataset_infos_dict: assert os.path.exists(os.path.join(_lowerCamelCase ,"""README.md""" ) )
663
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
1
"""simple docstring""" from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__=0.0 , a__ = None , a__ = "geglu" , a__ = None , a__ = False , a__ = False , a__ = False , a__ = False , a__ = True , a__ = "layer_norm" , a__ = False , ): super().__init__() _lowerCAmelCase : Optional[Any] = only_cross_attention _lowerCAmelCase : Tuple = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" _lowerCAmelCase : Optional[int] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm""" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( F"`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to" F" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}." ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: _lowerCAmelCase : Optional[Any] = AdaLayerNorm(a__ , a__ ) elif self.use_ada_layer_norm_zero: _lowerCAmelCase : List[Any] = AdaLayerNormZero(a__ , a__ ) else: _lowerCAmelCase : Any = nn.LayerNorm(a__ , elementwise_affine=a__ ) _lowerCAmelCase : Tuple = Attention( query_dim=a__ , heads=a__ , dim_head=a__ , dropout=a__ , bias=a__ , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=a__ , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. _lowerCAmelCase : Union[str, Any] = ( AdaLayerNorm(a__ , a__ ) if self.use_ada_layer_norm else nn.LayerNorm(a__ , elementwise_affine=a__ ) ) _lowerCAmelCase : Dict = Attention( query_dim=a__ , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=a__ , dim_head=a__ , dropout=a__ , bias=a__ , upcast_attention=a__ , ) # is self-attn if encoder_hidden_states is none else: _lowerCAmelCase : Optional[int] = None _lowerCAmelCase : Optional[int] = None # 3. Feed-forward _lowerCAmelCase : Optional[int] = nn.LayerNorm(a__ , elementwise_affine=a__ ) _lowerCAmelCase : int = FeedForward(a__ , dropout=a__ , activation_fn=a__ , final_dropout=a__ ) # let chunk size default to None _lowerCAmelCase : str = None _lowerCAmelCase : Optional[Any] = 0 def __A ( self , a__ , a__ ): # Sets chunk feed-forward _lowerCAmelCase : int = chunk_size _lowerCAmelCase : List[str] = dim def __A ( self , a__ , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , ): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: _lowerCAmelCase : Tuple = self.norma(a__ , a__ ) elif self.use_ada_layer_norm_zero: _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : List[Any] = self.norma( a__ , a__ , a__ , hidden_dtype=hidden_states.dtype ) else: _lowerCAmelCase : str = self.norma(a__ ) _lowerCAmelCase : Optional[Any] = cross_attention_kwargs if cross_attention_kwargs is not None else {} _lowerCAmelCase : List[Any] = self.attna( a__ , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=a__ , **a__ , ) if self.use_ada_layer_norm_zero: _lowerCAmelCase : str = gate_msa.unsqueeze(1 ) * attn_output _lowerCAmelCase : Dict = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: _lowerCAmelCase : Any = ( self.norma(a__ , a__ ) if self.use_ada_layer_norm else self.norma(a__ ) ) _lowerCAmelCase : Any = self.attna( a__ , encoder_hidden_states=a__ , attention_mask=a__ , **a__ , ) _lowerCAmelCase : List[Any] = attn_output + hidden_states # 3. Feed-forward _lowerCAmelCase : str = self.norma(a__ ) if self.use_ada_layer_norm_zero: _lowerCAmelCase : int = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( F"`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`." ) _lowerCAmelCase : Optional[Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size _lowerCAmelCase : str = torch.cat( [self.ff(a__ ) for hid_slice in norm_hidden_states.chunk(a__ , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: _lowerCAmelCase : Tuple = self.ff(a__ ) if self.use_ada_layer_norm_zero: _lowerCAmelCase : Optional[int] = gate_mlp.unsqueeze(1 ) * ff_output _lowerCAmelCase : List[Any] = ff_output + hidden_states return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__ = None , a__ = 4 , a__ = 0.0 , a__ = "geglu" , a__ = False , ): super().__init__() _lowerCAmelCase : Union[str, Any] = int(dim * mult ) _lowerCAmelCase : Optional[Any] = dim_out if dim_out is not None else dim if activation_fn == "gelu": _lowerCAmelCase : Optional[int] = GELU(a__ , a__ ) if activation_fn == "gelu-approximate": _lowerCAmelCase : Any = GELU(a__ , a__ , approximate="""tanh""" ) elif activation_fn == "geglu": _lowerCAmelCase : int = GEGLU(a__ , a__ ) elif activation_fn == "geglu-approximate": _lowerCAmelCase : Any = ApproximateGELU(a__ , a__ ) _lowerCAmelCase : List[str] = nn.ModuleList([] ) # project in self.net.append(a__ ) # project dropout self.net.append(nn.Dropout(a__ ) ) # project out self.net.append(nn.Linear(a__ , a__ ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(a__ ) ) def __A ( self , a__ ): for module in self.net: _lowerCAmelCase : Dict = module(a__ ) return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ = "none" ): super().__init__() _lowerCAmelCase : Tuple = nn.Linear(a__ , a__ ) _lowerCAmelCase : int = approximate def __A ( self , a__ ): if gate.device.type != "mps": return F.gelu(a__ , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def __A ( self , a__ ): _lowerCAmelCase : Dict = self.proj(a__ ) _lowerCAmelCase : List[Any] = self.gelu(a__ ) return hidden_states class __A ( nn.Module ): def __init__( self , a__ , a__ ): super().__init__() _lowerCAmelCase : Dict = nn.Linear(a__ , dim_out * 2 ) def __A ( self , a__ ): if gate.device.type != "mps": return F.gelu(a__ ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def __A ( self , a__ ): _lowerCAmelCase , _lowerCAmelCase : Optional[int] = self.proj(a__ ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(a__ ) class __A ( nn.Module ): def __init__( self , a__ , a__ ): super().__init__() _lowerCAmelCase : Optional[Any] = nn.Linear(a__ , a__ ) def __A ( self , a__ ): _lowerCAmelCase : Dict = self.proj(a__ ) return x * torch.sigmoid(1.7_0_2 * x ) class __A ( nn.Module ): def __init__( self , a__ , a__ ): super().__init__() _lowerCAmelCase : Any = nn.Embedding(a__ , a__ ) _lowerCAmelCase : Tuple = nn.SiLU() _lowerCAmelCase : int = nn.Linear(a__ , embedding_dim * 2 ) _lowerCAmelCase : Optional[Any] = nn.LayerNorm(a__ , elementwise_affine=a__ ) def __A ( self , a__ , a__ ): _lowerCAmelCase : Union[str, Any] = self.linear(self.silu(self.emb(a__ ) ) ) _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = torch.chunk(a__ , 2 ) _lowerCAmelCase : Dict = self.norm(a__ ) * (1 + scale) + shift return x class __A ( nn.Module ): def __init__( self , a__ , a__ ): super().__init__() _lowerCAmelCase : Optional[int] = CombinedTimestepLabelEmbeddings(a__ , a__ ) _lowerCAmelCase : List[str] = nn.SiLU() _lowerCAmelCase : Union[str, Any] = nn.Linear(a__ , 6 * embedding_dim , bias=a__ ) _lowerCAmelCase : Union[str, Any] = nn.LayerNorm(a__ , elementwise_affine=a__ , eps=1e-6 ) def __A ( self , a__ , a__ , a__ , a__=None ): _lowerCAmelCase : Union[str, Any] = self.linear(self.silu(self.emb(a__ , a__ , hidden_dtype=a__ ) ) ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : str = emb.chunk(6 , dim=1 ) _lowerCAmelCase : Union[str, Any] = self.norm(a__ ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __A ( nn.Module ): def __init__( self , a__ , a__ , a__ , a__ = None , a__ = 1e-5 ): super().__init__() _lowerCAmelCase : List[str] = num_groups _lowerCAmelCase : Union[str, Any] = eps if act_fn is None: _lowerCAmelCase : Optional[Any] = None else: _lowerCAmelCase : Optional[Any] = get_activation(a__ ) _lowerCAmelCase : Union[str, Any] = nn.Linear(a__ , out_dim * 2 ) def __A ( self , a__ , a__ ): if self.act: _lowerCAmelCase : Optional[int] = self.act(a__ ) _lowerCAmelCase : Union[str, Any] = self.linear(a__ ) _lowerCAmelCase : Any = emb[:, :, None, None] _lowerCAmelCase , _lowerCAmelCase : List[Any] = emb.chunk(2 , dim=1 ) _lowerCAmelCase : str = F.group_norm(a__ , self.num_groups , eps=self.eps ) _lowerCAmelCase : Union[str, Any] = x * (1 + scale) + shift return x
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Any = ["image_processor", "tokenizer"] _UpperCamelCase : Any = "AutoImageProcessor" _UpperCamelCase : Union[str, Any] = "AutoTokenizer" def __init__( self , a__=None , a__=None , **a__ ): _lowerCAmelCase : int = 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__ , ) _lowerCAmelCase : List[Any] = kwargs.pop("""feature_extractor""" ) _lowerCAmelCase : List[Any] = 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__ ) _lowerCAmelCase : Optional[int] = self.image_processor _lowerCAmelCase : Optional[int] = False def __call__( self , *a__ , **a__ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*a__ , **a__ ) _lowerCAmelCase : str = kwargs.pop("""images""" , a__ ) _lowerCAmelCase : int = kwargs.pop("""text""" , a__ ) if len(a__ ) > 0: _lowerCAmelCase : str = args[0] _lowerCAmelCase : List[Any] = args[1:] if images is None and text is None: raise ValueError("""You need to specify either an `images` or `text` input to process.""" ) if images is not None: _lowerCAmelCase : Optional[int] = self.image_processor(a__ , *a__ , **a__ ) if text is not None: _lowerCAmelCase : Any = self.tokenizer(a__ , **a__ ) if text is None: return inputs elif images is None: return encodings else: _lowerCAmelCase : int = encodings["""input_ids"""] return inputs def __A ( self , *a__ , **a__ ): return self.tokenizer.batch_decode(*a__ , **a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.decode(*a__ , **a__ ) @contextmanager def __A ( self ): warnings.warn( """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """ """labels by using the argument `text` of the regular `__call__` method (either in the same call as """ """your images inputs, or in a separate call.""" ) _lowerCAmelCase : List[str] = True _lowerCAmelCase : Optional[int] = self.tokenizer yield _lowerCAmelCase : Dict = self.image_processor _lowerCAmelCase : str = False def __A ( self , a__ , a__=False , a__=None ): if added_vocab is None: _lowerCAmelCase : str = self.tokenizer.get_added_vocab() _lowerCAmelCase : Dict = {} while tokens: _lowerCAmelCase : Any = re.search(r"""<s_(.*?)>""" , a__ , re.IGNORECASE ) if start_token is None: break _lowerCAmelCase : List[str] = start_token.group(1 ) _lowerCAmelCase : List[str] = re.search(rF"</s_{key}>" , a__ , re.IGNORECASE ) _lowerCAmelCase : str = start_token.group() if end_token is None: _lowerCAmelCase : Optional[int] = tokens.replace(a__ , """""" ) else: _lowerCAmelCase : Tuple = end_token.group() _lowerCAmelCase : List[Any] = re.escape(a__ ) _lowerCAmelCase : Any = re.escape(a__ ) _lowerCAmelCase : Dict = re.search(F"{start_token_escaped}(.*?){end_token_escaped}" , a__ , re.IGNORECASE ) if content is not None: _lowerCAmelCase : List[str] = content.group(1 ).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node _lowerCAmelCase : int = self.tokenajson(a__ , is_inner_value=a__ , added_vocab=a__ ) if value: if len(a__ ) == 1: _lowerCAmelCase : int = value[0] _lowerCAmelCase : Optional[int] = value else: # leaf nodes _lowerCAmelCase : Tuple = [] for leaf in content.split(r"""<sep/>""" ): _lowerCAmelCase : Optional[int] = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": _lowerCAmelCase : Dict = leaf[1:-2] # for categorical special tokens output[key].append(a__ ) if len(output[key] ) == 1: _lowerCAmelCase : List[str] = output[key][0] _lowerCAmelCase : Tuple = tokens[tokens.find(a__ ) + len(a__ ) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] , is_inner_value=a__ , added_vocab=a__ ) if len(a__ ): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def __A ( self ): 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 ): warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , a__ , ) return self.image_processor
663
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = DiTPipeline _UpperCamelCase : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS _UpperCamelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } _UpperCamelCase : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS _UpperCamelCase : Union[str, Any] = False def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=a__ , activation_fn="""gelu-approximate""" , num_embeds_ada_norm=1000 , norm_type="""ada_norm_zero""" , norm_elementwise_affine=a__ , ) _lowerCAmelCase : Optional[int] = AutoencoderKL() _lowerCAmelCase : Union[str, Any] = DDIMScheduler() _lowerCAmelCase : Optional[Any] = {"""transformer""": transformer.eval(), """vae""": vae.eval(), """scheduler""": scheduler} return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Any = torch.manual_seed(a__ ) else: _lowerCAmelCase : Tuple = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Any = { """class_labels""": [1], """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : Tuple = self.get_dummy_components() _lowerCAmelCase : Optional[int] = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Tuple = self.get_dummy_inputs(a__ ) _lowerCAmelCase : List[str] = pipe(**a__ ).images _lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) _lowerCAmelCase : List[Any] = np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7] ) _lowerCAmelCase : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1e-3 ) def __A ( self ): self._test_inference_batch_single_identical(relax_max_difference=a__ , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __A ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): def __A ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : List[str] = torch.manual_seed(0 ) _lowerCAmelCase : int = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-256""" ) pipe.to("""cuda""" ) _lowerCAmelCase : Dict = ["""vase""", """umbrella""", """white shark""", """white wolf"""] _lowerCAmelCase : Union[str, Any] = pipe.get_label_ids(a__ ) _lowerCAmelCase : Any = pipe(a__ , generator=a__ , num_inference_steps=40 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( F"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1e-2 def __A ( self ): _lowerCAmelCase : str = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-512""" ) _lowerCAmelCase : Dict = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("""cuda""" ) _lowerCAmelCase : List[str] = ["""vase""", """umbrella"""] _lowerCAmelCase : Optional[int] = pipe.get_label_ids(a__ ) _lowerCAmelCase : str = torch.manual_seed(0 ) _lowerCAmelCase : List[str] = pipe(a__ , generator=a__ , num_inference_steps=25 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" F"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1e-1
663
1
"""simple docstring""" import os import re 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 _a : Optional[int] = logging.get_logger(__name__) _a : Optional[Any] = {'vocab_file': 'spiece.model'} _a : Union[str, Any] = { 'vocab_file': { 'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model', 'google/bigbird-roberta-large': ( 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model' ), 'google/bigbird-base-trivia-itc': ( 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model' ), } } _a : Union[str, Any] = { 'google/bigbird-roberta-base': 4_096, 'google/bigbird-roberta-large': 4_096, 'google/bigbird-base-trivia-itc': 4_096, } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : str = VOCAB_FILES_NAMES _UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : str = ["input_ids", "attention_mask"] _UpperCamelCase : List[int] = [] def __init__( self , a__ , a__="<unk>" , a__="<s>" , a__="</s>" , a__="<pad>" , a__="[SEP]" , a__="[MASK]" , a__="[CLS]" , a__ = None , **a__ , ): _lowerCAmelCase : Union[str, Any] = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else bos_token _lowerCAmelCase : Tuple = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else eos_token _lowerCAmelCase : Any = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else unk_token _lowerCAmelCase : int = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else pad_token _lowerCAmelCase : int = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else cls_token _lowerCAmelCase : Tuple = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else sep_token # Mask token behave like a normal word, i.e. include the space before it _lowerCAmelCase : Union[str, Any] = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else mask_token _lowerCAmelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=a__ , eos_token=a__ , unk_token=a__ , pad_token=a__ , sep_token=a__ , mask_token=a__ , cls_token=a__ , sp_model_kwargs=self.sp_model_kwargs , **a__ , ) _lowerCAmelCase : Optional[Any] = vocab_file _lowerCAmelCase : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(a__ ) @property def __A ( self ): return self.sp_model.get_piece_size() def __A ( self ): _lowerCAmelCase : str = {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 ): _lowerCAmelCase : int = self.__dict__.copy() _lowerCAmelCase : str = None return state def __setstate__( self , a__ ): _lowerCAmelCase : str = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _lowerCAmelCase : Optional[Any] = {} _lowerCAmelCase : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __A ( self , a__ ): return self.sp_model.encode(a__ , out_type=a__ ) def __A ( self , a__ ): return self.sp_model.piece_to_id(a__ ) def __A ( self , a__ ): _lowerCAmelCase : Any = self.sp_model.IdToPiece(a__ ) return token def __A ( self , a__ ): _lowerCAmelCase : List[Any] = [] _lowerCAmelCase : List[Any] = """""" _lowerCAmelCase : int = 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 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : int = [] else: current_sub_tokens.append(a__ ) _lowerCAmelCase : List[str] = False out_string += self.sp_model.decode(a__ ) return out_string.strip() def __A ( self , a__ , a__ = False , a__ = None , a__ = True , **a__ , ): _lowerCAmelCase : Any = kwargs.pop("""use_source_tokenizer""" , a__ ) _lowerCAmelCase : Tuple = self.convert_ids_to_tokens(a__ , skip_special_tokens=a__ ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 _lowerCAmelCase : Optional[int] = [] _lowerCAmelCase : Optional[int] = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(a__ ) ) _lowerCAmelCase : str = [] sub_texts.append(a__ ) else: current_sub_text.append(a__ ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(a__ ) ) # Mimic the behavior of the Rust tokenizer: # No space before [MASK] and [SEP] if spaces_between_special_tokens: _lowerCAmelCase : Tuple = re.sub(r""" (\[(MASK|SEP)\])""" , r"""\1""" , """ """.join(a__ ) ) else: _lowerCAmelCase : Tuple = """""".join(a__ ) _lowerCAmelCase : Optional[Any] = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: _lowerCAmelCase : Any = self.clean_up_tokenization(a__ ) return clean_text else: return text def __A ( self , a__ , a__ = None ): if not os.path.isdir(a__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _lowerCAmelCase : Any = 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: _lowerCAmelCase : Tuple = self.sp_model.serialized_model_proto() fi.write(a__ ) return (out_vocab_file,) def __A ( self , a__ , a__ = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _lowerCAmelCase : Dict = [self.cls_token_id] _lowerCAmelCase : Dict = [self.sep_token_id] return cls + token_ids_a + sep + token_ids_a + sep def __A ( self , a__ , a__ = None , a__ = False ): 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 None: return [1] + ([0] * len(a__ )) + [1] return [1] + ([0] * len(a__ )) + [1] + ([0] * len(a__ )) + [1] def __A ( self , a__ , a__ = None ): _lowerCAmelCase : int = [self.sep_token_id] _lowerCAmelCase : List[str] = [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]
663
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _a : Tuple = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , *a__ , **a__ ): warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , a__ , ) super().__init__(*a__ , **a__ )
663
1
"""simple docstring""" from collections import defaultdict from math import gcd def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1500000 ) -> int: _lowerCAmelCase : defaultdict = defaultdict(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1 ,_lowerCamelCase ,2 ): if gcd(_lowerCamelCase ,_lowerCamelCase ) > 1: continue _lowerCAmelCase : str = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(_lowerCamelCase ,limit + 1 ,_lowerCamelCase ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" import argparse import json import subprocess def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Any ) -> List[Any]: _lowerCAmelCase : Tuple = [] _lowerCAmelCase : Optional[int] = ( f"curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"" """ https://api.github.com/repos/huggingface/transformers/actions/runners""" ) _lowerCAmelCase : List[str] = subprocess.run(_lowerCamelCase ,shell=_lowerCamelCase ,stdout=subprocess.PIPE ) _lowerCAmelCase : int = output.stdout.decode("""utf-8""" ) _lowerCAmelCase : Tuple = json.loads(_lowerCamelCase ) _lowerCAmelCase : int = status["""runners"""] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_lowerCamelCase ) # save the result so we can report them on Slack with open("""offline_runners.txt""" ,"""w""" ) as fp: fp.write(json.dumps(_lowerCamelCase ) ) if len(_lowerCamelCase ) > 0: _lowerCAmelCase : int = """\n""".join([x["""name"""] for x in offline_runners] ) raise ValueError(f"The following runners are offline:\n{failed}" ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> Optional[int]: return values.split(""",""" ) _a : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--target_runners', default=None, type=list_str, required=True, help='Comma-separated list of runners to check status.', ) parser.add_argument( '--token', default=None, type=str, required=True, help='A token that has actions:read permission.' ) _a : Tuple = parser.parse_args() get_runner_status(args.target_runners, args.token)
663
1
"""simple docstring""" import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : str = (PNDMScheduler,) _UpperCamelCase : Optional[Any] = (("num_inference_steps", 50),) def __A ( self , **a__ ): _lowerCAmelCase : Optional[int] = { """num_train_timesteps""": 1000, """beta_start""": 0.0_0_0_1, """beta_end""": 0.0_2, """beta_schedule""": """linear""", } config.update(**a__ ) return config def __A ( self , a__=0 , **a__ ): _lowerCAmelCase : str = dict(self.forward_default_kwargs ) _lowerCAmelCase : str = kwargs.pop("""num_inference_steps""" , a__ ) _lowerCAmelCase : Tuple = self.dummy_sample _lowerCAmelCase : Dict = 0.1 * sample _lowerCAmelCase : Optional[Any] = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: _lowerCAmelCase : Any = self.get_scheduler_config(**a__ ) _lowerCAmelCase : Any = scheduler_class(**a__ ) scheduler.set_timesteps(a__ ) # copy over dummy past residuals _lowerCAmelCase : Optional[Any] = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(a__ ) _lowerCAmelCase : Any = scheduler_class.from_pretrained(a__ ) new_scheduler.set_timesteps(a__ ) # copy over dummy past residuals _lowerCAmelCase : str = dummy_past_residuals[:] _lowerCAmelCase : Optional[int] = scheduler.step_prk(a__ , a__ , a__ , **a__ ).prev_sample _lowerCAmelCase : Any = new_scheduler.step_prk(a__ , a__ , a__ , **a__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" _lowerCAmelCase : Tuple = scheduler.step_plms(a__ , a__ , a__ , **a__ ).prev_sample _lowerCAmelCase : Optional[int] = new_scheduler.step_plms(a__ , a__ , a__ , **a__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __A ( self ): pass def __A ( self , a__=0 , **a__ ): _lowerCAmelCase : List[str] = dict(self.forward_default_kwargs ) _lowerCAmelCase : Any = kwargs.pop("""num_inference_steps""" , a__ ) _lowerCAmelCase : Any = self.dummy_sample _lowerCAmelCase : str = 0.1 * sample _lowerCAmelCase : List[Any] = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] for scheduler_class in self.scheduler_classes: _lowerCAmelCase : Optional[int] = self.get_scheduler_config() _lowerCAmelCase : str = scheduler_class(**a__ ) scheduler.set_timesteps(a__ ) # copy over dummy past residuals (must be after setting timesteps) _lowerCAmelCase : Tuple = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(a__ ) _lowerCAmelCase : List[Any] = scheduler_class.from_pretrained(a__ ) # copy over dummy past residuals new_scheduler.set_timesteps(a__ ) # copy over dummy past residual (must be after setting timesteps) _lowerCAmelCase : Optional[int] = dummy_past_residuals[:] _lowerCAmelCase : int = scheduler.step_prk(a__ , a__ , a__ , **a__ ).prev_sample _lowerCAmelCase : int = new_scheduler.step_prk(a__ , a__ , a__ , **a__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" _lowerCAmelCase : List[str] = scheduler.step_plms(a__ , a__ , a__ , **a__ ).prev_sample _lowerCAmelCase : Optional[Any] = new_scheduler.step_plms(a__ , a__ , a__ , **a__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def __A ( self , **a__ ): _lowerCAmelCase : List[Any] = self.scheduler_classes[0] _lowerCAmelCase : Dict = self.get_scheduler_config(**a__ ) _lowerCAmelCase : Tuple = scheduler_class(**a__ ) _lowerCAmelCase : List[Any] = 10 _lowerCAmelCase : Optional[int] = self.dummy_model() _lowerCAmelCase : int = self.dummy_sample_deter scheduler.set_timesteps(a__ ) for i, t in enumerate(scheduler.prk_timesteps ): _lowerCAmelCase : int = model(a__ , a__ ) _lowerCAmelCase : List[Any] = scheduler.step_prk(a__ , a__ , a__ ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): _lowerCAmelCase : Dict = model(a__ , a__ ) _lowerCAmelCase : Tuple = scheduler.step_plms(a__ , a__ , a__ ).prev_sample return sample def __A ( self ): _lowerCAmelCase : Dict = dict(self.forward_default_kwargs ) _lowerCAmelCase : Optional[Any] = kwargs.pop("""num_inference_steps""" , a__ ) for scheduler_class in self.scheduler_classes: _lowerCAmelCase : Any = self.get_scheduler_config() _lowerCAmelCase : Dict = scheduler_class(**a__ ) _lowerCAmelCase : Optional[int] = self.dummy_sample _lowerCAmelCase : Optional[int] = 0.1 * sample if num_inference_steps is not None and hasattr(a__ , """set_timesteps""" ): scheduler.set_timesteps(a__ ) elif num_inference_steps is not None and not hasattr(a__ , """set_timesteps""" ): _lowerCAmelCase : Optional[int] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) _lowerCAmelCase : List[Any] = [residual + 0.2, residual + 0.1_5, residual + 0.1, residual + 0.0_5] _lowerCAmelCase : Dict = dummy_past_residuals[:] _lowerCAmelCase : Union[str, Any] = scheduler.step_prk(a__ , 0 , a__ , **a__ ).prev_sample _lowerCAmelCase : Any = scheduler.step_prk(a__ , 1 , a__ , **a__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) _lowerCAmelCase : Tuple = scheduler.step_plms(a__ , 0 , a__ , **a__ ).prev_sample _lowerCAmelCase : List[Any] = scheduler.step_plms(a__ , 1 , a__ , **a__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __A ( self ): for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=a__ ) def __A ( self ): for steps_offset in [0, 1]: self.check_over_configs(steps_offset=a__ ) _lowerCAmelCase : int = self.scheduler_classes[0] _lowerCAmelCase : Optional[int] = self.get_scheduler_config(steps_offset=1 ) _lowerCAmelCase : Optional[Any] = scheduler_class(**a__ ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def __A ( self ): for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1] , [0.0_0_2, 0.0_2] ): self.check_over_configs(beta_start=a__ , beta_end=a__ ) def __A ( self ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=a__ ) def __A ( self ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=a__ ) def __A ( self ): for t in [1, 5, 10]: self.check_over_forward(time_step=a__ ) def __A ( self ): for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=a__ ) def __A ( self ): # earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3 _lowerCAmelCase : Dict = 27 for scheduler_class in self.scheduler_classes: _lowerCAmelCase : Optional[int] = self.dummy_sample _lowerCAmelCase : Union[str, Any] = 0.1 * sample _lowerCAmelCase : Union[str, Any] = self.get_scheduler_config() _lowerCAmelCase : int = scheduler_class(**a__ ) scheduler.set_timesteps(a__ ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): _lowerCAmelCase : Optional[Any] = scheduler.step_prk(a__ , a__ , a__ ).prev_sample def __A ( self ): with self.assertRaises(a__ ): _lowerCAmelCase : List[Any] = self.scheduler_classes[0] _lowerCAmelCase : List[Any] = self.get_scheduler_config() _lowerCAmelCase : Optional[Any] = scheduler_class(**a__ ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def __A ( self ): _lowerCAmelCase : Dict = self.full_loop() _lowerCAmelCase : Union[str, Any] = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : int = torch.mean(torch.abs(a__ ) ) assert abs(result_sum.item() - 1_9_8.1_3_1_8 ) < 1e-2 assert abs(result_mean.item() - 0.2_5_8_0 ) < 1e-3 def __A ( self ): _lowerCAmelCase : List[str] = self.full_loop(prediction_type="""v_prediction""" ) _lowerCAmelCase : Optional[int] = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : int = torch.mean(torch.abs(a__ ) ) assert abs(result_sum.item() - 6_7.3_9_8_6 ) < 1e-2 assert abs(result_mean.item() - 0.0_8_7_8 ) < 1e-3 def __A ( self ): # We specify different beta, so that the first alpha is 0.99 _lowerCAmelCase : str = self.full_loop(set_alpha_to_one=a__ , beta_start=0.0_1 ) _lowerCAmelCase : Dict = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : Tuple = torch.mean(torch.abs(a__ ) ) assert abs(result_sum.item() - 2_3_0.0_3_9_9 ) < 1e-2 assert abs(result_mean.item() - 0.2_9_9_5 ) < 1e-3 def __A ( self ): # We specify different beta, so that the first alpha is 0.99 _lowerCAmelCase : Dict = self.full_loop(set_alpha_to_one=a__ , beta_start=0.0_1 ) _lowerCAmelCase : Optional[Any] = torch.sum(torch.abs(a__ ) ) _lowerCAmelCase : Union[str, Any] = torch.mean(torch.abs(a__ ) ) assert abs(result_sum.item() - 1_8_6.9_4_8_2 ) < 1e-2 assert abs(result_mean.item() - 0.2_4_3_4 ) < 1e-3
663
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _a : List[str] = { 'configuration_poolformer': [ 'POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PoolFormerConfig', 'PoolFormerOnnxConfig', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Optional[int] = ['PoolFormerFeatureExtractor'] _a : str = ['PoolFormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : List[str] = [ 'POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'PoolFormerForImageClassification', 'PoolFormerModel', 'PoolFormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_poolformer import ( POOLFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, PoolFormerConfig, PoolFormerOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_poolformer import PoolFormerFeatureExtractor from .image_processing_poolformer import PoolFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_poolformer import ( POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, PoolFormerForImageClassification, PoolFormerModel, PoolFormerPreTrainedModel, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure)
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ) -> int: _lowerCAmelCase : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 ,limit + 1 ): if phi[i] == i - 1: for j in range(2 * i ,limit + 1 ,_lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int=28123 ) -> int: _lowerCAmelCase : Dict = [1] * (limit + 1) for i in range(2 ,int(limit**0.5 ) + 1 ): sum_divs[i * i] += i for k in range(i + 1 ,limit // i + 1 ): sum_divs[k * i] += k + i _lowerCAmelCase : Union[str, Any] = set() _lowerCAmelCase : List[Any] = 0 for n in range(1 ,limit + 1 ): if sum_divs[n] > n: abundants.add(_lowerCamelCase ) if not any((n - a in abundants) for a in abundants ): res += n return res if __name__ == "__main__": print(solution())
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Tuple = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : list[str] ) -> str: _lowerCAmelCase : List[str] = """""" for word_or_phrase in separated: if not isinstance(_lowerCamelCase ,_lowerCamelCase ): raise Exception("""join() accepts only strings to be joined""" ) joined += word_or_phrase + separator return joined.strip(_lowerCamelCase ) if __name__ == "__main__": from doctest import testmod testmod()
663
"""simple docstring""" from PIL import Image def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Image ,_lowerCamelCase : int ) -> Image: _lowerCAmelCase : Any = (259 * (level + 255)) / (255 * (259 - level)) def contrast(_lowerCamelCase : int ) -> int: return int(128 + factor * (c - 128) ) return img.point(_lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 _a : str = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
663
1
"""simple docstring""" import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast _a : Union[str, Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __A ( datasets.BuilderConfig ): _UpperCamelCase : int = 10_000 _UpperCamelCase : Optional[List[str]] = None _UpperCamelCase : Optional[datasets.Features] = None class __A ( datasets.ArrowBasedBuilder ): _UpperCamelCase : List[str] = ParquetConfig def __A ( self ): return datasets.DatasetInfo(features=self.config.features ) def __A ( self , a__ ): if not self.config.data_files: raise ValueError(F"At least one data file must be specified, but got data_files={self.config.data_files}" ) _lowerCAmelCase : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(a__ , (str, list, tuple) ): _lowerCAmelCase : Union[str, Any] = data_files if isinstance(a__ , a__ ): _lowerCAmelCase : Optional[Any] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowerCAmelCase : int = [dl_manager.iter_files(a__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""files""": files} )] _lowerCAmelCase : Optional[Any] = [] for split_name, files in data_files.items(): if isinstance(a__ , a__ ): _lowerCAmelCase : List[Any] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowerCAmelCase : int = [dl_manager.iter_files(a__ ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(a__ ): with open(a__ , """rb""" ) as f: _lowerCAmelCase : Tuple = datasets.Features.from_arrow_schema(pq.read_schema(a__ ) ) break splits.append(datasets.SplitGenerator(name=a__ , gen_kwargs={"""files""": files} ) ) return splits def __A ( self , a__ ): if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example _lowerCAmelCase : List[Any] = table_cast(a__ , self.info.features.arrow_schema ) return pa_table def __A ( self , a__ ): _lowerCAmelCase : Union[str, Any] = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F"Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'" ) for file_idx, file in enumerate(itertools.chain.from_iterable(a__ ) ): with open(a__ , """rb""" ) as f: _lowerCAmelCase : Tuple = pq.ParquetFile(a__ ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): _lowerCAmelCase : Tuple = pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield F"{file_idx}_{batch_idx}", self._cast_table(a__ ) except ValueError as e: logger.error(F"Failed to read file '{file}' with error {type(a__ )}: {e}" ) raise
663
"""simple docstring""" class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A : def __init__( self ): _lowerCAmelCase : Union[str, Any] = [ [], [], [], ] def __A ( self , a__ , a__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(a__ ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def __A ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self ): return "\n".join(F"Priority {i}: {q}" for i, q in enumerate(self.queues ) ) class __A : def __init__( self ): _lowerCAmelCase : int = [] def __A ( self , a__ ): if len(self.queue ) == 100: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(a__ ) def __A ( self ): if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: _lowerCAmelCase : int = min(self.queue ) self.queue.remove(a__ ) return data def __str__( self ): return str(self.queue ) def SCREAMING_SNAKE_CASE ( ) -> str: _lowerCAmelCase : Union[str, Any] = FixedPriorityQueue() fpq.enqueue(0 ,10 ) fpq.enqueue(1 ,70 ) fpq.enqueue(0 ,100 ) fpq.enqueue(2 ,1 ) fpq.enqueue(2 ,5 ) fpq.enqueue(1 ,7 ) fpq.enqueue(2 ,4 ) fpq.enqueue(1 ,64 ) fpq.enqueue(0 ,128 ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: _lowerCAmelCase : Tuple = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(100 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(128 ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
663
1
"""simple docstring""" import unittest import numpy as np def SCREAMING_SNAKE_CASE ( _lowerCamelCase : np.ndarray ,_lowerCamelCase : np.ndarray ,_lowerCamelCase : np.ndarray ,_lowerCamelCase : np.ndarray | None = None ,) -> np.ndarray: _lowerCAmelCase : Any = np.shape(_lowerCamelCase ) _lowerCAmelCase : List[str] = np.shape(_lowerCamelCase ) _lowerCAmelCase : List[Any] = np.shape(_lowerCamelCase ) if shape_a[0] != shape_b[0]: _lowerCAmelCase : str = ( """Expected the same number of rows for A and B. """ f"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(_lowerCamelCase ) if shape_b[1] != shape_c[1]: _lowerCAmelCase : Dict = ( """Expected the same number of columns for B and C. """ f"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(_lowerCamelCase ) _lowerCAmelCase : List[str] = pseudo_inv if a_inv is None: try: _lowerCAmelCase : Tuple = np.linalg.inv(_lowerCamelCase ) except np.linalg.LinAlgError: raise ValueError( """Input matrix A is not invertible. Cannot compute Schur complement.""" ) return mat_c - mat_b.T @ a_inv @ mat_b class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : List[Any] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) _lowerCAmelCase : List[str] = np.array([[0, 3], [3, 0], [2, 3]] ) _lowerCAmelCase : Optional[int] = np.array([[2, 1], [6, 3]] ) _lowerCAmelCase : Optional[int] = schur_complement(a__ , a__ , a__ ) _lowerCAmelCase : Dict = np.block([[a, b], [b.T, c]] ) _lowerCAmelCase : Any = np.linalg.det(a__ ) _lowerCAmelCase : str = np.linalg.det(a__ ) _lowerCAmelCase : List[str] = np.linalg.det(a__ ) self.assertAlmostEqual(a__ , det_a * det_s ) def __A ( self ): _lowerCAmelCase : Optional[Any] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) _lowerCAmelCase : Dict = np.array([[0, 3], [3, 0], [2, 3]] ) _lowerCAmelCase : Any = np.array([[2, 1], [6, 3]] ) with self.assertRaises(a__ ): schur_complement(a__ , a__ , a__ ) def __A ( self ): _lowerCAmelCase : Optional[Any] = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) _lowerCAmelCase : Any = np.array([[0, 3], [3, 0], [2, 3]] ) _lowerCAmelCase : Optional[Any] = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(a__ ): schur_complement(a__ , a__ , a__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
663
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __A ( unittest.TestCase ): _UpperCamelCase : str = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING _UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = AudioClassificationPipeline(model=a__ , feature_extractor=a__ ) # test with a raw waveform _lowerCAmelCase : Optional[int] = np.zeros((34000,) ) _lowerCAmelCase : Optional[Any] = np.zeros((14000,) ) return audio_classifier, [audioa, audio] def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = examples _lowerCAmelCase : List[Any] = audio_classifier(a__ ) # by default a model is initialized with num_labels=2 self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) _lowerCAmelCase : Tuple = audio_classifier(a__ , top_k=1 ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) self.run_torchaudio(a__ ) @require_torchaudio def __A ( self , a__ ): import datasets # test with a local file _lowerCAmelCase : int = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) _lowerCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] _lowerCAmelCase : str = audio_classifier(a__ ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) @require_torch def __A ( self ): _lowerCAmelCase : int = """anton-l/wav2vec2-random-tiny-classifier""" _lowerCAmelCase : Optional[Any] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : Any = np.ones((8000,) ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) _lowerCAmelCase : List[str] = [ {"""score""": 0.0_8_4_2, """label""": """no"""}, {"""score""": 0.0_8_3_8, """label""": """up"""}, {"""score""": 0.0_8_3_7, """label""": """go"""}, {"""score""": 0.0_8_3_4, """label""": """right"""}, ] _lowerCAmelCase : str = [ {"""score""": 0.0_8_4_5, """label""": """stop"""}, {"""score""": 0.0_8_4_4, """label""": """on"""}, {"""score""": 0.0_8_4_1, """label""": """right"""}, {"""score""": 0.0_8_3_4, """label""": """left"""}, ] self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) _lowerCAmelCase : int = {"""array""": np.ones((8000,) ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate} _lowerCAmelCase : int = audio_classifier(a__ , top_k=4 ) self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __A ( self ): import datasets _lowerCAmelCase : Optional[Any] = """superb/wav2vec2-base-superb-ks""" _lowerCAmelCase : List[str] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : str = datasets.load_dataset("""anton-l/superb_dummy""" , """ks""" , split="""test""" ) _lowerCAmelCase : Optional[Any] = np.array(dataset[3]["""speech"""] , dtype=np.floataa ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) self.assertEqual( nested_simplify(a__ , decimals=3 ) , [ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] , ) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def __A ( self ): pass
663
1
"""simple docstring""" from __future__ import annotations _a : Any = 10 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : list[int] ) -> list[int]: _lowerCAmelCase : Dict = 1 _lowerCAmelCase : Tuple = max(_lowerCamelCase ) while placement <= max_digit: # declare and initialize empty buckets _lowerCAmelCase : list[list] = [[] for _ in range(_lowerCamelCase )] # split list_of_ints between the buckets for i in list_of_ints: _lowerCAmelCase : Tuple = int((i / placement) % RADIX ) buckets[tmp].append(_lowerCamelCase ) # put each buckets' contents into list_of_ints _lowerCAmelCase : List[Any] = 0 for b in range(_lowerCamelCase ): for i in buckets[b]: _lowerCAmelCase : str = i a += 1 # move to next placement *= RADIX return list_of_ints if __name__ == "__main__": import doctest doctest.testmod()
663
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _a : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _a : int = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ,_lowerCamelCase : Dict=8 ) -> Any: _lowerCAmelCase : List[Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _lowerCAmelCase : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Any=512 ,_lowerCamelCase : Dict=512 ) -> List[Any]: _lowerCAmelCase : Any = pil_image.resize((w, h) ,resample=Image.BICUBIC ,reducing_gap=1 ) _lowerCAmelCase : Dict = np.array(pil_image.convert("""RGB""" ) ) _lowerCAmelCase : List[str] = arr.astype(np.floataa ) / 1_27.5 - 1 _lowerCAmelCase : int = np.transpose(_lowerCamelCase ,[2, 0, 1] ) _lowerCAmelCase : Optional[Any] = torch.from_numpy(_lowerCamelCase ).unsqueeze(0 ) return image class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__ , a__ , ): super().__init__() self.register_modules( unet=a__ , scheduler=a__ , movq=a__ , ) _lowerCAmelCase : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __A ( self , a__ , a__ , a__ ): # get the original timestep using init_timestep _lowerCAmelCase : Optional[Any] = min(int(num_inference_steps * strength ) , a__ ) _lowerCAmelCase : List[Any] = max(num_inference_steps - init_timestep , 0 ) _lowerCAmelCase : Dict = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__=None ): if not isinstance(a__ , (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(a__ )}" ) _lowerCAmelCase : Union[str, Any] = image.to(device=a__ , dtype=a__ ) _lowerCAmelCase : int = batch_size * num_images_per_prompt if image.shape[1] == 4: _lowerCAmelCase : int = image else: if isinstance(a__ , a__ ) and len(a__ ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(a__ )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) elif isinstance(a__ , a__ ): _lowerCAmelCase : Optional[int] = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a__ ) ] _lowerCAmelCase : Optional[int] = torch.cat(a__ , dim=0 ) else: _lowerCAmelCase : List[Any] = self.movq.encode(a__ ).latent_dist.sample(a__ ) _lowerCAmelCase : Dict = self.movq.config.scaling_factor * init_latents _lowerCAmelCase : str = torch.cat([init_latents] , dim=0 ) _lowerCAmelCase : Dict = init_latents.shape _lowerCAmelCase : str = randn_tensor(a__ , generator=a__ , device=a__ , dtype=a__ ) # get latents _lowerCAmelCase : Optional[Any] = self.scheduler.add_noise(a__ , a__ , a__ ) _lowerCAmelCase : int = init_latents return latents def __A ( self , a__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _lowerCAmelCase : str = torch.device(F"cuda:{gpu_id}" ) _lowerCAmelCase : int = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a__ , a__ ) def __A ( self , a__=0 ): if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) _lowerCAmelCase : Optional[int] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=a__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _lowerCAmelCase : List[str] = None for cpu_offloaded_model in [self.unet, self.movq]: _lowerCAmelCase , _lowerCAmelCase : str = cpu_offload_with_hook(a__ , a__ , prev_module_hook=a__ ) # We'll offload the last model manually. _lowerCAmelCase : Tuple = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A ( self ): if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(a__ , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a__ ) def __call__( self , a__ , a__ , a__ , a__ = 512 , a__ = 512 , a__ = 100 , a__ = 4.0 , a__ = 0.3 , a__ = 1 , a__ = None , a__ = "pil" , a__ = True , ): _lowerCAmelCase : Dict = self._execution_device _lowerCAmelCase : Optional[Any] = guidance_scale > 1.0 if isinstance(a__ , a__ ): _lowerCAmelCase : Dict = torch.cat(a__ , dim=0 ) _lowerCAmelCase : Dict = image_embeds.shape[0] if isinstance(a__ , a__ ): _lowerCAmelCase : List[Any] = torch.cat(a__ , dim=0 ) if do_classifier_free_guidance: _lowerCAmelCase : int = image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Any = negative_image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Optional[int] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=a__ ) if not isinstance(a__ , a__ ): _lowerCAmelCase : Any = [image] if not all(isinstance(a__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(a__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) _lowerCAmelCase : Tuple = torch.cat([prepare_image(a__ , a__ , a__ ) for i in image] , dim=0 ) _lowerCAmelCase : Union[str, Any] = image.to(dtype=image_embeds.dtype , device=a__ ) _lowerCAmelCase : Union[str, Any] = self.movq.encode(a__ )["""latents"""] _lowerCAmelCase : Tuple = latents.repeat_interleave(a__ , dim=0 ) self.scheduler.set_timesteps(a__ , device=a__ ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.get_timesteps(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = timesteps[:1].repeat(batch_size * num_images_per_prompt ) _lowerCAmelCase , _lowerCAmelCase : Dict = downscale_height_and_width(a__ , a__ , self.movq_scale_factor ) _lowerCAmelCase : List[str] = self.prepare_latents( a__ , a__ , a__ , a__ , image_embeds.dtype , a__ , a__ ) for i, t in enumerate(self.progress_bar(a__ ) ): # expand the latents if we are doing classifier free guidance _lowerCAmelCase : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowerCAmelCase : int = {"""image_embeds""": image_embeds} _lowerCAmelCase : List[str] = self.unet( sample=a__ , timestep=a__ , encoder_hidden_states=a__ , added_cond_kwargs=a__ , return_dict=a__ , )[0] if do_classifier_free_guidance: _lowerCAmelCase , _lowerCAmelCase : Optional[int] = noise_pred.split(latents.shape[1] , dim=1 ) _lowerCAmelCase , _lowerCAmelCase : List[Any] = noise_pred.chunk(2 ) _lowerCAmelCase , _lowerCAmelCase : Tuple = variance_pred.chunk(2 ) _lowerCAmelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _lowerCAmelCase : List[str] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , """variance_type""" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _lowerCAmelCase , _lowerCAmelCase : Dict = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _lowerCAmelCase : List[str] = self.scheduler.step( a__ , a__ , a__ , generator=a__ , )[0] # post-processing _lowerCAmelCase : int = self.movq.decode(a__ , force_not_quantize=a__ )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: _lowerCAmelCase : List[Any] = image * 0.5 + 0.5 _lowerCAmelCase : Any = image.clamp(0 , 1 ) _lowerCAmelCase : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _lowerCAmelCase : List[str] = self.numpy_to_pil(a__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a__ )
663
1
"""simple docstring""" from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Optional[Any] = ["image_processor", "tokenizer"] _UpperCamelCase : Any = "AutoImageProcessor" _UpperCamelCase : int = "AutoTokenizer" def __init__( self , a__ , a__ ): super().__init__(a__ , a__ ) _lowerCAmelCase : Union[str, Any] = self.image_processor def __call__( self , a__=None , a__=None , a__=None , **a__ ): if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _lowerCAmelCase : Dict = self.tokenizer(a__ , return_tensors=a__ , **a__ ) if images is not None: _lowerCAmelCase : Tuple = self.image_processor(a__ , return_tensors=a__ , **a__ ) if text is not None and images is not None: _lowerCAmelCase : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a__ ) , tensor_type=a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.batch_decode(*a__ , **a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.decode(*a__ , **a__ ) @property def __A ( self ): return ["input_ids", "attention_mask", "pixel_values"]
663
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
1
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL.Image from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, PILImageResampling, get_image_size, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging _a : Dict = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Dict = ["pixel_values"] def __init__( self , a__ = True , a__ = 32 , a__=PILImageResampling.BILINEAR , a__ = True , **a__ , ): _lowerCAmelCase : Optional[Any] = do_resize _lowerCAmelCase : Optional[Any] = do_rescale _lowerCAmelCase : List[str] = size_divisor _lowerCAmelCase : Optional[Any] = resample super().__init__(**a__ ) def __A ( self , a__ , a__ , a__ , a__ = None , **a__ ): _lowerCAmelCase , _lowerCAmelCase : Optional[int] = get_image_size(a__ ) # Rounds the height and width down to the closest multiple of size_divisor _lowerCAmelCase : List[Any] = height // size_divisor * size_divisor _lowerCAmelCase : Any = width // size_divisor * size_divisor _lowerCAmelCase : Tuple = resize(a__ , (new_h, new_w) , resample=a__ , data_format=a__ , **a__ ) return image def __A ( self , a__ , a__ , a__ = None , **a__ ): return rescale(image=a__ , scale=a__ , data_format=a__ , **a__ ) def __A ( self , a__ , a__ = None , a__ = None , a__=None , a__ = None , a__ = None , a__ = ChannelDimension.FIRST , **a__ , ): _lowerCAmelCase : Tuple = do_resize if do_resize is not None else self.do_resize _lowerCAmelCase : Dict = do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase : Dict = size_divisor if size_divisor is not None else self.size_divisor _lowerCAmelCase : List[Any] = resample if resample is not None else self.resample if do_resize and size_divisor is None: raise ValueError("""size_divisor is required for resizing""" ) _lowerCAmelCase : List[str] = make_list_of_images(a__ ) if not valid_images(a__ ): raise ValueError("""Invalid image(s)""" ) # All transformations expect numpy arrays. _lowerCAmelCase : Union[str, Any] = [to_numpy_array(a__ ) for img in images] if do_resize: _lowerCAmelCase : int = [self.resize(a__ , size_divisor=a__ , resample=a__ ) for image in images] if do_rescale: _lowerCAmelCase : Tuple = [self.rescale(a__ , scale=1 / 255 ) for image in images] _lowerCAmelCase : int = [to_channel_dimension_format(a__ , a__ ) for image in images] _lowerCAmelCase : Optional[int] = {"""pixel_values""": images} return BatchFeature(data=a__ , tensor_type=a__ )
663
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : str = CpmAntTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """<d>""", """</d>""", """<s>""", """</s>""", """</_>""", """<unk>""", """<pad>""", """</n>""", """我""", """是""", """C""", """P""", """M""", """A""", """n""", """t""", ] _lowerCAmelCase : int = 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] ) ) @tooslow def __A ( self ): _lowerCAmelCase : Tuple = CpmAntTokenizer.from_pretrained("""openbmb/cpm-ant-10b""" ) _lowerCAmelCase : Optional[Any] = """今天天气真好!""" _lowerCAmelCase : Any = ["""今天""", """天气""", """真""", """好""", """!"""] _lowerCAmelCase : str = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = """今天天气真好!""" _lowerCAmelCase : Optional[Any] = [tokenizer.bos_token] + tokens _lowerCAmelCase : Optional[int] = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) _lowerCAmelCase : Tuple = tokenizer.decode(a__ ) self.assertEqual(a__ , a__ )
663
1
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: _a : str = None _a : List[str] = logging.get_logger(__name__) _a : List[str] = {'vocab_file': 'sentencepiece.bpe.model', 'tokenizer_file': 'tokenizer.json'} _a : Optional[int] = { 'vocab_file': { 'facebook/mbart-large-en-ro': ( 'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model' ), 'facebook/mbart-large-cc25': ( 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model' ), }, 'tokenizer_file': { 'facebook/mbart-large-en-ro': 'https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json', 'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json', }, } _a : Union[str, Any] = { 'facebook/mbart-large-en-ro': 1_024, 'facebook/mbart-large-cc25': 1_024, } # fmt: off _a : List[Any] = ['ar_AR', 'cs_CZ', 'de_DE', 'en_XX', 'es_XX', 'et_EE', 'fi_FI', 'fr_XX', 'gu_IN', 'hi_IN', 'it_IT', 'ja_XX', 'kk_KZ', 'ko_KR', 'lt_LT', 'lv_LV', 'my_MM', 'ne_NP', 'nl_XX', 'ro_RO', 'ru_RU', 'si_LK', 'tr_TR', 'vi_VN', 'zh_CN'] class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[str] = VOCAB_FILES_NAMES _UpperCamelCase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : int = ["input_ids", "attention_mask"] _UpperCamelCase : Tuple = MBartTokenizer _UpperCamelCase : List[int] = [] _UpperCamelCase : List[int] = [] def __init__( self , a__=None , a__=None , a__="<s>" , a__="</s>" , a__="</s>" , a__="<s>" , a__="<unk>" , a__="<pad>" , a__="<mask>" , a__=None , a__=None , a__=None , **a__ , ): # Mask token behave like a normal word, i.e. include the space before it _lowerCAmelCase : Dict = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else mask_token super().__init__( vocab_file=a__ , tokenizer_file=a__ , bos_token=a__ , eos_token=a__ , sep_token=a__ , cls_token=a__ , unk_token=a__ , pad_token=a__ , mask_token=a__ , src_lang=a__ , tgt_lang=a__ , additional_special_tokens=a__ , **a__ , ) _lowerCAmelCase : Optional[int] = vocab_file _lowerCAmelCase : Dict = False if not self.vocab_file else True _lowerCAmelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({"""additional_special_tokens""": _additional_special_tokens} ) _lowerCAmelCase : str = { lang_code: self.convert_tokens_to_ids(a__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } _lowerCAmelCase : Optional[int] = src_lang if src_lang is not None else """en_XX""" _lowerCAmelCase : Optional[Any] = self.convert_tokens_to_ids(self._src_lang ) _lowerCAmelCase : Optional[Any] = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def __A ( self ): return self._src_lang @src_lang.setter def __A ( self , a__ ): _lowerCAmelCase : Dict = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __A ( self , a__ , a__ = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def __A ( self , a__ , a__ = None ): _lowerCAmelCase : Any = [self.sep_token_id] _lowerCAmelCase : Optional[int] = [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] def __A ( self , a__ , a__ , a__ , a__ , **a__ ): if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) _lowerCAmelCase : Optional[int] = src_lang _lowerCAmelCase : int = self(a__ , add_special_tokens=a__ , return_tensors=a__ , **a__ ) _lowerCAmelCase : Dict = self.convert_tokens_to_ids(a__ ) _lowerCAmelCase : Optional[int] = tgt_lang_id return inputs def __A ( self , a__ , a__ = "en_XX" , a__ = None , a__ = "ro_RO" , **a__ , ): _lowerCAmelCase : Dict = src_lang _lowerCAmelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(a__ , a__ , **a__ ) def __A ( self ): return self.set_src_lang_special_tokens(self.src_lang ) def __A ( self ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def __A ( self , a__ ): _lowerCAmelCase : Optional[Any] = self.convert_tokens_to_ids(a__ ) _lowerCAmelCase : Tuple = [] _lowerCAmelCase : Dict = [self.eos_token_id, self.cur_lang_code] _lowerCAmelCase : Optional[int] = self.convert_ids_to_tokens(self.prefix_tokens ) _lowerCAmelCase : Dict = self.convert_ids_to_tokens(self.suffix_tokens ) _lowerCAmelCase : Any = processors.TemplateProcessing( single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str , pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __A ( self , a__ ): _lowerCAmelCase : Any = self.convert_tokens_to_ids(a__ ) _lowerCAmelCase : Tuple = [] _lowerCAmelCase : List[Any] = [self.eos_token_id, self.cur_lang_code] _lowerCAmelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) _lowerCAmelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) _lowerCAmelCase : str = processors.TemplateProcessing( single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str , pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __A ( self , a__ , a__ = None ): if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(a__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory." ) return _lowerCAmelCase : Tuple = 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__ ): copyfile(self.vocab_file , a__ ) return (out_vocab_file,)
663
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
1
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. _a : Dict = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. _a : str = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. _a : Optional[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1_000)) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : str ) -> tuple[str, float]: _lowerCAmelCase : Dict = len([g for position, g in enumerate(_lowerCamelCase ) if g == main_target[position]] ) return (item, float(_lowerCamelCase )) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : str ) -> tuple[str, str]: _lowerCAmelCase : Optional[Any] = random.randint(0 ,len(_lowerCamelCase ) - 1 ) _lowerCAmelCase : str = parent_a[:random_slice] + parent_a[random_slice:] _lowerCAmelCase : List[str] = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : list[str] ) -> str: _lowerCAmelCase : str = list(_lowerCamelCase ) if random.uniform(0 ,1 ) < MUTATION_PROBABILITY: _lowerCAmelCase : Dict = random.choice(_lowerCamelCase ) return "".join(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : tuple[str, float] ,_lowerCamelCase : list[tuple[str, float]] ,_lowerCamelCase : list[str] ,) -> list[str]: _lowerCAmelCase : Optional[int] = [] # Generate more children proportionally to the fitness score. _lowerCAmelCase : List[str] = int(parent_a[1] * 100 ) + 1 _lowerCAmelCase : str = 10 if child_n >= 10 else child_n for _ in range(_lowerCamelCase ): _lowerCAmelCase : Optional[int] = population_score[random.randint(0 ,_lowerCamelCase )][0] _lowerCAmelCase , _lowerCAmelCase : Optional[int] = crossover(parent_a[0] ,_lowerCamelCase ) # Append new string to the population list. pop.append(mutate(_lowerCamelCase ,_lowerCamelCase ) ) pop.append(mutate(_lowerCamelCase ,_lowerCamelCase ) ) return pop def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : list[str] ,_lowerCamelCase : bool = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: _lowerCAmelCase : Optional[int] = f"{N_POPULATION} must be bigger than {N_SELECTED}" raise ValueError(_lowerCamelCase ) # Verify that the target contains no genes besides the ones inside genes variable. _lowerCAmelCase : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: _lowerCAmelCase : str = f"{not_in_genes_list} is not in genes list, evolution cannot converge" raise ValueError(_lowerCamelCase ) # Generate random starting population. _lowerCAmelCase : str = [] for _ in range(_lowerCamelCase ): population.append("""""".join([random.choice(_lowerCamelCase ) for i in range(len(_lowerCamelCase ) )] ) ) # Just some logs to know what the algorithms is doing. _lowerCAmelCase , _lowerCAmelCase : List[str] = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(_lowerCamelCase ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. _lowerCAmelCase : int = [evaluate(_lowerCamelCase ,_lowerCamelCase ) for item in population] # Check if there is a matching evolution. _lowerCAmelCase : int = sorted(_lowerCamelCase ,key=lambda _lowerCamelCase : x[1] ,reverse=_lowerCamelCase ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( f"\nGeneration: {generation}" f"\nTotal Population:{total_population}" f"\nBest score: {population_score[0][1]}" f"\nBest string: {population_score[0][0]}" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. _lowerCAmelCase : List[str] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(_lowerCamelCase ) # Normalize population score to be between 0 and 1. _lowerCAmelCase : List[str] = [ (item, score / len(_lowerCamelCase )) for item, score in population_score ] # This is selection for i in range(_lowerCamelCase ): population.extend(select(population_score[int(_lowerCamelCase )] ,_lowerCamelCase ,_lowerCamelCase ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(_lowerCamelCase ) > N_POPULATION: break if __name__ == "__main__": _a : Optional[int] = ( 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!' ) _a : Dict = list( ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm' 'nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\' ) _a , _a , _a : List[str] = basic(target_str, genes_list) print( F"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}""" )
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input _a : List[str] = 'Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine' def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: _lowerCAmelCase : Any = _ask_options( """In which compute environment are you running?""" ,["""This machine""", """AWS (Amazon SageMaker)"""] ,_convert_compute_environment ,) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: _lowerCAmelCase : str = get_sagemaker_input() else: _lowerCAmelCase : Union[str, Any] = get_cluster_input() return config def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any]=None ) -> Optional[Any]: if subparsers is not None: _lowerCAmelCase : Union[str, Any] = subparsers.add_parser("""config""" ,description=_lowerCamelCase ) else: _lowerCAmelCase : Tuple = argparse.ArgumentParser("""Accelerate config command""" ,description=_lowerCamelCase ) parser.add_argument( """--config_file""" ,default=_lowerCamelCase ,help=( """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """ """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """ """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """ """with 'huggingface'.""" ) ,) if subparsers is not None: parser.set_defaults(func=_lowerCamelCase ) return parser def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> List[Any]: _lowerCAmelCase : Optional[Any] = get_user_input() if args.config_file is not None: _lowerCAmelCase : Tuple = args.config_file else: if not os.path.isdir(_lowerCamelCase ): os.makedirs(_lowerCamelCase ) _lowerCAmelCase : List[Any] = default_yaml_config_file if config_file.endswith(""".json""" ): config.to_json_file(_lowerCamelCase ) else: config.to_yaml_file(_lowerCamelCase ) print(f"accelerate configuration saved at {config_file}" ) def SCREAMING_SNAKE_CASE ( ) -> int: _lowerCAmelCase : List[str] = config_command_parser() _lowerCAmelCase : List[Any] = parser.parse_args() config_command(_lowerCamelCase ) if __name__ == "__main__": main()
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> bool: 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...') _a : int = int(input('Enter number: ').strip()) print(F"""{number} is {"" if perfect(number) else "not "}a Perfect Number.""")
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available, ) _a : Optional[Any] = { 'configuration_speecht5': [ 'SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP', 'SpeechT5Config', 'SpeechT5HifiGanConfig', ], 'feature_extraction_speecht5': ['SpeechT5FeatureExtractor'], 'processing_speecht5': ['SpeechT5Processor'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : int = ['SpeechT5Tokenizer'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Union[str, Any] = [ 'SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST', 'SpeechT5ForSpeechToText', 'SpeechT5ForSpeechToSpeech', 'SpeechT5ForTextToSpeech', 'SpeechT5Model', 'SpeechT5PreTrainedModel', 'SpeechT5HifiGan', ] if TYPE_CHECKING: from .configuration_speechta import ( SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP, SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP, SpeechTaConfig, SpeechTaHifiGanConfig, ) from .feature_extraction_speechta import SpeechTaFeatureExtractor from .processing_speechta import SpeechTaProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speechta import SpeechTaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speechta import ( SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaModel, SpeechTaPreTrainedModel, ) else: import sys _a : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
1
"""simple docstring""" import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class __A ( unittest.TestCase ): @slow def __A ( self ): for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(a__ ): _lowerCAmelCase : Optional[int] = AutoConfig.from_pretrained(a__ ) self.assertIsNotNone(a__ ) self.assertIsInstance(a__ , a__ ) _lowerCAmelCase : str = FlaxAutoModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) self.assertIsInstance(a__ , a__ ) @slow def __A ( self ): for model_name in ["roberta-base", "roberta-large"]: with self.subTest(a__ ): _lowerCAmelCase : Tuple = AutoConfig.from_pretrained(a__ ) self.assertIsNotNone(a__ ) self.assertIsInstance(a__ , a__ ) _lowerCAmelCase : Tuple = FlaxAutoModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) self.assertIsInstance(a__ , a__ ) @slow def __A ( self ): for model_name in ["bert-base-cased", "bert-large-uncased"]: _lowerCAmelCase : Union[str, Any] = AutoTokenizer.from_pretrained(a__ ) _lowerCAmelCase : Optional[Any] = FlaxBertModel.from_pretrained(a__ ) _lowerCAmelCase : int = tokenizer("""Do you support jax jitted function?""" , return_tensors=TensorType.JAX ) @jax.jit def eval(**a__ ): return model(**a__ ) eval(**a__ ).block_until_ready() @slow def __A ( self ): for model_name in ["roberta-base", "roberta-large"]: _lowerCAmelCase : Optional[Any] = AutoTokenizer.from_pretrained(a__ ) _lowerCAmelCase : str = FlaxRobertaModel.from_pretrained(a__ ) _lowerCAmelCase : List[str] = tokenizer("""Do you support jax jitted function?""" , return_tensors=TensorType.JAX ) @jax.jit def eval(**a__ ): return model(**a__ ) eval(**a__ ).block_until_ready() def __A ( self ): with self.assertRaisesRegex( a__ , """bert-base is not a local folder and is not a valid model identifier""" ): _lowerCAmelCase : List[str] = FlaxAutoModel.from_pretrained("""bert-base""" ) def __A ( self ): with self.assertRaisesRegex( a__ , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): _lowerCAmelCase : Any = FlaxAutoModel.from_pretrained(a__ , revision="""aaaaaa""" ) def __A ( self ): with self.assertRaisesRegex( a__ , """hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack""" , ): _lowerCAmelCase : int = FlaxAutoModel.from_pretrained("""hf-internal-testing/config-no-model""" ) def __A ( self ): with self.assertRaisesRegex(a__ , """Use `from_pt=True` to load this model""" ): _lowerCAmelCase : int = FlaxAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only""" )
663
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
1
"""simple docstring""" import numpy as np class __A : def __init__( self ): _lowerCAmelCase : List[Any] = (0, 0) _lowerCAmelCase : List[Any] = None _lowerCAmelCase : str = 0 _lowerCAmelCase : List[Any] = 0 _lowerCAmelCase : str = 0 def __eq__( self , a__ ): return self.position == cell.position def __A ( self ): print(self.position ) class __A : def __init__( self , a__=(5, 5) ): _lowerCAmelCase : Optional[Any] = np.zeros(a__ ) _lowerCAmelCase : Dict = world_size[0] _lowerCAmelCase : List[Any] = world_size[1] def __A ( self ): print(self.w ) def __A ( self , a__ ): _lowerCAmelCase : Dict = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] _lowerCAmelCase : List[Any] = cell.position[0] _lowerCAmelCase : Union[str, Any] = cell.position[1] _lowerCAmelCase : Optional[Any] = [] for n in neughbour_cord: _lowerCAmelCase : Dict = current_x + n[0] _lowerCAmelCase : Union[str, Any] = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: _lowerCAmelCase : int = Cell() _lowerCAmelCase : Tuple = (x, y) _lowerCAmelCase : Optional[Any] = cell neighbours.append(a__ ) return neighbours def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Optional[int] ,_lowerCamelCase : List[str] ) -> Optional[int]: _lowerCAmelCase : str = [] _lowerCAmelCase : Any = [] _open.append(_lowerCamelCase ) while _open: _lowerCAmelCase : Optional[int] = np.argmin([n.f for n in _open] ) _lowerCAmelCase : Optional[Any] = _open[min_f] _closed.append(_open.pop(_lowerCamelCase ) ) if current == goal: break for n in world.get_neigbours(_lowerCamelCase ): for c in _closed: if c == n: continue _lowerCAmelCase : Dict = current.g + 1 _lowerCAmelCase , _lowerCAmelCase : Optional[int] = n.position _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = goal.position _lowerCAmelCase : Optional[Any] = (ya - ya) ** 2 + (xa - xa) ** 2 _lowerCAmelCase : Dict = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(_lowerCamelCase ) _lowerCAmelCase : Tuple = [] while current.parent is not None: path.append(current.position ) _lowerCAmelCase : int = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": _a : Tuple = Gridworld() # Start position and goal _a : int = Cell() _a : Optional[int] = (0, 0) _a : Optional[int] = Cell() _a : List[Any] = (4, 4) print(F"""path from {start.position} to {goal.position}""") _a : int = astar(world, start, goal) # Just for visual reasons. for i in s: _a : List[str] = 1 print(world.w)
663
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _a : Optional[Any] = { 'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'], 'feature_extraction_whisper': ['WhisperFeatureExtractor'], 'processing_whisper': ['WhisperProcessor'], 'tokenization_whisper': ['WhisperTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Optional[Any] = ['WhisperTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Union[str, Any] = [ 'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'WhisperForConditionalGeneration', 'WhisperModel', 'WhisperPreTrainedModel', 'WhisperForAudioClassification', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Optional[Any] = [ 'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFWhisperForConditionalGeneration', 'TFWhisperModel', 'TFWhisperPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'FlaxWhisperForConditionalGeneration', 'FlaxWhisperModel', 'FlaxWhisperPreTrainedModel', 'FlaxWhisperForAudioClassification', ] if TYPE_CHECKING: from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig from .feature_extraction_whisper import WhisperFeatureExtractor from .processing_whisper import WhisperProcessor from .tokenization_whisper import WhisperTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_whisper_fast import WhisperTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_whisper import ( WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, WhisperForAudioClassification, WhisperForConditionalGeneration, WhisperModel, WhisperPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_whisper import ( TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST, TFWhisperForConditionalGeneration, TFWhisperModel, TFWhisperPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_whisper import ( FlaxWhisperForAudioClassification, FlaxWhisperForConditionalGeneration, FlaxWhisperModel, FlaxWhisperPreTrainedModel, ) else: import sys _a : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" _a : Optional[Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[str] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : str = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
663
1
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : int = ["image_processor", "tokenizer"] _UpperCamelCase : Dict = "ChineseCLIPImageProcessor" _UpperCamelCase : List[str] = ("BertTokenizer", "BertTokenizerFast") def __init__( self , a__=None , a__=None , **a__ ): _lowerCAmelCase : Optional[int] = 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__ , ) _lowerCAmelCase : List[str] = kwargs.pop("""feature_extractor""" ) _lowerCAmelCase : Dict = 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__ ) _lowerCAmelCase : Any = self.image_processor def __call__( self , a__=None , a__=None , a__=None , **a__ ): if text is None and images is None: raise ValueError("""You have to specify either text or images. Both cannot be none.""" ) if text is not None: _lowerCAmelCase : List[str] = self.tokenizer(a__ , return_tensors=a__ , **a__ ) if images is not None: _lowerCAmelCase : str = self.image_processor(a__ , return_tensors=a__ , **a__ ) if text is not None and images is not None: _lowerCAmelCase : int = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a__ ) , tensor_type=a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.batch_decode(*a__ , **a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.decode(*a__ , **a__ ) @property def __A ( self ): _lowerCAmelCase : Any = self.tokenizer.model_input_names _lowerCAmelCase : Tuple = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self ): warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , a__ , ) return self.image_processor_class
663
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Tuple = 13 _lowerCAmelCase : Tuple = 7 _lowerCAmelCase : Any = 30 _lowerCAmelCase : Optional[int] = self.seq_length + self.mem_len _lowerCAmelCase : Dict = 15 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : Any = True _lowerCAmelCase : List[str] = 99 _lowerCAmelCase : List[Any] = [10, 50, 80] _lowerCAmelCase : Tuple = 32 _lowerCAmelCase : int = 32 _lowerCAmelCase : Dict = 4 _lowerCAmelCase : List[str] = 8 _lowerCAmelCase : Tuple = 128 _lowerCAmelCase : Any = 2 _lowerCAmelCase : List[Any] = 2 _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Optional[Any] = 1 _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : List[Any] = 3 _lowerCAmelCase : Optional[int] = self.vocab_size - 1 _lowerCAmelCase : Dict = 0.0_1 def __A ( self ): _lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Union[str, Any] = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[int] = TFTransfoXLModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() _lowerCAmelCase : Optional[Any] = {"""input_ids""": input_ids_a, """mems""": mems_a} _lowerCAmelCase , _lowerCAmelCase : List[Any] = model(a__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : int = TFTransfoXLLMHeadModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase : Dict = {"""input_ids""": input_ids_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = model([input_ids_a, mems_a] ).to_tuple() _lowerCAmelCase : Any = {"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFTransfoXLForSequenceClassification(a__ ) _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : Dict = config_and_inputs _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids_a} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : Any = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : str = False _UpperCamelCase : str = False _UpperCamelCase : Tuple = False _UpperCamelCase : Any = False def __A ( self , a__ , a__ , a__ , a__ , a__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLModelTester(self ) _lowerCAmelCase : List[Any] = ConfigTester(self , config_class=a__ , d_embed=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a__ ) def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a__ ) def __A ( self ): _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase : List[Any] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(a__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _lowerCAmelCase : str = model.get_output_embeddings() assert isinstance(a__ , tf.keras.layers.Layer ) _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None else: _lowerCAmelCase : Union[str, Any] = model.get_output_embeddings() assert x is None _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None def __A ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Union[str, Any] = TFTransfoXLModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" ) def __A ( self ): pass @require_tf class __A ( unittest.TestCase ): @unittest.skip("""Skip test until #12651 is resolved.""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" ) # fmt: off _lowerCAmelCase : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _lowerCAmelCase : List[Any] = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _lowerCAmelCase : Tuple = model.generate(a__ , max_length=200 , do_sample=a__ ) self.assertListEqual(output_ids[0].numpy().tolist() , a__ )
663
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel @require_tf class __A : _UpperCamelCase : List[str] = BlenderbotSmallConfig _UpperCamelCase : str = {} _UpperCamelCase : Dict = "gelu" def __init__( self , a__ , a__=13 , a__=7 , a__=True , a__=False , a__=99 , a__=32 , a__=2 , a__=4 , a__=37 , a__=0.1 , a__=0.1 , a__=20 , a__=2 , a__=1 , a__=0 , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Dict = batch_size _lowerCAmelCase : Any = seq_length _lowerCAmelCase : List[str] = is_training _lowerCAmelCase : Tuple = use_labels _lowerCAmelCase : List[Any] = vocab_size _lowerCAmelCase : str = hidden_size _lowerCAmelCase : Any = num_hidden_layers _lowerCAmelCase : Optional[int] = num_attention_heads _lowerCAmelCase : Any = intermediate_size _lowerCAmelCase : List[str] = hidden_dropout_prob _lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob _lowerCAmelCase : Any = max_position_embeddings _lowerCAmelCase : Optional[int] = eos_token_id _lowerCAmelCase : str = pad_token_id _lowerCAmelCase : int = bos_token_id def __A ( self ): _lowerCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) _lowerCAmelCase : str = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) _lowerCAmelCase : Optional[int] = tf.concat([input_ids, eos_tensor] , axis=1 ) _lowerCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Tuple = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) _lowerCAmelCase : Dict = prepare_blenderbot_small_inputs_dict(a__ , a__ , a__ ) return config, inputs_dict def __A ( self , a__ , a__ ): _lowerCAmelCase : Optional[Any] = TFBlenderbotSmallModel(config=a__ ).get_decoder() _lowerCAmelCase : List[str] = inputs_dict["""input_ids"""] _lowerCAmelCase : List[Any] = input_ids[:1, :] _lowerCAmelCase : Union[str, Any] = inputs_dict["""attention_mask"""][:1, :] _lowerCAmelCase : str = inputs_dict["""head_mask"""] _lowerCAmelCase : List[Any] = 1 # first forward pass _lowerCAmelCase : str = model(a__ , attention_mask=a__ , head_mask=a__ , use_cache=a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _lowerCAmelCase : int = ids_tensor((self.batch_size, 3) , config.vocab_size ) _lowerCAmelCase : Union[str, Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and _lowerCAmelCase : int = tf.concat([input_ids, next_tokens] , axis=-1 ) _lowerCAmelCase : Dict = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) _lowerCAmelCase : Any = model(a__ , attention_mask=a__ )[0] _lowerCAmelCase : Tuple = model(a__ , attention_mask=a__ , past_key_values=a__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice _lowerCAmelCase : str = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) _lowerCAmelCase : str = output_from_no_past[:, -3:, random_slice_idx] _lowerCAmelCase : Union[str, Any] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(a__ , a__ , rtol=1e-3 ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : str ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Any=None ,_lowerCamelCase : Tuple=None ,_lowerCamelCase : Tuple=None ,_lowerCamelCase : List[str]=None ,_lowerCamelCase : Tuple=None ,) -> Optional[int]: if attention_mask is None: _lowerCAmelCase : int = tf.cast(tf.math.not_equal(_lowerCamelCase ,config.pad_token_id ) ,tf.inta ) if decoder_attention_mask is None: _lowerCAmelCase : Tuple = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape ,dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] ,config.pad_token_id ) ,tf.inta ), ] ,axis=-1 ,) if head_mask is None: _lowerCAmelCase : Tuple = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _lowerCAmelCase : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _lowerCAmelCase : Union[str, Any] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else () ) _UpperCamelCase : Dict = (TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else () _UpperCamelCase : Optional[Any] = ( { "conversational": TFBlenderbotSmallForConditionalGeneration, "feature-extraction": TFBlenderbotSmallModel, "summarization": TFBlenderbotSmallForConditionalGeneration, "text2text-generation": TFBlenderbotSmallForConditionalGeneration, "translation": TFBlenderbotSmallForConditionalGeneration, } if is_tf_available() else {} ) _UpperCamelCase : List[str] = True _UpperCamelCase : List[Any] = False _UpperCamelCase : Optional[int] = False def __A ( self ): _lowerCAmelCase : str = TFBlenderbotSmallModelTester(self ) _lowerCAmelCase : int = ConfigTester(self , config_class=a__ ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): _lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*a__ ) @require_tokenizers @require_tf class __A ( unittest.TestCase ): _UpperCamelCase : List[Any] = [ "Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like " " i'm going to throw up.\nand why is that?" ] _UpperCamelCase : Union[str, Any] = "facebook/blenderbot_small-90M" @cached_property def __A ( self ): # use "old" tokenizer here because of bug when downloading new tokenizer return BlenderbotSmallTokenizer.from_pretrained("""facebook/blenderbot-90M""" ) @cached_property def __A ( self ): _lowerCAmelCase : Any = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def __A ( self ): _lowerCAmelCase : Any = self.tokenizer(self.src_text , return_tensors="""tf""" ) _lowerCAmelCase : Tuple = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=a__ , ) _lowerCAmelCase : str = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=a__ )[0] assert generated_words in ( "i don't know. i just feel like i'm going to throw up. it's not fun.", "i'm not sure. i just feel like i've been feeling like i have to be in a certain place", "i'm not sure. i just feel like i've been in a bad situation.", )
663
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _a : str = logging.get_logger(__name__) _a : Union[str, Any] = { 'facebook/data2vec-text-base': 'https://huggingface.co/data2vec/resolve/main/config.json', } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Tuple = "data2vec-text" def __init__( self , a__=30522 , a__=768 , a__=12 , a__=12 , a__=3072 , a__="gelu" , a__=0.1 , a__=0.1 , a__=512 , a__=2 , a__=0.0_2 , a__=1e-12 , a__=1 , a__=0 , a__=2 , a__="absolute" , a__=True , a__=None , **a__ , ): super().__init__(pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ , **a__ ) _lowerCAmelCase : Optional[int] = vocab_size _lowerCAmelCase : List[str] = hidden_size _lowerCAmelCase : Any = num_hidden_layers _lowerCAmelCase : List[str] = num_attention_heads _lowerCAmelCase : Any = hidden_act _lowerCAmelCase : List[str] = intermediate_size _lowerCAmelCase : Union[str, Any] = hidden_dropout_prob _lowerCAmelCase : List[str] = attention_probs_dropout_prob _lowerCAmelCase : Optional[int] = max_position_embeddings _lowerCAmelCase : Tuple = type_vocab_size _lowerCAmelCase : int = initializer_range _lowerCAmelCase : List[str] = layer_norm_eps _lowerCAmelCase : Optional[Any] = position_embedding_type _lowerCAmelCase : Dict = use_cache _lowerCAmelCase : int = classifier_dropout class __A ( SCREAMING_SNAKE_CASE_ ): @property def __A ( self ): if self.task == "multiple-choice": _lowerCAmelCase : int = {0: """batch""", 1: """choice""", 2: """sequence"""} else: _lowerCAmelCase : str = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : Dict ,_lowerCamelCase : Union[str, Any] ) -> Tuple: # Initialise PyTorch model _lowerCAmelCase : str = RemBertConfig.from_json_file(_lowerCamelCase ) print("""Building PyTorch model from configuration: {}""".format(str(_lowerCamelCase ) ) ) _lowerCAmelCase : Optional[Any] = RemBertModel(_lowerCamelCase ) # Load weights from tf checkpoint load_tf_weights_in_rembert(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # Save pytorch-model print("""Save PyTorch model to {}""".format(_lowerCamelCase ) ) torch.save(model.state_dict() ,_lowerCamelCase ) if __name__ == "__main__": _a : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--rembert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained RemBERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) _a : int = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
663
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = DiTPipeline _UpperCamelCase : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS _UpperCamelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } _UpperCamelCase : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS _UpperCamelCase : Union[str, Any] = False def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=a__ , activation_fn="""gelu-approximate""" , num_embeds_ada_norm=1000 , norm_type="""ada_norm_zero""" , norm_elementwise_affine=a__ , ) _lowerCAmelCase : Optional[int] = AutoencoderKL() _lowerCAmelCase : Union[str, Any] = DDIMScheduler() _lowerCAmelCase : Optional[Any] = {"""transformer""": transformer.eval(), """vae""": vae.eval(), """scheduler""": scheduler} return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Any = torch.manual_seed(a__ ) else: _lowerCAmelCase : Tuple = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Any = { """class_labels""": [1], """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : Tuple = self.get_dummy_components() _lowerCAmelCase : Optional[int] = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Tuple = self.get_dummy_inputs(a__ ) _lowerCAmelCase : List[str] = pipe(**a__ ).images _lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) _lowerCAmelCase : List[Any] = np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7] ) _lowerCAmelCase : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1e-3 ) def __A ( self ): self._test_inference_batch_single_identical(relax_max_difference=a__ , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __A ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): def __A ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : List[str] = torch.manual_seed(0 ) _lowerCAmelCase : int = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-256""" ) pipe.to("""cuda""" ) _lowerCAmelCase : Dict = ["""vase""", """umbrella""", """white shark""", """white wolf"""] _lowerCAmelCase : Union[str, Any] = pipe.get_label_ids(a__ ) _lowerCAmelCase : Any = pipe(a__ , generator=a__ , num_inference_steps=40 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( F"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1e-2 def __A ( self ): _lowerCAmelCase : str = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-512""" ) _lowerCAmelCase : Dict = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("""cuda""" ) _lowerCAmelCase : List[str] = ["""vase""", """umbrella"""] _lowerCAmelCase : Optional[int] = pipe.get_label_ids(a__ ) _lowerCAmelCase : str = torch.manual_seed(0 ) _lowerCAmelCase : List[str] = pipe(a__ , generator=a__ , num_inference_steps=25 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" F"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1e-1
663
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class __A : _UpperCamelCase : Optional[int] = PegasusConfig _UpperCamelCase : Dict = {} _UpperCamelCase : Optional[Any] = "gelu" def __init__( self , a__ , a__=13 , a__=7 , a__=True , a__=False , a__=99 , a__=32 , a__=2 , a__=4 , a__=37 , a__=0.1 , a__=0.1 , a__=40 , a__=2 , a__=1 , a__=0 , ): _lowerCAmelCase : Union[str, Any] = parent _lowerCAmelCase : Optional[int] = batch_size _lowerCAmelCase : List[Any] = seq_length _lowerCAmelCase : str = is_training _lowerCAmelCase : Dict = use_labels _lowerCAmelCase : List[str] = vocab_size _lowerCAmelCase : List[str] = hidden_size _lowerCAmelCase : int = num_hidden_layers _lowerCAmelCase : Dict = num_attention_heads _lowerCAmelCase : int = intermediate_size _lowerCAmelCase : Any = hidden_dropout_prob _lowerCAmelCase : Optional[Any] = attention_probs_dropout_prob _lowerCAmelCase : int = max_position_embeddings _lowerCAmelCase : int = eos_token_id _lowerCAmelCase : Optional[Any] = pad_token_id _lowerCAmelCase : Dict = bos_token_id def __A ( self ): _lowerCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) _lowerCAmelCase : int = tf.concat([input_ids, eos_tensor] , axis=1 ) _lowerCAmelCase : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Any = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) _lowerCAmelCase : str = prepare_pegasus_inputs_dict(a__ , a__ , a__ ) return config, inputs_dict def __A ( self , a__ , a__ ): _lowerCAmelCase : Union[str, Any] = TFPegasusModel(config=a__ ).get_decoder() _lowerCAmelCase : Optional[int] = inputs_dict["""input_ids"""] _lowerCAmelCase : Union[str, Any] = input_ids[:1, :] _lowerCAmelCase : Optional[Any] = inputs_dict["""attention_mask"""][:1, :] _lowerCAmelCase : Dict = inputs_dict["""head_mask"""] _lowerCAmelCase : Any = 1 # first forward pass _lowerCAmelCase : List[str] = model(a__ , attention_mask=a__ , head_mask=a__ , use_cache=a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _lowerCAmelCase : int = ids_tensor((self.batch_size, 3) , config.vocab_size ) _lowerCAmelCase : str = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and _lowerCAmelCase : Optional[int] = tf.concat([input_ids, next_tokens] , axis=-1 ) _lowerCAmelCase : List[Any] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) _lowerCAmelCase : Any = model(a__ , attention_mask=a__ )[0] _lowerCAmelCase : Tuple = model(a__ , attention_mask=a__ , past_key_values=a__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice _lowerCAmelCase : str = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) _lowerCAmelCase : Union[str, Any] = output_from_no_past[:, -3:, random_slice_idx] _lowerCAmelCase : Tuple = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(a__ , a__ , rtol=1e-3 ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ,_lowerCamelCase : Optional[Any]=None ,_lowerCamelCase : List[Any]=None ,_lowerCamelCase : Any=None ,_lowerCamelCase : List[str]=None ,_lowerCamelCase : str=None ,) -> int: if attention_mask is None: _lowerCAmelCase : Tuple = tf.cast(tf.math.not_equal(_lowerCamelCase ,config.pad_token_id ) ,tf.inta ) if decoder_attention_mask is None: _lowerCAmelCase : List[str] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape ,dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] ,config.pad_token_id ) ,tf.inta ), ] ,axis=-1 ,) if head_mask is None: _lowerCAmelCase : Union[str, Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _lowerCAmelCase : int = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _lowerCAmelCase : int = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = (TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () _UpperCamelCase : List[Any] = (TFPegasusForConditionalGeneration,) if is_tf_available() else () _UpperCamelCase : Optional[Any] = ( { "conversational": TFPegasusForConditionalGeneration, "feature-extraction": TFPegasusModel, "summarization": TFPegasusForConditionalGeneration, "text2text-generation": TFPegasusForConditionalGeneration, "translation": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) _UpperCamelCase : List[Any] = True _UpperCamelCase : Optional[Any] = False _UpperCamelCase : Optional[Any] = False def __A ( self ): _lowerCAmelCase : int = TFPegasusModelTester(self ) _lowerCAmelCase : str = ConfigTester(self , config_class=a__ ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): _lowerCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*a__ ) @require_sentencepiece @require_tokenizers @require_tf class __A ( unittest.TestCase ): _UpperCamelCase : int = [ " PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.", " The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" ", ] _UpperCamelCase : str = [ "California's largest electricity provider has cut power to hundreds of thousands of customers in an effort to" " reduce the risk of wildfires.", "N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.", ] # differs slightly from pytorch, likely due to numerical differences in linear layers _UpperCamelCase : Tuple = "google/pegasus-xsum" @cached_property def __A ( self ): return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __A ( self ): _lowerCAmelCase : Optional[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __A ( self , **a__ ): _lowerCAmelCase : Optional[Any] = self.translate_src_text(**a__ ) assert self.expected_text == generated_words def __A ( self , **a__ ): _lowerCAmelCase : Optional[int] = self.tokenizer(self.src_text , **a__ , padding=a__ , return_tensors="""tf""" ) _lowerCAmelCase : List[str] = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=a__ , ) _lowerCAmelCase : int = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=a__ ) return generated_words @slow def __A ( self ): self._assert_generated_batch_equal_expected()
663
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _a : Tuple = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , *a__ , **a__ ): warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , a__ , ) super().__init__(*a__ , **a__ )
663
1
"""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 _a : Dict = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. _a : str = direct_transformers_import(PATH_TO_TRANSFORMERS) _a : Any = transformers.models.auto.configuration_auto.CONFIG_MAPPING _a : Union[str, 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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ,_lowerCamelCase : Any ,_lowerCamelCase : str ,_lowerCamelCase : Optional[int] ) -> int: _lowerCAmelCase : Optional[int] = 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 ): _lowerCAmelCase : List[Any] = 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}\"" ,_lowerCamelCase ,) is not None ): _lowerCAmelCase : List[str] = 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: _lowerCAmelCase : Optional[Any] = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files _lowerCAmelCase : Tuple = [ """bos_index""", """eos_index""", """pad_index""", """unk_index""", """mask_index""", """image_size""", """use_cache""", """out_features""", """out_indices""", ] _lowerCAmelCase : int = ["""encoder_no_repeat_ngram_size"""] # Special cases to be allowed _lowerCAmelCase : Union[str, Any] = True if not attribute_used: _lowerCAmelCase : int = 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: _lowerCAmelCase : Dict = True elif attribute in ["tie_word_embeddings"] and default_value is False: _lowerCAmelCase : Dict = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: _lowerCAmelCase : Optional[int] = True elif attribute.endswith("""_token_id""" ): _lowerCAmelCase : List[str] = True # configuration class specific cases if not case_allowed: _lowerCAmelCase : List[Any] = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ ,[] ) _lowerCAmelCase : Union[str, Any] = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> Tuple: _lowerCAmelCase : Dict = dict(inspect.signature(config_class.__init__ ).parameters ) _lowerCAmelCase : List[str] = [x for x in list(signature.keys() ) if x not in ["""self""", """kwargs"""]] _lowerCAmelCase : Union[str, Any] = [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 _lowerCAmelCase : str = {} if len(config_class.attribute_map ) > 0: _lowerCAmelCase : int = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files _lowerCAmelCase : int = inspect.getsourcefile(_lowerCamelCase ) _lowerCAmelCase : Dict = os.path.dirname(_lowerCamelCase ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. _lowerCAmelCase : Union[str, Any] = [os.path.join(_lowerCamelCase ,_lowerCamelCase ) for fn in os.listdir(_lowerCamelCase ) if fn.startswith("""modeling_""" )] # Get the source code strings _lowerCAmelCase : int = [] for path in modeling_paths: if os.path.isfile(_lowerCamelCase ): with open(_lowerCamelCase ) as fp: modeling_sources.append(fp.read() ) _lowerCAmelCase : Dict = [] for config_param, default_value in zip(_lowerCamelCase ,_lowerCamelCase ): # `attributes` here is all the variant names for `config_param` _lowerCAmelCase : Union[str, Any] = [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(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ): unused_attributes.append(attributes[0] ) return sorted(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> List[str]: _lowerCAmelCase : List[str] = {} 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.) _lowerCAmelCase : Optional[Any] = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) ,lambda _lowerCamelCase : inspect.isclass(_lowerCamelCase ) and issubclass(_lowerCamelCase ,_lowerCamelCase ) and inspect.getmodule(_lowerCamelCase ) == inspect.getmodule(_config_class ) ,) ] for config_class in config_classes_in_module: _lowerCAmelCase : Union[str, Any] = check_config_attributes_being_used(_lowerCamelCase ) if len(_lowerCamelCase ) > 0: _lowerCAmelCase : List[Any] = unused_attributes if len(_lowerCamelCase ) > 0: _lowerCAmelCase : Tuple = """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(_lowerCamelCase ) if __name__ == "__main__": check_config_attributes()
663
"""simple docstring""" import argparse import json import subprocess def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Any ) -> List[Any]: _lowerCAmelCase : Tuple = [] _lowerCAmelCase : Optional[int] = ( f"curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"" """ https://api.github.com/repos/huggingface/transformers/actions/runners""" ) _lowerCAmelCase : List[str] = subprocess.run(_lowerCamelCase ,shell=_lowerCamelCase ,stdout=subprocess.PIPE ) _lowerCAmelCase : int = output.stdout.decode("""utf-8""" ) _lowerCAmelCase : Tuple = json.loads(_lowerCamelCase ) _lowerCAmelCase : int = status["""runners"""] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_lowerCamelCase ) # save the result so we can report them on Slack with open("""offline_runners.txt""" ,"""w""" ) as fp: fp.write(json.dumps(_lowerCamelCase ) ) if len(_lowerCamelCase ) > 0: _lowerCAmelCase : int = """\n""".join([x["""name"""] for x in offline_runners] ) raise ValueError(f"The following runners are offline:\n{failed}" ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> Optional[int]: return values.split(""",""" ) _a : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--target_runners', default=None, type=list_str, required=True, help='Comma-separated list of runners to check status.', ) parser.add_argument( '--token', default=None, type=str, required=True, help='A token that has actions:read permission.' ) _a : Tuple = parser.parse_args() get_runner_status(args.target_runners, args.token)
663
1
"""simple docstring""" import numpy class __A : def __init__( self , a__ , a__ ): _lowerCAmelCase : int = input_array # Random initial weights are assigned where first argument is the # number of nodes in previous layer and second argument is the # number of nodes in the next layer. # Random initial weights are assigned. # self.input_array.shape[1] is used to represent number of nodes in input layer. # First hidden layer consists of 4 nodes. _lowerCAmelCase : Union[str, Any] = numpy.random.rand( self.input_array.shape[1] , 4 ) # Random initial values for the first hidden layer. # First hidden layer has 4 nodes. # Second hidden layer has 3 nodes. _lowerCAmelCase : List[str] = numpy.random.rand( 4 , 3 ) # Random initial values for the second hidden layer. # Second hidden layer has 3 nodes. # Output layer has 1 node. _lowerCAmelCase : List[Any] = numpy.random.rand(3 , 1 ) # Real output values provided. _lowerCAmelCase : Union[str, Any] = output_array # Predicted output values by the neural network. # Predicted_output array initially consists of zeroes. _lowerCAmelCase : int = numpy.zeros(output_array.shape ) def __A ( self ): _lowerCAmelCase : Dict = sigmoid( numpy.dot(self.input_array , self.input_layer_and_first_hidden_layer_weights ) ) # layer_between_first_hidden_layer_and_second_hidden_layer is the layer # connecting the first hidden set of nodes with the second hidden set of nodes. _lowerCAmelCase : str = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) # layer_between_second_hidden_layer_and_output is the layer connecting # second hidden layer with the output node. _lowerCAmelCase : Tuple = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return self.layer_between_second_hidden_layer_and_output def __A ( self ): _lowerCAmelCase : str = numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer.T , 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , ) _lowerCAmelCase : Tuple = numpy.dot( self.layer_between_input_and_first_hidden_layer.T , numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , ) _lowerCAmelCase : Optional[Any] = numpy.dot( self.input_array.T , numpy.dot( numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output ) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer ) , self.first_hidden_layer_and_second_hidden_layer_weights.T , ) * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer ) , ) self.input_layer_and_first_hidden_layer_weights += ( updated_input_layer_and_first_hidden_layer_weights ) self.first_hidden_layer_and_second_hidden_layer_weights += ( updated_first_hidden_layer_and_second_hidden_layer_weights ) self.second_hidden_layer_and_output_layer_weights += ( updated_second_hidden_layer_and_output_layer_weights ) def __A ( self , a__ , a__ , a__ ): for iteration in range(1 , iterations + 1 ): _lowerCAmelCase : Any = self.feedforward() self.back_propagation() if give_loss: _lowerCAmelCase : Union[str, Any] = numpy.mean(numpy.square(output - self.feedforward() ) ) print(F"Iteration {iteration} Loss: {loss}" ) def __A ( self , a__ ): _lowerCAmelCase : str = input_arr _lowerCAmelCase : List[Any] = sigmoid( numpy.dot(self.array , self.input_layer_and_first_hidden_layer_weights ) ) _lowerCAmelCase : Tuple = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , ) ) _lowerCAmelCase : Optional[int] = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , ) ) return int(self.layer_between_second_hidden_layer_and_output > 0.6 ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : numpy.ndarray ) -> numpy.ndarray: return 1 / (1 + numpy.exp(-value )) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : numpy.ndarray ) -> numpy.ndarray: return (value) * (1 - (value)) def SCREAMING_SNAKE_CASE ( ) -> int: _lowerCAmelCase : List[Any] = numpy.array( ( [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], ) ,dtype=numpy.floataa ,) # True output values for the given input values. _lowerCAmelCase : Optional[int] = numpy.array(([0], [1], [1], [0], [1], [0], [0], [1]) ,dtype=numpy.floataa ) # Calling neural network class. _lowerCAmelCase : str = TwoHiddenLayerNeuralNetwork( input_array=_lowerCamelCase ,output_array=_lowerCamelCase ) # Calling training function. # Set give_loss to True if you want to see loss in every iteration. neural_network.train(output=_lowerCamelCase ,iterations=10 ,give_loss=_lowerCamelCase ) return neural_network.predict(numpy.array(([1, 1, 1]) ,dtype=numpy.floataa ) ) if __name__ == "__main__": example()
663
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( ) -> int: return 1 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else five_pence(x - 5 ) + two_pence(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else ten_pence(x - 10 ) + five_pence(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else twenty_pence(x - 20 ) + ten_pence(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else fifty_pence(x - 50 ) + twenty_pence(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else one_pound(x - 100 ) + fifty_pence(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: return 0 if x < 0 else two_pound(x - 200 ) + one_pound(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 200 ) -> int: return two_pound(_lowerCamelCase ) if __name__ == "__main__": print(solution(int(input().strip())))
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ) -> int: _lowerCAmelCase : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 ,limit + 1 ): if phi[i] == i - 1: for j in range(2 * i ,limit + 1 ,_lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
663
1
"""simple docstring""" from __future__ import annotations def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ,_lowerCamelCase : float ,_lowerCamelCase : float ) -> dict[str, float]: 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()
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Tuple = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[Any] = WavaVecaPhonemeCTCTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Tuple = ( """<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː """ """ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː """ """ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 """ """oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ """ """pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ """ """yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ """ """əʊ S ɡʲ onɡ2 u\" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ """ """ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ """ """ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ """ """uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ """ """ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ """ """ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ """ """ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4""" ).split(""" """ ) _lowerCAmelCase : Any = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : int = {"""pad_token""": """<pad>""", """unk_token""": """<unk>""", """bos_token""": """<s>""", """eos_token""": """</s>"""} _lowerCAmelCase : Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) def __A ( self , a__ , a__=False , a__=20 , a__=5 ): _lowerCAmelCase : Tuple = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=a__ )) for i in range(len(a__ ) )] _lowerCAmelCase : List[Any] = list(filter(lambda a__ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=a__ ) , a__ ) ) if max_length is not None and len(a__ ) > max_length: _lowerCAmelCase : Optional[Any] = toks[:max_length] if min_length is not None and len(a__ ) < min_length and len(a__ ) > 0: while len(a__ ) < min_length: _lowerCAmelCase : List[str] = toks + toks # toks_str = [t[1] for t in toks] _lowerCAmelCase : Any = [t[0] for t in toks] # Ensure consistency _lowerCAmelCase : str = tokenizer.decode(a__ , clean_up_tokenization_spaces=a__ ) if " " not in output_txt and len(a__ ) > 1: _lowerCAmelCase : int = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=a__ ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=a__ ) ) if with_prefix_space: _lowerCAmelCase : Dict = """ """ + output_txt _lowerCAmelCase : int = tokenizer.encode(a__ , add_special_tokens=a__ ) return output_txt, output_ids def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self ): _lowerCAmelCase : List[Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) # check adding a single token tokenizer.add_tokens("""xxx""" ) _lowerCAmelCase : List[str] = tokenizer("""m xxx ɪ""" , do_phonemize=a__ ).input_ids self.assertEqual(a__ , [13, 392, 17] ) # xxx should be last token tokenizer.add_tokens(["""aaa""", """bbb""", """ccc"""] ) _lowerCAmelCase : Dict = tokenizer("""m aaa ɪ ccc""" , do_phonemize=a__ ).input_ids self.assertEqual(a__ , [13, 393, 17, 395] ) # aaa and ccc should be after xxx and 2 after aaa _lowerCAmelCase : Any = tokenizer("""maɪ c""" , do_phonemize=a__ ).input_ids self.assertEqual(a__ , [3, 200] ) # mai should be <unk> (=3) def __A ( self ): _lowerCAmelCase : str = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _lowerCAmelCase : Any = """Hello how are you""" _lowerCAmelCase : str = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) self.assertEqual(a__ , """h ə l oʊ h aʊ ɑːɹ j uː""" ) def __A ( self ): _lowerCAmelCase : List[str] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _lowerCAmelCase : Union[str, Any] = """Hello how are you""" _lowerCAmelCase : str = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(a__ ).input_ids , tokenizer(a__ , do_phonemize=a__ ).input_ids ) def __A ( self ): _lowerCAmelCase : int = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _lowerCAmelCase : Dict = """Hello how are you""" _lowerCAmelCase : Dict = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(tokenizer(a__ ).input_ids ) self.assertEqual(a__ , a__ ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _lowerCAmelCase : Union[str, Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] _lowerCAmelCase : Tuple = tokenizer.decode(sample_ids[0] ) _lowerCAmelCase : Any = tokenizer.batch_decode(a__ ) self.assertEqual(a__ , batch_tokens[0] ) self.assertEqual(a__ , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) def __A ( self ): _lowerCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _lowerCAmelCase : str = """Hello how are you""" _lowerCAmelCase : Optional[int] = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) self.assertEqual(a__ , """h ə l oʊ | h aʊ | ɑːɹ | j uː |""" ) def __A ( self ): _lowerCAmelCase : Any = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _lowerCAmelCase : int = """Hello how are you""" _lowerCAmelCase : List[Any] = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(a__ ).input_ids , tokenizer(a__ , do_phonemize=a__ ).input_ids ) def __A ( self ): _lowerCAmelCase : List[str] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off _lowerCAmelCase : List[str] = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter _lowerCAmelCase : List[str] = tokenizer.decode(sample_ids[0] ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(a__ ) self.assertEqual(a__ , batch_tokens[0] ) self.assertEqual(a__ , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) # decode with no word_del_token filter _lowerCAmelCase : List[Any] = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=a__ ) _lowerCAmelCase : List[str] = tokenizer.batch_decode(a__ , filter_word_delimiter_token=a__ ) self.assertEqual(a__ , batch_tokens[0] ) self.assertEqual(a__ , ["""k s ɾ | ɾ l | ɭʲ""", """| j ð | s j ð s oːɹ"""] ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _lowerCAmelCase : Tuple = """Hello how are you""" _lowerCAmelCase : Dict = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) _lowerCAmelCase : Optional[int] = tokenizer.decode(tokenizer(a__ ).input_ids , filter_word_delimiter_token=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): _lowerCAmelCase : Dict = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _lowerCAmelCase : Optional[int] = """Hello how are you""" _lowerCAmelCase : Any = tokenizer.phonemize(a__ , phonemizer_lang="""en-us""" ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(tokenizer(a__ ).input_ids , filter_word_delimiter_token=a__ ) self.assertEqual(""" """.join([p.strip() for p in phonemes.split(""" |""" )] ).strip() , a__ ) def __A ( self ): _lowerCAmelCase : str = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token=a__ ) _lowerCAmelCase : Tuple = """Hello how are you""" _lowerCAmelCase : Union[str, Any] = tokenizer(a__ , phonemizer_lang="""en-us""" ).input_ids _lowerCAmelCase : Union[str, Any] = tokenizer(a__ , phonemizer_lang="""fr-fr""" ).input_ids self.assertNotEqual(a__ , a__ ) _lowerCAmelCase : Dict = tokenizer.decode(a__ ) _lowerCAmelCase : Dict = tokenizer.decode(a__ ) self.assertEqual(a__ , """h ə l oʊ h aʊ ɑːɹ j uː""" ) self.assertEqual(a__ , """ɛ l o h aʊ a ʁ j u""" ) def __A ( self ): _lowerCAmelCase : Any = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _lowerCAmelCase : Union[str, Any] = """Hello how Are you""" _lowerCAmelCase : Optional[Any] = """hello how are you""" _lowerCAmelCase : List[str] = tokenizer(a__ ).input_ids _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ).input_ids self.assertEqual(a__ , a__ ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) tokenizer.add_tokens(["""!""", """?"""] ) tokenizer.add_special_tokens({"""cls_token""": """$$$"""} ) # fmt: off _lowerCAmelCase : str = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 392, 392, 393, 392, 392, 393, 394, 394], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 394, 394], ] # fmt: on _lowerCAmelCase : Tuple = tokenizer.batch_decode(a__ ) self.assertEqual(a__ , ["""k s ɾ ɾ l ɭʲ!?!? $$$""", """j ð s j ð s oːɹ $$$"""] ) @staticmethod def __A ( a__ , a__ ): _lowerCAmelCase : List[str] = [d[key] for d in offsets] return retrieved_list def __A ( self ): _lowerCAmelCase : List[Any] = self.get_tokenizer(word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" _lowerCAmelCase : Union[str, Any] = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on _lowerCAmelCase : Tuple = tokenizer.decode(a__ , output_char_offsets=a__ , filter_word_delimiter_token=a__ ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue("""text""" in outputs ) self.assertTrue("""char_offsets""" in outputs ) self.assertTrue(isinstance(a__ , a__ ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(""" """.join(self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) , ["""k""", """s""", """ɾ""", """ɾ""", """|""", """ɾ""", """l""", """|""", """ɭʲ"""] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """start_offset""" ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """end_offset""" ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def __A ( self ): _lowerCAmelCase : int = self.get_tokenizer(word_delimiter_token="""|""" ) def check_list_tuples_equal(a__ , a__ ): self.assertTrue(isinstance(a__ , a__ ) ) self.assertTrue(isinstance(outputs_list[0] , a__ ) ) # transform list to ModelOutput _lowerCAmelCase : List[Any] = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch["""text"""] , outputs_batch_a["""text"""] ) def recursive_check(a__ , a__ ): if isinstance(a__ , a__ ): [recursive_check(a__ , a__ ) for la, la in zip(a__ , a__ )] self.assertEqual(a__ , a__ ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch["""char_offsets"""] , outputs_batch_a["""char_offsets"""] ) # fmt: off _lowerCAmelCase : Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char _lowerCAmelCase : str = tokenizer.batch_decode(a__ , output_char_offsets=a__ ) _lowerCAmelCase : List[Any] = [tokenizer.decode(a__ , output_char_offsets=a__ ) for ids in sample_ids] check_list_tuples_equal(a__ , a__ ) @unittest.skip("""Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes""" ) def __A ( self ): pass @unittest.skip("""Wav2Vec2PhonemeTokenizer always puts spaces between phonemes""" ) def __A ( self ): pass @unittest.skip("""encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency""" ) def __A ( self ): pass @unittest.skip("""Wav2Vec2PhonemeModel has no max model length => no testing""" ) def __A ( self ): pass def __A ( self ): _lowerCAmelCase : Dict = self.get_tokenizers(do_lower_case=a__ ) for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}" ): _lowerCAmelCase : Dict = tokenizer.vocab_size _lowerCAmelCase : List[str] = len(a__ ) self.assertNotEqual(a__ , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) _lowerCAmelCase : Tuple = ["""aaaaa bbbbbb""", """cccccccccdddddddd"""] _lowerCAmelCase : List[Any] = tokenizer.add_tokens(a__ ) _lowerCAmelCase : Optional[Any] = tokenizer.vocab_size _lowerCAmelCase : Optional[Any] = len(a__ ) self.assertNotEqual(a__ , 0 ) self.assertEqual(a__ , a__ ) self.assertEqual(a__ , len(a__ ) ) self.assertEqual(a__ , all_size + len(a__ ) ) _lowerCAmelCase : str = tokenizer.encode("""aaaaa bbbbbb low cccccccccdddddddd l""" , add_special_tokens=a__ ) self.assertGreaterEqual(len(a__ ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) _lowerCAmelCase : str = {"""eos_token""": """>>>>|||<||<<|<<""", """pad_token""": """<<<<<|||>|>>>>|>"""} _lowerCAmelCase : Dict = tokenizer.add_special_tokens(a__ ) _lowerCAmelCase : int = tokenizer.vocab_size _lowerCAmelCase : Any = len(a__ ) self.assertNotEqual(a__ , 0 ) self.assertEqual(a__ , a__ ) self.assertEqual(a__ , len(a__ ) ) self.assertEqual(a__ , all_size_a + len(a__ ) ) _lowerCAmelCase : Any = tokenizer.encode( """>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l""" , add_special_tokens=a__ ) self.assertGreaterEqual(len(a__ ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def __A ( self ): pass @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def __A ( self ): pass def __A ( self ): # The default common tokenizer tests assumes that the output of `convert_tokens_to_string` is a string which # is not the case for Wav2Vec2PhonemeCTCTokenizer. _lowerCAmelCase : List[str] = self.get_tokenizers(fast=a__ , do_lower_case=a__ ) for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}" ): _lowerCAmelCase : Tuple = ["""ð""", """ɪ""", """s""", """ɪ""", """z""", """ɐ""", """t""", """ɛ""", """k""", """s""", """t"""] _lowerCAmelCase : Tuple = tokenizer.convert_tokens_to_string(a__ ) self.assertIsInstance(output["""text"""] , a__ )
663
"""simple docstring""" from PIL import Image def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Image ,_lowerCamelCase : int ) -> Image: _lowerCAmelCase : Any = (259 * (level + 255)) / (255 * (259 - level)) def contrast(_lowerCamelCase : int ) -> int: return int(128 + factor * (c - 128) ) return img.point(_lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 _a : str = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
663
1
"""simple docstring""" import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Any = OpenAIGPTTokenizer _UpperCamelCase : Tuple = OpenAIGPTTokenizerFast _UpperCamelCase : List[Any] = True _UpperCamelCase : Tuple = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] _lowerCAmelCase : Union[str, Any] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : int = ["""#version: 0.2""", """l o""", """lo w""", """e r</w>""", """"""] _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" ) as fp: fp.write(json.dumps(a__ ) ) with open(self.merges_file , """w""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , a__ ): return "lower newer", "lower newer" def __A ( self ): _lowerCAmelCase : Optional[Any] = OpenAIGPTTokenizer(self.vocab_file , self.merges_file ) _lowerCAmelCase : Optional[int] = """lower""" _lowerCAmelCase : Optional[Any] = ["""low""", """er</w>"""] _lowerCAmelCase : Union[str, Any] = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : int = tokens + ["""<unk>"""] _lowerCAmelCase : List[Any] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : Any = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Tuple = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[Any] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Any = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): pass @require_ftfy @require_spacy @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ ): pass
663
"""simple docstring""" class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A : def __init__( self ): _lowerCAmelCase : Union[str, Any] = [ [], [], [], ] def __A ( self , a__ , a__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(a__ ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def __A ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self ): return "\n".join(F"Priority {i}: {q}" for i, q in enumerate(self.queues ) ) class __A : def __init__( self ): _lowerCAmelCase : int = [] def __A ( self , a__ ): if len(self.queue ) == 100: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(a__ ) def __A ( self ): if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: _lowerCAmelCase : int = min(self.queue ) self.queue.remove(a__ ) return data def __str__( self ): return str(self.queue ) def SCREAMING_SNAKE_CASE ( ) -> str: _lowerCAmelCase : Union[str, Any] = FixedPriorityQueue() fpq.enqueue(0 ,10 ) fpq.enqueue(1 ,70 ) fpq.enqueue(0 ,100 ) fpq.enqueue(2 ,1 ) fpq.enqueue(2 ,5 ) fpq.enqueue(1 ,7 ) fpq.enqueue(2 ,4 ) fpq.enqueue(1 ,64 ) fpq.enqueue(0 ,128 ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: _lowerCAmelCase : Tuple = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(100 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(128 ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
663
1
"""simple docstring""" from __future__ import annotations import os from collections.abc import Mapping _a : Union[str, Any] = tuple[int, int] class __A : def __init__( self , a__ , a__ ): _lowerCAmelCase : set[int] = vertices _lowerCAmelCase : dict[EdgeT, int] = { (min(a__ ), max(a__ )): weight for edge, weight in edges.items() } def __A ( self , a__ , a__ ): self.vertices.add(edge[0] ) self.vertices.add(edge[1] ) _lowerCAmelCase : Optional[Any] = weight def __A ( self ): _lowerCAmelCase : Graph = Graph({min(self.vertices )} , {} ) _lowerCAmelCase : EdgeT _lowerCAmelCase : int _lowerCAmelCase : EdgeT _lowerCAmelCase : int while len(subgraph.vertices ) < len(self.vertices ): _lowerCAmelCase : Optional[int] = max(self.edges.values() ) + 1 for edge, weight in self.edges.items(): if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): if weight < min_weight: _lowerCAmelCase : str = edge _lowerCAmelCase : Any = weight subgraph.add_edge(a__ , a__ ) return subgraph def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str = "p107_network.txt" ) -> int: _lowerCAmelCase : str = os.path.abspath(os.path.dirname(_lowerCamelCase ) ) _lowerCAmelCase : str = os.path.join(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : dict[EdgeT, int] = {} _lowerCAmelCase : list[str] _lowerCAmelCase : int _lowerCAmelCase : int with open(_lowerCamelCase ) as f: _lowerCAmelCase : Optional[Any] = f.read().strip().split("""\n""" ) _lowerCAmelCase : int = [line.split(""",""" ) for line in data] for edgea in range(1 ,len(_lowerCamelCase ) ): for edgea in range(_lowerCamelCase ): if adjaceny_matrix[edgea][edgea] != "-": _lowerCAmelCase : Union[str, Any] = int(adjaceny_matrix[edgea][edgea] ) _lowerCAmelCase : Graph = Graph(set(range(len(_lowerCamelCase ) ) ) ,_lowerCamelCase ) _lowerCAmelCase : Graph = graph.prims_algorithm() _lowerCAmelCase : int = sum(graph.edges.values() ) _lowerCAmelCase : int = sum(subgraph.edges.values() ) return initial_total - optimal_total if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __A ( unittest.TestCase ): _UpperCamelCase : str = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING _UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = AudioClassificationPipeline(model=a__ , feature_extractor=a__ ) # test with a raw waveform _lowerCAmelCase : Optional[int] = np.zeros((34000,) ) _lowerCAmelCase : Optional[Any] = np.zeros((14000,) ) return audio_classifier, [audioa, audio] def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = examples _lowerCAmelCase : List[Any] = audio_classifier(a__ ) # by default a model is initialized with num_labels=2 self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) _lowerCAmelCase : Tuple = audio_classifier(a__ , top_k=1 ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) self.run_torchaudio(a__ ) @require_torchaudio def __A ( self , a__ ): import datasets # test with a local file _lowerCAmelCase : int = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) _lowerCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] _lowerCAmelCase : str = audio_classifier(a__ ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) @require_torch def __A ( self ): _lowerCAmelCase : int = """anton-l/wav2vec2-random-tiny-classifier""" _lowerCAmelCase : Optional[Any] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : Any = np.ones((8000,) ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) _lowerCAmelCase : List[str] = [ {"""score""": 0.0_8_4_2, """label""": """no"""}, {"""score""": 0.0_8_3_8, """label""": """up"""}, {"""score""": 0.0_8_3_7, """label""": """go"""}, {"""score""": 0.0_8_3_4, """label""": """right"""}, ] _lowerCAmelCase : str = [ {"""score""": 0.0_8_4_5, """label""": """stop"""}, {"""score""": 0.0_8_4_4, """label""": """on"""}, {"""score""": 0.0_8_4_1, """label""": """right"""}, {"""score""": 0.0_8_3_4, """label""": """left"""}, ] self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) _lowerCAmelCase : int = {"""array""": np.ones((8000,) ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate} _lowerCAmelCase : int = audio_classifier(a__ , top_k=4 ) self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __A ( self ): import datasets _lowerCAmelCase : Optional[Any] = """superb/wav2vec2-base-superb-ks""" _lowerCAmelCase : List[str] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : str = datasets.load_dataset("""anton-l/superb_dummy""" , """ks""" , split="""test""" ) _lowerCAmelCase : Optional[Any] = np.array(dataset[3]["""speech"""] , dtype=np.floataa ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) self.assertEqual( nested_simplify(a__ , decimals=3 ) , [ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] , ) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def __A ( self ): pass
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000 ) -> int: _lowerCAmelCase : int = -1 _lowerCAmelCase : Any = 0 for a in range(1 ,n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c _lowerCAmelCase : int = (n * n - 2 * a * n) // (2 * n - 2 * a) _lowerCAmelCase : Optional[Any] = n - a - b if c * c == (a * a + b * b): _lowerCAmelCase : Tuple = a * b * c if candidate >= product: _lowerCAmelCase : Union[str, Any] = candidate return product if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _a : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _a : int = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ,_lowerCamelCase : Dict=8 ) -> Any: _lowerCAmelCase : List[Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _lowerCAmelCase : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Any=512 ,_lowerCamelCase : Dict=512 ) -> List[Any]: _lowerCAmelCase : Any = pil_image.resize((w, h) ,resample=Image.BICUBIC ,reducing_gap=1 ) _lowerCAmelCase : Dict = np.array(pil_image.convert("""RGB""" ) ) _lowerCAmelCase : List[str] = arr.astype(np.floataa ) / 1_27.5 - 1 _lowerCAmelCase : int = np.transpose(_lowerCamelCase ,[2, 0, 1] ) _lowerCAmelCase : Optional[Any] = torch.from_numpy(_lowerCamelCase ).unsqueeze(0 ) return image class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__ , a__ , ): super().__init__() self.register_modules( unet=a__ , scheduler=a__ , movq=a__ , ) _lowerCAmelCase : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __A ( self , a__ , a__ , a__ ): # get the original timestep using init_timestep _lowerCAmelCase : Optional[Any] = min(int(num_inference_steps * strength ) , a__ ) _lowerCAmelCase : List[Any] = max(num_inference_steps - init_timestep , 0 ) _lowerCAmelCase : Dict = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__=None ): if not isinstance(a__ , (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(a__ )}" ) _lowerCAmelCase : Union[str, Any] = image.to(device=a__ , dtype=a__ ) _lowerCAmelCase : int = batch_size * num_images_per_prompt if image.shape[1] == 4: _lowerCAmelCase : int = image else: if isinstance(a__ , a__ ) and len(a__ ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(a__ )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) elif isinstance(a__ , a__ ): _lowerCAmelCase : Optional[int] = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a__ ) ] _lowerCAmelCase : Optional[int] = torch.cat(a__ , dim=0 ) else: _lowerCAmelCase : List[Any] = self.movq.encode(a__ ).latent_dist.sample(a__ ) _lowerCAmelCase : Dict = self.movq.config.scaling_factor * init_latents _lowerCAmelCase : str = torch.cat([init_latents] , dim=0 ) _lowerCAmelCase : Dict = init_latents.shape _lowerCAmelCase : str = randn_tensor(a__ , generator=a__ , device=a__ , dtype=a__ ) # get latents _lowerCAmelCase : Optional[Any] = self.scheduler.add_noise(a__ , a__ , a__ ) _lowerCAmelCase : int = init_latents return latents def __A ( self , a__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _lowerCAmelCase : str = torch.device(F"cuda:{gpu_id}" ) _lowerCAmelCase : int = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a__ , a__ ) def __A ( self , a__=0 ): if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) _lowerCAmelCase : Optional[int] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=a__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _lowerCAmelCase : List[str] = None for cpu_offloaded_model in [self.unet, self.movq]: _lowerCAmelCase , _lowerCAmelCase : str = cpu_offload_with_hook(a__ , a__ , prev_module_hook=a__ ) # We'll offload the last model manually. _lowerCAmelCase : Tuple = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A ( self ): if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(a__ , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a__ ) def __call__( self , a__ , a__ , a__ , a__ = 512 , a__ = 512 , a__ = 100 , a__ = 4.0 , a__ = 0.3 , a__ = 1 , a__ = None , a__ = "pil" , a__ = True , ): _lowerCAmelCase : Dict = self._execution_device _lowerCAmelCase : Optional[Any] = guidance_scale > 1.0 if isinstance(a__ , a__ ): _lowerCAmelCase : Dict = torch.cat(a__ , dim=0 ) _lowerCAmelCase : Dict = image_embeds.shape[0] if isinstance(a__ , a__ ): _lowerCAmelCase : List[Any] = torch.cat(a__ , dim=0 ) if do_classifier_free_guidance: _lowerCAmelCase : int = image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Any = negative_image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Optional[int] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=a__ ) if not isinstance(a__ , a__ ): _lowerCAmelCase : Any = [image] if not all(isinstance(a__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(a__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) _lowerCAmelCase : Tuple = torch.cat([prepare_image(a__ , a__ , a__ ) for i in image] , dim=0 ) _lowerCAmelCase : Union[str, Any] = image.to(dtype=image_embeds.dtype , device=a__ ) _lowerCAmelCase : Union[str, Any] = self.movq.encode(a__ )["""latents"""] _lowerCAmelCase : Tuple = latents.repeat_interleave(a__ , dim=0 ) self.scheduler.set_timesteps(a__ , device=a__ ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.get_timesteps(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = timesteps[:1].repeat(batch_size * num_images_per_prompt ) _lowerCAmelCase , _lowerCAmelCase : Dict = downscale_height_and_width(a__ , a__ , self.movq_scale_factor ) _lowerCAmelCase : List[str] = self.prepare_latents( a__ , a__ , a__ , a__ , image_embeds.dtype , a__ , a__ ) for i, t in enumerate(self.progress_bar(a__ ) ): # expand the latents if we are doing classifier free guidance _lowerCAmelCase : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowerCAmelCase : int = {"""image_embeds""": image_embeds} _lowerCAmelCase : List[str] = self.unet( sample=a__ , timestep=a__ , encoder_hidden_states=a__ , added_cond_kwargs=a__ , return_dict=a__ , )[0] if do_classifier_free_guidance: _lowerCAmelCase , _lowerCAmelCase : Optional[int] = noise_pred.split(latents.shape[1] , dim=1 ) _lowerCAmelCase , _lowerCAmelCase : List[Any] = noise_pred.chunk(2 ) _lowerCAmelCase , _lowerCAmelCase : Tuple = variance_pred.chunk(2 ) _lowerCAmelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _lowerCAmelCase : List[str] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , """variance_type""" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _lowerCAmelCase , _lowerCAmelCase : Dict = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _lowerCAmelCase : List[str] = self.scheduler.step( a__ , a__ , a__ , generator=a__ , )[0] # post-processing _lowerCAmelCase : int = self.movq.decode(a__ , force_not_quantize=a__ )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: _lowerCAmelCase : List[Any] = image * 0.5 + 0.5 _lowerCAmelCase : Any = image.clamp(0 , 1 ) _lowerCAmelCase : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _lowerCAmelCase : List[str] = self.numpy_to_pil(a__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a__ )
663
1
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
1
"""simple docstring""" # 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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : Tuple ,_lowerCamelCase : Any ,_lowerCamelCase : Tuple ) -> List[Any]: _lowerCAmelCase : Optional[int] = multiprocessing.Manager() _lowerCAmelCase : Optional[Any] = manager.list() _lowerCAmelCase : Dict = multiprocessing.Process(target=_lowerCamelCase ,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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ,_lowerCamelCase : Tuple ,_lowerCamelCase : Union[str, Any] ) -> Any: with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil _lowerCAmelCase : List[Any] = shutil.rmtree _lowerCAmelCase : List[Any] = os.rmdir _lowerCAmelCase : Optional[Any] = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: _lowerCAmelCase : List[Any] = {} with swallow_io(): with time_limit(_lowerCamelCase ): exec(_lowerCamelCase ,_lowerCamelCase ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(f"failed: {e}" ) # Needed for cleaning up. _lowerCAmelCase : Tuple = rmtree _lowerCAmelCase : Optional[Any] = rmdir _lowerCAmelCase : List[Any] = chdir @contextlib.contextmanager def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ) -> Union[str, Any]: def signal_handler(_lowerCamelCase : List[str] ,_lowerCamelCase : str ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL ,_lowerCamelCase ) signal.signal(signal.SIGALRM ,_lowerCamelCase ) try: yield finally: signal.setitimer(signal.ITIMER_REAL ,0 ) @contextlib.contextmanager def SCREAMING_SNAKE_CASE ( ) -> List[Any]: _lowerCAmelCase : Optional[Any] = WriteOnlyStringIO() with contextlib.redirect_stdout(_lowerCamelCase ): with contextlib.redirect_stderr(_lowerCamelCase ): with redirect_stdin(_lowerCamelCase ): yield @contextlib.contextmanager def SCREAMING_SNAKE_CASE ( ) -> List[Any]: with tempfile.TemporaryDirectory() as dirname: with chdir(_lowerCamelCase ): yield dirname class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A ( io.StringIO ): def __A ( self , *a__ , **a__ ): raise OSError def __A ( self , *a__ , **a__ ): raise OSError def __A ( self , *a__ , **a__ ): raise OSError def __A ( self , *a__ , **a__ ): return False class __A ( contextlib._RedirectStream ): # type: ignore _UpperCamelCase : Dict = "stdin" @contextlib.contextmanager def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict ) -> Tuple: if root == ".": yield return _lowerCAmelCase : List[Any] = os.getcwd() os.chdir(_lowerCamelCase ) try: yield except BaseException as exc: raise exc finally: os.chdir(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any]=None ) -> int: 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 _lowerCAmelCase : Any = None _lowerCAmelCase : Tuple = None import os _lowerCAmelCase : int = """1""" _lowerCAmelCase : Union[str, Any] = None _lowerCAmelCase : int = None _lowerCAmelCase : Optional[int] = None _lowerCAmelCase : Any = None _lowerCAmelCase : int = None _lowerCAmelCase : Union[str, Any] = None _lowerCAmelCase : str = None _lowerCAmelCase : Optional[Any] = None _lowerCAmelCase : Dict = None _lowerCAmelCase : Optional[int] = None _lowerCAmelCase : Optional[Any] = None _lowerCAmelCase : Optional[int] = None _lowerCAmelCase : Tuple = None _lowerCAmelCase : str = None _lowerCAmelCase : Dict = None _lowerCAmelCase : List[str] = None _lowerCAmelCase : Any = None _lowerCAmelCase : Any = None _lowerCAmelCase : str = None _lowerCAmelCase : Any = None _lowerCAmelCase : str = None _lowerCAmelCase : Dict = None _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Dict = None _lowerCAmelCase : Any = None _lowerCAmelCase : Union[str, Any] = None _lowerCAmelCase : Optional[Any] = None import shutil _lowerCAmelCase : Optional[Any] = None _lowerCAmelCase : Tuple = None _lowerCAmelCase : Optional[int] = None import subprocess _lowerCAmelCase : List[Any] = None # type: ignore _lowerCAmelCase : Tuple = None import sys _lowerCAmelCase : Optional[Any] = None _lowerCAmelCase : Tuple = None _lowerCAmelCase : List[str] = None _lowerCAmelCase : List[str] = None _lowerCAmelCase : Dict = None
663
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : str = CpmAntTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """<d>""", """</d>""", """<s>""", """</s>""", """</_>""", """<unk>""", """<pad>""", """</n>""", """我""", """是""", """C""", """P""", """M""", """A""", """n""", """t""", ] _lowerCAmelCase : int = 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] ) ) @tooslow def __A ( self ): _lowerCAmelCase : Tuple = CpmAntTokenizer.from_pretrained("""openbmb/cpm-ant-10b""" ) _lowerCAmelCase : Optional[Any] = """今天天气真好!""" _lowerCAmelCase : Any = ["""今天""", """天气""", """真""", """好""", """!"""] _lowerCAmelCase : str = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = """今天天气真好!""" _lowerCAmelCase : Optional[Any] = [tokenizer.bos_token] + tokens _lowerCAmelCase : Optional[int] = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) _lowerCAmelCase : Tuple = tokenizer.decode(a__ ) self.assertEqual(a__ , a__ )
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _a : List[str] = { 'configuration_tapas': ['TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TapasConfig'], 'tokenization_tapas': ['TapasTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST', 'TapasForMaskedLM', 'TapasForQuestionAnswering', 'TapasForSequenceClassification', 'TapasModel', 'TapasPreTrainedModel', 'load_tf_weights_in_tapas', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFTapasForMaskedLM', 'TFTapasForQuestionAnswering', 'TFTapasForSequenceClassification', 'TFTapasModel', 'TFTapasPreTrainedModel', ] if TYPE_CHECKING: from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig from .tokenization_tapas import TapasTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tapas import ( TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasPreTrainedModel, load_tf_weights_in_tapas, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_tapas import ( TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST, TFTapasForMaskedLM, TFTapasForQuestionAnswering, TFTapasForSequenceClassification, TFTapasModel, TFTapasPreTrainedModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
1
"""simple docstring""" import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers _a : Optional[int] = 'python tqdm regex requests packaging filelock numpy tokenizers'.split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append('dataclasses') if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append('importlib_metadata') for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(F"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Optional[int]=None ) -> str: require_version(deps[pkg] ,_lowerCamelCase )
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> bool: 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...') _a : int = int(input('Enter number: ').strip()) print(F"""{number} is {"" if perfect(number) else "not "}a Perfect Number.""")
663
1
"""simple docstring""" _a : dict[str, float] = { "joule": 1.0, "kilojoule": 1_000, "megajoule": 1_000_000, "gigajoule": 1_000_000_000, "wattsecond": 1.0, "watthour": 3_600, "kilowatthour": 3_600_000, "newtonmeter": 1.0, "calorie_nutr": 4_186.8, "kilocalorie_nutr": 4_186_800.00, "electronvolt": 1.6_02_17_66_34e-19, "britishthermalunit_it": 1_055.05_585, "footpound": 1.35_58_18, } def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : str ,_lowerCamelCase : float ) -> float: if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: _lowerCAmelCase : Tuple = ( f"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" f"Valid values are: {', '.join(_lowerCamelCase )}" ) raise ValueError(_lowerCamelCase ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
663
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
1
"""simple docstring""" from math import sqrt import numpy as np from sympy import symbols # Coefficient # Speed of light (m/s) _a : Dict = 299_792_458 # Symbols _a , _a , _a , _a : Union[str, Any] = symbols('ct x y z') def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ) -> float: if velocity > c: raise ValueError("""Speed must not exceed light speed 299,792,458 [m/s]!""" ) elif velocity < 1: # Usually the speed should be much higher than 1 (c order of magnitude) raise ValueError("""Speed must be greater than or equal to 1!""" ) return velocity / c def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ) -> float: return 1 / sqrt(1 - beta(_lowerCamelCase ) ** 2 ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ) -> np.ndarray: return np.array( [ [gamma(_lowerCamelCase ), -gamma(_lowerCamelCase ) * beta(_lowerCamelCase ), 0, 0], [-gamma(_lowerCamelCase ) * beta(_lowerCamelCase ), gamma(_lowerCamelCase ), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ] ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ,_lowerCamelCase : np.ndarray | None = None ) -> np.ndarray: # Ensure event is not empty if event is None: _lowerCAmelCase : Tuple = np.array([ct, x, y, z] ) # Symbolic four vector else: event[0] *= c # x0 is ct (speed of light * time) return transformation_matrix(_lowerCamelCase ) @ event if __name__ == "__main__": import doctest doctest.testmod() # Example of symbolic vector: _a : Union[str, Any] = transform(29_979_245) print('Example of four vector: ') print(F"""ct' = {four_vector[0]}""") print(F"""x' = {four_vector[1]}""") print(F"""y' = {four_vector[2]}""") print(F"""z' = {four_vector[3]}""") # Substitute symbols with numerical values _a : Optional[Any] = {ct: c, x: 1, y: 1, z: 1} _a : Tuple = [four_vector[i].subs(sub_dict) for i in range(4)] print(F"""\n{numerical_vector}""")
663
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
1
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging _a : List[Any] = logging.get_logger(__name__) # TODO: upload to AWS _a : Dict = { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json' ), } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : int = "retribert" def __init__( self , a__=30522 , a__=768 , a__=8 , a__=12 , a__=3072 , a__="gelu" , a__=0.1 , a__=0.1 , a__=512 , a__=2 , a__=0.0_2 , a__=1e-12 , a__=True , a__=128 , a__=0 , **a__ , ): super().__init__(pad_token_id=a__ , **a__ ) _lowerCAmelCase : Dict = vocab_size _lowerCAmelCase : Dict = hidden_size _lowerCAmelCase : Tuple = num_hidden_layers _lowerCAmelCase : str = num_attention_heads _lowerCAmelCase : Tuple = hidden_act _lowerCAmelCase : int = intermediate_size _lowerCAmelCase : List[Any] = hidden_dropout_prob _lowerCAmelCase : str = attention_probs_dropout_prob _lowerCAmelCase : Optional[int] = max_position_embeddings _lowerCAmelCase : str = type_vocab_size _lowerCAmelCase : Optional[int] = initializer_range _lowerCAmelCase : List[str] = layer_norm_eps _lowerCAmelCase : str = share_encoders _lowerCAmelCase : int = projection_dim
663
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
1
"""simple docstring""" import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : int = "Wav2Vec2FeatureExtractor" _UpperCamelCase : Optional[Any] = "AutoTokenizer" def __init__( self , a__ , a__ ): super().__init__(a__ , a__ ) _lowerCAmelCase : str = self.feature_extractor _lowerCAmelCase : Dict = False @classmethod def __A ( cls , a__ , **a__ ): try: return super().from_pretrained(a__ , **a__ ) except OSError: warnings.warn( F"Loading a tokenizer inside {cls.__name__} from a config that does not" """ include a `tokenizer_class` attribute is deprecated and will be """ """removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`""" """ attribute to either your `config.json` or `tokenizer_config.json` """ """file to suppress this warning: """ , a__ , ) _lowerCAmelCase : Optional[int] = WavaVecaFeatureExtractor.from_pretrained(a__ , **a__ ) _lowerCAmelCase : Tuple = WavaVecaCTCTokenizer.from_pretrained(a__ , **a__ ) return cls(feature_extractor=a__ , tokenizer=a__ ) def __call__( self , *a__ , **a__ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*a__ , **a__ ) if "raw_speech" in kwargs: warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""" ) _lowerCAmelCase : str = kwargs.pop("""raw_speech""" ) else: _lowerCAmelCase : Optional[int] = kwargs.pop("""audio""" , a__ ) _lowerCAmelCase : str = kwargs.pop("""sampling_rate""" , a__ ) _lowerCAmelCase : Any = kwargs.pop("""text""" , a__ ) if len(a__ ) > 0: _lowerCAmelCase : List[Any] = args[0] _lowerCAmelCase : List[Any] = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if audio is not None: _lowerCAmelCase : Optional[Any] = self.feature_extractor(a__ , *a__ , sampling_rate=a__ , **a__ ) if text is not None: _lowerCAmelCase : int = self.tokenizer(a__ , **a__ ) if text is None: return inputs elif audio is None: return encodings else: _lowerCAmelCase : Optional[int] = encodings["""input_ids"""] return inputs def __A ( self , *a__ , **a__ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*a__ , **a__ ) _lowerCAmelCase : List[Any] = kwargs.pop("""input_features""" , a__ ) _lowerCAmelCase : List[Any] = kwargs.pop("""labels""" , a__ ) if len(a__ ) > 0: _lowerCAmelCase : Union[str, Any] = args[0] _lowerCAmelCase : Optional[Any] = args[1:] if input_features is not None: _lowerCAmelCase : Dict = self.feature_extractor.pad(a__ , *a__ , **a__ ) if labels is not None: _lowerCAmelCase : Optional[Any] = self.tokenizer.pad(a__ , **a__ ) if labels is None: return input_features elif input_features is None: return labels else: _lowerCAmelCase : List[Any] = labels["""input_ids"""] return input_features def __A ( self , *a__ , **a__ ): return self.tokenizer.batch_decode(*a__ , **a__ ) def __A ( self , *a__ , **a__ ): return self.tokenizer.decode(*a__ , **a__ ) @contextmanager def __A ( self ): warnings.warn( """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """ """labels by using the argument `text` of the regular `__call__` method (either in the same call as """ """your audio inputs, or in a separate call.""" ) _lowerCAmelCase : str = True _lowerCAmelCase : str = self.tokenizer yield _lowerCAmelCase : Union[str, Any] = self.feature_extractor _lowerCAmelCase : Optional[Any] = False
663
"""simple docstring""" _a : Optional[Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[str] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : str = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Tuple = 13 _lowerCAmelCase : Tuple = 7 _lowerCAmelCase : Any = 30 _lowerCAmelCase : Optional[int] = self.seq_length + self.mem_len _lowerCAmelCase : Dict = 15 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : Any = True _lowerCAmelCase : List[str] = 99 _lowerCAmelCase : List[Any] = [10, 50, 80] _lowerCAmelCase : Tuple = 32 _lowerCAmelCase : int = 32 _lowerCAmelCase : Dict = 4 _lowerCAmelCase : List[str] = 8 _lowerCAmelCase : Tuple = 128 _lowerCAmelCase : Any = 2 _lowerCAmelCase : List[Any] = 2 _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Optional[Any] = 1 _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : List[Any] = 3 _lowerCAmelCase : Optional[int] = self.vocab_size - 1 _lowerCAmelCase : Dict = 0.0_1 def __A ( self ): _lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Union[str, Any] = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[int] = TFTransfoXLModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() _lowerCAmelCase : Optional[Any] = {"""input_ids""": input_ids_a, """mems""": mems_a} _lowerCAmelCase , _lowerCAmelCase : List[Any] = model(a__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : int = TFTransfoXLLMHeadModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase : Dict = {"""input_ids""": input_ids_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = model([input_ids_a, mems_a] ).to_tuple() _lowerCAmelCase : Any = {"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFTransfoXLForSequenceClassification(a__ ) _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : Dict = config_and_inputs _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids_a} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : Any = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : str = False _UpperCamelCase : str = False _UpperCamelCase : Tuple = False _UpperCamelCase : Any = False def __A ( self , a__ , a__ , a__ , a__ , a__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLModelTester(self ) _lowerCAmelCase : List[Any] = ConfigTester(self , config_class=a__ , d_embed=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a__ ) def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a__ ) def __A ( self ): _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase : List[Any] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(a__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _lowerCAmelCase : str = model.get_output_embeddings() assert isinstance(a__ , tf.keras.layers.Layer ) _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None else: _lowerCAmelCase : Union[str, Any] = model.get_output_embeddings() assert x is None _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None def __A ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Union[str, Any] = TFTransfoXLModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" ) def __A ( self ): pass @require_tf class __A ( unittest.TestCase ): @unittest.skip("""Skip test until #12651 is resolved.""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" ) # fmt: off _lowerCAmelCase : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _lowerCAmelCase : List[Any] = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _lowerCAmelCase : Tuple = model.generate(a__ , max_length=200 , do_sample=a__ ) self.assertListEqual(output_ids[0].numpy().tolist() , a__ )
663
1
"""simple docstring""" _a : Dict = 256 # Modulus to hash a string _a : str = 1_000_003 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : str ) -> bool: _lowerCAmelCase : List[str] = len(_lowerCamelCase ) _lowerCAmelCase : str = len(_lowerCamelCase ) if p_len > t_len: return False _lowerCAmelCase : Dict = 0 _lowerCAmelCase : int = 0 _lowerCAmelCase : Optional[Any] = 1 # Calculating the hash of pattern and substring of text for i in range(_lowerCamelCase ): _lowerCAmelCase : Optional[Any] = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus _lowerCAmelCase : Tuple = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue _lowerCAmelCase : Optional[int] = (modulus_power * alphabet_size) % modulus for i in range(0 ,t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash _lowerCAmelCase : Optional[int] = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def SCREAMING_SNAKE_CASE ( ) -> None: _lowerCAmelCase : str = """abc1abc12""" _lowerCAmelCase : Optional[int] = """alskfjaldsabc1abc1abc12k23adsfabcabc""" _lowerCAmelCase : Optional[int] = """alskfjaldsk23adsfabcabc""" assert rabin_karp(_lowerCamelCase ,_lowerCamelCase ) and not rabin_karp(_lowerCamelCase ,_lowerCamelCase ) # Test 2) _lowerCAmelCase : Optional[int] = """ABABX""" _lowerCAmelCase : Tuple = """ABABZABABYABABX""" assert rabin_karp(_lowerCamelCase ,_lowerCamelCase ) # Test 3) _lowerCAmelCase : Any = """AAAB""" _lowerCAmelCase : Optional[Any] = """ABAAAAAB""" assert rabin_karp(_lowerCamelCase ,_lowerCamelCase ) # Test 4) _lowerCAmelCase : str = """abcdabcy""" _lowerCAmelCase : Dict = """abcxabcdabxabcdabcdabcy""" assert rabin_karp(_lowerCamelCase ,_lowerCamelCase ) # Test 5) _lowerCAmelCase : str = """Lü""" _lowerCAmelCase : int = """Lüsai""" assert rabin_karp(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : List[str] = """Lue""" assert not rabin_karp(_lowerCamelCase ,_lowerCamelCase ) print("""Success.""" ) if __name__ == "__main__": test_rabin_karp()
663
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
1
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging _a : Optional[Any] = logging.get_logger(__name__) if is_vision_available(): import PIL class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[str] = ["pixel_values"] def __init__( self , a__ = True , a__ = None , a__ = PILImageResampling.BICUBIC , a__ = True , a__ = None , a__ = True , a__ = 1 / 255 , a__ = True , a__ = None , a__ = None , a__ = True , **a__ , ): super().__init__(**a__ ) _lowerCAmelCase : List[str] = size if size is not None else {"""shortest_edge""": 224} _lowerCAmelCase : Union[str, Any] = get_size_dict(a__ , default_to_square=a__ ) _lowerCAmelCase : int = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} _lowerCAmelCase : Optional[Any] = get_size_dict(a__ , default_to_square=a__ , param_name="""crop_size""" ) _lowerCAmelCase : str = do_resize _lowerCAmelCase : Tuple = size _lowerCAmelCase : Dict = resample _lowerCAmelCase : Any = do_center_crop _lowerCAmelCase : Tuple = crop_size _lowerCAmelCase : List[Any] = do_rescale _lowerCAmelCase : Optional[Any] = rescale_factor _lowerCAmelCase : Optional[int] = do_normalize _lowerCAmelCase : Dict = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _lowerCAmelCase : Any = image_std if image_std is not None else OPENAI_CLIP_STD _lowerCAmelCase : List[str] = do_convert_rgb def __A ( self , a__ , a__ , a__ = PILImageResampling.BICUBIC , a__ = None , **a__ , ): _lowerCAmelCase : Optional[int] = get_size_dict(a__ , default_to_square=a__ ) if "shortest_edge" not in size: raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}" ) _lowerCAmelCase : str = get_resize_output_image_size(a__ , size=size["""shortest_edge"""] , default_to_square=a__ ) return resize(a__ , size=a__ , resample=a__ , data_format=a__ , **a__ ) def __A ( self , a__ , a__ , a__ = None , **a__ , ): _lowerCAmelCase : Optional[Any] = get_size_dict(a__ ) if "height" not in size or "width" not in size: raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}" ) return center_crop(a__ , size=(size["""height"""], size["""width"""]) , data_format=a__ , **a__ ) def __A ( self , a__ , a__ , a__ = None , **a__ , ): return rescale(a__ , scale=a__ , data_format=a__ , **a__ ) def __A ( self , a__ , a__ , a__ , a__ = None , **a__ , ): return normalize(a__ , mean=a__ , std=a__ , data_format=a__ , **a__ ) def __A ( self , a__ , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = ChannelDimension.FIRST , **a__ , ): _lowerCAmelCase : Optional[Any] = do_resize if do_resize is not None else self.do_resize _lowerCAmelCase : str = size if size is not None else self.size _lowerCAmelCase : Dict = get_size_dict(a__ , param_name="""size""" , default_to_square=a__ ) _lowerCAmelCase : Tuple = resample if resample is not None else self.resample _lowerCAmelCase : List[str] = do_center_crop if do_center_crop is not None else self.do_center_crop _lowerCAmelCase : Union[str, Any] = crop_size if crop_size is not None else self.crop_size _lowerCAmelCase : Any = get_size_dict(a__ , param_name="""crop_size""" , default_to_square=a__ ) _lowerCAmelCase : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale _lowerCAmelCase : str = rescale_factor if rescale_factor is not None else self.rescale_factor _lowerCAmelCase : int = do_normalize if do_normalize is not None else self.do_normalize _lowerCAmelCase : List[Any] = image_mean if image_mean is not None else self.image_mean _lowerCAmelCase : Optional[Any] = image_std if image_std is not None else self.image_std _lowerCAmelCase : Union[str, Any] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _lowerCAmelCase : Any = make_list_of_images(a__ ) if not valid_images(a__ ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: _lowerCAmelCase : List[Any] = [convert_to_rgb(a__ ) for image in images] # All transformations expect numpy arrays. _lowerCAmelCase : List[str] = [to_numpy_array(a__ ) for image in images] if do_resize: _lowerCAmelCase : Dict = [self.resize(image=a__ , size=a__ , resample=a__ ) for image in images] if do_center_crop: _lowerCAmelCase : str = [self.center_crop(image=a__ , size=a__ ) for image in images] if do_rescale: _lowerCAmelCase : Optional[Any] = [self.rescale(image=a__ , scale=a__ ) for image in images] if do_normalize: _lowerCAmelCase : Union[str, Any] = [self.normalize(image=a__ , mean=a__ , std=a__ ) for image in images] _lowerCAmelCase : Optional[int] = [to_channel_dimension_format(a__ , a__ ) for image in images] _lowerCAmelCase : Dict = {"""pixel_values""": images} return BatchFeature(data=a__ , tensor_type=a__ )
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( ) -> Tuple: _lowerCAmelCase : str = 0 for i in range(1 ,1001 ): total += i**i return str(_lowerCamelCase )[-10:] if __name__ == "__main__": print(solution())
663
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = DiTPipeline _UpperCamelCase : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS _UpperCamelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } _UpperCamelCase : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS _UpperCamelCase : Union[str, Any] = False def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=a__ , activation_fn="""gelu-approximate""" , num_embeds_ada_norm=1000 , norm_type="""ada_norm_zero""" , norm_elementwise_affine=a__ , ) _lowerCAmelCase : Optional[int] = AutoencoderKL() _lowerCAmelCase : Union[str, Any] = DDIMScheduler() _lowerCAmelCase : Optional[Any] = {"""transformer""": transformer.eval(), """vae""": vae.eval(), """scheduler""": scheduler} return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Any = torch.manual_seed(a__ ) else: _lowerCAmelCase : Tuple = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Any = { """class_labels""": [1], """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : Tuple = self.get_dummy_components() _lowerCAmelCase : Optional[int] = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Tuple = self.get_dummy_inputs(a__ ) _lowerCAmelCase : List[str] = pipe(**a__ ).images _lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) _lowerCAmelCase : List[Any] = np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7] ) _lowerCAmelCase : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1e-3 ) def __A ( self ): self._test_inference_batch_single_identical(relax_max_difference=a__ , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __A ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): def __A ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : List[str] = torch.manual_seed(0 ) _lowerCAmelCase : int = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-256""" ) pipe.to("""cuda""" ) _lowerCAmelCase : Dict = ["""vase""", """umbrella""", """white shark""", """white wolf"""] _lowerCAmelCase : Union[str, Any] = pipe.get_label_ids(a__ ) _lowerCAmelCase : Any = pipe(a__ , generator=a__ , num_inference_steps=40 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( F"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1e-2 def __A ( self ): _lowerCAmelCase : str = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-512""" ) _lowerCAmelCase : Dict = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("""cuda""" ) _lowerCAmelCase : List[str] = ["""vase""", """umbrella"""] _lowerCAmelCase : Optional[int] = pipe.get_label_ids(a__ ) _lowerCAmelCase : str = torch.manual_seed(0 ) _lowerCAmelCase : List[str] = pipe(a__ , generator=a__ , num_inference_steps=25 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" F"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1e-1
663
1
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _a : Tuple = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , *a__ , **a__ ): warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , a__ , ) super().__init__(*a__ , **a__ )
663
1
"""simple docstring""" from typing import Dict, Optional import numpy as np import datasets _a : str = '\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n' _a : Any = '\nArgs:\n predictions (`List[ndarray]`):\n List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n references (`List[ndarray]`):\n List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n num_labels (`int`):\n Number of classes (categories).\n ignore_index (`int`):\n Index that will be ignored during evaluation.\n nan_to_num (`int`, *optional*):\n If specified, NaN values will be replaced by the number defined by the user.\n label_map (`dict`, *optional*):\n If specified, dictionary mapping old label indices to new label indices.\n reduce_labels (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n `Dict[str, float | ndarray]` comprising various elements:\n - *mean_iou* (`float`):\n Mean Intersection-over-Union (IoU averaged over all categories).\n - *mean_accuracy* (`float`):\n Mean accuracy (averaged over all categories).\n - *overall_accuracy* (`float`):\n Overall accuracy on all images.\n - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n Per category accuracy.\n - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n Per category IoU.\n\nExamples:\n\n >>> import numpy as np\n\n >>> mean_iou = datasets.load_metric("mean_iou")\n\n >>> # suppose one has 3 different segmentation maps predicted\n >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n >>> predicted = [predicted_1, predicted_2, predicted_3]\n >>> ground_truth = [actual_1, actual_2, actual_3]\n\n >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0. , 0. , 0.375, 0.4 , 0.5 , 0. , 0.5 , 1. , 1. , 1. ]), \'per_category_accuracy\': array([0. , 0. , 0.75 , 0.66666667, 1. , 0. , 0.5 , 1. , 1. , 1. ])}\n' _a : Optional[int] = '\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : List[str] ,_lowerCamelCase : List[Any] ,_lowerCamelCase : bool ,_lowerCamelCase : Optional[Dict[int, int]] = None ,_lowerCamelCase : bool = False ,) -> List[str]: if label_map is not None: for old_id, new_id in label_map.items(): _lowerCAmelCase : Dict = new_id # turn into Numpy arrays _lowerCAmelCase : Dict = np.array(_lowerCamelCase ) _lowerCAmelCase : str = np.array(_lowerCamelCase ) if reduce_labels: _lowerCAmelCase : Optional[int] = 255 _lowerCAmelCase : Union[str, Any] = label - 1 _lowerCAmelCase : Optional[Any] = 255 _lowerCAmelCase : Optional[Any] = label != ignore_index _lowerCAmelCase : List[str] = np.not_equal(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : Optional[int] = pred_label[mask] _lowerCAmelCase : Tuple = np.array(_lowerCamelCase )[mask] _lowerCAmelCase : Tuple = pred_label[pred_label == label] _lowerCAmelCase : int = np.histogram(_lowerCamelCase ,bins=_lowerCamelCase ,range=(0, num_labels - 1) )[0] _lowerCAmelCase : Dict = np.histogram(_lowerCamelCase ,bins=_lowerCamelCase ,range=(0, num_labels - 1) )[0] _lowerCAmelCase : List[str] = np.histogram(_lowerCamelCase ,bins=_lowerCamelCase ,range=(0, num_labels - 1) )[0] _lowerCAmelCase : int = area_pred_label + area_label - area_intersect return area_intersect, area_union, area_pred_label, area_label def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : str ,_lowerCamelCase : Any ,_lowerCamelCase : bool ,_lowerCamelCase : Optional[Dict[int, int]] = None ,_lowerCamelCase : bool = False ,) -> Optional[int]: _lowerCAmelCase : Tuple = np.zeros((num_labels,) ,dtype=np.floataa ) _lowerCAmelCase : Any = np.zeros((num_labels,) ,dtype=np.floataa ) _lowerCAmelCase : List[Any] = np.zeros((num_labels,) ,dtype=np.floataa ) _lowerCAmelCase : Optional[Any] = np.zeros((num_labels,) ,dtype=np.floataa ) for result, gt_seg_map in zip(_lowerCamelCase ,_lowerCamelCase ): _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Dict = intersect_and_union( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) total_area_intersect += area_intersect total_area_union += area_union total_area_pred_label += area_pred_label total_area_label += area_label return total_area_intersect, total_area_union, total_area_pred_label, total_area_label def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Union[str, Any] ,_lowerCamelCase : bool ,_lowerCamelCase : Optional[int] = None ,_lowerCamelCase : Optional[Dict[int, int]] = None ,_lowerCamelCase : bool = False ,) -> Dict: _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Any = total_intersect_and_union( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # compute metrics _lowerCAmelCase : Optional[Any] = {} _lowerCAmelCase : Dict = total_area_intersect.sum() / total_area_label.sum() _lowerCAmelCase : str = total_area_intersect / total_area_union _lowerCAmelCase : Dict = total_area_intersect / total_area_label _lowerCAmelCase : Union[str, Any] = np.nanmean(_lowerCamelCase ) _lowerCAmelCase : Dict = np.nanmean(_lowerCamelCase ) _lowerCAmelCase : List[str] = all_acc _lowerCAmelCase : Any = iou _lowerCAmelCase : Optional[int] = acc if nan_to_num is not None: _lowerCAmelCase : Optional[int] = {metric: np.nan_to_num(_lowerCamelCase ,nan=_lowerCamelCase ) for metric, metric_value in metrics.items()} return metrics @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): def __A ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( # 1st Seq - height dim, 2nd - width dim { """predictions""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), """references""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16""" ) ) ), } ) , reference_urls=[ """https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py""" ] , ) def __A ( self , a__ , a__ , a__ , a__ , a__ = None , a__ = None , a__ = False , ): _lowerCAmelCase : str = mean_iou( results=a__ , gt_seg_maps=a__ , num_labels=a__ , ignore_index=a__ , nan_to_num=a__ , label_map=a__ , reduce_labels=a__ , ) return iou_result
663
"""simple docstring""" import argparse import json import subprocess def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Any ) -> List[Any]: _lowerCAmelCase : Tuple = [] _lowerCAmelCase : Optional[int] = ( f"curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"" """ https://api.github.com/repos/huggingface/transformers/actions/runners""" ) _lowerCAmelCase : List[str] = subprocess.run(_lowerCamelCase ,shell=_lowerCamelCase ,stdout=subprocess.PIPE ) _lowerCAmelCase : int = output.stdout.decode("""utf-8""" ) _lowerCAmelCase : Tuple = json.loads(_lowerCamelCase ) _lowerCAmelCase : int = status["""runners"""] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_lowerCamelCase ) # save the result so we can report them on Slack with open("""offline_runners.txt""" ,"""w""" ) as fp: fp.write(json.dumps(_lowerCamelCase ) ) if len(_lowerCamelCase ) > 0: _lowerCAmelCase : int = """\n""".join([x["""name"""] for x in offline_runners] ) raise ValueError(f"The following runners are offline:\n{failed}" ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> Optional[int]: return values.split(""",""" ) _a : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--target_runners', default=None, type=list_str, required=True, help='Comma-separated list of runners to check status.', ) parser.add_argument( '--token', default=None, type=str, required=True, help='A token that has actions:read permission.' ) _a : Tuple = parser.parse_args() get_runner_status(args.target_runners, args.token)
663
1
"""simple docstring""" from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def SCREAMING_SNAKE_CASE ( _lowerCamelCase : NDArray[floataa] ,_lowerCamelCase : NDArray[floataa] ,_lowerCamelCase : list[int] ,_lowerCamelCase : int ,) -> list[float]: _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = coefficient_matrix.shape _lowerCAmelCase , _lowerCAmelCase : Optional[int] = constant_matrix.shape if rowsa != colsa: _lowerCAmelCase : int = f"Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}" raise ValueError(_lowerCamelCase ) if colsa != 1: _lowerCAmelCase : str = f"Constant matrix must be nx1 but received {rowsa}x{colsa}" raise ValueError(_lowerCamelCase ) if rowsa != rowsa: _lowerCAmelCase : Any = ( """Coefficient and constant matrices dimensions must be nxn and nx1 but """ f"received {rowsa}x{colsa} and {rowsa}x{colsa}" ) raise ValueError(_lowerCamelCase ) if len(_lowerCamelCase ) != rowsa: _lowerCAmelCase : Dict = ( """Number of initial values must be equal to number of rows in coefficient """ f"matrix but received {len(_lowerCamelCase )} and {rowsa}" ) raise ValueError(_lowerCamelCase ) if iterations <= 0: raise ValueError("""Iterations must be at least 1""" ) _lowerCAmelCase : NDArray[floataa] = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = table.shape strictly_diagonally_dominant(_lowerCamelCase ) # Iterates the whole matrix for given number of times for _ in range(_lowerCamelCase ): _lowerCAmelCase : int = [] for row in range(_lowerCamelCase ): _lowerCAmelCase : str = 0 for col in range(_lowerCamelCase ): if col == row: _lowerCAmelCase : int = table[row][col] elif col == cols - 1: _lowerCAmelCase : Tuple = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] _lowerCAmelCase : List[str] = (temp + val) / denom new_val.append(_lowerCamelCase ) _lowerCAmelCase : Optional[int] = new_val return [float(_lowerCamelCase ) for i in new_val] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : NDArray[floataa] ) -> bool: _lowerCAmelCase , _lowerCAmelCase : Any = table.shape _lowerCAmelCase : Union[str, Any] = True for i in range(0 ,_lowerCamelCase ): _lowerCAmelCase : Union[str, Any] = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("""Coefficient matrix is not strictly diagonally dominant""" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
663
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
663
1
"""simple docstring""" from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available from .timesteps import ( fastaa_timesteps, smartaa_timesteps, smartaa_timesteps, smartaaa_timesteps, smartaaa_timesteps, superaa_timesteps, superaa_timesteps, superaaa_timesteps, ) @dataclass class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Union[List[PIL.Image.Image], np.ndarray] _UpperCamelCase : Optional[List[bool]] _UpperCamelCase : Optional[List[bool]] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_if import IFPipeline from .pipeline_if_imgaimg import IFImgaImgPipeline from .pipeline_if_imgaimg_superresolution import IFImgaImgSuperResolutionPipeline from .pipeline_if_inpainting import IFInpaintingPipeline from .pipeline_if_inpainting_superresolution import IFInpaintingSuperResolutionPipeline from .pipeline_if_superresolution import IFSuperResolutionPipeline from .safety_checker import IFSafetyChecker from .watermark import IFWatermarker
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ) -> int: _lowerCAmelCase : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 ,limit + 1 ): if phi[i] == i - 1: for j in range(2 * i ,limit + 1 ,_lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Union[str, Any] = ['MLukeTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Tuple = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def SCREAMING_SNAKE_CASE ( _lowerCamelCase : float ,_lowerCamelCase : float ,_lowerCamelCase : bool = False ) -> list[float]: if radian_mode: return [magnitude * cos(_lowerCamelCase ), magnitude * sin(_lowerCamelCase )] return [magnitude * cos(radians(_lowerCamelCase ) ), magnitude * sin(radians(_lowerCamelCase ) )] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : NDArray[floataa] ,_lowerCamelCase : NDArray[floataa] ,_lowerCamelCase : float = 10**-1 ) -> bool: _lowerCAmelCase : NDArray[floataa] = cross(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : float = sum(_lowerCamelCase ) return abs(_lowerCamelCase ) < eps if __name__ == "__main__": # Test to check if it works _a : Any = array( [ polar_force(7_18.4, 180 - 30), polar_force(8_79.54, 45), polar_force(100, -90), ] ) _a : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg _a : List[str] = array( [ polar_force(30 * 9.81, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) _a : str = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg _a : Tuple = array([[0, -2_000], [0, -1_200], [0, 15_600], [0, -12_400]]) _a : Any = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
663
"""simple docstring""" from PIL import Image def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Image ,_lowerCamelCase : int ) -> Image: _lowerCAmelCase : Any = (259 * (level + 255)) / (255 * (259 - level)) def contrast(_lowerCamelCase : int ) -> int: return int(128 + factor * (c - 128) ) return img.point(_lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 _a : str = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
663
1
"""simple docstring""" from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ,_lowerCamelCase : int = 10 ) -> int: _lowerCAmelCase : defaultdict = defaultdict(_lowerCamelCase ) for outer_width in range(3 ,(t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: _lowerCAmelCase : Union[str, Any] = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) ,1 ) else: _lowerCAmelCase : Dict = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(_lowerCamelCase ,outer_width - 1 ,2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 10 ) if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A : def __init__( self ): _lowerCAmelCase : Union[str, Any] = [ [], [], [], ] def __A ( self , a__ , a__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(a__ ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def __A ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self ): return "\n".join(F"Priority {i}: {q}" for i, q in enumerate(self.queues ) ) class __A : def __init__( self ): _lowerCAmelCase : int = [] def __A ( self , a__ ): if len(self.queue ) == 100: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(a__ ) def __A ( self ): if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: _lowerCAmelCase : int = min(self.queue ) self.queue.remove(a__ ) return data def __str__( self ): return str(self.queue ) def SCREAMING_SNAKE_CASE ( ) -> str: _lowerCAmelCase : Union[str, Any] = FixedPriorityQueue() fpq.enqueue(0 ,10 ) fpq.enqueue(1 ,70 ) fpq.enqueue(0 ,100 ) fpq.enqueue(2 ,1 ) fpq.enqueue(2 ,5 ) fpq.enqueue(1 ,7 ) fpq.enqueue(2 ,4 ) fpq.enqueue(1 ,64 ) fpq.enqueue(0 ,128 ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: _lowerCAmelCase : Tuple = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(100 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(128 ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
663
1
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __A ( unittest.TestCase ): _UpperCamelCase : str = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING _UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = AudioClassificationPipeline(model=a__ , feature_extractor=a__ ) # test with a raw waveform _lowerCAmelCase : Optional[int] = np.zeros((34000,) ) _lowerCAmelCase : Optional[Any] = np.zeros((14000,) ) return audio_classifier, [audioa, audio] def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = examples _lowerCAmelCase : List[Any] = audio_classifier(a__ ) # by default a model is initialized with num_labels=2 self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) _lowerCAmelCase : Tuple = audio_classifier(a__ , top_k=1 ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) self.run_torchaudio(a__ ) @require_torchaudio def __A ( self , a__ ): import datasets # test with a local file _lowerCAmelCase : int = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) _lowerCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] _lowerCAmelCase : str = audio_classifier(a__ ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) @require_torch def __A ( self ): _lowerCAmelCase : int = """anton-l/wav2vec2-random-tiny-classifier""" _lowerCAmelCase : Optional[Any] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : Any = np.ones((8000,) ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) _lowerCAmelCase : List[str] = [ {"""score""": 0.0_8_4_2, """label""": """no"""}, {"""score""": 0.0_8_3_8, """label""": """up"""}, {"""score""": 0.0_8_3_7, """label""": """go"""}, {"""score""": 0.0_8_3_4, """label""": """right"""}, ] _lowerCAmelCase : str = [ {"""score""": 0.0_8_4_5, """label""": """stop"""}, {"""score""": 0.0_8_4_4, """label""": """on"""}, {"""score""": 0.0_8_4_1, """label""": """right"""}, {"""score""": 0.0_8_3_4, """label""": """left"""}, ] self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) _lowerCAmelCase : int = {"""array""": np.ones((8000,) ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate} _lowerCAmelCase : int = audio_classifier(a__ , top_k=4 ) self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __A ( self ): import datasets _lowerCAmelCase : Optional[Any] = """superb/wav2vec2-base-superb-ks""" _lowerCAmelCase : List[str] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : str = datasets.load_dataset("""anton-l/superb_dummy""" , """ks""" , split="""test""" ) _lowerCAmelCase : Optional[Any] = np.array(dataset[3]["""speech"""] , dtype=np.floataa ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) self.assertEqual( nested_simplify(a__ , decimals=3 ) , [ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] , ) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def __A ( self ): pass
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _a : Tuple = {'configuration_swin': ['SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SwinConfig', 'SwinOnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'SWIN_PRETRAINED_MODEL_ARCHIVE_LIST', 'SwinForImageClassification', 'SwinForMaskedImageModeling', 'SwinModel', 'SwinPreTrainedModel', 'SwinBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : List[str] = [ 'TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFSwinForImageClassification', 'TFSwinForMaskedImageModeling', 'TFSwinModel', 'TFSwinPreTrainedModel', ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _a : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _a : int = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ,_lowerCamelCase : Dict=8 ) -> Any: _lowerCAmelCase : List[Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _lowerCAmelCase : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Any=512 ,_lowerCamelCase : Dict=512 ) -> List[Any]: _lowerCAmelCase : Any = pil_image.resize((w, h) ,resample=Image.BICUBIC ,reducing_gap=1 ) _lowerCAmelCase : Dict = np.array(pil_image.convert("""RGB""" ) ) _lowerCAmelCase : List[str] = arr.astype(np.floataa ) / 1_27.5 - 1 _lowerCAmelCase : int = np.transpose(_lowerCamelCase ,[2, 0, 1] ) _lowerCAmelCase : Optional[Any] = torch.from_numpy(_lowerCamelCase ).unsqueeze(0 ) return image class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__ , a__ , ): super().__init__() self.register_modules( unet=a__ , scheduler=a__ , movq=a__ , ) _lowerCAmelCase : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __A ( self , a__ , a__ , a__ ): # get the original timestep using init_timestep _lowerCAmelCase : Optional[Any] = min(int(num_inference_steps * strength ) , a__ ) _lowerCAmelCase : List[Any] = max(num_inference_steps - init_timestep , 0 ) _lowerCAmelCase : Dict = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__=None ): if not isinstance(a__ , (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(a__ )}" ) _lowerCAmelCase : Union[str, Any] = image.to(device=a__ , dtype=a__ ) _lowerCAmelCase : int = batch_size * num_images_per_prompt if image.shape[1] == 4: _lowerCAmelCase : int = image else: if isinstance(a__ , a__ ) and len(a__ ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(a__ )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) elif isinstance(a__ , a__ ): _lowerCAmelCase : Optional[int] = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a__ ) ] _lowerCAmelCase : Optional[int] = torch.cat(a__ , dim=0 ) else: _lowerCAmelCase : List[Any] = self.movq.encode(a__ ).latent_dist.sample(a__ ) _lowerCAmelCase : Dict = self.movq.config.scaling_factor * init_latents _lowerCAmelCase : str = torch.cat([init_latents] , dim=0 ) _lowerCAmelCase : Dict = init_latents.shape _lowerCAmelCase : str = randn_tensor(a__ , generator=a__ , device=a__ , dtype=a__ ) # get latents _lowerCAmelCase : Optional[Any] = self.scheduler.add_noise(a__ , a__ , a__ ) _lowerCAmelCase : int = init_latents return latents def __A ( self , a__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _lowerCAmelCase : str = torch.device(F"cuda:{gpu_id}" ) _lowerCAmelCase : int = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a__ , a__ ) def __A ( self , a__=0 ): if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) _lowerCAmelCase : Optional[int] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=a__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _lowerCAmelCase : List[str] = None for cpu_offloaded_model in [self.unet, self.movq]: _lowerCAmelCase , _lowerCAmelCase : str = cpu_offload_with_hook(a__ , a__ , prev_module_hook=a__ ) # We'll offload the last model manually. _lowerCAmelCase : Tuple = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A ( self ): if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(a__ , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a__ ) def __call__( self , a__ , a__ , a__ , a__ = 512 , a__ = 512 , a__ = 100 , a__ = 4.0 , a__ = 0.3 , a__ = 1 , a__ = None , a__ = "pil" , a__ = True , ): _lowerCAmelCase : Dict = self._execution_device _lowerCAmelCase : Optional[Any] = guidance_scale > 1.0 if isinstance(a__ , a__ ): _lowerCAmelCase : Dict = torch.cat(a__ , dim=0 ) _lowerCAmelCase : Dict = image_embeds.shape[0] if isinstance(a__ , a__ ): _lowerCAmelCase : List[Any] = torch.cat(a__ , dim=0 ) if do_classifier_free_guidance: _lowerCAmelCase : int = image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Any = negative_image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Optional[int] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=a__ ) if not isinstance(a__ , a__ ): _lowerCAmelCase : Any = [image] if not all(isinstance(a__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(a__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) _lowerCAmelCase : Tuple = torch.cat([prepare_image(a__ , a__ , a__ ) for i in image] , dim=0 ) _lowerCAmelCase : Union[str, Any] = image.to(dtype=image_embeds.dtype , device=a__ ) _lowerCAmelCase : Union[str, Any] = self.movq.encode(a__ )["""latents"""] _lowerCAmelCase : Tuple = latents.repeat_interleave(a__ , dim=0 ) self.scheduler.set_timesteps(a__ , device=a__ ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.get_timesteps(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = timesteps[:1].repeat(batch_size * num_images_per_prompt ) _lowerCAmelCase , _lowerCAmelCase : Dict = downscale_height_and_width(a__ , a__ , self.movq_scale_factor ) _lowerCAmelCase : List[str] = self.prepare_latents( a__ , a__ , a__ , a__ , image_embeds.dtype , a__ , a__ ) for i, t in enumerate(self.progress_bar(a__ ) ): # expand the latents if we are doing classifier free guidance _lowerCAmelCase : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowerCAmelCase : int = {"""image_embeds""": image_embeds} _lowerCAmelCase : List[str] = self.unet( sample=a__ , timestep=a__ , encoder_hidden_states=a__ , added_cond_kwargs=a__ , return_dict=a__ , )[0] if do_classifier_free_guidance: _lowerCAmelCase , _lowerCAmelCase : Optional[int] = noise_pred.split(latents.shape[1] , dim=1 ) _lowerCAmelCase , _lowerCAmelCase : List[Any] = noise_pred.chunk(2 ) _lowerCAmelCase , _lowerCAmelCase : Tuple = variance_pred.chunk(2 ) _lowerCAmelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _lowerCAmelCase : List[str] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , """variance_type""" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _lowerCAmelCase , _lowerCAmelCase : Dict = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _lowerCAmelCase : List[str] = self.scheduler.step( a__ , a__ , a__ , generator=a__ , )[0] # post-processing _lowerCAmelCase : int = self.movq.decode(a__ , force_not_quantize=a__ )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: _lowerCAmelCase : List[Any] = image * 0.5 + 0.5 _lowerCAmelCase : Any = image.clamp(0 , 1 ) _lowerCAmelCase : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _lowerCAmelCase : List[str] = self.numpy_to_pil(a__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a__ )
663
1
"""simple docstring""" import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ) -> Optional[int]: if "model" in orig_key: _lowerCAmelCase : List[str] = orig_key.replace("""model.""" ,"""""" ) if "norm1" in orig_key: _lowerCAmelCase : Optional[Any] = orig_key.replace("""norm1""" ,"""attention.output.LayerNorm""" ) if "norm2" in orig_key: _lowerCAmelCase : List[Any] = orig_key.replace("""norm2""" ,"""output.LayerNorm""" ) if "norm" in orig_key: _lowerCAmelCase : List[str] = orig_key.replace("""norm""" ,"""LayerNorm""" ) if "transformer" in orig_key: _lowerCAmelCase : Optional[Any] = orig_key.split(""".""" )[0].split("""_""" )[-1] _lowerCAmelCase : str = orig_key.replace(f"transformer_{layer_num}" ,f"encoder.layer.{layer_num}" ) if "mha.attn" in orig_key: _lowerCAmelCase : List[Any] = orig_key.replace("""mha.attn""" ,"""attention.self""" ) if "mha" in orig_key: _lowerCAmelCase : str = orig_key.replace("""mha""" ,"""attention""" ) if "W_q" in orig_key: _lowerCAmelCase : int = orig_key.replace("""W_q""" ,"""self.query""" ) if "W_k" in orig_key: _lowerCAmelCase : Tuple = orig_key.replace("""W_k""" ,"""self.key""" ) if "W_v" in orig_key: _lowerCAmelCase : Optional[Any] = orig_key.replace("""W_v""" ,"""self.value""" ) if "ff1" in orig_key: _lowerCAmelCase : Optional[Any] = orig_key.replace("""ff1""" ,"""intermediate.dense""" ) if "ff2" in orig_key: _lowerCAmelCase : int = orig_key.replace("""ff2""" ,"""output.dense""" ) if "ff" in orig_key: _lowerCAmelCase : Any = orig_key.replace("""ff""" ,"""output.dense""" ) if "mlm_class" in orig_key: _lowerCAmelCase : Optional[Any] = orig_key.replace("""mlm.mlm_class""" ,"""cls.predictions.decoder""" ) if "mlm" in orig_key: _lowerCAmelCase : List[str] = orig_key.replace("""mlm""" ,"""cls.predictions.transform""" ) if "cls" not in orig_key: _lowerCAmelCase : List[str] = """yoso.""" + orig_key return orig_key def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ,_lowerCamelCase : List[Any] ) -> Tuple: for key in orig_state_dict.copy().keys(): _lowerCAmelCase : List[str] = orig_state_dict.pop(_lowerCamelCase ) if ("pooler" in key) or ("sen_class" in key): continue else: _lowerCAmelCase : List[str] = val _lowerCAmelCase : Any = orig_state_dict["""cls.predictions.decoder.bias"""] _lowerCAmelCase : str = torch.arange(_lowerCamelCase ).expand((1, -1) ) + 2 return orig_state_dict def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Dict ,_lowerCamelCase : Dict ) -> Any: _lowerCAmelCase : int = torch.load(_lowerCamelCase ,map_location="""cpu""" )["""model_state_dict"""] _lowerCAmelCase : Any = YosoConfig.from_json_file(_lowerCamelCase ) _lowerCAmelCase : Optional[int] = YosoForMaskedLM(_lowerCamelCase ) _lowerCAmelCase : int = convert_checkpoint_helper(config.max_position_embeddings ,_lowerCamelCase ) print(model.load_state_dict(_lowerCamelCase ) ) model.eval() model.save_pretrained(_lowerCamelCase ) print(f"Checkpoint successfuly converted. Model saved at {pytorch_dump_path}" ) if __name__ == "__main__": _a : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--pytorch_model_path', default=None, type=str, required=True, help='Path to YOSO pytorch checkpoint.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The json file for YOSO model config.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) _a : List[str] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
663
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
1
"""simple docstring""" from PIL import Image def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Image ,_lowerCamelCase : int ) -> Image: _lowerCAmelCase : Any = (259 * (level + 255)) / (255 * (259 - level)) def contrast(_lowerCamelCase : int ) -> int: return int(128 + factor * (c - 128) ) return img.point(_lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 _a : str = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
663
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : str = CpmAntTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """<d>""", """</d>""", """<s>""", """</s>""", """</_>""", """<unk>""", """<pad>""", """</n>""", """我""", """是""", """C""", """P""", """M""", """A""", """n""", """t""", ] _lowerCAmelCase : int = 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] ) ) @tooslow def __A ( self ): _lowerCAmelCase : Tuple = CpmAntTokenizer.from_pretrained("""openbmb/cpm-ant-10b""" ) _lowerCAmelCase : Optional[Any] = """今天天气真好!""" _lowerCAmelCase : Any = ["""今天""", """天气""", """真""", """好""", """!"""] _lowerCAmelCase : str = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = """今天天气真好!""" _lowerCAmelCase : Optional[Any] = [tokenizer.bos_token] + tokens _lowerCAmelCase : Optional[int] = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) _lowerCAmelCase : Tuple = tokenizer.decode(a__ ) self.assertEqual(a__ , a__ )
663
1
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _a : str = logging.get_logger(__name__) _a : Tuple = { 'google/pix2struct-textcaps-base': ( 'https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json' ), } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Tuple = "pix2struct_text_model" _UpperCamelCase : Optional[Any] = ["past_key_values"] _UpperCamelCase : Optional[int] = { "hidden_size": "hidden_size", "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , a__=50244 , a__=768 , a__=64 , a__=2048 , a__=12 , a__=12 , a__=32 , a__=128 , a__=0.1 , a__=1e-6 , a__=1.0 , a__="gelu_new" , a__=0 , a__=False , a__=0 , a__=1 , a__=False , a__=True , **a__ , ): _lowerCAmelCase : str = vocab_size _lowerCAmelCase : str = hidden_size _lowerCAmelCase : str = d_kv _lowerCAmelCase : Optional[Any] = d_ff _lowerCAmelCase : Optional[Any] = num_layers _lowerCAmelCase : Tuple = num_heads _lowerCAmelCase : Union[str, Any] = relative_attention_num_buckets _lowerCAmelCase : Union[str, Any] = relative_attention_max_distance _lowerCAmelCase : str = dropout_rate _lowerCAmelCase : Optional[int] = layer_norm_epsilon _lowerCAmelCase : Tuple = initializer_factor _lowerCAmelCase : str = use_cache _lowerCAmelCase : List[Any] = eos_token_id _lowerCAmelCase : str = decoder_start_token_id # for backwards compatibility _lowerCAmelCase : Tuple = dense_act_fn super().__init__( pad_token_id=a__ , eos_token_id=a__ , decoder_start_token_id=a__ , tie_word_embeddings=a__ , is_decoder=a__ , **a__ , ) @classmethod def __A ( cls , a__ , **a__ ): cls._set_token_in_kwargs(a__ ) _lowerCAmelCase , _lowerCAmelCase : Any = cls.get_config_dict(a__ , **a__ ) # get the text config dict if we are loading from Pix2StructConfig if config_dict.get("""model_type""" ) == "pix2struct": _lowerCAmelCase : str = config_dict["""text_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 ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[str] = "pix2struct_vision_model" def __init__( self , a__=768 , a__=768 , a__=2048 , a__=64 , a__=12 , a__=12 , a__="gelu_new" , a__=1e-6 , a__=0.0 , a__=0.0 , a__=1e-10 , a__=1.0 , a__=4096 , a__=32 , a__=128 , **a__ , ): super().__init__(**a__ ) _lowerCAmelCase : List[str] = hidden_size _lowerCAmelCase : Optional[Any] = patch_embed_hidden_size _lowerCAmelCase : Any = d_ff _lowerCAmelCase : Optional[Any] = dropout_rate _lowerCAmelCase : int = num_hidden_layers _lowerCAmelCase : Optional[int] = num_attention_heads _lowerCAmelCase : Tuple = initializer_range _lowerCAmelCase : Optional[Any] = initializer_factor _lowerCAmelCase : Optional[Any] = attention_dropout _lowerCAmelCase : Tuple = layer_norm_eps _lowerCAmelCase : int = dense_act_fn _lowerCAmelCase : Union[str, Any] = seq_len _lowerCAmelCase : Optional[Any] = relative_attention_num_buckets _lowerCAmelCase : Dict = relative_attention_max_distance _lowerCAmelCase : Union[str, Any] = d_kv @classmethod def __A ( cls , a__ , **a__ ): cls._set_token_in_kwargs(a__ ) _lowerCAmelCase , _lowerCAmelCase : Tuple = cls.get_config_dict(a__ , **a__ ) # get the vision config dict if we are loading from Pix2StructConfig if config_dict.get("""model_type""" ) == "pix2struct": _lowerCAmelCase : int = 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 ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Any = "pix2struct" _UpperCamelCase : int = True def __init__( self , a__=None , a__=None , a__=1.0 , a__=0.0_2 , a__=False , a__=False , a__=True , **a__ , ): super().__init__(tie_word_embeddings=a__ , is_encoder_decoder=a__ , **a__ ) if text_config is None: _lowerCAmelCase : Any = {} logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values.""" ) if vision_config is None: _lowerCAmelCase : List[str] = {} logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values.""" ) _lowerCAmelCase : Tuple = PixaStructTextConfig(**a__ ) _lowerCAmelCase : List[str] = PixaStructVisionConfig(**a__ ) _lowerCAmelCase : Optional[int] = self.text_config.decoder_start_token_id _lowerCAmelCase : List[str] = self.text_config.pad_token_id _lowerCAmelCase : Dict = self.text_config.eos_token_id _lowerCAmelCase : Optional[int] = initializer_factor _lowerCAmelCase : Optional[Any] = initializer_range _lowerCAmelCase : Optional[Any] = self.initializer_range _lowerCAmelCase : Union[str, Any] = self.initializer_range _lowerCAmelCase : List[Any] = is_vqa @classmethod def __A ( cls , a__ , a__ , **a__ ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **a__ ) def __A ( self ): _lowerCAmelCase : Optional[int] = copy.deepcopy(self.__dict__ ) _lowerCAmelCase : Optional[Any] = self.text_config.to_dict() _lowerCAmelCase : Union[str, Any] = self.vision_config.to_dict() _lowerCAmelCase : Any = self.__class__.model_type return output
663
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
1
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> Any: _lowerCAmelCase : Tuple = 384 _lowerCAmelCase : Optional[Any] = 7 if "tiny" in model_name: _lowerCAmelCase : List[Any] = 96 _lowerCAmelCase : List[str] = (2, 2, 6, 2) _lowerCAmelCase : int = (3, 6, 12, 24) elif "small" in model_name: _lowerCAmelCase : Any = 96 _lowerCAmelCase : Dict = (2, 2, 18, 2) _lowerCAmelCase : Any = (3, 6, 12, 24) elif "base" in model_name: _lowerCAmelCase : Optional[int] = 128 _lowerCAmelCase : int = (2, 2, 18, 2) _lowerCAmelCase : Union[str, Any] = (4, 8, 16, 32) _lowerCAmelCase : str = 12 _lowerCAmelCase : List[Any] = 512 elif "large" in model_name: _lowerCAmelCase : List[str] = 192 _lowerCAmelCase : Tuple = (2, 2, 18, 2) _lowerCAmelCase : Union[str, Any] = (6, 12, 24, 48) _lowerCAmelCase : Union[str, Any] = 12 _lowerCAmelCase : Optional[int] = 768 # set label information _lowerCAmelCase : Dict = 150 _lowerCAmelCase : List[Any] = """huggingface/label-files""" _lowerCAmelCase : Optional[Any] = """ade20k-id2label.json""" _lowerCAmelCase : Union[str, Any] = json.load(open(hf_hub_download(_lowerCamelCase ,_lowerCamelCase ,repo_type="""dataset""" ) ,"""r""" ) ) _lowerCAmelCase : Tuple = {int(_lowerCamelCase ): v for k, v in idalabel.items()} _lowerCAmelCase : int = {v: k for k, v in idalabel.items()} _lowerCAmelCase : List[str] = SwinConfig( embed_dim=_lowerCamelCase ,depths=_lowerCamelCase ,num_heads=_lowerCamelCase ,window_size=_lowerCamelCase ,out_features=["""stage1""", """stage2""", """stage3""", """stage4"""] ,) _lowerCAmelCase : Dict = UperNetConfig( backbone_config=_lowerCamelCase ,auxiliary_in_channels=_lowerCamelCase ,num_labels=_lowerCamelCase ,idalabel=_lowerCamelCase ,labelaid=_lowerCamelCase ,) return config def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Optional[Any]: _lowerCAmelCase : List[str] = [] # fmt: off # stem rename_keys.append(("""backbone.patch_embed.projection.weight""", """backbone.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""backbone.patch_embed.projection.bias""", """backbone.embeddings.patch_embeddings.projection.bias""") ) rename_keys.append(("""backbone.patch_embed.norm.weight""", """backbone.embeddings.norm.weight""") ) rename_keys.append(("""backbone.patch_embed.norm.bias""", """backbone.embeddings.norm.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}.blocks.{j}.norm1.weight", f"backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.norm1.bias", f"backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table", f"backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index", f"backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight", f"backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias", f"backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.norm2.weight", f"backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.norm2.bias", f"backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight", f"backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias", f"backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight", f"backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight") ) rename_keys.append((f"backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias", f"backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias") ) if i < 3: rename_keys.append((f"backbone.stages.{i}.downsample.reduction.weight", f"backbone.encoder.layers.{i}.downsample.reduction.weight") ) rename_keys.append((f"backbone.stages.{i}.downsample.norm.weight", f"backbone.encoder.layers.{i}.downsample.norm.weight") ) rename_keys.append((f"backbone.stages.{i}.downsample.norm.bias", f"backbone.encoder.layers.{i}.downsample.norm.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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : Dict ,_lowerCamelCase : Optional[int] ) -> Optional[Any]: _lowerCAmelCase : Optional[int] = dct.pop(_lowerCamelCase ) _lowerCAmelCase : Dict = val def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Any ,_lowerCamelCase : int ) -> str: _lowerCAmelCase : Tuple = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _lowerCAmelCase : Optional[Any] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _lowerCAmelCase : str = state_dict.pop(f"backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight" ) _lowerCAmelCase : List[Any] = state_dict.pop(f"backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict _lowerCAmelCase : Optional[Any] = in_proj_weight[:dim, :] _lowerCAmelCase : Union[str, Any] = in_proj_bias[: dim] _lowerCAmelCase : List[str] = in_proj_weight[ dim : dim * 2, : ] _lowerCAmelCase : Tuple = in_proj_bias[ dim : dim * 2 ] _lowerCAmelCase : Tuple = in_proj_weight[ -dim :, : ] _lowerCAmelCase : Any = in_proj_bias[-dim :] # fmt: on def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[int] ) -> Optional[Any]: _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = x.shape _lowerCAmelCase : Any = x.reshape(_lowerCamelCase ,4 ,in_channel // 4 ) _lowerCAmelCase : Optional[Any] = x[:, [0, 2, 1, 3], :].transpose(1 ,2 ).reshape(_lowerCamelCase ,_lowerCamelCase ) return x def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ) -> Dict: _lowerCAmelCase , _lowerCAmelCase : str = x.shape _lowerCAmelCase : List[str] = x.reshape(_lowerCamelCase ,in_channel // 4 ,4 ) _lowerCAmelCase : int = x[:, :, [0, 2, 1, 3]].transpose(1 ,2 ).reshape(_lowerCamelCase ,_lowerCamelCase ) return x def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Dict: _lowerCAmelCase : Tuple = x.shape[0] _lowerCAmelCase : Optional[Any] = x.reshape(4 ,in_channel // 4 ) _lowerCAmelCase : Optional[int] = x[[0, 2, 1, 3], :].transpose(0 ,1 ).reshape(_lowerCamelCase ) return x def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> List[str]: _lowerCAmelCase : Tuple = x.shape[0] _lowerCAmelCase : Any = x.reshape(in_channel // 4 ,4 ) _lowerCAmelCase : Any = x[:, [0, 2, 1, 3]].transpose(0 ,1 ).reshape(_lowerCamelCase ) return x def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ,_lowerCamelCase : List[Any] ,_lowerCamelCase : List[Any] ) -> List[Any]: _lowerCAmelCase : Optional[Any] = { """upernet-swin-tiny""": """https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth""", """upernet-swin-small""": """https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth""", """upernet-swin-base""": """https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth""", """upernet-swin-large""": """https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth""", } _lowerCAmelCase : Union[str, Any] = model_name_to_url[model_name] _lowerCAmelCase : Optional[Any] = torch.hub.load_state_dict_from_url(_lowerCamelCase ,map_location="""cpu""" ,file_name=_lowerCamelCase )[ """state_dict""" ] for name, param in state_dict.items(): print(_lowerCamelCase ,param.shape ) _lowerCAmelCase : Optional[Any] = get_upernet_config(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = UperNetForSemanticSegmentation(_lowerCamelCase ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): _lowerCAmelCase : Optional[int] = state_dict.pop(_lowerCamelCase ) if "bn" in key: _lowerCAmelCase : List[Any] = key.replace("""bn""" ,"""batch_norm""" ) _lowerCAmelCase : str = val # rename keys _lowerCAmelCase : int = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) read_in_q_k_v(_lowerCamelCase ,config.backbone_config ) # fix downsample parameters for key, value in state_dict.items(): if "downsample" in key: if "reduction" in key: _lowerCAmelCase : Any = reverse_correct_unfold_reduction_order(_lowerCamelCase ) if "norm" in key: _lowerCAmelCase : Any = reverse_correct_unfold_norm_order(_lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) # verify on image _lowerCAmelCase : Optional[Any] = """https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg""" _lowerCAmelCase : List[Any] = Image.open(requests.get(_lowerCamelCase ,stream=_lowerCamelCase ).raw ).convert("""RGB""" ) _lowerCAmelCase : Tuple = SegformerImageProcessor() _lowerCAmelCase : Optional[Any] = processor(_lowerCamelCase ,return_tensors="""pt""" ).pixel_values with torch.no_grad(): _lowerCAmelCase : Dict = model(_lowerCamelCase ) _lowerCAmelCase : List[str] = outputs.logits print(logits.shape ) print("""First values of logits:""" ,logits[0, 0, :3, :3] ) # assert values if model_name == "upernet-swin-tiny": _lowerCAmelCase : Union[str, Any] = torch.tensor( [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]] ) elif model_name == "upernet-swin-small": _lowerCAmelCase : Tuple = torch.tensor( [[-7.19_21, -7.19_21, -6.95_32], [-7.19_21, -7.19_21, -6.95_32], [-7.09_08, -7.09_08, -6.85_34]] ) elif model_name == "upernet-swin-base": _lowerCAmelCase : Optional[Any] = torch.tensor( [[-6.58_51, -6.58_51, -6.43_30], [-6.58_51, -6.58_51, -6.43_30], [-6.47_63, -6.47_63, -6.32_54]] ) elif model_name == "upernet-swin-large": _lowerCAmelCase : Tuple = torch.tensor( [[-7.52_97, -7.52_97, -7.38_02], [-7.52_97, -7.52_97, -7.38_02], [-7.40_44, -7.40_44, -7.25_86]] ) print("""Logits:""" ,outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] ,_lowerCamelCase ,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(_lowerCamelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_lowerCamelCase ) 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 : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='upernet-swin-tiny', type=str, choices=[F"""upernet-swin-{size}""" for size in ['tiny', 'small', 'base', 'large']], help='Name of the Swin + 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 : Optional[Any] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
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 SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> int: random.seed(_lowerCamelCase ) np.random.seed(_lowerCamelCase ) torch.manual_seed(_lowerCamelCase ) torch.cuda.manual_seed_all(_lowerCamelCase ) # ^^ safe to call this function even if cuda is not available class __A : def __init__( self , a__ , a__ = 0.9_9_9_9 , a__ = 0.0 , a__ = 0 , a__ = False , a__ = 1.0 , a__ = 2 / 3 , a__ = None , a__ = None , **a__ , ): if isinstance(a__ , torch.nn.Module ): _lowerCAmelCase : Union[str, Any] = ( """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__ , ) _lowerCAmelCase : Optional[Any] = parameters.parameters() # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility _lowerCAmelCase : List[Any] = True if kwargs.get("""max_value""" , a__ ) is not None: _lowerCAmelCase : List[Any] = """The `max_value` argument is deprecated. Please use `decay` instead.""" deprecate("""max_value""" , """1.0.0""" , a__ , standard_warn=a__ ) _lowerCAmelCase : str = kwargs["""max_value"""] if kwargs.get("""min_value""" , a__ ) is not None: _lowerCAmelCase : List[Any] = """The `min_value` argument is deprecated. Please use `min_decay` instead.""" deprecate("""min_value""" , """1.0.0""" , a__ , standard_warn=a__ ) _lowerCAmelCase : Tuple = kwargs["""min_value"""] _lowerCAmelCase : Optional[int] = list(a__ ) _lowerCAmelCase : List[Any] = [p.clone().detach() for p in parameters] if kwargs.get("""device""" , a__ ) is not None: _lowerCAmelCase : Union[str, Any] = """The `device` argument is deprecated. Please use `to` instead.""" deprecate("""device""" , """1.0.0""" , a__ , standard_warn=a__ ) self.to(device=kwargs["""device"""] ) _lowerCAmelCase : Any = None _lowerCAmelCase : Dict = decay _lowerCAmelCase : Optional[int] = min_decay _lowerCAmelCase : Union[str, Any] = update_after_step _lowerCAmelCase : Union[str, Any] = use_ema_warmup _lowerCAmelCase : Tuple = inv_gamma _lowerCAmelCase : Tuple = power _lowerCAmelCase : Union[str, Any] = 0 _lowerCAmelCase : Union[str, Any] = None # set in `step()` _lowerCAmelCase : List[str] = model_cls _lowerCAmelCase : List[str] = model_config @classmethod def __A ( cls , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = model_cls.load_config(a__ , return_unused_kwargs=a__ ) _lowerCAmelCase : Dict = model_cls.from_pretrained(a__ ) _lowerCAmelCase : Tuple = cls(model.parameters() , model_cls=a__ , model_config=model.config ) ema_model.load_state_dict(a__ ) return ema_model def __A ( self , a__ ): 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__.""" ) _lowerCAmelCase : Optional[int] = self.model_cls.from_config(self.model_config ) _lowerCAmelCase : Any = 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__ ): _lowerCAmelCase : str = max(0 , optimization_step - self.update_after_step - 1 ) if step <= 0: return 0.0 if self.use_ema_warmup: _lowerCAmelCase : List[str] = 1 - (1 + step / self.inv_gamma) ** -self.power else: _lowerCAmelCase : Tuple = (1 + step) / (10 + step) _lowerCAmelCase : List[Any] = min(a__ , self.decay ) # make sure decay is not smaller than min_decay _lowerCAmelCase : Tuple = max(a__ , self.min_decay ) return cur_decay_value @torch.no_grad() def __A ( self , a__ ): if isinstance(a__ , torch.nn.Module ): _lowerCAmelCase : Any = ( """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__ , ) _lowerCAmelCase : Union[str, Any] = parameters.parameters() _lowerCAmelCase : str = list(a__ ) self.optimization_step += 1 # Compute the decay factor for the exponential moving average. _lowerCAmelCase : int = self.get_decay(self.optimization_step ) _lowerCAmelCase : Any = decay _lowerCAmelCase : str = 1 - decay _lowerCAmelCase : Optional[int] = 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(): _lowerCAmelCase : str = 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__ ): _lowerCAmelCase : Optional[int] = 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 ): _lowerCAmelCase : List[Any] = [ 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 ): 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__ ): _lowerCAmelCase : Dict = [param.detach().cpu().clone() for param in parameters] def __A ( self , a__ ): 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. _lowerCAmelCase : List[Any] = None def __A ( self , a__ ): _lowerCAmelCase : List[str] = copy.deepcopy(a__ ) _lowerCAmelCase : Union[str, Any] = 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""" ) _lowerCAmelCase : int = state_dict.get("""min_decay""" , self.min_decay ) if not isinstance(self.min_decay , a__ ): raise ValueError("""Invalid min_decay""" ) _lowerCAmelCase : Union[str, Any] = state_dict.get("""optimization_step""" , self.optimization_step ) if not isinstance(self.optimization_step , a__ ): raise ValueError("""Invalid optimization_step""" ) _lowerCAmelCase : str = state_dict.get("""update_after_step""" , self.update_after_step ) if not isinstance(self.update_after_step , a__ ): raise ValueError("""Invalid update_after_step""" ) _lowerCAmelCase : Dict = state_dict.get("""use_ema_warmup""" , self.use_ema_warmup ) if not isinstance(self.use_ema_warmup , a__ ): raise ValueError("""Invalid use_ema_warmup""" ) _lowerCAmelCase : List[str] = state_dict.get("""inv_gamma""" , self.inv_gamma ) if not isinstance(self.inv_gamma , (float, int) ): raise ValueError("""Invalid inv_gamma""" ) _lowerCAmelCase : Optional[int] = state_dict.get("""power""" , self.power ) if not isinstance(self.power , (float, int) ): raise ValueError("""Invalid power""" ) _lowerCAmelCase : List[Any] = state_dict.get("""shadow_params""" , a__ ) if shadow_params is not None: _lowerCAmelCase : int = 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""" )
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> bool: 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...') _a : int = int(input('Enter number: ').strip()) print(F"""{number} is {"" if perfect(number) else "not "}a Perfect Number.""")
663
1
"""simple docstring""" import numpy as np from transformers import Pipeline def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> Dict: _lowerCAmelCase : List[str] = np.max(_lowerCamelCase ,axis=-1 ,keepdims=_lowerCamelCase ) _lowerCAmelCase : List[str] = np.exp(outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 ,keepdims=_lowerCamelCase ) class __A ( SCREAMING_SNAKE_CASE_ ): def __A ( self , **a__ ): _lowerCAmelCase : int = {} if "second_text" in kwargs: _lowerCAmelCase : Optional[int] = kwargs["""second_text"""] return preprocess_kwargs, {}, {} def __A ( self , a__ , a__=None ): return self.tokenizer(a__ , text_pair=a__ , return_tensors=self.framework ) def __A ( self , a__ ): return self.model(**a__ ) def __A ( self , a__ ): _lowerCAmelCase : Any = model_outputs.logits[0].numpy() _lowerCAmelCase : List[Any] = softmax(a__ ) _lowerCAmelCase : List[str] = np.argmax(a__ ) _lowerCAmelCase : Optional[int] = self.model.config.idalabel[best_class] _lowerCAmelCase : Optional[Any] = probabilities[best_class].item() _lowerCAmelCase : Any = logits.tolist() return {"label": label, "score": score, "logits": logits}
663
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
1
"""simple docstring""" _a : Any = {'a': ['c', 'b'], 'b': ['d', 'e'], 'c': [], 'd': [], 'e': []} _a : Any = ['a', 'b', 'c', 'd', 'e'] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ) -> int: _lowerCAmelCase : List[Any] = start # add current to visited visited.append(_lowerCamelCase ) _lowerCAmelCase : Union[str, Any] = edges[current] for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: _lowerCAmelCase : str = topological_sort(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # if all neighbors visited add current to sort sort.append(_lowerCamelCase ) # if all vertices haven't been visited select a new one to visit if len(_lowerCamelCase ) != len(_lowerCamelCase ): for vertice in vertices: if vertice not in visited: _lowerCAmelCase : List[Any] = topological_sort(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # return sort return sort if __name__ == "__main__": _a : Tuple = topological_sort('a', [], []) print(sort)
663
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : int = StableDiffusionInpaintPipeline _UpperCamelCase : str = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS _UpperCamelCase : Optional[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS _UpperCamelCase : List[Any] = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess _UpperCamelCase : str = frozenset([] ) def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Union[str, Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=a__ , ) _lowerCAmelCase : Optional[int] = PNDMScheduler(skip_prk_steps=a__ ) torch.manual_seed(0 ) _lowerCAmelCase : List[str] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) _lowerCAmelCase : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="""gelu""" , projection_dim=512 , ) _lowerCAmelCase : List[str] = CLIPTextModel(a__ ) _lowerCAmelCase : Tuple = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) _lowerCAmelCase : str = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def __A ( self , a__ , a__=0 ): # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched _lowerCAmelCase : str = floats_tensor((1, 3, 32, 32) , rng=random.Random(a__ ) ).to(a__ ) _lowerCAmelCase : List[str] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _lowerCAmelCase : str = Image.fromarray(np.uinta(a__ ) ).convert("""RGB""" ).resize((64, 64) ) _lowerCAmelCase : List[Any] = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((64, 64) ) if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Tuple = torch.manual_seed(a__ ) else: _lowerCAmelCase : List[Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Union[str, Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def __A ( self ): _lowerCAmelCase : Dict = """cpu""" # ensure determinism for the device-dependent torch.Generator _lowerCAmelCase : Dict = self.get_dummy_components() _lowerCAmelCase : Any = StableDiffusionInpaintPipeline(**a__ ) _lowerCAmelCase : Any = sd_pipe.to(a__ ) sd_pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = self.get_dummy_inputs(a__ ) _lowerCAmelCase : Optional[int] = sd_pipe(**a__ ).images _lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _lowerCAmelCase : int = np.array([0.4_7_2_7, 0.5_7_3_5, 0.3_9_4_1, 0.5_4_4_6, 0.5_9_2_6, 0.4_3_9_4, 0.5_0_6_2, 0.4_6_5_4, 0.4_4_7_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _lowerCAmelCase : List[Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _lowerCAmelCase : List[Any] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) _lowerCAmelCase : str = """stabilityai/stable-diffusion-2-inpainting""" _lowerCAmelCase : Optional[Any] = StableDiffusionInpaintPipeline.from_pretrained(a__ , safety_checker=a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _lowerCAmelCase : Tuple = """Face of a yellow cat, high resolution, sitting on a park bench""" _lowerCAmelCase : Tuple = torch.manual_seed(0 ) _lowerCAmelCase : Optional[int] = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , output_type="""np""" , ) _lowerCAmelCase : Optional[Any] = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9e-3 def __A ( self ): _lowerCAmelCase : int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _lowerCAmelCase : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _lowerCAmelCase : Any = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) _lowerCAmelCase : Optional[Any] = """stabilityai/stable-diffusion-2-inpainting""" _lowerCAmelCase : str = StableDiffusionInpaintPipeline.from_pretrained( a__ , torch_dtype=torch.floataa , safety_checker=a__ , ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing() _lowerCAmelCase : int = """Face of a yellow cat, high resolution, sitting on a park bench""" _lowerCAmelCase : List[str] = torch.manual_seed(0 ) _lowerCAmelCase : Any = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , output_type="""np""" , ) _lowerCAmelCase : List[str] = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5e-1 def __A ( self ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _lowerCAmelCase : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _lowerCAmelCase : List[str] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _lowerCAmelCase : Optional[Any] = """stabilityai/stable-diffusion-2-inpainting""" _lowerCAmelCase : Optional[int] = PNDMScheduler.from_pretrained(a__ , subfolder="""scheduler""" ) _lowerCAmelCase : Optional[int] = StableDiffusionInpaintPipeline.from_pretrained( a__ , safety_checker=a__ , scheduler=a__ , torch_dtype=torch.floataa , ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() _lowerCAmelCase : Tuple = """Face of a yellow cat, high resolution, sitting on a park bench""" _lowerCAmelCase : Optional[Any] = torch.manual_seed(0 ) _lowerCAmelCase : str = pipe( prompt=a__ , image=a__ , mask_image=a__ , generator=a__ , num_inference_steps=2 , output_type="""np""" , ) _lowerCAmelCase : int = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.6_5 * 10**9
663
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
1
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __A ( unittest.TestCase ): _UpperCamelCase : str = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING _UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = AudioClassificationPipeline(model=a__ , feature_extractor=a__ ) # test with a raw waveform _lowerCAmelCase : Optional[int] = np.zeros((34000,) ) _lowerCAmelCase : Optional[Any] = np.zeros((14000,) ) return audio_classifier, [audioa, audio] def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = examples _lowerCAmelCase : List[Any] = audio_classifier(a__ ) # by default a model is initialized with num_labels=2 self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) _lowerCAmelCase : Tuple = audio_classifier(a__ , top_k=1 ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) self.run_torchaudio(a__ ) @require_torchaudio def __A ( self , a__ ): import datasets # test with a local file _lowerCAmelCase : int = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) _lowerCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] _lowerCAmelCase : str = audio_classifier(a__ ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) @require_torch def __A ( self ): _lowerCAmelCase : int = """anton-l/wav2vec2-random-tiny-classifier""" _lowerCAmelCase : Optional[Any] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : Any = np.ones((8000,) ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) _lowerCAmelCase : List[str] = [ {"""score""": 0.0_8_4_2, """label""": """no"""}, {"""score""": 0.0_8_3_8, """label""": """up"""}, {"""score""": 0.0_8_3_7, """label""": """go"""}, {"""score""": 0.0_8_3_4, """label""": """right"""}, ] _lowerCAmelCase : str = [ {"""score""": 0.0_8_4_5, """label""": """stop"""}, {"""score""": 0.0_8_4_4, """label""": """on"""}, {"""score""": 0.0_8_4_1, """label""": """right"""}, {"""score""": 0.0_8_3_4, """label""": """left"""}, ] self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) _lowerCAmelCase : int = {"""array""": np.ones((8000,) ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate} _lowerCAmelCase : int = audio_classifier(a__ , top_k=4 ) self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __A ( self ): import datasets _lowerCAmelCase : Optional[Any] = """superb/wav2vec2-base-superb-ks""" _lowerCAmelCase : List[str] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : str = datasets.load_dataset("""anton-l/superb_dummy""" , """ks""" , split="""test""" ) _lowerCAmelCase : Optional[Any] = np.array(dataset[3]["""speech"""] , dtype=np.floataa ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) self.assertEqual( nested_simplify(a__ , decimals=3 ) , [ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] , ) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def __A ( self ): pass
663
"""simple docstring""" _a : Optional[Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[str] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : str = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
663
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _a : Tuple = logging.get_logger(__name__) _a : List[Any] = { 'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json', 'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json', } class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[Any] = "luke" def __init__( self , a__=50267 , a__=500000 , a__=768 , a__=256 , a__=12 , a__=12 , a__=3072 , a__="gelu" , a__=0.1 , a__=0.1 , a__=512 , a__=2 , a__=0.0_2 , a__=1e-12 , a__=True , a__=None , a__=1 , a__=0 , a__=2 , **a__ , ): super().__init__(pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ , **a__ ) _lowerCAmelCase : str = vocab_size _lowerCAmelCase : Tuple = entity_vocab_size _lowerCAmelCase : List[str] = hidden_size _lowerCAmelCase : int = entity_emb_size _lowerCAmelCase : Dict = num_hidden_layers _lowerCAmelCase : List[Any] = num_attention_heads _lowerCAmelCase : Union[str, Any] = hidden_act _lowerCAmelCase : Tuple = intermediate_size _lowerCAmelCase : Union[str, Any] = hidden_dropout_prob _lowerCAmelCase : Any = attention_probs_dropout_prob _lowerCAmelCase : List[Any] = max_position_embeddings _lowerCAmelCase : Any = type_vocab_size _lowerCAmelCase : Union[str, Any] = initializer_range _lowerCAmelCase : Union[str, Any] = layer_norm_eps _lowerCAmelCase : int = use_entity_aware_attention _lowerCAmelCase : Any = classifier_dropout
663
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Tuple = 13 _lowerCAmelCase : Tuple = 7 _lowerCAmelCase : Any = 30 _lowerCAmelCase : Optional[int] = self.seq_length + self.mem_len _lowerCAmelCase : Dict = 15 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : Any = True _lowerCAmelCase : List[str] = 99 _lowerCAmelCase : List[Any] = [10, 50, 80] _lowerCAmelCase : Tuple = 32 _lowerCAmelCase : int = 32 _lowerCAmelCase : Dict = 4 _lowerCAmelCase : List[str] = 8 _lowerCAmelCase : Tuple = 128 _lowerCAmelCase : Any = 2 _lowerCAmelCase : List[Any] = 2 _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Optional[Any] = 1 _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : List[Any] = 3 _lowerCAmelCase : Optional[int] = self.vocab_size - 1 _lowerCAmelCase : Dict = 0.0_1 def __A ( self ): _lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Union[str, Any] = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[int] = TFTransfoXLModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() _lowerCAmelCase : Optional[Any] = {"""input_ids""": input_ids_a, """mems""": mems_a} _lowerCAmelCase , _lowerCAmelCase : List[Any] = model(a__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : int = TFTransfoXLLMHeadModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase : Dict = {"""input_ids""": input_ids_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = model([input_ids_a, mems_a] ).to_tuple() _lowerCAmelCase : Any = {"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFTransfoXLForSequenceClassification(a__ ) _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : Dict = config_and_inputs _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids_a} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : Any = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : str = False _UpperCamelCase : str = False _UpperCamelCase : Tuple = False _UpperCamelCase : Any = False def __A ( self , a__ , a__ , a__ , a__ , a__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLModelTester(self ) _lowerCAmelCase : List[Any] = ConfigTester(self , config_class=a__ , d_embed=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a__ ) def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a__ ) def __A ( self ): _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase : List[Any] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(a__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _lowerCAmelCase : str = model.get_output_embeddings() assert isinstance(a__ , tf.keras.layers.Layer ) _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None else: _lowerCAmelCase : Union[str, Any] = model.get_output_embeddings() assert x is None _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None def __A ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Union[str, Any] = TFTransfoXLModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" ) def __A ( self ): pass @require_tf class __A ( unittest.TestCase ): @unittest.skip("""Skip test until #12651 is resolved.""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" ) # fmt: off _lowerCAmelCase : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _lowerCAmelCase : List[Any] = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _lowerCAmelCase : Tuple = model.generate(a__ , max_length=200 , do_sample=a__ ) self.assertListEqual(output_ids[0].numpy().tolist() , a__ )
663
1
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ) -> Optional[Any]: _lowerCAmelCase : Optional[Any] = [] _lowerCAmelCase : int = [] _lowerCAmelCase : Any = { """^""": 3, """*""": 2, """/""": 2, """%""": 2, """+""": 1, """-""": 1, } # Priority of each operator _lowerCAmelCase : Any = len(_lowerCamelCase ) if (len(_lowerCamelCase ) > 7) else 7 # Print table header for output print( """Symbol""".center(8 ) ,"""Stack""".center(_lowerCamelCase ) ,"""Postfix""".center(_lowerCamelCase ) ,sep=""" | """ ,) print("""-""" * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(_lowerCamelCase ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(_lowerCamelCase ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(_lowerCamelCase ) == 0: stack.append(_lowerCamelCase ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(_lowerCamelCase ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(_lowerCamelCase ) # push x to stack print( x.center(8 ) ,("""""".join(_lowerCamelCase )).ljust(_lowerCamelCase ) ,("""""".join(_lowerCamelCase )).ljust(_lowerCamelCase ) ,sep=""" | """ ,) # Output in tabular format while len(_lowerCamelCase ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( """ """.center(8 ) ,("""""".join(_lowerCamelCase )).ljust(_lowerCamelCase ) ,("""""".join(_lowerCamelCase )).ljust(_lowerCamelCase ) ,sep=""" | """ ,) # Output in tabular format return "".join(_lowerCamelCase ) # return Postfix as str def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ) -> Union[str, Any]: _lowerCAmelCase : Tuple = list(infix[::-1] ) # reverse the infix equation for i in range(len(_lowerCamelCase ) ): if infix[i] == "(": _lowerCAmelCase : Optional[Any] = """)""" # change "(" to ")" elif infix[i] == ")": _lowerCAmelCase : Tuple = """(""" # change ")" to "(" return (infix_2_postfix("""""".join(_lowerCamelCase ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": _a : Optional[int] = input('\nEnter an Infix Equation = ') # Input an Infix equation _a : Union[str, Any] = ''.join(Infix.split()) # Remove spaces from the input print('\n\t', Infix, '(Infix) -> ', infix_2_prefix(Infix), '(Prefix)')
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Any = "Salesforce/blip-image-captioning-base" _UpperCamelCase : Any = ( "This is a tool that generates a description of an image. It takes an input named `image` which should be the " "image to caption, and returns a text that contains the description in English." ) _UpperCamelCase : Dict = "image_captioner" _UpperCamelCase : Dict = AutoModelForVisionaSeq _UpperCamelCase : str = ["image"] _UpperCamelCase : List[str] = ["text"] def __init__( self , *a__ , **a__ ): requires_backends(self , ["""vision"""] ) super().__init__(*a__ , **a__ ) def __A ( self , a__ ): return self.pre_processor(images=a__ , return_tensors="""pt""" ) def __A ( self , a__ ): return self.model.generate(**a__ ) def __A ( self , a__ ): return self.pre_processor.batch_decode(a__ , skip_special_tokens=a__ )[0].strip()
663
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = DiTPipeline _UpperCamelCase : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS _UpperCamelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } _UpperCamelCase : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS _UpperCamelCase : Union[str, Any] = False def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=a__ , activation_fn="""gelu-approximate""" , num_embeds_ada_norm=1000 , norm_type="""ada_norm_zero""" , norm_elementwise_affine=a__ , ) _lowerCAmelCase : Optional[int] = AutoencoderKL() _lowerCAmelCase : Union[str, Any] = DDIMScheduler() _lowerCAmelCase : Optional[Any] = {"""transformer""": transformer.eval(), """vae""": vae.eval(), """scheduler""": scheduler} return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : Any = torch.manual_seed(a__ ) else: _lowerCAmelCase : Tuple = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Any = { """class_labels""": [1], """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : Tuple = self.get_dummy_components() _lowerCAmelCase : Optional[int] = self.pipeline_class(**a__ ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Tuple = self.get_dummy_inputs(a__ ) _lowerCAmelCase : List[str] = pipe(**a__ ).images _lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) _lowerCAmelCase : List[Any] = np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7] ) _lowerCAmelCase : Optional[int] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(a__ , 1e-3 ) def __A ( self ): self._test_inference_batch_single_identical(relax_max_difference=a__ , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __A ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): def __A ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : List[str] = torch.manual_seed(0 ) _lowerCAmelCase : int = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-256""" ) pipe.to("""cuda""" ) _lowerCAmelCase : Dict = ["""vase""", """umbrella""", """white shark""", """white wolf"""] _lowerCAmelCase : Union[str, Any] = pipe.get_label_ids(a__ ) _lowerCAmelCase : Any = pipe(a__ , generator=a__ , num_inference_steps=40 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( F"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1e-2 def __A ( self ): _lowerCAmelCase : str = DiTPipeline.from_pretrained("""facebook/DiT-XL-2-512""" ) _lowerCAmelCase : Dict = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("""cuda""" ) _lowerCAmelCase : List[str] = ["""vase""", """umbrella"""] _lowerCAmelCase : Optional[int] = pipe.get_label_ids(a__ ) _lowerCAmelCase : str = torch.manual_seed(0 ) _lowerCAmelCase : List[str] = pipe(a__ , generator=a__ , num_inference_steps=25 , output_type="""np""" ).images for word, image in zip(a__ , a__ ): _lowerCAmelCase : str = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" F"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1e-1
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : int ,_lowerCamelCase : int ) -> int: if exponent == 1: return base if exponent % 2 == 0: _lowerCAmelCase : Dict = _modexpt(_lowerCamelCase ,exponent // 2 ,_lowerCamelCase ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(_lowerCamelCase ,exponent - 1 ,_lowerCamelCase )) % modulo_value def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1777 ,_lowerCamelCase : int = 1855 ,_lowerCamelCase : int = 8 ) -> int: _lowerCAmelCase : List[Any] = base for _ in range(1 ,_lowerCamelCase ): _lowerCAmelCase : Optional[Any] = _modexpt(_lowerCamelCase ,_lowerCamelCase ,10**digits ) return result if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _a : Tuple = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , *a__ , **a__ ): warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , a__ , ) super().__init__(*a__ , **a__ )
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : list ) -> int: if not grid or not grid[0]: raise TypeError("""The grid does not contain the appropriate information""" ) for cell_n in range(1 ,len(grid[0] ) ): grid[0][cell_n] += grid[0][cell_n - 1] _lowerCAmelCase : Optional[int] = grid[0] for row_n in range(1 ,len(_lowerCamelCase ) ): _lowerCAmelCase : str = grid[row_n] _lowerCAmelCase : Any = fill_row(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : Optional[int] = grid[row_n] return grid[-1][-1] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : list ,_lowerCamelCase : list ) -> list: current_row[0] += row_above[0] for cell_n in range(1 ,len(_lowerCamelCase ) ): current_row[cell_n] += min(current_row[cell_n - 1] ,row_above[cell_n] ) return current_row if __name__ == "__main__": import doctest doctest.testmod()
663
"""simple docstring""" import argparse import json import subprocess def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Any ) -> List[Any]: _lowerCAmelCase : Tuple = [] _lowerCAmelCase : Optional[int] = ( f"curl -H \"Accept: application/vnd.github+json\" -H \"Authorization: Bearer {token}\"" """ https://api.github.com/repos/huggingface/transformers/actions/runners""" ) _lowerCAmelCase : List[str] = subprocess.run(_lowerCamelCase ,shell=_lowerCamelCase ,stdout=subprocess.PIPE ) _lowerCAmelCase : int = output.stdout.decode("""utf-8""" ) _lowerCAmelCase : Tuple = json.loads(_lowerCamelCase ) _lowerCAmelCase : int = status["""runners"""] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_lowerCamelCase ) # save the result so we can report them on Slack with open("""offline_runners.txt""" ,"""w""" ) as fp: fp.write(json.dumps(_lowerCamelCase ) ) if len(_lowerCamelCase ) > 0: _lowerCAmelCase : int = """\n""".join([x["""name"""] for x in offline_runners] ) raise ValueError(f"The following runners are offline:\n{failed}" ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ) -> Optional[int]: return values.split(""",""" ) _a : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--target_runners', default=None, type=list_str, required=True, help='Comma-separated list of runners to check status.', ) parser.add_argument( '--token', default=None, type=str, required=True, help='A token that has actions:read permission.' ) _a : Tuple = parser.parse_args() get_runner_status(args.target_runners, args.token)
663
1
"""simple docstring""" import os 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 _a : Optional[int] = logging.get_logger(__name__) _a : Any = '▁' _a : Any = {'vocab_file': 'sentencepiece.bpe.model', 'monolingual_vocab_file': 'dict.txt'} _a : int = { 'vocab_file': { 'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model', }, 'monolingual_vocab_file': { 'vinai/bartpho-syllable': 'https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt', }, } _a : Dict = {'vinai/bartpho-syllable': 1_024} class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[Any] = VOCAB_FILES_NAMES _UpperCamelCase : Any = PRETRAINED_VOCAB_FILES_MAP _UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCamelCase : List[str] = ["input_ids", "attention_mask"] def __init__( self , a__ , a__ , a__="<s>" , a__="</s>" , a__="</s>" , a__="<s>" , a__="<unk>" , a__="<pad>" , a__="<mask>" , a__ = None , **a__ , ): # Mask token behave like a normal word, i.e. include the space before it _lowerCAmelCase : str = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else mask_token _lowerCAmelCase : List[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=a__ , eos_token=a__ , unk_token=a__ , sep_token=a__ , cls_token=a__ , pad_token=a__ , mask_token=a__ , sp_model_kwargs=self.sp_model_kwargs , **a__ , ) _lowerCAmelCase : Any = vocab_file _lowerCAmelCase : Union[str, Any] = monolingual_vocab_file _lowerCAmelCase : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(a__ ) ) # Load the reduced vocab # Keep order of special tokens for backward compatibility _lowerCAmelCase : Dict = {} _lowerCAmelCase : Optional[int] = 0 for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]: if str(a__ ) not in self.fairseq_tokens_to_ids: _lowerCAmelCase : Union[str, Any] = cnt cnt += 1 with open(a__ , """r""" , encoding="""utf-8""" ) as f: for line in f.readlines(): _lowerCAmelCase : Optional[Any] = line.strip().split()[0] _lowerCAmelCase : Union[str, Any] = len(self.fairseq_tokens_to_ids ) if str(a__ ) not in self.fairseq_tokens_to_ids: _lowerCAmelCase : List[str] = len(self.fairseq_tokens_to_ids ) _lowerCAmelCase : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ): _lowerCAmelCase : Optional[int] = self.__dict__.copy() _lowerCAmelCase : List[Any] = None _lowerCAmelCase : List[Any] = self.sp_model.serialized_model_proto() return state def __setstate__( self , a__ ): _lowerCAmelCase : str = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _lowerCAmelCase : Tuple = {} _lowerCAmelCase : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __A ( self , a__ , a__ = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _lowerCAmelCase : Union[str, Any] = [self.cls_token_id] _lowerCAmelCase : str = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A ( self , a__ , a__ = None , a__ = False ): 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 None: return [1] + ([0] * len(a__ )) + [1] return [1] + ([0] * len(a__ )) + [1, 1] + ([0] * len(a__ )) + [1] def __A ( self , a__ , a__ = None ): _lowerCAmelCase : str = [self.sep_token_id] _lowerCAmelCase : List[str] = [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] @property def __A ( self ): return len(self.fairseq_ids_to_tokens ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = {self.convert_ids_to_tokens(a__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __A ( self , a__ ): return self.sp_model.encode(a__ , out_type=a__ ) def __A ( self , a__ ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] else: return self.unk_token_id def __A ( self , a__ ): return self.fairseq_ids_to_tokens[index] def __A ( self , a__ ): _lowerCAmelCase : Dict = """""".join(a__ ).replace(a__ , """ """ ).strip() return out_string def __A ( self , a__ , a__ = None ): if not os.path.isdir(a__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return _lowerCAmelCase : Optional[int] = os.path.join( a__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : Any = os.path.join( a__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""monolingual_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: _lowerCAmelCase : str = self.sp_model.serialized_model_proto() fi.write(a__ ) if os.path.abspath(self.monolingual_vocab_file ) != os.path.abspath( a__ ) and os.path.isfile(self.monolingual_vocab_file ): copyfile(self.monolingual_vocab_file , a__ ) elif not os.path.isfile(self.monolingual_vocab_file ): with open(a__ , """w""" , encoding="""utf-8""" ) as fp: for token in self.fairseq_tokens_to_ids: if token not in self.all_special_tokens: fp.write(F"{str(a__ )} \n" ) return out_vocab_file, out_monolingual_vocab_file
663
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 50000000 ) -> int: _lowerCAmelCase : Union[str, Any] = set() _lowerCAmelCase : Tuple = int((limit - 24) ** (1 / 2) ) _lowerCAmelCase : str = set(range(3 ,prime_square_limit + 1 ,2 ) ) primes.add(2 ) for p in range(3 ,prime_square_limit + 1 ,2 ): if p not in primes: continue primes.difference_update(set(range(p * p ,prime_square_limit + 1 ,_lowerCamelCase ) ) ) for primea in primes: _lowerCAmelCase : Optional[Any] = primea * primea for primea in primes: _lowerCAmelCase : Any = primea * primea * primea if square + cube >= limit - 16: break for primea in primes: _lowerCAmelCase : int = primea * primea * primea * primea _lowerCAmelCase : Union[str, Any] = square + cube + tetr if total >= limit: break ret.add(_lowerCamelCase ) return len(_lowerCamelCase ) if __name__ == "__main__": print(F"""{solution() = }""")
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ) -> int: _lowerCAmelCase : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 ,limit + 1 ): if phi[i] == i - 1: for j in range(2 * i ,limit + 1 ,_lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int = 1000000 ) -> int: _lowerCAmelCase : List[str] = [i - 1 for i in range(limit + 1 )] for i in range(2 ,limit + 1 ): if phi[i] == i - 1: for j in range(2 * i ,limit + 1 ,_lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a : Tuple = {'configuration_wavlm': ['WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WavLMConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : str = [ 'WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'WavLMForAudioFrameClassification', 'WavLMForCTC', 'WavLMForSequenceClassification', 'WavLMForXVector', 'WavLMModel', 'WavLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys _a : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import DistilBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers.models.distilbert.modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Any = parent _lowerCAmelCase : str = 13 _lowerCAmelCase : Dict = 7 _lowerCAmelCase : Optional[int] = True _lowerCAmelCase : Optional[Any] = True _lowerCAmelCase : Dict = False _lowerCAmelCase : Optional[Any] = True _lowerCAmelCase : Union[str, Any] = 99 _lowerCAmelCase : List[str] = 32 _lowerCAmelCase : List[str] = 2 _lowerCAmelCase : str = 4 _lowerCAmelCase : Tuple = 37 _lowerCAmelCase : Dict = """gelu""" _lowerCAmelCase : Union[str, Any] = 0.1 _lowerCAmelCase : Dict = 0.1 _lowerCAmelCase : Tuple = 512 _lowerCAmelCase : Optional[int] = 16 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : Optional[int] = 0.0_2 _lowerCAmelCase : int = 3 _lowerCAmelCase : Tuple = 4 _lowerCAmelCase : Union[str, Any] = None def __A ( self ): _lowerCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Any = None if self.use_input_mask: _lowerCAmelCase : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) _lowerCAmelCase : List[str] = None _lowerCAmelCase : int = None _lowerCAmelCase : List[Any] = None if self.use_labels: _lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowerCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _lowerCAmelCase : int = ids_tensor([self.batch_size] , self.num_choices ) _lowerCAmelCase : Optional[int] = DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFDistilBertModel(config=a__ ) _lowerCAmelCase : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} _lowerCAmelCase : List[Any] = model(a__ ) _lowerCAmelCase : Optional[int] = [input_ids, input_mask] _lowerCAmelCase : Tuple = 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__ ): _lowerCAmelCase : List[str] = TFDistilBertForMaskedLM(config=a__ ) _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} _lowerCAmelCase : List[str] = model(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__ ): _lowerCAmelCase : List[str] = TFDistilBertForQuestionAnswering(config=a__ ) _lowerCAmelCase : Tuple = { """input_ids""": input_ids, """attention_mask""": input_mask, } _lowerCAmelCase : Optional[Any] = model(a__ ) 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 , a__ , a__ , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Any = self.num_labels _lowerCAmelCase : Dict = TFDistilBertForSequenceClassification(a__ ) _lowerCAmelCase : str = {"""input_ids""": input_ids, """attention_mask""": input_mask} _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[Any] = self.num_choices _lowerCAmelCase : Optional[Any] = TFDistilBertForMultipleChoice(a__ ) _lowerCAmelCase : str = tf.tile(tf.expand_dims(a__ , 1 ) , (1, self.num_choices, 1) ) _lowerCAmelCase : Tuple = tf.tile(tf.expand_dims(a__ , 1 ) , (1, self.num_choices, 1) ) _lowerCAmelCase : Optional[int] = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, } _lowerCAmelCase : Union[str, Any] = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = self.num_labels _lowerCAmelCase : int = TFDistilBertForTokenClassification(a__ ) _lowerCAmelCase : Union[str, Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} _lowerCAmelCase : str = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self ): _lowerCAmelCase : List[Any] = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : List[str] = config_and_inputs _lowerCAmelCase : Optional[int] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Tuple = ( ( TFDistilBertModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertForMultipleChoice, ) if is_tf_available() else None ) _UpperCamelCase : Tuple = ( { "feature-extraction": TFDistilBertModel, "fill-mask": TFDistilBertForMaskedLM, "question-answering": TFDistilBertForQuestionAnswering, "text-classification": TFDistilBertForSequenceClassification, "token-classification": TFDistilBertForTokenClassification, "zero-shot": TFDistilBertForSequenceClassification, } if is_tf_available() else {} ) _UpperCamelCase : Any = False _UpperCamelCase : List[str] = False def __A ( self ): _lowerCAmelCase : Optional[int] = TFDistilBertModelTester(self ) _lowerCAmelCase : List[str] = ConfigTester(self , config_class=a__ , dim=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): _lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*a__ ) def __A ( self ): _lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*a__ ) def __A ( self ): _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*a__ ) def __A ( self ): _lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*a__ ) def __A ( self ): _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*a__ ) @slow def __A ( self ): for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ): _lowerCAmelCase : int = TFDistilBertModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @require_tf class __A ( unittest.TestCase ): @slow def __A ( self ): _lowerCAmelCase : int = TFDistilBertModel.from_pretrained("""distilbert-base-uncased""" ) _lowerCAmelCase : Dict = tf.constant([[0, 1, 2, 3, 4, 5]] ) _lowerCAmelCase : Optional[Any] = model(a__ )[0] _lowerCAmelCase : Dict = [1, 6, 768] self.assertEqual(output.shape , a__ ) _lowerCAmelCase : Optional[Any] = tf.constant( [ [ [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9], [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4], [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , a__ , atol=1e-4 )
663
"""simple docstring""" from PIL import Image def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Image ,_lowerCamelCase : int ) -> Image: _lowerCAmelCase : Any = (259 * (level + 255)) / (255 * (259 - level)) def contrast(_lowerCamelCase : int ) -> int: return int(128 + factor * (c - 128) ) return img.point(_lowerCamelCase ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change contrast to 170 _a : str = change_contrast(img, 170) cont_img.save('image_data/lena_high_contrast.png', format='png')
663
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_pytesseract, require_torch from transformers.utils import is_pytesseract_available, is_torch_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_pytesseract_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __A ( unittest.TestCase ): def __init__( self , a__ , a__=7 , a__=3 , a__=18 , a__=30 , a__=400 , a__=True , a__=None , a__=True , ): _lowerCAmelCase : Optional[Any] = size if size is not None else {"""height""": 18, """width""": 18} _lowerCAmelCase : Tuple = parent _lowerCAmelCase : Union[str, Any] = batch_size _lowerCAmelCase : List[Any] = num_channels _lowerCAmelCase : Union[str, Any] = image_size _lowerCAmelCase : Union[str, Any] = min_resolution _lowerCAmelCase : int = max_resolution _lowerCAmelCase : str = do_resize _lowerCAmelCase : Optional[Any] = size _lowerCAmelCase : Union[str, Any] = apply_ocr def __A ( self ): return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Union[str, Any] = LayoutLMvaImageProcessor if is_pytesseract_available() else None def __A ( self ): _lowerCAmelCase : Union[str, Any] = LayoutLMvaImageProcessingTester(self ) @property def __A ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a__ , """do_resize""" ) ) self.assertTrue(hasattr(a__ , """size""" ) ) self.assertTrue(hasattr(a__ , """apply_ocr""" ) ) def __A ( self ): _lowerCAmelCase : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _lowerCAmelCase : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def __A ( self ): pass def __A ( self ): # Initialize image_processing _lowerCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _lowerCAmelCase : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=a__ ) for image in image_inputs: self.assertIsInstance(a__ , Image.Image ) # Test not batched input _lowerCAmelCase : str = image_processing(image_inputs[0] , return_tensors="""pt""" ) self.assertEqual( encoding.pixel_values.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) self.assertIsInstance(encoding.words , a__ ) self.assertIsInstance(encoding.boxes , a__ ) # Test batched _lowerCAmelCase : Optional[int] = 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 ): # Initialize image_processing _lowerCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _lowerCAmelCase : List[Any] = 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 _lowerCAmelCase : List[Any] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _lowerCAmelCase : Tuple = 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 ): # Initialize image_processing _lowerCAmelCase : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _lowerCAmelCase : Any = 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 _lowerCAmelCase : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched _lowerCAmelCase : int = 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 ): # with apply_OCR = True _lowerCAmelCase : Tuple = LayoutLMvaImageProcessor() from datasets import load_dataset _lowerCAmelCase : List[Any] = load_dataset("""hf-internal-testing/fixtures_docvqa""" , split="""test""" ) _lowerCAmelCase : Tuple = Image.open(ds[0]["""file"""] ).convert("""RGB""" ) _lowerCAmelCase : List[str] = image_processing(a__ , return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 _lowerCAmelCase : Optional[int] = [["""11:14""", """to""", """11:39""", """a.m""", """11:39""", """to""", """11:44""", """a.m.""", """11:44""", """a.m.""", """to""", """12:25""", """p.m.""", """12:25""", """to""", """12:58""", """p.m.""", """12:58""", """to""", """4:00""", """p.m.""", """2:00""", """to""", """5:00""", """p.m.""", """Coffee""", """Break""", """Coffee""", """will""", """be""", """served""", """for""", """men""", """and""", """women""", """in""", """the""", """lobby""", """adjacent""", """to""", """exhibit""", """area.""", """Please""", """move""", """into""", """exhibit""", """area.""", """(Exhibits""", """Open)""", """TRRF""", """GENERAL""", """SESSION""", """(PART""", """|)""", """Presiding:""", """Lee""", """A.""", """Waller""", """TRRF""", """Vice""", """President""", """“Introductory""", """Remarks”""", """Lee""", """A.""", """Waller,""", """TRRF""", """Vice""", """Presi-""", """dent""", """Individual""", """Interviews""", """with""", """TRRF""", """Public""", """Board""", """Members""", """and""", """Sci-""", """entific""", """Advisory""", """Council""", """Mem-""", """bers""", """Conducted""", """by""", """TRRF""", """Treasurer""", """Philip""", """G.""", """Kuehn""", """to""", """get""", """answers""", """which""", """the""", """public""", """refrigerated""", """warehousing""", """industry""", """is""", """looking""", """for.""", """Plus""", """questions""", """from""", """the""", """floor.""", """Dr.""", """Emil""", """M.""", """Mrak,""", """University""", """of""", """Cal-""", """ifornia,""", """Chairman,""", """TRRF""", """Board;""", """Sam""", """R.""", """Cecil,""", """University""", """of""", """Georgia""", """College""", """of""", """Agriculture;""", """Dr.""", """Stanley""", """Charm,""", """Tufts""", """University""", """School""", """of""", """Medicine;""", """Dr.""", """Robert""", """H.""", """Cotton,""", """ITT""", """Continental""", """Baking""", """Company;""", """Dr.""", """Owen""", """Fennema,""", """University""", """of""", """Wis-""", """consin;""", """Dr.""", """Robert""", """E.""", """Hardenburg,""", """USDA.""", """Questions""", """and""", """Answers""", """Exhibits""", """Open""", """Capt.""", """Jack""", """Stoney""", """Room""", """TRRF""", """Scientific""", """Advisory""", """Council""", """Meeting""", """Ballroom""", """Foyer"""]] # noqa: E231 _lowerCAmelCase : Tuple = [[[141, 57, 214, 69], [228, 58, 252, 69], [141, 75, 216, 88], [230, 79, 280, 88], [142, 260, 218, 273], [230, 261, 255, 273], [143, 279, 218, 290], [231, 282, 290, 291], [143, 342, 218, 354], [231, 345, 289, 355], [202, 362, 227, 373], [143, 379, 220, 392], [231, 382, 291, 394], [144, 714, 220, 726], [231, 715, 256, 726], [144, 732, 220, 745], [232, 736, 291, 747], [144, 769, 218, 782], [231, 770, 256, 782], [141, 788, 202, 801], [215, 791, 274, 804], [143, 826, 204, 838], [215, 826, 240, 838], [142, 844, 202, 857], [215, 847, 274, 859], [334, 57, 427, 69], [440, 57, 522, 69], [369, 75, 461, 88], [469, 75, 516, 88], [528, 76, 562, 88], [570, 76, 667, 88], [675, 75, 711, 87], [721, 79, 778, 88], [789, 75, 840, 88], [369, 97, 470, 107], [484, 94, 507, 106], [518, 94, 562, 107], [576, 94, 655, 110], [668, 94, 792, 109], [804, 95, 829, 107], [369, 113, 465, 125], [477, 116, 547, 125], [562, 113, 658, 125], [671, 116, 748, 125], [761, 113, 811, 125], [369, 131, 465, 143], [477, 133, 548, 143], [563, 130, 698, 145], [710, 130, 802, 146], [336, 171, 412, 183], [423, 171, 572, 183], [582, 170, 716, 184], [728, 171, 817, 187], [829, 171, 844, 186], [338, 197, 482, 212], [507, 196, 557, 209], [569, 196, 595, 208], [610, 196, 702, 209], [505, 214, 583, 226], [595, 214, 656, 227], [670, 215, 807, 227], [335, 259, 543, 274], [556, 259, 708, 272], [372, 279, 422, 291], [435, 279, 460, 291], [474, 279, 574, 292], [587, 278, 664, 291], [676, 278, 738, 291], [751, 279, 834, 291], [372, 298, 434, 310], [335, 341, 483, 354], [497, 341, 655, 354], [667, 341, 728, 354], [740, 341, 825, 354], [335, 360, 430, 372], [442, 360, 534, 372], [545, 359, 687, 372], [697, 360, 754, 372], [765, 360, 823, 373], [334, 378, 428, 391], [440, 378, 577, 394], [590, 378, 705, 391], [720, 378, 801, 391], [334, 397, 400, 409], [370, 416, 529, 429], [544, 416, 576, 432], [587, 416, 665, 428], [677, 416, 814, 429], [372, 435, 452, 450], [465, 434, 495, 447], [511, 434, 600, 447], [611, 436, 637, 447], [649, 436, 694, 451], [705, 438, 824, 447], [369, 453, 452, 466], [464, 454, 509, 466], [522, 453, 611, 469], [625, 453, 792, 469], [370, 472, 556, 488], [570, 472, 684, 487], [697, 472, 718, 485], [732, 472, 835, 488], [369, 490, 411, 503], [425, 490, 484, 503], [496, 490, 635, 506], [645, 490, 707, 503], [718, 491, 761, 503], [771, 490, 840, 503], [336, 510, 374, 521], [388, 510, 447, 522], [460, 510, 489, 521], [503, 510, 580, 522], [592, 509, 736, 525], [745, 509, 770, 522], [781, 509, 840, 522], [338, 528, 434, 541], [448, 528, 596, 541], [609, 527, 687, 540], [700, 528, 792, 541], [336, 546, 397, 559], [407, 546, 431, 559], [443, 546, 525, 560], [537, 546, 680, 562], [688, 546, 714, 559], [722, 546, 837, 562], [336, 565, 449, 581], [461, 565, 485, 577], [497, 565, 665, 581], [681, 565, 718, 577], [732, 565, 837, 580], [337, 584, 438, 597], [452, 583, 521, 596], [535, 584, 677, 599], [690, 583, 787, 596], [801, 583, 825, 596], [338, 602, 478, 615], [492, 602, 530, 614], [543, 602, 638, 615], [650, 602, 676, 614], [688, 602, 788, 615], [802, 602, 843, 614], [337, 621, 502, 633], [516, 621, 615, 637], [629, 621, 774, 636], [789, 621, 827, 633], [337, 639, 418, 652], [432, 640, 571, 653], [587, 639, 731, 655], [743, 639, 769, 652], [780, 639, 841, 652], [338, 658, 440, 673], [455, 658, 491, 670], [508, 658, 602, 671], [616, 658, 638, 670], [654, 658, 835, 674], [337, 677, 429, 689], [337, 714, 482, 726], [495, 714, 548, 726], [561, 714, 683, 726], [338, 770, 461, 782], [474, 769, 554, 785], [489, 788, 562, 803], [576, 788, 643, 801], [656, 787, 751, 804], [764, 788, 844, 801], [334, 825, 421, 838], [430, 824, 574, 838], [584, 824, 723, 841], [335, 844, 450, 857], [464, 843, 583, 860], [628, 862, 755, 875], [769, 861, 848, 878]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , a__ ) self.assertListEqual(encoding.boxes , a__ ) # with apply_OCR = False _lowerCAmelCase : Union[str, Any] = LayoutLMvaImageProcessor(apply_ocr=a__ ) _lowerCAmelCase : List[str] = image_processing(a__ , return_tensors="""pt""" ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 224, 224) )
663
"""simple docstring""" class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A ( SCREAMING_SNAKE_CASE_ ): pass class __A : def __init__( self ): _lowerCAmelCase : Union[str, Any] = [ [], [], [], ] def __A ( self , a__ , a__ ): try: if len(self.queues[priority] ) >= 100: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(a__ ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def __A ( self ): for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self ): return "\n".join(F"Priority {i}: {q}" for i, q in enumerate(self.queues ) ) class __A : def __init__( self ): _lowerCAmelCase : int = [] def __A ( self , a__ ): if len(self.queue ) == 100: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(a__ ) def __A ( self ): if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: _lowerCAmelCase : int = min(self.queue ) self.queue.remove(a__ ) return data def __str__( self ): return str(self.queue ) def SCREAMING_SNAKE_CASE ( ) -> str: _lowerCAmelCase : Union[str, Any] = FixedPriorityQueue() fpq.enqueue(0 ,10 ) fpq.enqueue(1 ,70 ) fpq.enqueue(0 ,100 ) fpq.enqueue(2 ,1 ) fpq.enqueue(2 ,5 ) fpq.enqueue(1 ,7 ) fpq.enqueue(2 ,4 ) fpq.enqueue(1 ,64 ) fpq.enqueue(0 ,128 ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(_lowerCamelCase ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: _lowerCAmelCase : Tuple = ElementPriorityQueue() epq.enqueue(10 ) epq.enqueue(70 ) epq.enqueue(100 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(64 ) epq.enqueue(128 ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(_lowerCamelCase ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
663
1
"""simple docstring""" import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( 'The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion' ) _a : Optional[int] = None _a : Tuple = { '7B': 11_008, '13B': 13_824, '30B': 17_920, '65B': 22_016, '70B': 28_672, } _a : List[str] = { '7B': 1, '7Bf': 1, '13B': 2, '13Bf': 2, '30B': 4, '65B': 8, '70B': 8, '70Bf': 8, } def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : int=1 ,_lowerCamelCase : List[Any]=256 ) -> Any: return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[str] ) -> List[str]: with open(_lowerCamelCase ,"""r""" ) as f: return json.load(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Any ) -> str: with open(_lowerCamelCase ,"""w""" ) as f: json.dump(_lowerCamelCase ,_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ,_lowerCamelCase : Tuple ,_lowerCamelCase : List[str] ,_lowerCamelCase : List[Any]=True ) -> Optional[Any]: os.makedirs(_lowerCamelCase ,exist_ok=_lowerCamelCase ) _lowerCAmelCase : List[Any] = os.path.join(_lowerCamelCase ,"""tmp""" ) os.makedirs(_lowerCamelCase ,exist_ok=_lowerCamelCase ) _lowerCAmelCase : Any = read_json(os.path.join(_lowerCamelCase ,"""params.json""" ) ) _lowerCAmelCase : List[Any] = NUM_SHARDS[model_size] _lowerCAmelCase : List[str] = params["""n_layers"""] _lowerCAmelCase : Tuple = params["""n_heads"""] _lowerCAmelCase : Tuple = n_heads // num_shards _lowerCAmelCase : List[Any] = params["""dim"""] _lowerCAmelCase : Optional[Any] = dim // n_heads _lowerCAmelCase : int = 1_00_00.0 _lowerCAmelCase : Any = 1.0 / (base ** (torch.arange(0 ,_lowerCamelCase ,2 ).float() / dims_per_head)) if "n_kv_heads" in params: _lowerCAmelCase : Optional[Any] = params["""n_kv_heads"""] # for GQA / MQA _lowerCAmelCase : Optional[int] = n_heads_per_shard // num_key_value_heads _lowerCAmelCase : Any = dim // num_key_value_heads else: # compatibility with other checkpoints _lowerCAmelCase : int = n_heads _lowerCAmelCase : Optional[Any] = n_heads_per_shard _lowerCAmelCase : List[Any] = dim # permute for sliced rotary def permute(_lowerCamelCase : Optional[Any] ,_lowerCamelCase : Union[str, Any]=n_heads ,_lowerCamelCase : Union[str, Any]=dim ,_lowerCamelCase : Tuple=dim ): return w.view(_lowerCamelCase ,dima // n_heads // 2 ,2 ,_lowerCamelCase ).transpose(1 ,2 ).reshape(_lowerCamelCase ,_lowerCamelCase ) print(f"Fetching all parameters from the checkpoint at {input_base_path}." ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) _lowerCAmelCase : Union[str, Any] = torch.load(os.path.join(_lowerCamelCase ,"""consolidated.00.pth""" ) ,map_location="""cpu""" ) else: # Sharded _lowerCAmelCase : List[Any] = [ torch.load(os.path.join(_lowerCamelCase ,f"consolidated.{i:02d}.pth" ) ,map_location="""cpu""" ) for i in range(_lowerCamelCase ) ] _lowerCAmelCase : Optional[int] = 0 _lowerCAmelCase : Tuple = {"""weight_map""": {}} for layer_i in range(_lowerCamelCase ): _lowerCAmelCase : Union[str, Any] = f"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _lowerCAmelCase : Tuple = { f"model.layers.{layer_i}.self_attn.q_proj.weight": permute( loaded[f"layers.{layer_i}.attention.wq.weight"] ), f"model.layers.{layer_i}.self_attn.k_proj.weight": permute( loaded[f"layers.{layer_i}.attention.wk.weight"] ), f"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[f"layers.{layer_i}.attention.wv.weight"], f"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[f"layers.{layer_i}.attention.wo.weight"], f"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w1.weight"], f"model.layers.{layer_i}.mlp.down_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w2.weight"], f"model.layers.{layer_i}.mlp.up_proj.weight": loaded[f"layers.{layer_i}.feed_forward.w3.weight"], f"model.layers.{layer_i}.input_layernorm.weight": loaded[f"layers.{layer_i}.attention_norm.weight"], f"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[f"layers.{layer_i}.ffn_norm.weight"], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. _lowerCAmelCase : List[Any] = { f"model.layers.{layer_i}.input_layernorm.weight": loaded[0][ f"layers.{layer_i}.attention_norm.weight" ].clone(), f"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][ f"layers.{layer_i}.ffn_norm.weight" ].clone(), } _lowerCAmelCase : List[str] = permute( torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wq.weight"].view(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) for i in range(_lowerCamelCase ) ] ,dim=0 ,).reshape(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : int = permute( torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wk.weight"].view( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) for i in range(_lowerCamelCase ) ] ,dim=0 ,).reshape(_lowerCamelCase ,_lowerCamelCase ) ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,) _lowerCAmelCase : Optional[int] = torch.cat( [ loaded[i][f"layers.{layer_i}.attention.wv.weight"].view( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) for i in range(_lowerCamelCase ) ] ,dim=0 ,).reshape(_lowerCamelCase ,_lowerCamelCase ) _lowerCAmelCase : Dict = torch.cat( [loaded[i][f"layers.{layer_i}.attention.wo.weight"] for i in range(_lowerCamelCase )] ,dim=1 ) _lowerCAmelCase : Any = torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w1.weight"] for i in range(_lowerCamelCase )] ,dim=0 ) _lowerCAmelCase : Optional[int] = torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w2.weight"] for i in range(_lowerCamelCase )] ,dim=1 ) _lowerCAmelCase : List[Any] = torch.cat( [loaded[i][f"layers.{layer_i}.feed_forward.w3.weight"] for i in range(_lowerCamelCase )] ,dim=0 ) _lowerCAmelCase : str = inv_freq for k, v in state_dict.items(): _lowerCAmelCase : Union[str, Any] = filename param_count += v.numel() torch.save(_lowerCamelCase ,os.path.join(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Union[str, Any] = f"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded _lowerCAmelCase : Dict = { """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: _lowerCAmelCase : int = { """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(_lowerCamelCase )] ,dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(_lowerCamelCase )] ,dim=0 ), } for k, v in state_dict.items(): _lowerCAmelCase : Dict = filename param_count += v.numel() torch.save(_lowerCamelCase ,os.path.join(_lowerCamelCase ,_lowerCamelCase ) ) # Write configs _lowerCAmelCase : int = {"""total_size""": param_count * 2} write_json(_lowerCamelCase ,os.path.join(_lowerCamelCase ,"""pytorch_model.bin.index.json""" ) ) _lowerCAmelCase : Any = params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 _lowerCAmelCase : Optional[int] = params["""multiple_of"""] if """multiple_of""" in params else 256 _lowerCAmelCase : Optional[Any] = LlamaConfig( hidden_size=_lowerCamelCase ,intermediate_size=compute_intermediate_size(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) ,num_attention_heads=params["""n_heads"""] ,num_hidden_layers=params["""n_layers"""] ,rms_norm_eps=params["""norm_eps"""] ,num_key_value_heads=_lowerCamelCase ,) config.save_pretrained(_lowerCamelCase ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) _lowerCAmelCase : Optional[Any] = LlamaForCausalLM.from_pretrained(_lowerCamelCase ,torch_dtype=torch.floataa ,low_cpu_mem_usage=_lowerCamelCase ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(_lowerCamelCase ,safe_serialization=_lowerCamelCase ) shutil.rmtree(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Union[str, Any] ) -> List[Any]: # Initialize the tokenizer based on the `spm` model _lowerCAmelCase : List[Any] = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(f"Saving a {tokenizer_class.__name__} to {tokenizer_path}." ) _lowerCAmelCase : Union[str, Any] = tokenizer_class(_lowerCamelCase ) tokenizer.save_pretrained(_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> Dict: _lowerCAmelCase : str = argparse.ArgumentParser() parser.add_argument( """--input_dir""" ,help="""Location of LLaMA weights, which contains tokenizer.model and model folders""" ,) parser.add_argument( """--model_size""" ,choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""] ,) parser.add_argument( """--output_dir""" ,help="""Location to write HF model and tokenizer""" ,) parser.add_argument("""--safe_serialization""" ,type=_lowerCamelCase ,help="""Whether or not to save using `safetensors`.""" ) _lowerCAmelCase : Union[str, Any] = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir ,input_base_path=os.path.join(args.input_dir ,args.model_size ) ,model_size=args.model_size ,safe_serialization=args.safe_serialization ,) _lowerCAmelCase : str = os.path.join(args.input_dir ,"""tokenizer.model""" ) write_tokenizer(args.output_dir ,_lowerCamelCase ) if __name__ == "__main__": main()
663
"""simple docstring""" import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class __A ( unittest.TestCase ): _UpperCamelCase : str = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING _UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def __A ( self , a__ , a__ , a__ ): _lowerCAmelCase : List[Any] = AudioClassificationPipeline(model=a__ , feature_extractor=a__ ) # test with a raw waveform _lowerCAmelCase : Optional[int] = np.zeros((34000,) ) _lowerCAmelCase : Optional[Any] = np.zeros((14000,) ) return audio_classifier, [audioa, audio] def __A ( self , a__ , a__ ): _lowerCAmelCase , _lowerCAmelCase : List[Any] = examples _lowerCAmelCase : List[Any] = audio_classifier(a__ ) # by default a model is initialized with num_labels=2 self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) _lowerCAmelCase : Tuple = audio_classifier(a__ , top_k=1 ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) self.run_torchaudio(a__ ) @require_torchaudio def __A ( self , a__ ): import datasets # test with a local file _lowerCAmelCase : int = datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) _lowerCAmelCase : List[Any] = dataset[0]["""audio"""]["""array"""] _lowerCAmelCase : str = audio_classifier(a__ ) self.assertEqual( a__ , [ {"""score""": ANY(a__ ), """label""": ANY(a__ )}, {"""score""": ANY(a__ ), """label""": ANY(a__ )}, ] , ) @require_torch def __A ( self ): _lowerCAmelCase : int = """anton-l/wav2vec2-random-tiny-classifier""" _lowerCAmelCase : Optional[Any] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : Any = np.ones((8000,) ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) _lowerCAmelCase : List[str] = [ {"""score""": 0.0_8_4_2, """label""": """no"""}, {"""score""": 0.0_8_3_8, """label""": """up"""}, {"""score""": 0.0_8_3_7, """label""": """go"""}, {"""score""": 0.0_8_3_4, """label""": """right"""}, ] _lowerCAmelCase : str = [ {"""score""": 0.0_8_4_5, """label""": """stop"""}, {"""score""": 0.0_8_4_4, """label""": """on"""}, {"""score""": 0.0_8_4_1, """label""": """right"""}, {"""score""": 0.0_8_3_4, """label""": """left"""}, ] self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) _lowerCAmelCase : int = {"""array""": np.ones((8000,) ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate} _lowerCAmelCase : int = audio_classifier(a__ , top_k=4 ) self.assertIn(nested_simplify(a__ , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def __A ( self ): import datasets _lowerCAmelCase : Optional[Any] = """superb/wav2vec2-base-superb-ks""" _lowerCAmelCase : List[str] = pipeline("""audio-classification""" , model=a__ ) _lowerCAmelCase : str = datasets.load_dataset("""anton-l/superb_dummy""" , """ks""" , split="""test""" ) _lowerCAmelCase : Optional[Any] = np.array(dataset[3]["""speech"""] , dtype=np.floataa ) _lowerCAmelCase : List[str] = audio_classifier(a__ , top_k=4 ) self.assertEqual( nested_simplify(a__ , decimals=3 ) , [ {"""score""": 0.9_8_1, """label""": """go"""}, {"""score""": 0.0_0_7, """label""": """up"""}, {"""score""": 0.0_0_6, """label""": """_unknown_"""}, {"""score""": 0.0_0_1, """label""": """down"""}, ] , ) @require_tf @unittest.skip("""Audio classification is not implemented for TF""" ) def __A ( self ): pass
663
1
"""simple docstring""" import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _a : Optional[Any] = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] def __init__( self , a__="</s>" , a__="<unk>" , a__="<pad>" , a__=125 , a__=None , **a__ , ): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: _lowerCAmelCase : Dict = [F"<extra_id_{i}>" for i in range(a__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens _lowerCAmelCase : int = len(set(filter(lambda a__ : bool("""extra_id""" in str(a__ ) ) , a__ ) ) ) if extra_tokens != extra_ids: raise ValueError( F"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" """ provided to ByT5Tokenizer. In this case the additional_special_tokens must include the""" """ extra_ids tokens""" ) _lowerCAmelCase : Any = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else pad_token _lowerCAmelCase : Any = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else eos_token _lowerCAmelCase : Union[str, Any] = AddedToken(a__ , lstrip=a__ , rstrip=a__ ) if isinstance(a__ , a__ ) else unk_token super().__init__( eos_token=a__ , unk_token=a__ , pad_token=a__ , extra_ids=a__ , additional_special_tokens=a__ , **a__ , ) _lowerCAmelCase : int = extra_ids _lowerCAmelCase : Optional[int] = 2**8 # utf is 8 bits # define special tokens dict _lowerCAmelCase : Dict[int, str] = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } _lowerCAmelCase : Dict = len(self.special_tokens_encoder ) _lowerCAmelCase : Union[str, Any] = len(a__ ) for i, token in enumerate(a__ ): _lowerCAmelCase : Any = self.vocab_size + i - n _lowerCAmelCase : Dict[str, int] = {v: k for k, v in self.special_tokens_encoder.items()} @property def __A ( self ): return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def __A ( self , a__ , a__ = None , a__ = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=a__ , token_ids_a=a__ , already_has_special_tokens=a__ ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(a__ )) + [1] return ([0] * len(a__ )) + [1] + ([0] * len(a__ )) + [1] def __A ( self , a__ ): if len(a__ ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( F"This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated" """ eos tokens being added.""" ) return token_ids else: return token_ids + [self.eos_token_id] def __A ( self , a__ , a__ = None ): _lowerCAmelCase : Optional[Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __A ( self , a__ , a__ = None ): _lowerCAmelCase : Tuple = self._add_eos_if_not_present(a__ ) if token_ids_a is None: return token_ids_a else: _lowerCAmelCase : int = self._add_eos_if_not_present(a__ ) return token_ids_a + token_ids_a def __A ( self , a__ ): _lowerCAmelCase : Tuple = [chr(a__ ) for i in text.encode("""utf-8""" )] return tokens def __A ( self , a__ ): if token in self.special_tokens_encoder: _lowerCAmelCase : Tuple = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: _lowerCAmelCase : List[Any] = self.added_tokens_encoder[token] elif len(a__ ) != 1: _lowerCAmelCase : List[Any] = self.unk_token_id else: _lowerCAmelCase : List[Any] = ord(a__ ) + self._num_special_tokens return token_id def __A ( self , a__ ): if index in self.special_tokens_decoder: _lowerCAmelCase : Optional[int] = self.special_tokens_decoder[index] else: _lowerCAmelCase : Dict = chr(index - self._num_special_tokens ) return token def __A ( self , a__ ): _lowerCAmelCase : Dict = B"""""" for token in tokens: if token in self.special_tokens_decoder: _lowerCAmelCase : Tuple = self.special_tokens_decoder[token].encode("""utf-8""" ) elif token in self.added_tokens_decoder: _lowerCAmelCase : List[str] = self.special_tokens_decoder[token].encode("""utf-8""" ) elif token in self.special_tokens_encoder: _lowerCAmelCase : Union[str, Any] = token.encode("""utf-8""" ) elif token in self.added_tokens_encoder: _lowerCAmelCase : Dict = token.encode("""utf-8""" ) else: _lowerCAmelCase : Optional[int] = bytes([ord(a__ )] ) bstring += tok_string _lowerCAmelCase : Any = bstring.decode("""utf-8""" , errors="""ignore""" ) return string def __A ( self , a__ , a__ = None ): return ()
663
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) _a : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name _a : int = '\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline\n >>> from diffusers.utils import load_image\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n ... )\n >>> pipe_prior.to("cuda")\n\n >>> prompt = "A red cartoon frog, 4k"\n >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)\n\n >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(\n ... "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16\n ... )\n >>> pipe.to("cuda")\n\n >>> init_image = load_image(\n ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n ... "/kandinsky/frog.png"\n ... )\n\n >>> image = pipe(\n ... image=init_image,\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=100,\n ... strength=0.2,\n ... ).images\n\n >>> image[0].save("red_frog.png")\n ```\n' def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Optional[Any] ,_lowerCamelCase : Dict ,_lowerCamelCase : Dict=8 ) -> Any: _lowerCAmelCase : List[Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _lowerCAmelCase : Optional[Any] = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ,_lowerCamelCase : Any=512 ,_lowerCamelCase : Dict=512 ) -> List[Any]: _lowerCAmelCase : Any = pil_image.resize((w, h) ,resample=Image.BICUBIC ,reducing_gap=1 ) _lowerCAmelCase : Dict = np.array(pil_image.convert("""RGB""" ) ) _lowerCAmelCase : List[str] = arr.astype(np.floataa ) / 1_27.5 - 1 _lowerCAmelCase : int = np.transpose(_lowerCamelCase ,[2, 0, 1] ) _lowerCAmelCase : Optional[Any] = torch.from_numpy(_lowerCamelCase ).unsqueeze(0 ) return image class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__ , a__ , a__ , ): super().__init__() self.register_modules( unet=a__ , scheduler=a__ , movq=a__ , ) _lowerCAmelCase : Optional[Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __A ( self , a__ , a__ , a__ ): # get the original timestep using init_timestep _lowerCAmelCase : Optional[Any] = min(int(num_inference_steps * strength ) , a__ ) _lowerCAmelCase : List[Any] = max(num_inference_steps - init_timestep , 0 ) _lowerCAmelCase : Dict = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def __A ( self , a__ , a__ , a__ , a__ , a__ , a__ , a__=None ): if not isinstance(a__ , (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(a__ )}" ) _lowerCAmelCase : Union[str, Any] = image.to(device=a__ , dtype=a__ ) _lowerCAmelCase : int = batch_size * num_images_per_prompt if image.shape[1] == 4: _lowerCAmelCase : int = image else: if isinstance(a__ , a__ ) and len(a__ ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(a__ )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) elif isinstance(a__ , a__ ): _lowerCAmelCase : Optional[int] = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a__ ) ] _lowerCAmelCase : Optional[int] = torch.cat(a__ , dim=0 ) else: _lowerCAmelCase : List[Any] = self.movq.encode(a__ ).latent_dist.sample(a__ ) _lowerCAmelCase : Dict = self.movq.config.scaling_factor * init_latents _lowerCAmelCase : str = torch.cat([init_latents] , dim=0 ) _lowerCAmelCase : Dict = init_latents.shape _lowerCAmelCase : str = randn_tensor(a__ , generator=a__ , device=a__ , dtype=a__ ) # get latents _lowerCAmelCase : Optional[Any] = self.scheduler.add_noise(a__ , a__ , a__ ) _lowerCAmelCase : int = init_latents return latents def __A ( self , a__=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) _lowerCAmelCase : str = torch.device(F"cuda:{gpu_id}" ) _lowerCAmelCase : int = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a__ , a__ ) def __A ( self , a__=0 ): if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) _lowerCAmelCase : Optional[int] = torch.device(F"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=a__ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _lowerCAmelCase : List[str] = None for cpu_offloaded_model in [self.unet, self.movq]: _lowerCAmelCase , _lowerCAmelCase : str = cpu_offload_with_hook(a__ , a__ , prev_module_hook=a__ ) # We'll offload the last model manually. _lowerCAmelCase : Tuple = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A ( self ): if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(a__ , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a__ ) def __call__( self , a__ , a__ , a__ , a__ = 512 , a__ = 512 , a__ = 100 , a__ = 4.0 , a__ = 0.3 , a__ = 1 , a__ = None , a__ = "pil" , a__ = True , ): _lowerCAmelCase : Dict = self._execution_device _lowerCAmelCase : Optional[Any] = guidance_scale > 1.0 if isinstance(a__ , a__ ): _lowerCAmelCase : Dict = torch.cat(a__ , dim=0 ) _lowerCAmelCase : Dict = image_embeds.shape[0] if isinstance(a__ , a__ ): _lowerCAmelCase : List[Any] = torch.cat(a__ , dim=0 ) if do_classifier_free_guidance: _lowerCAmelCase : int = image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Any = negative_image_embeds.repeat_interleave(a__ , dim=0 ) _lowerCAmelCase : Optional[int] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=a__ ) if not isinstance(a__ , a__ ): _lowerCAmelCase : Any = [image] if not all(isinstance(a__ , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( F"Input is in incorrect format: {[type(a__ ) for i in image]}. Currently, we only support PIL image and pytorch tensor" ) _lowerCAmelCase : Tuple = torch.cat([prepare_image(a__ , a__ , a__ ) for i in image] , dim=0 ) _lowerCAmelCase : Union[str, Any] = image.to(dtype=image_embeds.dtype , device=a__ ) _lowerCAmelCase : Union[str, Any] = self.movq.encode(a__ )["""latents"""] _lowerCAmelCase : Tuple = latents.repeat_interleave(a__ , dim=0 ) self.scheduler.set_timesteps(a__ , device=a__ ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.get_timesteps(a__ , a__ , a__ ) _lowerCAmelCase : Union[str, Any] = timesteps[:1].repeat(batch_size * num_images_per_prompt ) _lowerCAmelCase , _lowerCAmelCase : Dict = downscale_height_and_width(a__ , a__ , self.movq_scale_factor ) _lowerCAmelCase : List[str] = self.prepare_latents( a__ , a__ , a__ , a__ , image_embeds.dtype , a__ , a__ ) for i, t in enumerate(self.progress_bar(a__ ) ): # expand the latents if we are doing classifier free guidance _lowerCAmelCase : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowerCAmelCase : int = {"""image_embeds""": image_embeds} _lowerCAmelCase : List[str] = self.unet( sample=a__ , timestep=a__ , encoder_hidden_states=a__ , added_cond_kwargs=a__ , return_dict=a__ , )[0] if do_classifier_free_guidance: _lowerCAmelCase , _lowerCAmelCase : Optional[int] = noise_pred.split(latents.shape[1] , dim=1 ) _lowerCAmelCase , _lowerCAmelCase : List[Any] = noise_pred.chunk(2 ) _lowerCAmelCase , _lowerCAmelCase : Tuple = variance_pred.chunk(2 ) _lowerCAmelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _lowerCAmelCase : List[str] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , """variance_type""" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _lowerCAmelCase , _lowerCAmelCase : Dict = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _lowerCAmelCase : List[str] = self.scheduler.step( a__ , a__ , a__ , generator=a__ , )[0] # post-processing _lowerCAmelCase : int = self.movq.decode(a__ , force_not_quantize=a__ )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: _lowerCAmelCase : List[Any] = image * 0.5 + 0.5 _lowerCAmelCase : Any = image.clamp(0 , 1 ) _lowerCAmelCase : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _lowerCAmelCase : List[str] = self.numpy_to_pil(a__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=a__ )
663
1
"""simple docstring""" import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class __A ( unittest.TestCase ): 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.0_2 , a__=4 , ): _lowerCAmelCase : Any = parent _lowerCAmelCase : int = batch_size _lowerCAmelCase : Tuple = seq_length _lowerCAmelCase : Union[str, Any] = is_training _lowerCAmelCase : Union[str, Any] = use_attention_mask _lowerCAmelCase : str = use_token_type_ids _lowerCAmelCase : Tuple = use_labels _lowerCAmelCase : Optional[int] = vocab_size _lowerCAmelCase : int = hidden_size _lowerCAmelCase : Any = num_hidden_layers _lowerCAmelCase : Optional[Any] = num_attention_heads _lowerCAmelCase : Optional[int] = intermediate_size _lowerCAmelCase : List[str] = hidden_act _lowerCAmelCase : List[Any] = hidden_dropout_prob _lowerCAmelCase : Tuple = attention_probs_dropout_prob _lowerCAmelCase : Union[str, Any] = max_position_embeddings _lowerCAmelCase : List[Any] = type_vocab_size _lowerCAmelCase : Optional[Any] = type_sequence_label_size _lowerCAmelCase : List[Any] = initializer_range _lowerCAmelCase : List[str] = num_choices def __A ( self ): _lowerCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[int] = None if self.use_attention_mask: _lowerCAmelCase : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) _lowerCAmelCase : List[Any] = None if self.use_token_type_ids: _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _lowerCAmelCase : Optional[Any] = RobertaConfig( 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 ): _lowerCAmelCase : Any = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Dict = config_and_inputs _lowerCAmelCase : List[str] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict def __A ( self ): _lowerCAmelCase : int = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = config_and_inputs _lowerCAmelCase : Optional[int] = True _lowerCAmelCase : Optional[int] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) _lowerCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : int = True _UpperCamelCase : Dict = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def __A ( self ): _lowerCAmelCase : int = FlaxRobertaModelTester(self ) @slow def __A ( self ): for model_class_name in self.all_model_classes: _lowerCAmelCase : Tuple = model_class_name.from_pretrained("""roberta-base""" , from_pt=a__ ) _lowerCAmelCase : Tuple = model(np.ones((1, 1) ) ) self.assertIsNotNone(a__ )
663
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
1
"""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 ): def __init__( self ): super().__init__() _lowerCAmelCase : Any = nn.Linear(3 , 4 ) _lowerCAmelCase : int = nn.BatchNormad(4 ) _lowerCAmelCase : List[str] = nn.Linear(4 , 5 ) def __A ( self , a__ ): return self.lineara(self.batchnorm(self.lineara(a__ ) ) ) class __A ( SCREAMING_SNAKE_CASE_ ): def __A ( self , a__ , *a__ , **a__ ): return (args[0] + 1,) + args[1:], kwargs class __A ( SCREAMING_SNAKE_CASE_ ): def __A ( self , a__ , a__ ): return output + 1 class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = ModelForTest() _lowerCAmelCase : Optional[Any] = 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 ): _lowerCAmelCase : Optional[int] = ModelForTest() _lowerCAmelCase : int = 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 ): _lowerCAmelCase : Tuple = ModelForTest() _lowerCAmelCase : Optional[int] = torch.randn(2 , 3 ) _lowerCAmelCase : Optional[Any] = test_model(x + 1 ) _lowerCAmelCase : Optional[int] = test_model(x + 2 ) _lowerCAmelCase : Tuple = PreForwardHook() add_hook_to_module(a__ , a__ ) _lowerCAmelCase : List[str] = 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 _lowerCAmelCase : int = PreForwardHook() add_hook_to_module(a__ , a__ ) _lowerCAmelCase : Union[str, Any] = 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 _lowerCAmelCase : Tuple = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(a__ , a__ ) _lowerCAmelCase : Union[str, Any] = test_model(a__ ) assert torch.allclose(a__ , a__ , atol=1e-5 ) def __A ( self ): _lowerCAmelCase : List[str] = ModelForTest() _lowerCAmelCase : int = torch.randn(2 , 3 ) _lowerCAmelCase : List[Any] = test_model(a__ ) _lowerCAmelCase : Any = PostForwardHook() add_hook_to_module(a__ , a__ ) _lowerCAmelCase : Optional[int] = 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 _lowerCAmelCase : int = PostForwardHook() add_hook_to_module(a__ , a__ ) _lowerCAmelCase : str = 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 _lowerCAmelCase : Optional[Any] = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(a__ , a__ ) _lowerCAmelCase : Union[str, Any] = test_model(a__ ) assert torch.allclose(a__ , output + 2 , atol=1e-5 ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = ModelForTest() _lowerCAmelCase : Optional[Any] = torch.randn(2 , 3 ) _lowerCAmelCase : Any = test_model(a__ ) _lowerCAmelCase : Tuple = PostForwardHook() add_hook_to_module(a__ , a__ ) _lowerCAmelCase : str = test_model(a__ ) self.assertTrue(torch.allclose(a__ , output + 1 ) ) self.assertTrue(outputa.requires_grad ) _lowerCAmelCase : List[str] = True _lowerCAmelCase : Tuple = test_model(a__ ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def __A ( self ): _lowerCAmelCase : Optional[Any] = 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 _lowerCAmelCase : List[Any] = torch.randn(2 , 3 ) _lowerCAmelCase : Optional[int] = 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__ ) ) _lowerCAmelCase : Union[str, Any] = torch.randn(2 , 3 ).to(0 ) _lowerCAmelCase : List[Any] = model(a__ ) self.assertEqual(output.device , torch.device(0 ) ) def __A ( self ): _lowerCAmelCase : Any = 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 _lowerCAmelCase : List[str] = {"""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 _lowerCAmelCase : Optional[Any] = torch.device(hook_kwargs["""execution_device"""] ) self.assertEqual(model.batchnorm.running_mean.device , a__ ) _lowerCAmelCase : Dict = torch.randn(2 , 3 ) _lowerCAmelCase : Union[str, Any] = 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 _lowerCAmelCase : Optional[int] = { """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""" ) ) _lowerCAmelCase : Union[str, Any] = torch.randn(2 , 3 ) _lowerCAmelCase : Any = 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 ): _lowerCAmelCase : Tuple = 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 _lowerCAmelCase : Tuple = 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 _lowerCAmelCase : Dict = torch.device(a__ ) self.assertEqual(model.batchnorm.running_mean.device , a__ ) _lowerCAmelCase : Any = torch.randn(2 , 3 ) _lowerCAmelCase : Dict = 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""" ) ) _lowerCAmelCase : Union[str, Any] = torch.randn(2 , 3 ) _lowerCAmelCase : Union[str, Any] = 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 ): _lowerCAmelCase : Any = 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 _lowerCAmelCase : str = 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 _lowerCAmelCase : int = torch.device(a__ ) self.assertEqual(model.batchnorm.running_mean.device , a__ ) _lowerCAmelCase : Dict = torch.randn(2 , 3 ) _lowerCAmelCase : Any = 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""" ) ) _lowerCAmelCase : List[str] = torch.randn(2 , 3 ) _lowerCAmelCase : Any = 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""" ) )
663
"""simple docstring""" import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : str = CpmAntTokenizer _UpperCamelCase : List[Any] = False def __A ( self ): super().setUp() _lowerCAmelCase : Dict = [ """<d>""", """</d>""", """<s>""", """</s>""", """</_>""", """<unk>""", """<pad>""", """</n>""", """我""", """是""", """C""", """P""", """M""", """A""", """n""", """t""", ] _lowerCAmelCase : int = 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] ) ) @tooslow def __A ( self ): _lowerCAmelCase : Tuple = CpmAntTokenizer.from_pretrained("""openbmb/cpm-ant-10b""" ) _lowerCAmelCase : Optional[Any] = """今天天气真好!""" _lowerCAmelCase : Any = ["""今天""", """天气""", """真""", """好""", """!"""] _lowerCAmelCase : str = tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = """今天天气真好!""" _lowerCAmelCase : Optional[Any] = [tokenizer.bos_token] + tokens _lowerCAmelCase : Optional[int] = [6, 9802, 14962, 2082, 831, 244] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) _lowerCAmelCase : Tuple = tokenizer.decode(a__ ) self.assertEqual(a__ , a__ )
663
1
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : int ) -> str: return "\n".join( f"{number} * {i} = {number * i}" for i in range(1 ,number_of_terms + 1 ) ) if __name__ == "__main__": print(multiplication_table(number=5, number_of_terms=10))
663
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = CodeGenTokenizer _UpperCamelCase : Dict = CodeGenTokenizerFast _UpperCamelCase : Tuple = True _UpperCamelCase : List[Any] = {"add_prefix_space": True} _UpperCamelCase : str = False def __A ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowerCAmelCase : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", """<|endoftext|>""", ] _lowerCAmelCase : Optional[int] = dict(zip(a__ , range(len(a__ ) ) ) ) _lowerCAmelCase : str = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _lowerCAmelCase : Any = {"""unk_token""": """<unk>"""} _lowerCAmelCase : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _lowerCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(a__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(a__ ) ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , **a__ ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **a__ ) def __A ( self , a__ ): _lowerCAmelCase : str = """lower newer""" _lowerCAmelCase : Tuple = """lower newer""" return input_text, output_text def __A ( self ): _lowerCAmelCase : str = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowerCAmelCase : int = """lower newer""" _lowerCAmelCase : List[str] = ["""\u0120low""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] _lowerCAmelCase : Optional[int] = tokenizer.tokenize(a__ , add_prefix_space=a__ ) self.assertListEqual(a__ , a__ ) _lowerCAmelCase : Tuple = tokens + [tokenizer.unk_token] _lowerCAmelCase : List[str] = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self ): if not self.test_rust_tokenizer: return _lowerCAmelCase : Optional[int] = self.get_tokenizer() _lowerCAmelCase : Optional[int] = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Any = """lower newer""" # Testing tokenization _lowerCAmelCase : Any = tokenizer.tokenize(a__ , add_prefix_space=a__ ) _lowerCAmelCase : int = rust_tokenizer.tokenize(a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids without special tokens _lowerCAmelCase : Union[str, Any] = tokenizer.encode(a__ , add_special_tokens=a__ , add_prefix_space=a__ ) _lowerCAmelCase : Dict = rust_tokenizer.encode(a__ , add_special_tokens=a__ ) self.assertListEqual(a__ , a__ ) # Testing conversion to ids with special tokens _lowerCAmelCase : int = self.get_rust_tokenizer(add_prefix_space=a__ ) _lowerCAmelCase : Optional[int] = tokenizer.encode(a__ , add_prefix_space=a__ ) _lowerCAmelCase : Any = rust_tokenizer.encode(a__ ) self.assertListEqual(a__ , a__ ) # Testing the unknown token _lowerCAmelCase : List[str] = tokens + [rust_tokenizer.unk_token] _lowerCAmelCase : Dict = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(a__ ) , a__ ) def __A ( self , *a__ , **a__ ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def __A ( self , a__=15 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowerCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained(a__ , **a__ ) # Simple input _lowerCAmelCase : Dict = """This is a simple input""" _lowerCAmelCase : Optional[int] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : Optional[int] = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : str = [ ("""This is a simple input 1""", """This is a simple input 2"""), ("""This is a simple pair 1""", """This is a simple pair 2"""), ] # Simple input tests self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Simple input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) # Pair input self.assertRaises(a__ , tokenizer_r.encode , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises(a__ , tokenizer_r.encode_plus , a__ , max_length=a__ , padding="""max_length""" ) # Pair input self.assertRaises( a__ , tokenizer_r.batch_encode_plus , a__ , max_length=a__ , padding="""max_length""" , ) def __A ( self ): _lowerCAmelCase : Any = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token="""<pad>""" ) # Simple input _lowerCAmelCase : Union[str, Any] = """This is a simple input""" _lowerCAmelCase : Dict = ["""This is a simple input looooooooong""", """This is a simple input"""] _lowerCAmelCase : Any = ("""This is a simple input""", """This is a pair""") _lowerCAmelCase : Optional[int] = [ ("""This is a simple input loooooong""", """This is a simple input"""), ("""This is a simple pair loooooong""", """This is a simple pair"""), ] _lowerCAmelCase : Optional[int] = tokenizer.pad_token_id _lowerCAmelCase : Any = tokenizer(a__ , padding="""max_length""" , max_length=30 , return_tensors="""np""" ) _lowerCAmelCase : str = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(*a__ , padding="""max_length""" , max_length=60 , return_tensors="""np""" ) _lowerCAmelCase : int = tokenizer(a__ , padding=a__ , truncate=a__ , return_tensors="""np""" ) # s # test single string max_length padding self.assertEqual(out_s["""input_ids"""].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s["""input_ids"""] ) self.assertTrue(0 in out_s["""attention_mask"""] ) # s2 # test automatic padding self.assertEqual(out_sa["""input_ids"""].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa["""input_ids"""][0] ) self.assertFalse(0 in out_sa["""attention_mask"""][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa["""input_ids"""][1] ) self.assertTrue(0 in out_sa["""attention_mask"""][1] ) # p # test single pair max_length padding self.assertEqual(out_p["""input_ids"""].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p["""input_ids"""] ) self.assertTrue(0 in out_p["""attention_mask"""] ) # p2 # test automatic padding pair self.assertEqual(out_pa["""input_ids"""].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa["""input_ids"""][0] ) self.assertFalse(0 in out_pa["""attention_mask"""][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa["""input_ids"""][1] ) self.assertTrue(0 in out_pa["""attention_mask"""][1] ) def __A ( self ): _lowerCAmelCase : List[str] = """$$$""" _lowerCAmelCase : str = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=a__ , add_bos_token=a__ ) _lowerCAmelCase : Tuple = """This is a simple input""" _lowerCAmelCase : Union[str, Any] = ["""This is a simple input 1""", """This is a simple input 2"""] _lowerCAmelCase : List[str] = tokenizer.bos_token_id _lowerCAmelCase : str = tokenizer(a__ ) _lowerCAmelCase : Union[str, Any] = tokenizer(a__ ) self.assertEqual(out_s.input_ids[0] , a__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) _lowerCAmelCase : Optional[Any] = tokenizer.decode(out_s.input_ids ) _lowerCAmelCase : Optional[int] = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , a__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def __A ( self ): _lowerCAmelCase : int = CodeGenTokenizer.from_pretrained("""Salesforce/codegen-350M-mono""" ) _lowerCAmelCase : Optional[int] = """\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#""" _lowerCAmelCase : List[Any] = """\nif len_a > len_b: result = a\nelse: result = b""" _lowerCAmelCase : Tuple = tokenizer.encode(a__ ) _lowerCAmelCase : Optional[Any] = ["""^#""", re.escape("""<|endoftext|>""" ), """^'''""", """^\"\"\"""", """\n\n\n"""] _lowerCAmelCase : int = tokenizer.decode(a__ , truncate_before_pattern=a__ ) self.assertEqual(a__ , a__ ) def __A ( self ): pass
663
1
"""simple docstring""" from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __A : pass
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _a : int = { 'configuration_altclip': [ 'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AltCLIPConfig', 'AltCLIPTextConfig', 'AltCLIPVisionConfig', ], 'processing_altclip': ['AltCLIPProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Dict = [ 'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST', 'AltCLIPPreTrainedModel', 'AltCLIPModel', 'AltCLIPTextModel', 'AltCLIPVisionModel', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys _a : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Optional[torch.FloatTensor] = None _UpperCamelCase : torch.FloatTensor = None _UpperCamelCase : Optional[Tuple[torch.FloatTensor]] = None _UpperCamelCase : Optional[Tuple[torch.FloatTensor]] = None class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , a__=1 , a__=0 , a__=2 , a__=512 , a__="cls" , a__=False , a__=True , **a__ , ): super().__init__(pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ , **a__ ) _lowerCAmelCase : Union[str, Any] = project_dim _lowerCAmelCase : Optional[int] = pooler_fn _lowerCAmelCase : Any = learn_encoder _lowerCAmelCase : Union[str, Any] = use_attention_mask class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : List[Any] = [R"pooler", R"logit_scale"] _UpperCamelCase : int = [R"position_ids", R"predictions.decoder.bias"] _UpperCamelCase : Dict = "roberta" _UpperCamelCase : Tuple = RobertaSeriesConfig def __init__( self , a__ ): super().__init__(a__ ) _lowerCAmelCase : Any = XLMRobertaModel(a__ ) _lowerCAmelCase : Union[str, Any] = nn.Linear(config.hidden_size , config.project_dim ) _lowerCAmelCase : List[Any] = getattr(a__ , """has_pre_transformation""" , a__ ) if self.has_pre_transformation: _lowerCAmelCase : List[Any] = nn.Linear(config.hidden_size , config.project_dim ) _lowerCAmelCase : Tuple = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps ) self.post_init() def __A ( self , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , ): _lowerCAmelCase : Optional[int] = return_dict if return_dict is not None else self.config.use_return_dict _lowerCAmelCase : Any = self.base_model( input_ids=a__ , attention_mask=a__ , token_type_ids=a__ , position_ids=a__ , head_mask=a__ , inputs_embeds=a__ , encoder_hidden_states=a__ , encoder_attention_mask=a__ , output_attentions=a__ , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=a__ , ) if self.has_pre_transformation: _lowerCAmelCase : List[str] = outputs["""hidden_states"""][-2] _lowerCAmelCase : Tuple = self.pre_LN(a__ ) _lowerCAmelCase : List[Any] = self.transformation_pre(a__ ) return TransformationModelOutput( projection_state=a__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: _lowerCAmelCase : Dict = self.transformation(outputs.last_hidden_state ) return TransformationModelOutput( projection_state=a__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
663
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ) -> bool: 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...') _a : int = int(input('Enter number: ').strip()) print(F"""{number} is {"" if perfect(number) else "not "}a Perfect Number.""")
663
1
"""simple docstring""" import os from tempfile import TemporaryDirectory from unittest import TestCase import pytest from absl.testing import parameterized from datasets import config from datasets.arrow_reader import HF_GCP_BASE_URL from datasets.builder import DatasetBuilder from datasets.dataset_dict import IterableDatasetDict from datasets.iterable_dataset import IterableDataset from datasets.load import dataset_module_factory, import_main_class from datasets.utils.file_utils import cached_path _a : Optional[Any] = [ {'dataset': 'wikipedia', 'config_name': '20220301.de'}, {'dataset': 'wikipedia', 'config_name': '20220301.en'}, {'dataset': 'wikipedia', 'config_name': '20220301.fr'}, {'dataset': 'wikipedia', 'config_name': '20220301.frr'}, {'dataset': 'wikipedia', 'config_name': '20220301.it'}, {'dataset': 'wikipedia', 'config_name': '20220301.simple'}, {'dataset': 'snli', 'config_name': 'plain_text'}, {'dataset': 'eli5', 'config_name': 'LFQA_reddit'}, {'dataset': 'wiki40b', 'config_name': 'en'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.nq.compressed'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.nq.no_index'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.multiset.no_index'}, {'dataset': 'natural_questions', 'config_name': 'default'}, ] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Dict=True ) -> List[str]: if with_config: return [ { "testcase_name": d["dataset"] + "/" + d["config_name"], "dataset": d["dataset"], "config_name": d["config_name"], } for d in DATASETS_ON_HF_GCP ] else: return [ {"testcase_name": dataset, "dataset": dataset} for dataset in {d["dataset"] for d in DATASETS_ON_HF_GCP} ] @parameterized.named_parameters(list_datasets_on_hf_gcp_parameters(with_config=SCREAMING_SNAKE_CASE_ ) ) class __A ( SCREAMING_SNAKE_CASE_ ): _UpperCamelCase : Tuple = None _UpperCamelCase : Dict = None def __A ( self , a__ , a__ ): with TemporaryDirectory() as tmp_dir: _lowerCAmelCase : int = dataset_module_factory(a__ , cache_dir=a__ ) _lowerCAmelCase : Union[str, Any] = import_main_class(dataset_module.module_path , dataset=a__ ) _lowerCAmelCase : DatasetBuilder = builder_cls( cache_dir=a__ , config_name=a__ , hash=dataset_module.hash , ) _lowerCAmelCase : Any = """/""".join( [ HF_GCP_BASE_URL, builder_instance._relative_data_dir(with_hash=a__ ).replace(os.sep , """/""" ), config.DATASET_INFO_FILENAME, ] ) _lowerCAmelCase : Optional[Any] = cached_path(a__ , cache_dir=a__ ) self.assertTrue(os.path.exists(a__ ) ) @pytest.mark.integration def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ) -> str: _lowerCAmelCase : str = tmp_path_factory.mktemp("""test_hf_gcp""" ) / """test_wikipedia_simple""" _lowerCAmelCase : List[str] = dataset_module_factory("""wikipedia""" ,cache_dir=_lowerCamelCase ) _lowerCAmelCase : Any = import_main_class(dataset_module.module_path ) _lowerCAmelCase : DatasetBuilder = builder_cls( cache_dir=_lowerCamelCase ,config_name="""20220301.frr""" ,hash=dataset_module.hash ,) # use the HF cloud storage, not the original download_and_prepare that uses apache-beam _lowerCAmelCase : Any = None builder_instance.download_and_prepare() _lowerCAmelCase : List[Any] = builder_instance.as_dataset() assert ds @pytest.mark.integration def SCREAMING_SNAKE_CASE ( _lowerCamelCase : List[Any] ) -> Optional[int]: _lowerCAmelCase : Optional[Any] = dataset_module_factory("""wikipedia""" ,cache_dir=_lowerCamelCase ) _lowerCAmelCase : List[Any] = import_main_class(dataset_module.module_path ,dataset=_lowerCamelCase ) _lowerCAmelCase : DatasetBuilder = builder_cls( cache_dir=_lowerCamelCase ,config_name="""20220301.frr""" ,hash=dataset_module.hash ,) _lowerCAmelCase : List[str] = builder_instance.as_streaming_dataset() assert ds assert isinstance(_lowerCamelCase ,_lowerCamelCase ) assert "train" in ds assert isinstance(ds["""train"""] ,_lowerCamelCase ) assert next(iter(ds["""train"""] ) )
663
"""simple docstring""" from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class __A : _UpperCamelCase : int _UpperCamelCase : Node | None = None _UpperCamelCase : Node | None = None def SCREAMING_SNAKE_CASE ( ) -> Node | None: _lowerCAmelCase : Tuple = Node(1 ) _lowerCAmelCase : int = Node(2 ) _lowerCAmelCase : int = Node(3 ) _lowerCAmelCase : Any = Node(4 ) _lowerCAmelCase : Dict = Node(5 ) return tree def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> list[int]: return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> int: return (max(height(root.left ) ,height(root.right ) ) + 1) if root else 0 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] if root is None: return output _lowerCAmelCase : Union[str, Any] = deque([root] ) while process_queue: _lowerCAmelCase : Optional[Any] = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left ,level - 1 ) populate_output(root.right ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> Sequence[Node | None]: _lowerCAmelCase : list[Any] = [] def populate_output(_lowerCamelCase : Node | None ,_lowerCamelCase : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right ,level - 1 ) populate_output(root.left ,level - 1 ) populate_output(_lowerCamelCase ,_lowerCamelCase ) return output def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Node | None ) -> Sequence[Node | None] | list[Any]: if root is None: return [] _lowerCAmelCase : list[Sequence[Node | None]] = [] _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : Dict = height(_lowerCamelCase ) for h in range(1 ,height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Any = 1 else: output.append(get_nodes_from_right_to_left(_lowerCamelCase ,_lowerCamelCase ) ) _lowerCAmelCase : Optional[int] = 0 return output def SCREAMING_SNAKE_CASE ( ) -> None: # Main function for testing. _lowerCAmelCase : int = make_tree() print(f"In-order Traversal: {inorder(_lowerCamelCase )}" ) print(f"Pre-order Traversal: {preorder(_lowerCamelCase )}" ) print(f"Post-order Traversal: {postorder(_lowerCamelCase )}" ,"""\n""" ) print(f"Height of Tree: {height(_lowerCamelCase )}" ,"""\n""" ) print("""Complete Level Order Traversal: """ ) print(level_order(_lowerCamelCase ) ,"""\n""" ) print("""Level-wise order Traversal: """ ) for level in range(1 ,height(_lowerCamelCase ) + 1 ): print(f"Level {level}:" ,get_nodes_from_left_to_right(_lowerCamelCase ,level=_lowerCamelCase ) ) print("""\nZigZag order Traversal: """ ) print(zigzag(_lowerCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
663
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _a : Tuple = logging.get_logger(__name__) class __A ( SCREAMING_SNAKE_CASE_ ): def __init__( self , *a__ , **a__ ): warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , a__ , ) super().__init__(*a__ , **a__ )
663
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : str = torch.nn.Linear(10 , 10 ) _lowerCAmelCase : Optional[Any] = torch.optim.SGD(model.parameters() , 0.1 ) _lowerCAmelCase : Optional[Any] = Accelerator() _lowerCAmelCase : Tuple = accelerator.prepare(a__ ) try: pickle.loads(pickle.dumps(a__ ) ) except Exception as e: self.fail(F"Accelerated optimizer pickling failed with {e}" ) AcceleratorState._reset_state()
663
1
"""simple docstring""" class __A : def __init__( self ): _lowerCAmelCase : dict[str, TrieNode] = {} # Mapping from char to TrieNode _lowerCAmelCase : Tuple = False def __A ( self , a__ ): for word in words: self.insert(a__ ) def __A ( self , a__ ): _lowerCAmelCase : Optional[Any] = self for char in word: if char not in curr.nodes: _lowerCAmelCase : List[str] = TrieNode() _lowerCAmelCase : str = curr.nodes[char] _lowerCAmelCase : int = True def __A ( self , a__ ): _lowerCAmelCase : Optional[int] = self for char in word: if char not in curr.nodes: return False _lowerCAmelCase : Tuple = curr.nodes[char] return curr.is_leaf def __A ( self , a__ ): def _delete(a__ , a__ , a__ ) -> bool: if index == len(a__ ): # If word does not exist if not curr.is_leaf: return False _lowerCAmelCase : Optional[Any] = False return len(curr.nodes ) == 0 _lowerCAmelCase : Union[str, Any] = word[index] _lowerCAmelCase : Tuple = curr.nodes.get(a__ ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted _lowerCAmelCase : Dict = _delete(a__ , a__ , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , a__ , 0 ) def SCREAMING_SNAKE_CASE ( _lowerCamelCase : TrieNode ,_lowerCamelCase : str ) -> None: if node.is_leaf: print(_lowerCamelCase ,end=""" """ ) for key, value in node.nodes.items(): print_words(_lowerCamelCase ,word + key ) def SCREAMING_SNAKE_CASE ( ) -> bool: _lowerCAmelCase : Dict = """banana bananas bandana band apple all beast""".split() _lowerCAmelCase : Dict = TrieNode() root.insert_many(_lowerCamelCase ) # print_words(root, "") assert all(root.find(_lowerCamelCase ) for word in words ) assert root.find("""banana""" ) assert not root.find("""bandanas""" ) assert not root.find("""apps""" ) assert root.find("""apple""" ) assert root.find("""all""" ) root.delete("""all""" ) assert not root.find("""all""" ) root.delete("""banana""" ) assert not root.find("""banana""" ) assert root.find("""bananas""" ) return True def SCREAMING_SNAKE_CASE ( _lowerCamelCase : str ,_lowerCamelCase : bool ) -> None: print(str(_lowerCamelCase ) ,"""works!""" if passes else """doesn't work :(""" ) def SCREAMING_SNAKE_CASE ( ) -> None: assert test_trie() def SCREAMING_SNAKE_CASE ( ) -> None: print_results("""Testing trie functionality""" ,test_trie() ) if __name__ == "__main__": main()
663
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Union[str, Any] ,_lowerCamelCase : Tuple ) -> Dict: _lowerCAmelCase : List[str] = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality _lowerCAmelCase : Any = len(vectors[0] ) # Will help select random centroids from among the available vectors _lowerCAmelCase : Any = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. _lowerCAmelCase : List[Any] = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION _lowerCAmelCase : str = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points _lowerCAmelCase : List[str] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float64""" ,[dim] ) _lowerCAmelCase : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) _lowerCAmelCase : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value _lowerCAmelCase : List[Any] = tf.placeholder("""int32""" ) _lowerCAmelCase : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase ,_lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input _lowerCAmelCase : Union[str, Any] = tf.placeholder("""float""" ,[None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors _lowerCAmelCase : Optional[int] = tf.reduce_mean(_lowerCamelCase ,0 ) ##Node for computing Euclidean distances # Placeholders for input _lowerCAmelCase : Dict = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : Any = tf.placeholder("""float""" ,[dim] ) _lowerCAmelCase : List[Any] = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase ,_lowerCamelCase ) ,2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input _lowerCAmelCase : Any = tf.placeholder("""float""" ,[noofclusters] ) _lowerCAmelCase : str = tf.argmin(_lowerCamelCase ,0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. _lowerCAmelCase : Optional[Any] = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. _lowerCAmelCase : List[str] = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): _lowerCAmelCase : int = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. _lowerCAmelCase : Any = [ sess.run(_lowerCamelCase ,feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input _lowerCAmelCase : Any = sess.run( _lowerCamelCase ,feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n] ,feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster _lowerCAmelCase : List[Any] = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location _lowerCAmelCase : Optional[int] = sess.run( _lowerCamelCase ,feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n] ,feed_dict={centroid_value: new_location} ) # Return centroids and assignments _lowerCAmelCase : Optional[int] = sess.run(_lowerCamelCase ) _lowerCAmelCase : List[Any] = sess.run(_lowerCamelCase ) return centroids, assignments
663
1
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Optional[int] = ShapEPipeline _UpperCamelCase : Optional[Any] = ["prompt"] _UpperCamelCase : Tuple = ["prompt"] _UpperCamelCase : Dict = [ "num_images_per_prompt", "num_inference_steps", "generator", "latents", "guidance_scale", "frame_size", "output_type", "return_dict", ] _UpperCamelCase : str = False @property def __A ( self ): return 32 @property def __A ( self ): return 32 @property def __A ( self ): return self.time_input_dim * 4 @property def __A ( self ): return 8 @property def __A ( self ): _lowerCAmelCase : Union[str, Any] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : List[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(a__ ) @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } _lowerCAmelCase : Any = PriorTransformer(**a__ ) return model @property def __A ( self ): torch.manual_seed(0 ) _lowerCAmelCase : Tuple = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } _lowerCAmelCase : Dict = ShapERenderer(**a__ ) return model def __A ( self ): _lowerCAmelCase : Union[str, Any] = self.dummy_prior _lowerCAmelCase : Any = self.dummy_text_encoder _lowerCAmelCase : List[Any] = self.dummy_tokenizer _lowerCAmelCase : Dict = self.dummy_renderer _lowerCAmelCase : List[Any] = HeunDiscreteScheduler( beta_schedule="""exp""" , num_train_timesteps=1024 , prediction_type="""sample""" , use_karras_sigmas=a__ , clip_sample=a__ , clip_sample_range=1.0 , ) _lowerCAmelCase : List[Any] = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __A ( self , a__ , a__=0 ): if str(a__ ).startswith("""mps""" ): _lowerCAmelCase : List[str] = torch.manual_seed(a__ ) else: _lowerCAmelCase : Union[str, Any] = torch.Generator(device=a__ ).manual_seed(a__ ) _lowerCAmelCase : Dict = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __A ( self ): _lowerCAmelCase : List[Any] = """cpu""" _lowerCAmelCase : List[Any] = self.get_dummy_components() _lowerCAmelCase : str = self.pipeline_class(**a__ ) _lowerCAmelCase : List[Any] = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = pipe(**self.get_dummy_inputs(a__ ) ) _lowerCAmelCase : List[str] = output.images[0] _lowerCAmelCase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) _lowerCAmelCase : Union[str, Any] = np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __A ( self ): # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __A ( self ): _lowerCAmelCase : Any = torch_device == """cpu""" _lowerCAmelCase : Dict = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=a__ , relax_max_difference=a__ , ) def __A ( self ): _lowerCAmelCase : int = self.get_dummy_components() _lowerCAmelCase : Optional[Any] = self.pipeline_class(**a__ ) _lowerCAmelCase : int = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : str = 1 _lowerCAmelCase : Optional[Any] = 2 _lowerCAmelCase : List[Any] = self.get_dummy_inputs(a__ ) for key in inputs.keys(): if key in self.batch_params: _lowerCAmelCase : str = batch_size * [inputs[key]] _lowerCAmelCase : Tuple = pipe(**a__ , num_images_per_prompt=a__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __A ( unittest.TestCase ): def __A ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ): _lowerCAmelCase : Dict = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/test_shap_e_np_out.npy""" ) _lowerCAmelCase : Union[str, Any] = ShapEPipeline.from_pretrained("""openai/shap-e""" ) _lowerCAmelCase : Tuple = pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) _lowerCAmelCase : Optional[int] = torch.Generator(device=a__ ).manual_seed(0 ) _lowerCAmelCase : Any = pipe( """a shark""" , generator=a__ , guidance_scale=1_5.0 , num_inference_steps=64 , frame_size=64 , output_type="""np""" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(a__ , a__ )
663
"""simple docstring""" _a : Optional[Any] = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _a : List[str] = [{'type': 'code', 'content': INSTALL_CONTENT}] _a : str = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
663
1
"""simple docstring""" import socket def SCREAMING_SNAKE_CASE ( ) -> int: _lowerCAmelCase : Dict = socket.socket(socket.AF_INET ,socket.SOCK_STREAM ) _lowerCAmelCase : Dict = socket.gethostname() _lowerCAmelCase : Tuple = 12312 sock.connect((host, port) ) sock.send(b"""Hello server!""" ) with open("""Received_file""" ,"""wb""" ) as out_file: print("""File opened""" ) print("""Receiving data...""" ) while True: _lowerCAmelCase : Optional[int] = sock.recv(1024 ) if not data: break out_file.write(_lowerCamelCase ) print("""Successfully received the file""" ) sock.close() print("""Connection closed""" ) if __name__ == "__main__": main()
663
"""simple docstring""" from __future__ import annotations import random import unittest from transformers import TransfoXLConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST, TFTransfoXLForSequenceClassification, TFTransfoXLLMHeadModel, TFTransfoXLModel, ) class __A : def __init__( self , a__ , ): _lowerCAmelCase : Optional[Any] = parent _lowerCAmelCase : Tuple = 13 _lowerCAmelCase : Tuple = 7 _lowerCAmelCase : Any = 30 _lowerCAmelCase : Optional[int] = self.seq_length + self.mem_len _lowerCAmelCase : Dict = 15 _lowerCAmelCase : List[Any] = True _lowerCAmelCase : Any = True _lowerCAmelCase : List[str] = 99 _lowerCAmelCase : List[Any] = [10, 50, 80] _lowerCAmelCase : Tuple = 32 _lowerCAmelCase : int = 32 _lowerCAmelCase : Dict = 4 _lowerCAmelCase : List[str] = 8 _lowerCAmelCase : Tuple = 128 _lowerCAmelCase : Any = 2 _lowerCAmelCase : List[Any] = 2 _lowerCAmelCase : List[Any] = None _lowerCAmelCase : Optional[Any] = 1 _lowerCAmelCase : Tuple = 0 _lowerCAmelCase : List[Any] = 3 _lowerCAmelCase : Optional[int] = self.vocab_size - 1 _lowerCAmelCase : Dict = 0.0_1 def __A ( self ): _lowerCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : List[str] = None if self.use_labels: _lowerCAmelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowerCAmelCase : Union[str, Any] = TransfoXLConfig( vocab_size=self.vocab_size , mem_len=self.mem_len , clamp_len=self.clamp_len , cutoffs=self.cutoffs , d_model=self.hidden_size , d_embed=self.d_embed , n_head=self.num_attention_heads , d_head=self.d_head , d_inner=self.d_inner , div_val=self.div_val , n_layer=self.num_hidden_layers , eos_token_id=self.eos_token_id , pad_token_id=self.vocab_size - 1 , init_range=self.init_range , num_labels=self.num_labels , ) return (config, input_ids_a, input_ids_a, lm_labels) def __A ( self ): random.seed(self.seed ) tf.random.set_seed(self.seed ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Optional[int] = TFTransfoXLModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() _lowerCAmelCase : Optional[Any] = {"""input_ids""": input_ids_a, """mems""": mems_a} _lowerCAmelCase , _lowerCAmelCase : List[Any] = model(a__ ).to_tuple() self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(hidden_states_a.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : int = TFTransfoXLLMHeadModel(a__ ) _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase : Dict = {"""input_ids""": input_ids_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : str = model(a__ ).to_tuple() _lowerCAmelCase , _lowerCAmelCase : Optional[Any] = model([input_ids_a, mems_a] ).to_tuple() _lowerCAmelCase : Any = {"""input_ids""": input_ids_a, """mems""": mems_a, """labels""": lm_labels} _lowerCAmelCase , _lowerCAmelCase : Optional[int] = model(a__ ).to_tuple() self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) self.parent.assertEqual(lm_logits_a.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertListEqual( [mem.shape for mem in mems_a] , [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers , ) def __A ( self , a__ , a__ , a__ , a__ ): _lowerCAmelCase : Tuple = TFTransfoXLForSequenceClassification(a__ ) _lowerCAmelCase : int = model(a__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() ((_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase) , (_lowerCAmelCase)) : Dict = config_and_inputs _lowerCAmelCase : List[Any] = {"""input_ids""": input_ids_a} return config, inputs_dict @require_tf class __A ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Dict = ( (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else () ) _UpperCamelCase : Tuple = () if is_tf_available() else () _UpperCamelCase : Any = ( { "feature-extraction": TFTransfoXLModel, "text-classification": TFTransfoXLForSequenceClassification, "text-generation": TFTransfoXLLMHeadModel, "zero-shot": TFTransfoXLForSequenceClassification, } if is_tf_available() else {} ) # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented _UpperCamelCase : str = False _UpperCamelCase : str = False _UpperCamelCase : Tuple = False _UpperCamelCase : Any = False def __A ( self , a__ , a__ , a__ , a__ , a__ ): if pipeline_test_casse_name == "TextGenerationPipelineTests": # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`. # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple # tokenizer. return True return False def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLModelTester(self ) _lowerCAmelCase : List[Any] = ConfigTester(self , config_class=a__ , d_embed=37 ) def __A ( self ): self.config_tester.run_common_tests() def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_model(*a__ ) def __A ( self ): self.model_tester.set_seed() _lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_lm_head(*a__ ) def __A ( self ): _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*a__ ) def __A ( self ): _lowerCAmelCase , _lowerCAmelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() _lowerCAmelCase : List[Any] = [TFTransfoXLForSequenceClassification] for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(a__ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class in list_other_models_with_output_ebd: _lowerCAmelCase : str = model.get_output_embeddings() assert isinstance(a__ , tf.keras.layers.Layer ) _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None else: _lowerCAmelCase : Union[str, Any] = model.get_output_embeddings() assert x is None _lowerCAmelCase : Optional[int] = model.get_bias() assert name is None def __A ( self ): # TODO JP: Make TransfoXL XLA compliant pass @slow def __A ( self ): for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Union[str, Any] = TFTransfoXLModel.from_pretrained(a__ ) self.assertIsNotNone(a__ ) @unittest.skip(reason="""This model doesn't play well with fit() due to not returning a single loss.""" ) def __A ( self ): pass @require_tf class __A ( unittest.TestCase ): @unittest.skip("""Skip test until #12651 is resolved.""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = TFTransfoXLLMHeadModel.from_pretrained("""transfo-xl-wt103""" ) # fmt: off _lowerCAmelCase : List[str] = tf.convert_to_tensor([[33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0]] , dtype=tf.intaa ) # noqa: E231 # fmt: on # In 1991 , the remains of Russian Tsar Nicholas II and his family # ( except for Alexei and Maria ) are discovered . # The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the # remainder of the story . 1883 Western Siberia , # a young Grigori Rasputin is asked by his father and a group of men to perform magic . # Rasputin has a vision and denounces one of the men as a horse thief . Although his # father initially slaps him for making such an accusation , Rasputin watches as the # man is chased outside and beaten . Twenty years later , Rasputin sees a vision of # the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous , # with people , even a bishop , begging for his blessing . <eod> </s> <eos> # fmt: off _lowerCAmelCase : List[Any] = [33,1297,2,1,1009,4,1109,11739,4762,358,5,25,245,22,1706,17,20098,5,3215,21,37,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,6224,831,16002,2,8,603,78967,29546,23,803,20,25,416,5,8,232,4,277,6,1855,4601,3,29546,54,8,3609,5,57211,49,4,1,277,18,8,1755,15691,3,341,25,416,693,42573,71,17,401,94,31,17919,2,29546,7873,18,1,435,23,11011,755,5,5167,3,7983,98,84,2,29546,3267,8,3609,4,1,4865,1075,2,6087,71,6,346,8,5854,3,29546,824,1400,1868,2,19,160,2,311,8,5496,2,20920,17,25,15097,3,24,24,0,33,1,1857,2,1,1009,4,1109,11739,4762,358,5,25,245,28,1110,3,13,1041,4,24,603,490,2,71477,20098,104447,2,20961,1,2604,4,1,329,3,0] # noqa: E231 # fmt: on # In 1991, the remains of Russian Tsar Nicholas II and his family ( # except for Alexei and Maria ) are discovered. The voice of young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story. # 1883 Western Siberia, a young Grigori Rasputin is asked by his father # and a group of men to perform magic. Rasputin has a vision and # denounces one of the men as a horse thief. Although his father initially # slaps him for making such an accusation, Rasputin watches as the man # is chased outside and beaten. Twenty years later, Rasputin sees a vision # of the Virgin Mary, prompting him to become a priest. # Rasputin quickly becomes famous, with people, even a bishop, begging for # his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar # Nicholas II and his family were discovered. The voice of <unk> young son, # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos> _lowerCAmelCase : Tuple = model.generate(a__ , max_length=200 , do_sample=a__ ) self.assertListEqual(output_ids[0].numpy().tolist() , a__ )
663
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
"""simple docstring""" import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( 'The `inpainting.py` script is outdated. Please use directly `from diffusers import' ' StableDiffusionInpaintPipeline` instead.' )
663
1
"""simple docstring""" import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _a : int = 16 _a : int = 32 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Accelerator ,_lowerCamelCase : int = 16 ) -> Tuple: _lowerCAmelCase : List[Any] = AutoTokenizer.from_pretrained("""bert-base-cased""" ) _lowerCAmelCase : Union[str, Any] = load_dataset("""glue""" ,"""mrpc""" ) def tokenize_function(_lowerCamelCase : Any ): # max_length=None => use the model max length (it's actually the default) _lowerCAmelCase : Tuple = tokenizer(examples["""sentence1"""] ,examples["""sentence2"""] ,truncation=_lowerCamelCase ,max_length=_lowerCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): _lowerCAmelCase : Optional[int] = datasets.map( _lowerCamelCase ,batched=_lowerCamelCase ,remove_columns=["""idx""", """sentence1""", """sentence2"""] ,) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _lowerCAmelCase : Dict = tokenized_datasets.rename_column("""label""" ,"""labels""" ) def collate_fn(_lowerCamelCase : Optional[int] ): # On TPU it's best to pad everything to the same length or training will be very slow. _lowerCAmelCase : Union[str, Any] = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": _lowerCAmelCase : Optional[int] = 16 elif accelerator.mixed_precision != "no": _lowerCAmelCase : int = 8 else: _lowerCAmelCase : Tuple = None return tokenizer.pad( _lowerCamelCase ,padding="""longest""" ,max_length=_lowerCamelCase ,pad_to_multiple_of=_lowerCamelCase ,return_tensors="""pt""" ,) # Instantiate dataloaders. _lowerCAmelCase : Union[str, Any] = DataLoader( tokenized_datasets["""train"""] ,shuffle=_lowerCamelCase ,collate_fn=_lowerCamelCase ,batch_size=_lowerCamelCase ) _lowerCAmelCase : Any = DataLoader( tokenized_datasets["""validation"""] ,shuffle=_lowerCamelCase ,collate_fn=_lowerCamelCase ,batch_size=_lowerCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _a : Optional[Any] = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE ( _lowerCamelCase : int ,_lowerCamelCase : Any ) -> List[Any]: # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""" ,_lowerCamelCase ) == "1": _lowerCAmelCase : List[Any] = 2 # Initialize accelerator _lowerCAmelCase : Tuple = Accelerator(cpu=args.cpu ,mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _lowerCAmelCase : List[str] = config["""lr"""] _lowerCAmelCase : Optional[int] = int(config["""num_epochs"""] ) _lowerCAmelCase : str = int(config["""seed"""] ) _lowerCAmelCase : List[Any] = int(config["""batch_size"""] ) _lowerCAmelCase : Tuple = evaluate.load("""glue""" ,"""mrpc""" ) # If the batch size is too big we use gradient accumulation _lowerCAmelCase : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: _lowerCAmelCase : Optional[int] = batch_size // MAX_GPU_BATCH_SIZE _lowerCAmelCase : Union[str, Any] = MAX_GPU_BATCH_SIZE set_seed(_lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = get_dataloaders(_lowerCamelCase ,_lowerCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _lowerCAmelCase : List[Any] = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""" ,return_dict=_lowerCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). _lowerCAmelCase : str = model.to(accelerator.device ) # Instantiate optimizer _lowerCAmelCase : Tuple = AdamW(params=model.parameters() ,lr=_lowerCamelCase ) # Instantiate scheduler _lowerCAmelCase : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=_lowerCamelCase ,num_warmup_steps=100 ,num_training_steps=(len(_lowerCamelCase ) * num_epochs) // gradient_accumulation_steps ,) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Tuple = accelerator.prepare( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # Now we train the model for epoch in range(_lowerCamelCase ): model.train() for step, batch in enumerate(_lowerCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) _lowerCAmelCase : Dict = model(**_lowerCamelCase ) _lowerCAmelCase : List[str] = outputs.loss _lowerCAmelCase : Tuple = loss / gradient_accumulation_steps accelerator.backward(_lowerCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() _lowerCAmelCase : Optional[Any] = 0 for step, batch in enumerate(_lowerCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _lowerCAmelCase : Optional[Any] = model(**_lowerCamelCase ) _lowerCAmelCase : int = outputs.logits.argmax(dim=-1 ) _lowerCAmelCase , _lowerCAmelCase : List[str] = accelerator.gather((predictions, batch["""labels"""]) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(_lowerCamelCase ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples _lowerCAmelCase : List[Any] = predictions[: len(eval_dataloader.dataset ) - samples_seen] _lowerCAmelCase : List[str] = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=_lowerCamelCase ,references=_lowerCamelCase ,) _lowerCAmelCase : Tuple = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" ,_lowerCamelCase ) def SCREAMING_SNAKE_CASE ( ) -> str: _lowerCAmelCase : List[str] = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""" ,type=_lowerCamelCase ,default=_lowerCamelCase ,choices=["""no""", """fp16""", """bf16""", """fp8"""] ,help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""" ,) parser.add_argument("""--cpu""" ,action="""store_true""" ,help="""If passed, will train on the CPU.""" ) _lowerCAmelCase : Any = parser.parse_args() _lowerCAmelCase : Dict = {"""lr""": 2e-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(_lowerCamelCase ,_lowerCamelCase ) if __name__ == "__main__": main()
663
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available _a : Union[str, Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a : Tuple = ['GPTSw3Tokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys _a : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
663
1