code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import os from typing import Dict, List, Union import tensorflow as tf from keras_nlp.tokenizers import BytePairTokenizer from tensorflow_text import pad_model_inputs from .tokenization_gpta import GPTaTokenizer class lowerCAmelCase_ ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Dict[str, int] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : int = None ) -> Union[str, Any]: '''simple docstring''' super().__init__() A: List[str] = pad_token_id A: int = max_length A: Tuple = vocab A: List[str] = merges A: Dict = BytePairTokenizer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , sequence_length=SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : Dict , SCREAMING_SNAKE_CASE_ : GPTaTokenizer , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[str]: '''simple docstring''' A: List[str] = [''' '''.join(SCREAMING_SNAKE_CASE_ ) for m in tokenizer.bpe_ranks.keys()] A: List[Any] = tokenizer.get_vocab() return cls(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, os.PathLike] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' A: str = GPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) return cls.from_tokenizer(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : int , SCREAMING_SNAKE_CASE_ : Tuple ) -> List[Any]: '''simple docstring''' return cls(**SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> List[Any]: '''simple docstring''' return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : int = None ) -> List[Any]: '''simple docstring''' A: int = self.tf_tokenizer(SCREAMING_SNAKE_CASE_ ) A: Dict = tf.ones_like(SCREAMING_SNAKE_CASE_ ) if self.pad_token_id is not None: # pad the tokens up to max length A: Any = max_length if max_length is not None else self.max_length if max_length is not None: A , A: str = pad_model_inputs( SCREAMING_SNAKE_CASE_ , max_seq_length=SCREAMING_SNAKE_CASE_ , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
334
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
1
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int=13 , SCREAMING_SNAKE_CASE_ : Any=7 , SCREAMING_SNAKE_CASE_ : int=True , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : str=False , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=99 , SCREAMING_SNAKE_CASE_ : List[str]=32 , SCREAMING_SNAKE_CASE_ : Optional[Any]=5 , SCREAMING_SNAKE_CASE_ : Dict=4 , SCREAMING_SNAKE_CASE_ : List[Any]=37 , SCREAMING_SNAKE_CASE_ : str="gelu" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE_ : Dict=0.1 , SCREAMING_SNAKE_CASE_ : List[Any]=5_12 , SCREAMING_SNAKE_CASE_ : Dict=16 , SCREAMING_SNAKE_CASE_ : Tuple=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : Optional[int]=3 , SCREAMING_SNAKE_CASE_ : Dict=4 , SCREAMING_SNAKE_CASE_ : List[Any]=None , ) -> Dict: '''simple docstring''' A: Optional[Any] = parent A: Union[str, Any] = batch_size A: Optional[Any] = seq_length A: Any = is_training A: Dict = use_input_mask A: int = use_token_type_ids A: List[Any] = use_labels A: Optional[int] = vocab_size A: Optional[int] = hidden_size A: Optional[int] = num_hidden_layers A: Optional[Any] = num_attention_heads A: Dict = intermediate_size A: Dict = hidden_act A: Optional[Any] = hidden_dropout_prob A: List[Any] = attention_probs_dropout_prob A: Optional[Any] = max_position_embeddings A: Optional[Any] = type_vocab_size A: int = type_sequence_label_size A: List[str] = initializer_range A: Optional[int] = num_labels A: int = num_choices A: Optional[int] = scope def _snake_case ( self : Dict ) -> str: '''simple docstring''' A: Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A: Union[str, Any] = None if self.use_input_mask: A: Dict = random_attention_mask([self.batch_size, self.seq_length] ) A: Dict = None if self.use_token_type_ids: A: Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A: Union[str, Any] = None A: Tuple = None A: int = None if self.use_labels: A: Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A: int = ids_tensor([self.batch_size] , self.num_choices ) A: Optional[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _snake_case ( self : List[str] ) -> Optional[int]: '''simple docstring''' return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , use_stable_embedding=SCREAMING_SNAKE_CASE_ , ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict ) -> List[str]: '''simple docstring''' A: int = OpenLlamaModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: List[Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) A: Tuple = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' A: int = True A: int = OpenLlamaModel(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: Tuple = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=SCREAMING_SNAKE_CASE_ , ) A: Any = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , ) A: Dict = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] , ) -> Any: '''simple docstring''' A: int = OpenLlamaForCausalLM(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: Optional[int] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict , ) -> List[Any]: '''simple docstring''' A: Optional[int] = True A: Any = True A: str = OpenLlamaForCausalLM(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() # first forward pass A: Optional[Any] = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ , ) A: Optional[Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids A: int = ids_tensor((self.batch_size, 3) , config.vocab_size ) A: str = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and A: Tuple = torch.cat([input_ids, next_tokens] , dim=-1 ) A: str = torch.cat([input_mask, next_mask] , dim=-1 ) A: Union[str, Any] = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , )['''hidden_states'''][0] A: Dict = model( SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ , encoder_attention_mask=SCREAMING_SNAKE_CASE_ , past_key_values=SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , )['''hidden_states'''][0] # select random slice A: Optional[Any] = ids_tensor((1,) , output_from_past.shape[-1] ).item() A: Dict = output_from_no_past[:, -3:, random_slice_idx].detach() A: Optional[Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) ) def _snake_case ( self : int ) -> Any: '''simple docstring''' A: List[str] = self.prepare_config_and_inputs() ( ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ): List[Any] = config_and_inputs A: str = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Dict = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) UpperCamelCase_ : Any = (OpenLlamaForCausalLM,) if is_torch_available() else () UpperCamelCase_ : int = ( { """feature-extraction""": OpenLlamaModel, """text-classification""": OpenLlamaForSequenceClassification, """text-generation""": OpenLlamaForCausalLM, """zero-shot""": OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase_ : Union[str, Any] = False UpperCamelCase_ : str = False def _snake_case ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Tuple = OpenLlamaModelTester(self ) A: int = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def _snake_case ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' self.config_tester.run_common_tests() def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str ) -> int: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: A: int = type self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A , A: Any = self.model_tester.prepare_config_and_inputs_for_common() A: Union[str, Any] = 3 A: Optional[Any] = input_dict['''input_ids'''] A: List[Any] = input_ids.ne(1 ).to(SCREAMING_SNAKE_CASE_ ) A: Dict = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A: Union[str, Any] = OpenLlamaForSequenceClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: List[str] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' A , A: List[Any] = self.model_tester.prepare_config_and_inputs_for_common() A: Tuple = 3 A: Optional[int] = '''single_label_classification''' A: Optional[Any] = input_dict['''input_ids'''] A: str = input_ids.ne(1 ).to(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A: Optional[int] = OpenLlamaForSequenceClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: List[Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' A , A: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() A: Any = 3 A: Tuple = '''multi_label_classification''' A: Dict = input_dict['''input_ids'''] A: Any = input_ids.ne(1 ).to(SCREAMING_SNAKE_CASE_ ) A: List[Any] = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) A: int = OpenLlamaForSequenceClassification(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() A: List[Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple ) -> Dict: '''simple docstring''' A , A: Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() A: Any = ids_tensor([1, 10] , config.vocab_size ) A: List[Any] = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A: Any = OpenLlamaModel(SCREAMING_SNAKE_CASE_ ) original_model.to(SCREAMING_SNAKE_CASE_ ) original_model.eval() A: int = original_model(SCREAMING_SNAKE_CASE_ ).last_hidden_state A: Any = original_model(SCREAMING_SNAKE_CASE_ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights A: Optional[Any] = {'''type''': scaling_type, '''factor''': 10.0} A: Tuple = OpenLlamaModel(SCREAMING_SNAKE_CASE_ ) scaled_model.to(SCREAMING_SNAKE_CASE_ ) scaled_model.eval() A: Optional[Any] = scaled_model(SCREAMING_SNAKE_CASE_ ).last_hidden_state A: Dict = scaled_model(SCREAMING_SNAKE_CASE_ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-5 ) )
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = len(__lowercase ) for i in range(length - 1 ): A: Dict = i for k in range(i + 1 , __lowercase ): if collection[k] < collection[least]: A: List[str] = k if least != i: A , A: Tuple = (collection[i], collection[least]) return collection if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
334
1
'''simple docstring''' import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' super().tearDown() gc.collect() def _snake_case ( self : Tuple ) -> Tuple: '''simple docstring''' A: List[str] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) A: Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) A: Optional[Any] = '''xvjiarui/stable-diffusion-2-inpainting''' A , A: Union[str, Any] = FlaxStableDiffusionInpaintPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ , safety_checker=SCREAMING_SNAKE_CASE_ ) A: Any = '''Face of a yellow cat, high resolution, sitting on a park bench''' A: str = jax.random.PRNGKey(0 ) A: Dict = 50 A: int = jax.device_count() A: Optional[Any] = num_samples * [prompt] A: Any = num_samples * [init_image] A: Union[str, Any] = num_samples * [mask_image] A , A , A: List[str] = pipeline.prepare_inputs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # shard inputs and rng A: Dict = replicate(SCREAMING_SNAKE_CASE_ ) A: Tuple = jax.random.split(SCREAMING_SNAKE_CASE_ , jax.device_count() ) A: Tuple = shard(SCREAMING_SNAKE_CASE_ ) A: Any = shard(SCREAMING_SNAKE_CASE_ ) A: Tuple = shard(SCREAMING_SNAKE_CASE_ ) A: str = pipeline( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , jit=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = output.images.reshape(SCREAMING_SNAKE_CASE_ , 5_12 , 5_12 , 3 ) A: Union[str, Any] = images[0, 2_53:2_56, 2_53:2_56, -1] A: str = jnp.asarray(jax.device_get(image_slice.flatten() ) ) A: Union[str, Any] = jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(f"""output_slice: {output_slice}""" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
334
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCamelCase = { '''configuration_convnext''': ['''CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvNextConfig''', '''ConvNextOnnxConfig'''] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''ConvNextFeatureExtractor'''] UpperCamelCase = ['''ConvNextImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvNextForImageClassification''', '''ConvNextModel''', '''ConvNextPreTrainedModel''', '''ConvNextBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TFConvNextForImageClassification''', '''TFConvNextModel''', '''TFConvNextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
334
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
1
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) UpperCamelCase = logging.getLogger(__name__) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : str = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase_ : bool = field(default=UpperCAmelCase_ , metadata={"""help""": """Whether tp freeze the encoder."""} ) UpperCamelCase_ : bool = field(default=UpperCAmelCase_ , metadata={"""help""": """Whether to freeze the embeddings."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : str = field( metadata={"""help""": """The input data dir. Should contain the .tsv files (or other data files) for the task."""} ) UpperCamelCase_ : Optional[str] = field( default="""summarization""" , metadata={"""help""": """Task name, summarization (or summarization_{dataset} for pegasus) or translation"""} , ) UpperCamelCase_ : Optional[int] = field( default=1024 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=128 , metadata={ """help""": ( """The maximum total sequence length for target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=142 , metadata={ """help""": ( """The maximum total sequence length for validation target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded. """ """This argument is also used to override the ``max_length`` param of ``model.generate``, which is used """ """during ``evaluate`` and ``predict``.""" ) } , ) UpperCamelCase_ : Optional[int] = field( default=142 , metadata={ """help""": ( """The maximum total sequence length for test target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase_ : Optional[int] = field(default=-1 , metadata={"""help""": """# training examples. -1 means use all."""} ) UpperCamelCase_ : Optional[int] = field(default=-1 , metadata={"""help""": """# validation examples. -1 means use all."""} ) UpperCamelCase_ : Optional[int] = field(default=-1 , metadata={"""help""": """# test examples. -1 means use all."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """Source language id for translation."""} ) UpperCamelCase_ : Optional[str] = field(default=UpperCAmelCase_ , metadata={"""help""": """Target language id for translation."""} ) UpperCamelCase_ : Optional[int] = field(default=UpperCAmelCase_ , metadata={"""help""": """# num_beams to use for evaluation."""} ) UpperCamelCase_ : bool = field( default=UpperCAmelCase_ , metadata={"""help""": """If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."""} , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Union[str, Any]: logger.info(F"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(F""" {key} = {metrics[key]}""" ) save_json(__lowercase , os.path.join(__lowercase , F"""{split}_results.json""" ) ) def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A: Tuple = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A , A , A: int = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A , A , A: Optional[int] = parser.parse_args_into_dataclasses() check_output_dir(__lowercase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info('''Training/evaluation parameters %s''' , __lowercase ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A: List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A: Tuple = ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''') for p in extra_model_params: if getattr(__lowercase , __lowercase , __lowercase ): assert hasattr(__lowercase , __lowercase ), F"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(__lowercase , __lowercase , getattr(__lowercase , __lowercase ) ) A: List[str] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) A: List[Any] = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf='''.ckpt''' in model_args.model_name_or_path , config=__lowercase , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(__lowercase , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: A: Tuple = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(__lowercase , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(__lowercase , __lowercase ): A: Any = tokenizer.lang_code_to_id[data_args.tgt_lang] else: A: List[Any] = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(__lowercase ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) A: Optional[int] = SeqaSeqDataset # Get datasets A: Any = ( dataset_class( __lowercase , type_path='''train''' , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_train else None ) A: Optional[Any] = ( dataset_class( __lowercase , type_path='''val''' , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) A: Any = ( dataset_class( __lowercase , type_path='''test''' , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or '''''' , ) if training_args.do_predict else None ) # Initialize our Trainer A: List[Any] = ( build_compute_metrics_fn(data_args.task , __lowercase ) if training_args.predict_with_generate else None ) A: int = SeqaSeqTrainer( model=__lowercase , args=__lowercase , data_args=__lowercase , train_dataset=__lowercase , eval_dataset=__lowercase , data_collator=SeqaSeqDataCollator( __lowercase , __lowercase , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=__lowercase , tokenizer=__lowercase , ) A: List[Any] = {} # Training if training_args.do_train: logger.info('''*** Train ***''' ) A: List[str] = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) A: Dict = train_result.metrics A: Tuple = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics('''train''' , __lowercase , training_args.output_dir ) all_metrics.update(__lowercase ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , '''trainer_state.json''' ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) A: int = trainer.evaluate(metric_key_prefix='''val''' ) A: Union[str, Any] = data_args.n_val A: Optional[Any] = round(metrics['''val_loss'''] , 4 ) if trainer.is_world_process_zero(): handle_metrics('''val''' , __lowercase , training_args.output_dir ) all_metrics.update(__lowercase ) if training_args.do_predict: logger.info('''*** Predict ***''' ) A: Union[str, Any] = trainer.predict(test_dataset=__lowercase , metric_key_prefix='''test''' ) A: Union[str, Any] = test_output.metrics A: List[Any] = data_args.n_test if trainer.is_world_process_zero(): A: Tuple = round(metrics['''test_loss'''] , 4 ) handle_metrics('''test''' , __lowercase , training_args.output_dir ) all_metrics.update(__lowercase ) if training_args.predict_with_generate: A: Optional[Any] = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=__lowercase , clean_up_tokenization_spaces=__lowercase ) A: Optional[Any] = lmap(str.strip , __lowercase ) write_txt_file(__lowercase , os.path.join(training_args.output_dir , '''test_generations.txt''' ) ) if trainer.is_world_process_zero(): save_json(__lowercase , os.path.join(training_args.output_dir , '''all_results.json''' ) ) return all_metrics def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
334
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = DebertaTokenizer UpperCamelCase_ : List[str] = True UpperCamelCase_ : int = DebertaTokenizerFast def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A: Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] A: int = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Union[str, Any] = {'''unk_token''': '''[UNK]'''} A: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: str = 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(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = '''lower newer''' A: str = '''lower newer''' return input_text, output_text def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: str = self.get_tokenizer() A: Any = '''lower newer''' A: Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] A: int = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokens + [tokenizer.unk_token] A: int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' A: str = self.get_tokenizer() A: List[str] = tokenizer('''Hello''' , '''World''' ) A: Union[str, Any] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Any = tokenizer.encode('''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: int = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: A: List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Dict = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: Any = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for seq in encoding['''input_ids''']] # fmt: off A: Any = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on A: Optional[int] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , SCREAMING_SNAKE_CASE_ ) for expected, decoded in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
1
'''simple docstring''' from typing import Optional, Tuple, Union import torch from einops import rearrange, reduce from diffusers import DDIMScheduler, DDPMScheduler, DiffusionPipeline, ImagePipelineOutput, UNetaDConditionModel from diffusers.schedulers.scheduling_ddim import DDIMSchedulerOutput from diffusers.schedulers.scheduling_ddpm import DDPMSchedulerOutput UpperCamelCase = 8 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=BITS ) -> List[Any]: A: Dict = x.device A: Any = (x * 2_5_5).int().clamp(0 , 2_5_5 ) A: Any = 2 ** torch.arange(bits - 1 , -1 , -1 , device=__lowercase ) A: Optional[int] = rearrange(__lowercase , '''d -> d 1 1''' ) A: str = rearrange(__lowercase , '''b c h w -> b c 1 h w''' ) A: int = ((x & mask) != 0).float() A: Any = rearrange(__lowercase , '''b c d h w -> b (c d) h w''' ) A: Optional[Any] = bits * 2 - 1 return bits def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=BITS ) -> Union[str, Any]: A: Dict = x.device A: Tuple = (x > 0).int() A: int = 2 ** torch.arange(bits - 1 , -1 , -1 , device=__lowercase , dtype=torch.intaa ) A: Any = rearrange(__lowercase , '''d -> d 1 1''' ) A: List[str] = rearrange(__lowercase , '''b (c d) h w -> b c d h w''' , d=8 ) A: Union[str, Any] = reduce(x * mask , '''b c d h w -> b c h w''' , '''sum''' ) return (dec / 2_5_5).clamp(0.0 , 1.0 ) def SCREAMING_SNAKE_CASE( self , __lowercase , __lowercase , __lowercase , __lowercase = 0.0 , __lowercase = True , __lowercase=None , __lowercase = True , ) -> Union[DDIMSchedulerOutput, Tuple]: if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf # Ideally, read DDIM paper in-detail understanding # Notation (<variable name> -> <name in paper> # - pred_noise_t -> e_theta(x_t, t) # - pred_original_sample -> f_theta(x_t, t) or x_0 # - std_dev_t -> sigma_t # - eta -> η # - pred_sample_direction -> "direction pointing to x_t" # - pred_prev_sample -> "x_t-1" # 1. get previous step value (=t-1) A: List[str] = timestep - self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas A: List[Any] = self.alphas_cumprod[timestep] A: List[str] = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod A: str = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A: str = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 # 4. Clip "predicted x_0" A: str = self.bit_scale if self.config.clip_sample: A: Optional[int] = torch.clamp(__lowercase , -scale , __lowercase ) # 5. compute variance: "sigma_t(η)" -> see formula (16) # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1) A: int = self._get_variance(__lowercase , __lowercase ) A: Optional[Any] = eta * variance ** 0.5 if use_clipped_model_output: # the model_output is always re-derived from the clipped x_0 in Glide A: Optional[int] = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A: List[str] = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * model_output # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A: List[Any] = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if eta > 0: # randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072 A: int = model_output.device if torch.is_tensor(__lowercase ) else '''cpu''' A: int = torch.randn(model_output.shape , dtype=model_output.dtype , generator=__lowercase ).to(__lowercase ) A: Optional[Any] = self._get_variance(__lowercase , __lowercase ) ** 0.5 * eta * noise A: Any = prev_sample + variance if not return_dict: return (prev_sample,) return DDIMSchedulerOutput(prev_sample=__lowercase , pred_original_sample=__lowercase ) def SCREAMING_SNAKE_CASE( self , __lowercase , __lowercase , __lowercase , __lowercase="epsilon" , __lowercase=None , __lowercase = True , ) -> Union[DDPMSchedulerOutput, Tuple]: A: str = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]: A , A: str = torch.split(__lowercase , sample.shape[1] , dim=1 ) else: A: Union[str, Any] = None # 1. compute alphas, betas A: Union[str, Any] = self.alphas_cumprod[t] A: Tuple = self.alphas_cumprod[t - 1] if t > 0 else self.one A: int = 1 - alpha_prod_t A: Optional[int] = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if prediction_type == "epsilon": A: List[str] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif prediction_type == "sample": A: List[Any] = model_output else: raise ValueError(F"""Unsupported prediction_type {prediction_type}.""" ) # 3. Clip "predicted x_0" A: List[Any] = self.bit_scale if self.config.clip_sample: A: Optional[int] = torch.clamp(__lowercase , -scale , __lowercase ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf A: Optional[int] = (alpha_prod_t_prev ** 0.5 * self.betas[t]) / beta_prod_t A: Tuple = self.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf A: Union[str, Any] = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise A: Optional[Any] = 0 if t > 0: A: List[str] = torch.randn( model_output.size() , dtype=model_output.dtype , layout=model_output.layout , generator=__lowercase ).to(model_output.device ) A: List[str] = (self._get_variance(__lowercase , predicted_variance=__lowercase ) ** 0.5) * noise A: int = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return DDPMSchedulerOutput(prev_sample=__lowercase , pred_original_sample=__lowercase ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : UNetaDConditionModel , SCREAMING_SNAKE_CASE_ : Union[DDIMScheduler, DDPMScheduler] , SCREAMING_SNAKE_CASE_ : Optional[float] = 1.0 , ) -> Dict: '''simple docstring''' super().__init__() A: Union[str, Any] = bit_scale A: List[Any] = ( ddim_bit_scheduler_step if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else ddpm_bit_scheduler_step ) self.register_modules(unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def __call__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] = 2_56 , SCREAMING_SNAKE_CASE_ : Optional[int] = 2_56 , SCREAMING_SNAKE_CASE_ : Optional[int] = 50 , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = 1 , SCREAMING_SNAKE_CASE_ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE_ : bool = True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> Union[Tuple, ImagePipelineOutput]: '''simple docstring''' A: Tuple = torch.randn( (batch_size, self.unet.config.in_channels, height, width) , generator=SCREAMING_SNAKE_CASE_ , ) A: List[str] = decimal_to_bits(SCREAMING_SNAKE_CASE_ ) * self.bit_scale A: Any = latents.to(self.device ) self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ) for t in self.progress_bar(self.scheduler.timesteps ): # predict the noise residual A: Dict = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).sample # compute the previous noisy sample x_t -> x_t-1 A: Optional[int] = self.scheduler.step(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).prev_sample A: List[Any] = bits_to_decimal(SCREAMING_SNAKE_CASE_ ) if output_type == "pil": A: List[str] = self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE_ )
334
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
1
'''simple docstring''' # Copyright 2023 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. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase = { '''configuration_vivit''': ['''VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VivitConfig'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VivitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''VivitModel''', '''VivitPreTrainedModel''', '''VivitForVideoClassification''', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
1
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import GLPNImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int=7 , SCREAMING_SNAKE_CASE_ : Any=3 , SCREAMING_SNAKE_CASE_ : List[str]=18 , SCREAMING_SNAKE_CASE_ : Tuple=30 , SCREAMING_SNAKE_CASE_ : List[Any]=4_00 , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : int=32 , SCREAMING_SNAKE_CASE_ : Any=True , ) -> List[Any]: '''simple docstring''' A: str = parent A: Any = batch_size A: List[str] = num_channels A: Union[str, Any] = image_size A: Optional[int] = min_resolution A: int = max_resolution A: Dict = do_resize A: List[Any] = size_divisor A: int = do_rescale def _snake_case ( self : Dict ) -> str: '''simple docstring''' return { "do_resize": self.do_resize, "size_divisor": self.size_divisor, "do_rescale": self.do_rescale, } @require_torch @require_vision class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : int = GLPNImageProcessor if is_vision_available() else None def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: str = GLPNImageProcessingTester(self ) @property def _snake_case ( self : Any ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _snake_case ( self : Optional[Any] ) -> Dict: '''simple docstring''' A: Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_resize''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''size_divisor''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''resample''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_rescale''' ) ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' pass def _snake_case ( self : Union[str, Any] ) -> int: '''simple docstring''' A: List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A: Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , Image.Image ) # Test not batched input (GLPNImageProcessor doesn't support batching) A: int = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A: List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , numpify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) # Test not batched input (GLPNImageProcessor doesn't support batching) A: Tuple = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 ) def _snake_case ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A: Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) # Test not batched input (GLPNImageProcessor doesn't support batching) A: Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 ) self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )
334
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
1
'''simple docstring''' import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = FlaxAutoencoderKL @property def _snake_case ( self : Dict ) -> List[str]: '''simple docstring''' A: str = 4 A: Dict = 3 A: Optional[int] = (32, 32) A: Optional[int] = jax.random.PRNGKey(0 ) A: Any = jax.random.uniform(SCREAMING_SNAKE_CASE_ , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def _snake_case ( self : Tuple ) -> str: '''simple docstring''' A: str = { '''block_out_channels''': [32, 64], '''in_channels''': 3, '''out_channels''': 3, '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''], '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''], '''latent_channels''': 4, } A: Optional[Any] = self.dummy_input return init_dict, inputs_dict
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''SCUT-DLVCLab/lilt-roberta-en-base''': ( '''https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json''' ), } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = """lilt""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]=3_05_22 , SCREAMING_SNAKE_CASE_ : Optional[Any]=7_68 , SCREAMING_SNAKE_CASE_ : List[Any]=12 , SCREAMING_SNAKE_CASE_ : Optional[int]=12 , SCREAMING_SNAKE_CASE_ : Dict=30_72 , SCREAMING_SNAKE_CASE_ : str="gelu" , SCREAMING_SNAKE_CASE_ : Dict=0.1 , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : str=5_12 , SCREAMING_SNAKE_CASE_ : int=2 , SCREAMING_SNAKE_CASE_ : Any=0.02 , SCREAMING_SNAKE_CASE_ : List[Any]=1E-12 , SCREAMING_SNAKE_CASE_ : Tuple=0 , SCREAMING_SNAKE_CASE_ : Optional[int]="absolute" , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : Optional[int]=4 , SCREAMING_SNAKE_CASE_ : Optional[int]=10_24 , **SCREAMING_SNAKE_CASE_ : str , ) -> int: '''simple docstring''' super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Tuple = vocab_size A: Dict = hidden_size A: str = num_hidden_layers A: int = num_attention_heads A: List[Any] = hidden_act A: List[str] = intermediate_size A: int = hidden_dropout_prob A: int = attention_probs_dropout_prob A: int = max_position_embeddings A: Dict = type_vocab_size A: List[Any] = initializer_range A: List[Any] = layer_norm_eps A: Any = position_embedding_type A: Dict = classifier_dropout A: Any = channel_shrink_ratio A: Optional[Any] = max_ad_position_embeddings
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for data in source_data: for i, el in enumerate(__lowercase ): if len(__lowercase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__lowercase ) ) return data_lists def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for dlist, weight in zip(__lowercase , __lowercase ): A: List[str] = min(__lowercase ) A: Union[str, Any] = max(__lowercase ) A: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A: List[str] = F"""Invalid weight of {weight:f} provided""" raise ValueError(__lowercase ) score_lists.append(__lowercase ) return score_lists def SCREAMING_SNAKE_CASE( __lowercase ) -> list[float]: A: list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__lowercase ): A: str = final_scores[j] + ele return final_scores def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: Any = get_data(__lowercase ) A: str = calculate_each_score(__lowercase , __lowercase ) A: int = generate_final_scores(__lowercase ) # append scores to source data for i, ele in enumerate(__lowercase ): source_data[i].append(__lowercase ) return source_data
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: Optional[int] = 0 A: Dict = len(__lowercase ) for i in range(n - 1 ): for j in range(i + 1 , __lowercase ): if arr[i] > arr[j]: num_inversions += 1 return num_inversions def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if len(__lowercase ) <= 1: return arr, 0 A: List[Any] = len(__lowercase ) // 2 A: Optional[Any] = arr[0:mid] A: Optional[Any] = arr[mid:] A , A: Union[str, Any] = count_inversions_recursive(__lowercase ) A , A: Any = count_inversions_recursive(__lowercase ) A , A: int = _count_cross_inversions(__lowercase , __lowercase ) A: Union[str, Any] = inversion_p + inversions_q + cross_inversions return c, num_inversions def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[int]: A: List[str] = [] A: Dict = 0 while i < len(__lowercase ) and j < len(__lowercase ): if p[i] > q[j]: # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) # These are all inversions. The claim emerges from the # property that P is sorted. num_inversion += len(__lowercase ) - i r.append(q[j] ) j += 1 else: r.append(p[i] ) i += 1 if i < len(__lowercase ): r.extend(p[i:] ) else: r.extend(q[j:] ) return r, num_inversion def SCREAMING_SNAKE_CASE( ) -> Tuple: A: Optional[int] = [1_0, 2, 1, 5, 5, 2, 1_1] # this arr has 8 inversions: # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) A: List[Any] = count_inversions_bf(__lowercase ) A , A: Union[str, Any] = count_inversions_recursive(__lowercase ) assert num_inversions_bf == num_inversions_recursive == 8 print('''number of inversions = ''' , __lowercase ) # testing an array with zero inversion (a sorted arr_1) arr_a.sort() A: Optional[int] = count_inversions_bf(__lowercase ) A , A: Dict = count_inversions_recursive(__lowercase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __lowercase ) # an empty list should also have zero inversions A: List[Any] = [] A: List[str] = count_inversions_bf(__lowercase ) A , A: List[Any] = count_inversions_recursive(__lowercase ) assert num_inversions_bf == num_inversions_recursive == 0 print('''number of inversions = ''' , __lowercase ) if __name__ == "__main__": main()
334
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
1
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 6_55_36 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : str = "fourier" , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , SCREAMING_SNAKE_CASE_ : Tuple[str] = "UNetMidBlock1D" , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : Tuple[int] = (32, 32, 64) , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[Any] = sample_size # time if time_embedding_type == "fourier": A: Tuple = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=SCREAMING_SNAKE_CASE_ , log=SCREAMING_SNAKE_CASE_ , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ ) A: List[str] = 2 * block_out_channels[0] elif time_embedding_type == "positional": A: str = Timesteps( block_out_channels[0] , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ , downscale_freq_shift=SCREAMING_SNAKE_CASE_ ) A: Any = block_out_channels[0] if use_timestep_embedding: A: Optional[Any] = block_out_channels[0] * 4 A: List[Any] = TimestepEmbedding( in_channels=SCREAMING_SNAKE_CASE_ , time_embed_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , out_dim=block_out_channels[0] , ) A: Optional[Any] = nn.ModuleList([] ) A: str = None A: str = nn.ModuleList([] ) A: Tuple = None # down A: Any = in_channels for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = output_channel A: List[Any] = block_out_channels[i] if i == 0: input_channel += extra_in_channels A: List[Any] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[int] = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid A: Union[str, Any] = get_mid_block( SCREAMING_SNAKE_CASE_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=SCREAMING_SNAKE_CASE_ , add_downsample=SCREAMING_SNAKE_CASE_ , ) # up A: Optional[Any] = list(reversed(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = reversed_block_out_channels[0] if out_block_type is None: A: int = out_channels else: A: Union[str, Any] = block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: List[Any] = output_channel A: int = ( reversed_block_out_channels[i + 1] if i < len(SCREAMING_SNAKE_CASE_ ) - 1 else final_upsample_channels ) A: Optional[int] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[Any] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) A: Any = output_channel # out A: List[str] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) A: Optional[int] = get_out_block( out_block_type=SCREAMING_SNAKE_CASE_ , num_groups_out=SCREAMING_SNAKE_CASE_ , embed_dim=block_out_channels[0] , out_channels=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , fc_dim=block_out_channels[-1] // 4 , ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[UNetaDOutput, Tuple]: '''simple docstring''' A: Any = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: List[str] = timesteps[None].to(sample.device ) A: int = self.time_proj(SCREAMING_SNAKE_CASE_ ) if self.config.use_timestep_embedding: A: List[Any] = self.time_mlp(SCREAMING_SNAKE_CASE_ ) else: A: str = timestep_embed[..., None] A: Union[str, Any] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) A: Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down A: List[str] = () for downsample_block in self.down_blocks: A , A: Optional[int] = downsample_block(hidden_states=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: A: Dict = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): A: List[Any] = down_block_res_samples[-1:] A: List[str] = down_block_res_samples[:-1] A: Optional[int] = upsample_block(SCREAMING_SNAKE_CASE_ , res_hidden_states_tuple=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) # 5. post-process if self.out_block: A: Any = self.out_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=SCREAMING_SNAKE_CASE_ )
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''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 UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list: # bit count represents no. of bits in the gray code if bit_count < 0: raise ValueError('''The given input must be positive''' ) # get the generated string sequence A: Optional[int] = gray_code_sequence_string(__lowercase ) # # convert them to integers for i in range(len(__lowercase ) ): A: Dict = int(sequence[i] , 2 ) return sequence def SCREAMING_SNAKE_CASE( __lowercase ) -> list: # The approach is a recursive one # Base case achieved when either n = 0 or n=1 if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] A: int = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits A: Union[str, Any] = gray_code_sequence_string(bit_count - 1 ) A: Dict = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): A: List[str] = '''0''' + smaller_sequence[i] sequence.append(__lowercase ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): A: List[Any] = '''1''' + smaller_sequence[i] sequence.append(__lowercase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
334
'''simple docstring''' from __future__ import annotations from typing import Any class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' pass class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> None: '''simple docstring''' A: Any = data A: Node | None = None def __iter__( self : Optional[int] ) -> List[str]: '''simple docstring''' A: List[str] = self A: Dict = [] while node: if node in visited: raise ContainsLoopError visited.append(SCREAMING_SNAKE_CASE_ ) yield node.data A: str = node.next_node @property def _snake_case ( self : List[str] ) -> bool: '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCamelCase = Node(1) UpperCamelCase = Node(2) UpperCamelCase = Node(3) UpperCamelCase = Node(4) print(root_node.has_loop) # False UpperCamelCase = root_node.next_node print(root_node.has_loop) # True UpperCamelCase = Node(5) UpperCamelCase = Node(6) UpperCamelCase = Node(5) UpperCamelCase = Node(6) print(root_node.has_loop) # False UpperCamelCase = Node(1) print(root_node.has_loop) # False
334
1
'''simple docstring''' import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: A: Tuple = SwinConfig(image_size=1_9_2 ) if "base" in model_name: A: List[str] = 6 A: List[Any] = 1_2_8 A: List[str] = (2, 2, 1_8, 2) A: Optional[int] = (4, 8, 1_6, 3_2) elif "large" in model_name: A: Optional[int] = 1_2 A: Dict = 1_9_2 A: int = (2, 2, 1_8, 2) A: str = (6, 1_2, 2_4, 4_8) else: raise ValueError('''Model not supported, only supports base and large variants''' ) A: str = window_size A: str = embed_dim A: str = depths A: Optional[Any] = num_heads return config def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if "encoder.mask_token" in name: A: Any = name.replace('''encoder.mask_token''' , '''embeddings.mask_token''' ) if "encoder.patch_embed.proj" in name: A: Optional[int] = name.replace('''encoder.patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "encoder.patch_embed.norm" in name: A: Optional[int] = name.replace('''encoder.patch_embed.norm''' , '''embeddings.norm''' ) if "attn.proj" in name: A: List[str] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: A: Any = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: A: List[Any] = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: A: Any = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: A: Dict = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: A: List[str] = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": A: str = '''layernorm.weight''' if name == "encoder.norm.bias": A: Any = '''layernorm.bias''' if "decoder" in name: pass else: A: Any = '''swin.''' + name return name def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in orig_state_dict.copy().keys(): A: Union[str, Any] = orig_state_dict.pop(__lowercase ) if "attn_mask" in key: pass elif "qkv" in key: A: int = key.split('''.''' ) A: Any = int(key_split[2] ) A: Union[str, Any] = int(key_split[4] ) A: Union[str, Any] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: A: Tuple = val[:dim, :] A: Any = val[ dim : dim * 2, : ] A: Union[str, Any] = val[-dim:, :] else: A: str = val[ :dim ] A: Optional[Any] = val[ dim : dim * 2 ] A: str = val[ -dim: ] else: A: str = val return orig_state_dict def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Dict = torch.load(__lowercase , map_location='''cpu''' )['''model'''] A: Union[str, Any] = get_swin_config(__lowercase ) A: List[Any] = SwinForMaskedImageModeling(__lowercase ) model.eval() A: str = convert_state_dict(__lowercase , __lowercase ) model.load_state_dict(__lowercase ) A: Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: List[Any] = ViTImageProcessor(size={'''height''': 1_9_2, '''width''': 1_9_2} ) A: Dict = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) A: int = image_processor(images=__lowercase , return_tensors='''pt''' ) with torch.no_grad(): A: Optional[int] = model(**__lowercase ).logits print(outputs.keys() ) 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(__lowercase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowercase ) if push_to_hub: print(F"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(F"""microsoft/{model_name}""" ) image_processor.push_to_hub(F"""microsoft/{model_name}""" ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''swin-base-simmim-window6-192''', type=str, choices=['''swin-base-simmim-window6-192''', '''swin-large-simmim-window12-192'''], help='''Name of the Swin SimMIM model you\'d like to convert.''', ) parser.add_argument( '''--checkpoint_path''', default='''/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth''', type=str, help='''Path to the original PyTorch checkpoint (.pth file).''', ) 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.''' ) UpperCamelCase = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
334
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase = 4 ) -> list[list[int]]: A: Tuple = abs(__lowercase ) or 4 return [[1 + x + y * row_size for x in range(__lowercase )] for y in range(__lowercase )] def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(transpose(__lowercase ) ) # OR.. transpose(reverse_column(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(reverse_column(__lowercase ) ) # OR.. reverse_column(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_column(transpose(__lowercase ) ) # OR.. transpose(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Union[str, Any] = [list(__lowercase ) for x in zip(*__lowercase )] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[int] = matrix[::-1] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[Any] = [x[::-1] for x in matrix] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> None: for i in matrix: print(*__lowercase ) if __name__ == "__main__": UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
334
1
'''simple docstring''' import os import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers.models.realm.configuration_realm import RealmConfig from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' A: int = tempfile.mkdtemp() A: Any = 5 # Realm tok A: Tuple = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''test''', '''question''', '''this''', '''is''', '''the''', '''first''', '''second''', '''third''', '''fourth''', '''fifth''', '''record''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] A: List[Any] = os.path.join(self.tmpdirname , '''realm_tokenizer''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = os.path.join(SCREAMING_SNAKE_CASE_ , 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] ) ) A: str = os.path.join(self.tmpdirname , '''realm_block_records''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> RealmTokenizer: '''simple docstring''' return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''realm_tokenizer''' ) ) def _snake_case ( self : Dict ) -> List[str]: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def _snake_case ( self : int ) -> Tuple: '''simple docstring''' A: Optional[int] = RealmConfig(num_block_records=self.num_block_records ) return config def _snake_case ( self : Tuple ) -> List[str]: '''simple docstring''' A: Tuple = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''question''': ['''foo''', '''bar'''], '''answers''': [['''Foo''', '''Bar'''], ['''Bar''']], } ) return dataset def _snake_case ( self : Any ) -> Any: '''simple docstring''' A: Tuple = np.array( [ b'''This is the first record''', b'''This is the second record''', b'''This is the third record''', b'''This is the fourth record''', b'''This is the fifth record''', b'''This is a longer longer longer record''', ] , dtype=SCREAMING_SNAKE_CASE_ , ) return block_records def _snake_case ( self : Any ) -> Any: '''simple docstring''' A: Optional[Any] = RealmRetriever( block_records=self.get_dummy_block_records() , tokenizer=self.get_tokenizer() , ) return retriever def _snake_case ( self : List[str] ) -> List[str]: '''simple docstring''' A: Dict = self.get_config() A: Any = self.get_dummy_retriever() A: Union[str, Any] = retriever.tokenizer A: Tuple = np.array([0, 3] , dtype='''long''' ) A: Optional[Any] = tokenizer(['''Test question'''] ).input_ids A: Optional[Any] = tokenizer( ['''the fourth'''] , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_token_type_ids=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , ).input_ids A: Optional[Any] = config.reader_seq_len A , A , A , A: Optional[int] = retriever( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , answer_ids=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , 2 ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , 2 ) self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , 2 ) self.assertEqual(concat_inputs.input_ids.shape , (2, 10) ) self.assertEqual(concat_inputs.attention_mask.shape , (2, 10) ) self.assertEqual(concat_inputs.token_type_ids.shape , (2, 10) ) self.assertEqual(concat_inputs.special_tokens_mask.shape , (2, 10) ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0] ) , ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''first''', '''record''', '''[SEP]'''] , ) self.assertEqual( tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1] ) , ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''fourth''', '''record''', '''[SEP]'''] , ) def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: Tuple = self.get_config() A: int = self.get_dummy_retriever() A: Any = retriever.tokenizer A: Union[str, Any] = np.array([0, 3, 5] , dtype='''long''' ) A: Union[str, Any] = tokenizer(['''Test question'''] ).input_ids A: Optional[int] = tokenizer( ['''the fourth''', '''longer longer'''] , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_token_type_ids=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , ).input_ids A: Any = config.reader_seq_len A , A , A , A: Dict = retriever( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , answer_ids=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' ) self.assertEqual([False, True, True] , SCREAMING_SNAKE_CASE_ ) self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]] , SCREAMING_SNAKE_CASE_ ) self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]] , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple ) -> Tuple: '''simple docstring''' A: Any = self.get_dummy_retriever() retriever.save_pretrained(os.path.join(self.tmpdirname , '''realm_block_records''' ) ) # Test local path A: str = retriever.from_pretrained(os.path.join(self.tmpdirname , '''realm_block_records''' ) ) self.assertEqual(retriever.block_records[0] , b'''This is the first record''' ) # Test mocked remote path with patch('''transformers.models.realm.retrieval_realm.hf_hub_download''' ) as mock_hf_hub_download: A: Tuple = os.path.join( os.path.join(self.tmpdirname , '''realm_block_records''' ) , _REALM_BLOCK_RECORDS_FILENAME ) A: str = RealmRetriever.from_pretrained('''google/realm-cc-news-pretrained-openqa''' ) self.assertEqual(retriever.block_records[0] , b'''This is the first record''' )
334
'''simple docstring''' from __future__ import annotations import numpy as np def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
334
1
'''simple docstring''' import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 UpperCamelCase = data_utils.TransfoXLTokenizer UpperCamelCase = data_utils.TransfoXLCorpus UpperCamelCase = data_utils UpperCamelCase = data_utils def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> Any: if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(__lowercase , '''rb''' ) as fp: A: Dict = pickle.load(__lowercase , encoding='''latin1''' ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) A: Optional[Any] = pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file'''] print(F"""Save vocabulary to {pytorch_vocab_dump_path}""" ) A: List[str] = corpus.vocab.__dict__ torch.save(__lowercase , __lowercase ) A: Tuple = corpus.__dict__ corpus_dict_no_vocab.pop('''vocab''' , __lowercase ) A: Dict = pytorch_dump_folder_path + '''/''' + CORPUS_NAME print(F"""Save dataset to {pytorch_dataset_dump_path}""" ) torch.save(__lowercase , __lowercase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model A: List[Any] = os.path.abspath(__lowercase ) A: Optional[int] = os.path.abspath(__lowercase ) print(F"""Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.""" ) # Initialise PyTorch model if transfo_xl_config_file == "": A: List[Any] = TransfoXLConfig() else: A: Dict = TransfoXLConfig.from_json_file(__lowercase ) print(F"""Building PyTorch model from configuration: {config}""" ) A: Any = TransfoXLLMHeadModel(__lowercase ) A: int = load_tf_weights_in_transfo_xl(__lowercase , __lowercase , __lowercase ) # Save pytorch-model A: Union[str, Any] = os.path.join(__lowercase , __lowercase ) A: List[Any] = os.path.join(__lowercase , __lowercase ) print(F"""Save PyTorch model to {os.path.abspath(__lowercase )}""" ) torch.save(model.state_dict() , __lowercase ) print(F"""Save configuration file to {os.path.abspath(__lowercase )}""" ) with open(__lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the folder to store the PyTorch model or dataset/vocab.''', ) parser.add_argument( '''--tf_checkpoint_path''', default='''''', type=str, help='''An optional path to a TensorFlow checkpoint path to be converted.''', ) parser.add_argument( '''--transfo_xl_config_file''', default='''''', type=str, help=( '''An optional config json file corresponding to the pre-trained BERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--transfo_xl_dataset_file''', default='''''', type=str, help='''An optional dataset file to be converted in a vocabulary.''', ) UpperCamelCase = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' import math import sys def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if number != int(__lowercase ): raise ValueError('''the value of input must be a natural number''' ) if number < 0: raise ValueError('''the value of input must not be a negative number''' ) if number == 0: return 1 A: int = [-1] * (number + 1) A: List[str] = 0 for i in range(1 , number + 1 ): A: Tuple = sys.maxsize A: Optional[int] = int(math.sqrt(__lowercase ) ) for j in range(1 , root + 1 ): A: str = 1 + answers[i - (j**2)] A: Dict = min(__lowercase , __lowercase ) A: str = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
334
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
334
1
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 6_55_36 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : str = "fourier" , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , SCREAMING_SNAKE_CASE_ : Tuple[str] = "UNetMidBlock1D" , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : Tuple[int] = (32, 32, 64) , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[Any] = sample_size # time if time_embedding_type == "fourier": A: Tuple = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=SCREAMING_SNAKE_CASE_ , log=SCREAMING_SNAKE_CASE_ , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ ) A: List[str] = 2 * block_out_channels[0] elif time_embedding_type == "positional": A: str = Timesteps( block_out_channels[0] , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ , downscale_freq_shift=SCREAMING_SNAKE_CASE_ ) A: Any = block_out_channels[0] if use_timestep_embedding: A: Optional[Any] = block_out_channels[0] * 4 A: List[Any] = TimestepEmbedding( in_channels=SCREAMING_SNAKE_CASE_ , time_embed_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , out_dim=block_out_channels[0] , ) A: Optional[Any] = nn.ModuleList([] ) A: str = None A: str = nn.ModuleList([] ) A: Tuple = None # down A: Any = in_channels for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = output_channel A: List[Any] = block_out_channels[i] if i == 0: input_channel += extra_in_channels A: List[Any] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[int] = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid A: Union[str, Any] = get_mid_block( SCREAMING_SNAKE_CASE_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=SCREAMING_SNAKE_CASE_ , add_downsample=SCREAMING_SNAKE_CASE_ , ) # up A: Optional[Any] = list(reversed(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = reversed_block_out_channels[0] if out_block_type is None: A: int = out_channels else: A: Union[str, Any] = block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: List[Any] = output_channel A: int = ( reversed_block_out_channels[i + 1] if i < len(SCREAMING_SNAKE_CASE_ ) - 1 else final_upsample_channels ) A: Optional[int] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[Any] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) A: Any = output_channel # out A: List[str] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) A: Optional[int] = get_out_block( out_block_type=SCREAMING_SNAKE_CASE_ , num_groups_out=SCREAMING_SNAKE_CASE_ , embed_dim=block_out_channels[0] , out_channels=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , fc_dim=block_out_channels[-1] // 4 , ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[UNetaDOutput, Tuple]: '''simple docstring''' A: Any = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: List[str] = timesteps[None].to(sample.device ) A: int = self.time_proj(SCREAMING_SNAKE_CASE_ ) if self.config.use_timestep_embedding: A: List[Any] = self.time_mlp(SCREAMING_SNAKE_CASE_ ) else: A: str = timestep_embed[..., None] A: Union[str, Any] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) A: Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down A: List[str] = () for downsample_block in self.down_blocks: A , A: Optional[int] = downsample_block(hidden_states=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: A: Dict = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): A: List[Any] = down_block_res_samples[-1:] A: List[str] = down_block_res_samples[:-1] A: Optional[int] = upsample_block(SCREAMING_SNAKE_CASE_ , res_hidden_states_tuple=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) # 5. post-process if self.out_block: A: Any = self.out_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=SCREAMING_SNAKE_CASE_ )
334
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 lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int=7 , SCREAMING_SNAKE_CASE_ : str=3 , SCREAMING_SNAKE_CASE_ : int=18 , SCREAMING_SNAKE_CASE_ : Optional[Any]=30 , SCREAMING_SNAKE_CASE_ : List[str]=4_00 , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : str=None , SCREAMING_SNAKE_CASE_ : Optional[Any]=True , ) -> Union[str, Any]: '''simple docstring''' A: Optional[Any] = size if size is not None else {'''height''': 18, '''width''': 18} A: List[str] = parent A: Optional[Any] = batch_size A: Optional[Any] = num_channels A: Tuple = image_size A: Dict = min_resolution A: Tuple = max_resolution A: Optional[int] = do_resize A: Optional[Any] = size A: int = apply_ocr def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' return {"do_resize": self.do_resize, "size": self.size, "apply_ocr": self.apply_ocr} @require_torch @require_pytesseract class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Dict = LayoutLMvaImageProcessor if is_pytesseract_available() else None def _snake_case ( self : Tuple ) -> List[str]: '''simple docstring''' A: str = LayoutLMvaImageProcessingTester(self ) @property def _snake_case ( self : Tuple ) -> List[str]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_resize''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''size''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''apply_ocr''' ) ) def _snake_case ( self : Optional[int] ) -> List[str]: '''simple docstring''' A: Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 18, '''width''': 18} ) A: Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {'''height''': 42, '''width''': 42} ) def _snake_case ( self : Tuple ) -> List[str]: '''simple docstring''' pass def _snake_case ( self : str ) -> str: '''simple docstring''' A: List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A: str = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , Image.Image ) # Test not batched input A: Dict = 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 , SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(encoding.boxes , SCREAMING_SNAKE_CASE_ ) # Test batched A: str = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' A: Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A: Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , numpify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) # Test not batched input A: List[str] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched A: Optional[int] = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' A: int = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A: str = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) # Test not batched input A: Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) # Test batched A: Tuple = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size['''height'''], self.image_processor_tester.size['''width'''], ) , ) def _snake_case ( self : int ) -> Dict: '''simple docstring''' A: Union[str, Any] = LayoutLMvaImageProcessor() from datasets import load_dataset A: List[Any] = load_dataset('''hf-internal-testing/fixtures_docvqa''' , split='''test''' ) A: Optional[Any] = Image.open(ds[0]['''file'''] ).convert('''RGB''' ) A: Optional[int] = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_24, 2_24) ) self.assertEqual(len(encoding.words ) , len(encoding.boxes ) ) # fmt: off # the words and boxes were obtained with Tesseract 4.1.1 A: Any = [['''11:14''', '''to''', '''11:39''', '''a.m''', '''11:39''', '''to''', '''11:44''', '''a.m.''', '''11:44''', '''a.m.''', '''to''', '''12:25''', '''p.m.''', '''12:25''', '''to''', '''12:58''', '''p.m.''', '''12:58''', '''to''', '''4:00''', '''p.m.''', '''2:00''', '''to''', '''5:00''', '''p.m.''', '''Coffee''', '''Break''', '''Coffee''', '''will''', '''be''', '''served''', '''for''', '''men''', '''and''', '''women''', '''in''', '''the''', '''lobby''', '''adjacent''', '''to''', '''exhibit''', '''area.''', '''Please''', '''move''', '''into''', '''exhibit''', '''area.''', '''(Exhibits''', '''Open)''', '''TRRF''', '''GENERAL''', '''SESSION''', '''(PART''', '''|)''', '''Presiding:''', '''Lee''', '''A.''', '''Waller''', '''TRRF''', '''Vice''', '''President''', '''“Introductory''', '''Remarks”''', '''Lee''', '''A.''', '''Waller,''', '''TRRF''', '''Vice''', '''Presi-''', '''dent''', '''Individual''', '''Interviews''', '''with''', '''TRRF''', '''Public''', '''Board''', '''Members''', '''and''', '''Sci-''', '''entific''', '''Advisory''', '''Council''', '''Mem-''', '''bers''', '''Conducted''', '''by''', '''TRRF''', '''Treasurer''', '''Philip''', '''G.''', '''Kuehn''', '''to''', '''get''', '''answers''', '''which''', '''the''', '''public''', '''refrigerated''', '''warehousing''', '''industry''', '''is''', '''looking''', '''for.''', '''Plus''', '''questions''', '''from''', '''the''', '''floor.''', '''Dr.''', '''Emil''', '''M.''', '''Mrak,''', '''University''', '''of''', '''Cal-''', '''ifornia,''', '''Chairman,''', '''TRRF''', '''Board;''', '''Sam''', '''R.''', '''Cecil,''', '''University''', '''of''', '''Georgia''', '''College''', '''of''', '''Agriculture;''', '''Dr.''', '''Stanley''', '''Charm,''', '''Tufts''', '''University''', '''School''', '''of''', '''Medicine;''', '''Dr.''', '''Robert''', '''H.''', '''Cotton,''', '''ITT''', '''Continental''', '''Baking''', '''Company;''', '''Dr.''', '''Owen''', '''Fennema,''', '''University''', '''of''', '''Wis-''', '''consin;''', '''Dr.''', '''Robert''', '''E.''', '''Hardenburg,''', '''USDA.''', '''Questions''', '''and''', '''Answers''', '''Exhibits''', '''Open''', '''Capt.''', '''Jack''', '''Stoney''', '''Room''', '''TRRF''', '''Scientific''', '''Advisory''', '''Council''', '''Meeting''', '''Ballroom''', '''Foyer''']] # noqa: E231 A: Any = [[[1_41, 57, 2_14, 69], [2_28, 58, 2_52, 69], [1_41, 75, 2_16, 88], [2_30, 79, 2_80, 88], [1_42, 2_60, 2_18, 2_73], [2_30, 2_61, 2_55, 2_73], [1_43, 2_79, 2_18, 2_90], [2_31, 2_82, 2_90, 2_91], [1_43, 3_42, 2_18, 3_54], [2_31, 3_45, 2_89, 3_55], [2_02, 3_62, 2_27, 3_73], [1_43, 3_79, 2_20, 3_92], [2_31, 3_82, 2_91, 3_94], [1_44, 7_14, 2_20, 7_26], [2_31, 7_15, 2_56, 7_26], [1_44, 7_32, 2_20, 7_45], [2_32, 7_36, 2_91, 7_47], [1_44, 7_69, 2_18, 7_82], [2_31, 7_70, 2_56, 7_82], [1_41, 7_88, 2_02, 8_01], [2_15, 7_91, 2_74, 8_04], [1_43, 8_26, 2_04, 8_38], [2_15, 8_26, 2_40, 8_38], [1_42, 8_44, 2_02, 8_57], [2_15, 8_47, 2_74, 8_59], [3_34, 57, 4_27, 69], [4_40, 57, 5_22, 69], [3_69, 75, 4_61, 88], [4_69, 75, 5_16, 88], [5_28, 76, 5_62, 88], [5_70, 76, 6_67, 88], [6_75, 75, 7_11, 87], [7_21, 79, 7_78, 88], [7_89, 75, 8_40, 88], [3_69, 97, 4_70, 1_07], [4_84, 94, 5_07, 1_06], [5_18, 94, 5_62, 1_07], [5_76, 94, 6_55, 1_10], [6_68, 94, 7_92, 1_09], [8_04, 95, 8_29, 1_07], [3_69, 1_13, 4_65, 1_25], [4_77, 1_16, 5_47, 1_25], [5_62, 1_13, 6_58, 1_25], [6_71, 1_16, 7_48, 1_25], [7_61, 1_13, 8_11, 1_25], [3_69, 1_31, 4_65, 1_43], [4_77, 1_33, 5_48, 1_43], [5_63, 1_30, 6_98, 1_45], [7_10, 1_30, 8_02, 1_46], [3_36, 1_71, 4_12, 1_83], [4_23, 1_71, 5_72, 1_83], [5_82, 1_70, 7_16, 1_84], [7_28, 1_71, 8_17, 1_87], [8_29, 1_71, 8_44, 1_86], [3_38, 1_97, 4_82, 2_12], [5_07, 1_96, 5_57, 2_09], [5_69, 1_96, 5_95, 2_08], [6_10, 1_96, 7_02, 2_09], [5_05, 2_14, 5_83, 2_26], [5_95, 2_14, 6_56, 2_27], [6_70, 2_15, 8_07, 2_27], [3_35, 2_59, 5_43, 2_74], [5_56, 2_59, 7_08, 2_72], [3_72, 2_79, 4_22, 2_91], [4_35, 2_79, 4_60, 2_91], [4_74, 2_79, 5_74, 2_92], [5_87, 2_78, 6_64, 2_91], [6_76, 2_78, 7_38, 2_91], [7_51, 2_79, 8_34, 2_91], [3_72, 2_98, 4_34, 3_10], [3_35, 3_41, 4_83, 3_54], [4_97, 3_41, 6_55, 3_54], [6_67, 3_41, 7_28, 3_54], [7_40, 3_41, 8_25, 3_54], [3_35, 3_60, 4_30, 3_72], [4_42, 3_60, 5_34, 3_72], [5_45, 3_59, 6_87, 3_72], [6_97, 3_60, 7_54, 3_72], [7_65, 3_60, 8_23, 3_73], [3_34, 3_78, 4_28, 3_91], [4_40, 3_78, 5_77, 3_94], [5_90, 3_78, 7_05, 3_91], [7_20, 3_78, 8_01, 3_91], [3_34, 3_97, 4_00, 4_09], [3_70, 4_16, 5_29, 4_29], [5_44, 4_16, 5_76, 4_32], [5_87, 4_16, 6_65, 4_28], [6_77, 4_16, 8_14, 4_29], [3_72, 4_35, 4_52, 4_50], [4_65, 4_34, 4_95, 4_47], [5_11, 4_34, 6_00, 4_47], [6_11, 4_36, 6_37, 4_47], [6_49, 4_36, 6_94, 4_51], [7_05, 4_38, 8_24, 4_47], [3_69, 4_53, 4_52, 4_66], [4_64, 4_54, 5_09, 4_66], [5_22, 4_53, 6_11, 4_69], [6_25, 4_53, 7_92, 4_69], [3_70, 4_72, 5_56, 4_88], [5_70, 4_72, 6_84, 4_87], [6_97, 4_72, 7_18, 4_85], [7_32, 4_72, 8_35, 4_88], [3_69, 4_90, 4_11, 5_03], [4_25, 4_90, 4_84, 5_03], [4_96, 4_90, 6_35, 5_06], [6_45, 4_90, 7_07, 5_03], [7_18, 4_91, 7_61, 5_03], [7_71, 4_90, 8_40, 5_03], [3_36, 5_10, 3_74, 5_21], [3_88, 5_10, 4_47, 5_22], [4_60, 5_10, 4_89, 5_21], [5_03, 5_10, 5_80, 5_22], [5_92, 5_09, 7_36, 5_25], [7_45, 5_09, 7_70, 5_22], [7_81, 5_09, 8_40, 5_22], [3_38, 5_28, 4_34, 5_41], [4_48, 5_28, 5_96, 5_41], [6_09, 5_27, 6_87, 5_40], [7_00, 5_28, 7_92, 5_41], [3_36, 5_46, 3_97, 5_59], [4_07, 5_46, 4_31, 5_59], [4_43, 5_46, 5_25, 5_60], [5_37, 5_46, 6_80, 5_62], [6_88, 5_46, 7_14, 5_59], [7_22, 5_46, 8_37, 5_62], [3_36, 5_65, 4_49, 5_81], [4_61, 5_65, 4_85, 5_77], [4_97, 5_65, 6_65, 5_81], [6_81, 5_65, 7_18, 5_77], [7_32, 5_65, 8_37, 5_80], [3_37, 5_84, 4_38, 5_97], [4_52, 5_83, 5_21, 5_96], [5_35, 5_84, 6_77, 5_99], [6_90, 5_83, 7_87, 5_96], [8_01, 5_83, 8_25, 5_96], [3_38, 6_02, 4_78, 6_15], [4_92, 6_02, 5_30, 6_14], [5_43, 6_02, 6_38, 6_15], [6_50, 6_02, 6_76, 6_14], [6_88, 6_02, 7_88, 6_15], [8_02, 6_02, 8_43, 6_14], [3_37, 6_21, 5_02, 6_33], [5_16, 6_21, 6_15, 6_37], [6_29, 6_21, 7_74, 6_36], [7_89, 6_21, 8_27, 6_33], [3_37, 6_39, 4_18, 6_52], [4_32, 6_40, 5_71, 6_53], [5_87, 6_39, 7_31, 6_55], [7_43, 6_39, 7_69, 6_52], [7_80, 6_39, 8_41, 6_52], [3_38, 6_58, 4_40, 6_73], [4_55, 6_58, 4_91, 6_70], [5_08, 6_58, 6_02, 6_71], [6_16, 6_58, 6_38, 6_70], [6_54, 6_58, 8_35, 6_74], [3_37, 6_77, 4_29, 6_89], [3_37, 7_14, 4_82, 7_26], [4_95, 7_14, 5_48, 7_26], [5_61, 7_14, 6_83, 7_26], [3_38, 7_70, 4_61, 7_82], [4_74, 7_69, 5_54, 7_85], [4_89, 7_88, 5_62, 8_03], [5_76, 7_88, 6_43, 8_01], [6_56, 7_87, 7_51, 8_04], [7_64, 7_88, 8_44, 8_01], [3_34, 8_25, 4_21, 8_38], [4_30, 8_24, 5_74, 8_38], [5_84, 8_24, 7_23, 8_41], [3_35, 8_44, 4_50, 8_57], [4_64, 8_43, 5_83, 8_60], [6_28, 8_62, 7_55, 8_75], [7_69, 8_61, 8_48, 8_78]]] # noqa: E231 # fmt: on self.assertListEqual(encoding.words , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(encoding.boxes , SCREAMING_SNAKE_CASE_ ) # with apply_OCR = False A: int = LayoutLMvaImageProcessor(apply_ocr=SCREAMING_SNAKE_CASE_ ) A: int = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) self.assertEqual(encoding.pixel_values.shape , (1, 3, 2_24, 2_24) )
334
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> None: '''simple docstring''' warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
334
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 lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : Optional[int] ) -> str: '''simple docstring''' for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(SCREAMING_SNAKE_CASE_ ): A: int = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Dict = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Any ) -> int: '''simple docstring''' for model_name in ["roberta-base", "roberta-large"]: with self.subTest(SCREAMING_SNAKE_CASE_ ): A: Any = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' for model_name in ["bert-base-cased", "bert-large-uncased"]: A: List[Any] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Dict = FlaxBertModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**SCREAMING_SNAKE_CASE_ : int ): return model(**SCREAMING_SNAKE_CASE_ ) eval(**SCREAMING_SNAKE_CASE_ ).block_until_ready() @slow def _snake_case ( self : Any ) -> Dict: '''simple docstring''' for model_name in ["roberta-base", "roberta-large"]: A: Optional[Any] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = FlaxRobertaModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**SCREAMING_SNAKE_CASE_ : int ): return model(**SCREAMING_SNAKE_CASE_ ) eval(**SCREAMING_SNAKE_CASE_ ).block_until_ready() def _snake_case ( self : Any ) -> Any: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''bert-base is not a local folder and is not a valid model identifier''' ): A: str = FlaxAutoModel.from_pretrained('''bert-base''' ) def _snake_case ( self : Any ) -> Any: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): A: Optional[Any] = FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_ , revision='''aaaaaa''' ) def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): A: Dict = FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' with self.assertRaisesRegex(SCREAMING_SNAKE_CASE_ , '''Use `from_pt=True` to load this model''' ): A: int = FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
334
'''simple docstring''' from collections import deque class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> None: '''simple docstring''' A: Union[str, Any] = process_name # process name A: List[str] = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A: Dict = arrival_time A: Optional[Any] = burst_time # remaining burst time A: Any = 0 # total time of the process wait in ready queue A: Any = 0 # time from arrival time to completion time class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int , ) -> None: '''simple docstring''' A: Dict = number_of_queues # time slice of queues that round robin algorithm applied A: int = time_slices # unfinished process is in this ready_queue A: Tuple = queue # current time A: int = current_time # finished process is in this sequence queue A: deque[Process] = deque() def _snake_case ( self : List[Any] ) -> list[str]: '''simple docstring''' A: str = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Optional[int] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Any = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: List[Any] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): completion_times.append(queue[i].stop_time ) return completion_times def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> list[int]: '''simple docstring''' return [q.burst_time for q in queue] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Process ) -> int: '''simple docstring''' process.waiting_time += self.current_time - process.stop_time return process.waiting_time def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> deque[Process]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of finished process while len(SCREAMING_SNAKE_CASE_ ) != 0: A: Optional[Any] = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A: Any = 0 # set the process's turnaround time because it is finished A: int = self.current_time - cp.arrival_time # set the completion time A: List[str] = self.current_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int ) -> tuple[deque[Process], deque[Process]]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(SCREAMING_SNAKE_CASE_ ) ): A: Dict = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A: Optional[Any] = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(SCREAMING_SNAKE_CASE_ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A: int = 0 # set the finish time A: Union[str, Any] = self.current_time # update the process' turnaround time because it is finished A: Tuple = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def _snake_case ( self : Optional[Any] ) -> deque[Process]: '''simple docstring''' for i in range(self.number_of_queues - 1 ): A , A: Optional[Any] = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) UpperCamelCase = MLFQ(number_of_queues, time_slices, queue, 0) UpperCamelCase = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f'waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print completion times of processes(P1, P2, P3, P4) print( f'completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print total turnaround times of processes(P1, P2, P3, P4) print( f'turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print sequence of finished processes print( f'sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}' )
334
1
'''simple docstring''' from typing import Dict, Iterable, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[str]: return [ int(1_0_0_0 * (box[0] / width) ), int(1_0_0_0 * (box[1] / height) ), int(1_0_0_0 * (box[2] / width) ), int(1_0_0_0 * (box[3] / height) ), ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Dict: A: Optional[int] = to_pil_image(__lowercase ) A , A: str = pil_image.size A: str = pytesseract.image_to_data(__lowercase , lang=__lowercase , output_type='''dict''' , config=__lowercase ) A , A , A , A , A: str = data['''text'''], data['''left'''], data['''top'''], data['''width'''], data['''height'''] # filter empty words and corresponding coordinates A: int = [idx for idx, word in enumerate(__lowercase ) if not word.strip()] A: Optional[Any] = [word for idx, word in enumerate(__lowercase ) if idx not in irrelevant_indices] A: Optional[int] = [coord for idx, coord in enumerate(__lowercase ) if idx not in irrelevant_indices] A: int = [coord for idx, coord in enumerate(__lowercase ) if idx not in irrelevant_indices] A: Optional[Any] = [coord for idx, coord in enumerate(__lowercase ) if idx not in irrelevant_indices] A: Union[str, Any] = [coord for idx, coord in enumerate(__lowercase ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format A: List[Any] = [] for x, y, w, h in zip(__lowercase , __lowercase , __lowercase , __lowercase ): A: Union[str, Any] = [x, y, x + w, y + h] actual_boxes.append(__lowercase ) # finally, normalize the bounding boxes A: List[str] = [] for box in actual_boxes: normalized_boxes.append(normalize_box(__lowercase , __lowercase , __lowercase ) ) assert len(__lowercase ) == len(__lowercase ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = ["""pixel_values"""] def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Dict[str, int] = None , SCREAMING_SNAKE_CASE_ : PILImageResampling = PILImageResampling.BILINEAR , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : float = 1 / 2_55 , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] = None , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] = None , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = "" , **SCREAMING_SNAKE_CASE_ : Optional[int] , ) -> None: '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = size if size is not None else {'''height''': 2_24, '''width''': 2_24} A: Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = do_resize A: str = size A: Optional[Any] = resample A: List[str] = do_rescale A: List[Any] = rescale_value A: Dict = do_normalize A: Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A: Optional[Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD A: Union[str, Any] = apply_ocr A: List[str] = ocr_lang A: Optional[Any] = tesseract_config def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Dict[str, int] , SCREAMING_SNAKE_CASE_ : PILImageResampling = PILImageResampling.BILINEAR , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> np.ndarray: '''simple docstring''' A: Any = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""" ) A: List[Any] = (size['''height'''], size['''width''']) return resize(SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Union[int, float] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> np.ndarray: '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] , SCREAMING_SNAKE_CASE_ : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE_ : Any , ) -> np.ndarray: '''simple docstring''' return normalize(SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : ImageInput , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Dict[str, int] = None , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : float = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] = None , SCREAMING_SNAKE_CASE_ : Union[float, Iterable[float]] = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : ChannelDimension = ChannelDimension.FIRST , **SCREAMING_SNAKE_CASE_ : Tuple , ) -> PIL.Image.Image: '''simple docstring''' A: int = do_resize if do_resize is not None else self.do_resize A: List[str] = size if size is not None else self.size A: List[str] = get_size_dict(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = resample if resample is not None else self.resample A: Dict = do_rescale if do_rescale is not None else self.do_rescale A: Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor A: Union[str, Any] = do_normalize if do_normalize is not None else self.do_normalize A: Optional[Any] = image_mean if image_mean is not None else self.image_mean A: List[str] = image_std if image_std is not None else self.image_std A: int = apply_ocr if apply_ocr is not None else self.apply_ocr A: Any = ocr_lang if ocr_lang is not None else self.ocr_lang A: Tuple = tesseract_config if tesseract_config is not None else self.tesseract_config A: Optional[int] = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize 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('''If do_normalize is True, image_mean and image_std must be specified.''' ) # All transformations expect numpy arrays. A: Optional[int] = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] # Tesseract OCR to get words + normalized bounding boxes if apply_ocr: requires_backends(self , '''pytesseract''' ) A: Any = [] A: Optional[Any] = [] for image in images: A , A: List[Any] = apply_tesseract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) words_batch.append(SCREAMING_SNAKE_CASE_ ) boxes_batch.append(SCREAMING_SNAKE_CASE_ ) if do_resize: A: Any = [self.resize(image=SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: A: Dict = [self.rescale(image=SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ ) for image in images] if do_normalize: A: int = [self.normalize(image=SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ ) for image in images] A: Tuple = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for image in images] A: Any = BatchFeature(data={'''pixel_values''': images} , tensor_type=SCREAMING_SNAKE_CASE_ ) if apply_ocr: A: Optional[Any] = words_batch A: List[str] = boxes_batch return data
334
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : list = field(default_factory=UpperCAmelCase_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tensor , SCREAMING_SNAKE_CASE_ : Tensor ) -> int: '''simple docstring''' A: List[str] = len(list(m.modules() ) ) == 1 or isinstance(SCREAMING_SNAKE_CASE_ , nn.Convad ) or isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ) if has_not_submodules: self.traced.append(SCREAMING_SNAKE_CASE_ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tensor ) -> Dict: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(SCREAMING_SNAKE_CASE_ ) [x.remove() for x in self.handles] return self @property def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return list(filter(lambda SCREAMING_SNAKE_CASE_ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 0 UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) def __call__( self : Any , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[Any]: '''simple docstring''' A: Dict = Tracker(self.dest )(SCREAMING_SNAKE_CASE_ ).parametrized A: Tuple = Tracker(self.src )(SCREAMING_SNAKE_CASE_ ).parametrized A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.src_skip , SCREAMING_SNAKE_CASE_ ) ) A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.dest_skip , SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise Exception( f"""Numbers of operations are different. Source module has {len(SCREAMING_SNAKE_CASE_ )} operations while""" f""" destination module has {len(SCREAMING_SNAKE_CASE_ )}.""" ) for dest_m, src_m in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"""Transfered from={src_m} to={dest_m}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase = True ) -> Any: print(F"""Converting {name}...""" ) with torch.no_grad(): A: Union[str, Any] = timm.create_model(__lowercase , pretrained=__lowercase ).eval() A: List[str] = ResNetForImageClassification(__lowercase ).eval() A: int = ModuleTransfer(src=__lowercase , dest=__lowercase ) A: List[str] = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(__lowercase ) assert torch.allclose(from_model(__lowercase ) , our_model(__lowercase ).logits ), "The model logits don't match the original one." A: str = F"""resnet{'-'.join(name.split('resnet' ) )}""" print(__lowercase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add model''' , use_temp_dir=__lowercase , ) # we can use the convnext one A: Any = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add image processor''' , use_temp_dir=__lowercase , ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> List[Any]: A: Union[str, Any] = '''imagenet-1k-id2label.json''' A: Union[str, Any] = 1_0_0_0 A: Optional[int] = (1, num_labels) A: Dict = '''huggingface/label-files''' A: Any = num_labels A: Union[str, Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Optional[int] = {int(__lowercase ): v for k, v in idalabel.items()} A: Optional[int] = idalabel A: List[str] = {v: k for k, v in idalabel.items()} A: str = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Optional[Any] = { '''resnet18''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet26''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet34''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet50''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet101''': ImageNetPreTrainedConfig( depths=[3, 4, 2_3, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet152''': ImageNetPreTrainedConfig( depths=[3, 8, 3_6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), } if model_name: convert_weight_and_push(__lowercase , names_to_config[model_name] , __lowercase , __lowercase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(__lowercase , __lowercase , __lowercase , __lowercase ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,''' ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
1
'''simple docstring''' import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Tuple: # Initialise PyTorch model. # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of # TapasConfig to False. # initialize configuration from json file A: str = TapasConfig.from_json_file(__lowercase ) # set absolute/relative position embeddings parameter A: Union[str, Any] = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": A: int = TapasForQuestionAnswering(config=__lowercase ) elif task == "WTQ": # run_task_main.py hparams A: int = 4 A: Any = True # hparam_utils.py hparams A: Tuple = 0.6_6_4_6_9_4 A: int = 0.2_0_7_9_5_1 A: Any = 0.1_2_1_1_9_4 A: Tuple = True A: int = True A: Union[str, Any] = False A: Optional[Any] = 0.0_3_5_2_5_1_3 A: List[str] = TapasForQuestionAnswering(config=__lowercase ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams A: str = 4 A: str = False # hparam_utils.py hparams A: List[str] = 3_6.4_5_1_9 A: Optional[int] = 0.9_0_3_4_2_1 A: Optional[int] = 2_2_2.0_8_8 A: str = True A: List[Any] = True A: Dict = True A: int = 0.7_6_3_1_4_1 A: Optional[int] = TapasForQuestionAnswering(config=__lowercase ) elif task == "TABFACT": A: Dict = TapasForSequenceClassification(config=__lowercase ) elif task == "MLM": A: str = TapasForMaskedLM(config=__lowercase ) elif task == "INTERMEDIATE_PRETRAINING": A: Any = TapasModel(config=__lowercase ) else: raise ValueError(F"""Task {task} not supported.""" ) print(F"""Building PyTorch model from configuration: {config}""" ) # Load weights from tf checkpoint load_tf_weights_in_tapas(__lowercase , __lowercase , __lowercase ) # Save pytorch-model (weights and configuration) print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(__lowercase ) # Save tokenizer files print(F"""Save tokenizer files to {pytorch_dump_path}""" ) A: Dict = TapasTokenizer(vocab_file=tf_checkpoint_path[:-1_0] + '''vocab.txt''' , model_max_length=5_1_2 ) tokenizer.save_pretrained(__lowercase ) print('''Used relative position embeddings:''' , model.config.reset_position_index_per_cell ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''SQA''', type=str, help='''Model task for which to convert a checkpoint. Defaults to SQA.''' ) parser.add_argument( '''--reset_position_index_per_cell''', default=False, action='''store_true''', help='''Whether to use relative position embeddings or not. Defaults to True.''', ) parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--tapas_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained TAPAS 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.''' ) UpperCamelCase = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
334
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase = 0 , __lowercase = 0 ) -> int: A: Union[str, Any] = right or len(__lowercase ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(__lowercase , __lowercase , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = len(__lowercase ) for i in range(length - 1 ): A: Dict = i for k in range(i + 1 , __lowercase ): if collection[k] < collection[least]: A: List[str] = k if least != i: A , A: Tuple = (collection[i], collection[least]) return collection if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
334
1
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : int = ["""image_processor""", """tokenizer"""] UpperCamelCase_ : str = """CLIPImageProcessor""" UpperCamelCase_ : int = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self : str , SCREAMING_SNAKE_CASE_ : Any=None , SCREAMING_SNAKE_CASE_ : Dict=None , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> int: '''simple docstring''' A: Union[str, Any] = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = kwargs.pop('''feature_extractor''' ) A: 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__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __call__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=None , SCREAMING_SNAKE_CASE_ : Tuple=None , **SCREAMING_SNAKE_CASE_ : int ) -> Optional[int]: '''simple docstring''' 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: A: str = self.tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if images is not None: A: List[str] = self.image_processor(SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if text is not None and images is not None: A: Optional[int] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_ ) , tensor_type=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , *SCREAMING_SNAKE_CASE_ : Optional[int] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Any , *SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : Dict ) -> Dict: '''simple docstring''' return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @property def _snake_case ( self : Optional[int] ) -> str: '''simple docstring''' A: Any = self.tokenizer.model_input_names A: Optional[int] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def _snake_case ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , SCREAMING_SNAKE_CASE_ , ) return self.image_processor_class @property def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , SCREAMING_SNAKE_CASE_ , ) return self.image_processor
334
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list: if len(__lowercase ) <= 1: return [tuple(__lowercase )] A: Any = [] def generate(__lowercase , __lowercase ): A: Union[str, Any] = [0] * n res.append(tuple(__lowercase ) ) A: Optional[int] = 0 while i < n: if c[i] < i: if i % 2 == 0: A , A: Union[str, Any] = arr[i], arr[0] else: A , A: List[str] = arr[i], arr[c[i]] res.append(tuple(__lowercase ) ) c[i] += 1 A: Dict = 0 else: A: str = 0 i += 1 generate(len(__lowercase ) , __lowercase ) return res if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
334
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: A: Optional[Any] = [0] * len(__lowercase ) A: Any = [] A: Any = [1] * len(__lowercase ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(__lowercase ) ): if indegree[i] == 0: queue.append(__lowercase ) while queue: A: str = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: A: int = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(__lowercase ) print(max(__lowercase ) ) # Adjacency list of Graph UpperCamelCase = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
334
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = DebertaTokenizer UpperCamelCase_ : List[str] = True UpperCamelCase_ : int = DebertaTokenizerFast def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A: Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] A: int = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Union[str, Any] = {'''unk_token''': '''[UNK]'''} A: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: str = 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(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = '''lower newer''' A: str = '''lower newer''' return input_text, output_text def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: str = self.get_tokenizer() A: Any = '''lower newer''' A: Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] A: int = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokens + [tokenizer.unk_token] A: int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' A: str = self.get_tokenizer() A: List[str] = tokenizer('''Hello''' , '''World''' ) A: Union[str, Any] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Any = tokenizer.encode('''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: int = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: A: List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Dict = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: Any = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for seq in encoding['''input_ids''']] # fmt: off A: Any = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on A: Optional[int] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , SCREAMING_SNAKE_CASE_ ) for expected, decoded in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' import logging import os from .state import PartialState class lowerCAmelCase_ ( logging.LoggerAdapter ): '''simple docstring''' @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: '''simple docstring''' if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) A: List[str] = kwargs.pop('''main_process_only''' , SCREAMING_SNAKE_CASE_ ) A: Dict = kwargs.pop('''in_order''' , SCREAMING_SNAKE_CASE_ ) if self.isEnabledFor(SCREAMING_SNAKE_CASE_ ): if self._should_log(SCREAMING_SNAKE_CASE_ ): A , A: str = self.process(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.logger.log(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) elif in_order: A: Optional[Any] = PartialState() for i in range(state.num_processes ): if i == state.process_index: A , A: Optional[Any] = self.process(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.logger.log(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) state.wait_for_everyone() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None ) -> int: if log_level is None: A: Dict = os.environ.get('''ACCELERATE_LOG_LEVEL''' , __lowercase ) A: Tuple = logging.getLogger(__lowercase ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(__lowercase , {} )
334
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
1
'''simple docstring''' import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) UpperCamelCase = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation='''relu''') ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation='''relu''')) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation='''relu''')) classifier.add(layers.Dense(units=1, activation='''sigmoid''')) # Compiling the CNN classifier.compile( optimizer='''adam''', loss='''binary_crossentropy''', metrics=['''accuracy'''] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') UpperCamelCase = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) UpperCamelCase = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) UpperCamelCase = train_datagen.flow_from_directory( '''dataset/training_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) UpperCamelCase = test_datagen.flow_from_directory( '''dataset/test_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save('''cnn.h5''') # Part 3 - Making new predictions UpperCamelCase = tf.keras.preprocessing.image.load_img( '''dataset/single_prediction/image.png''', target_size=(64, 64) ) UpperCamelCase = tf.keras.preprocessing.image.img_to_array(test_image) UpperCamelCase = np.expand_dims(test_image, axis=0) UpperCamelCase = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: UpperCamelCase = '''Normal''' if result[0][0] == 1: UpperCamelCase = '''Abnormality detected'''
334
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
1
'''simple docstring''' from dataclasses import dataclass, field from typing import Optional @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be trained."""} ) UpperCamelCase_ : Optional[str] = field( default="""./""" , metadata={"""help""": """Save dir where model repo is cloned and models updates are saved to."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot-clean-train""" , metadata={"""help""": """Name or path of training dataset."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot-clean-valid""" , metadata={"""help""": """Name or path of validation dataset."""} ) UpperCamelCase_ : Optional[int] = field(default=2 , metadata={"""help""": """Batch size for training."""} ) UpperCamelCase_ : Optional[int] = field(default=2 , metadata={"""help""": """Batch size for evaluation."""} ) UpperCamelCase_ : Optional[float] = field(default=0.1 , metadata={"""help""": """Value of weight decay."""} ) UpperCamelCase_ : Optional[int] = field( default=1_0000 , metadata={"""help""": """Size of buffer used to shuffle streaming dataset."""} ) UpperCamelCase_ : Optional[float] = field(default=2e-4 , metadata={"""help""": """Learning rate fo training."""} ) UpperCamelCase_ : Optional[str] = field(default="""cosine""" , metadata={"""help""": """Learning rate."""} ) UpperCamelCase_ : Optional[int] = field( default=750 , metadata={"""help""": """Number of warmup steps in the learning rate schedule."""} ) UpperCamelCase_ : Optional[int] = field( default=16 , metadata={"""help""": """Number of gradient accumulation steps."""} ) UpperCamelCase_ : Optional[bool] = field( default=UpperCAmelCase_ , metadata={"""help""": """Use gradient checkpointing to reduce memory footprint."""} ) UpperCamelCase_ : Optional[int] = field(default=5_0000 , metadata={"""help""": """Maximum number of training steps."""} ) UpperCamelCase_ : Optional[int] = field( default=-1 , metadata={"""help""": """Maximum number of evaluation steps. If -1 the full dataset is evaluated."""} ) UpperCamelCase_ : Optional[int] = field(default=1024 , metadata={"""help""": """Sequence lengths used for training."""} ) UpperCamelCase_ : Optional[int] = field(default=1 , metadata={"""help""": """Training seed."""} ) UpperCamelCase_ : Optional[int] = field( default=1024 , metadata={"""help""": """Interval to save checkpoints. Measured as number of forward passes not training steps."""} , ) UpperCamelCase_ : Optional[str] = field( default=UpperCAmelCase_ , metadata={"""help""": """States path if the training should continue from a checkpoint folder."""} ) UpperCamelCase_ : Optional[bool] = field(default=UpperCAmelCase_ , metadata={"""help""": """If True the data is pretokenized."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be evaluated."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot-clean-valid""" , metadata={"""help""": """Name or path of validation dataset."""} ) UpperCamelCase_ : Optional[int] = field(default=2 , metadata={"""help""": """Batch size used for evaluation."""} ) UpperCamelCase_ : Optional[int] = field( default=-1 , metadata={"""help""": """Maximum number of evaluation steps. If -1 the full dataset is evaluated."""} ) UpperCamelCase_ : Optional[int] = field(default=1024 , metadata={"""help""": """Length of sequences to be evaluated."""} ) UpperCamelCase_ : Optional[int] = field(default=1 , metadata={"""help""": """Random seed used for evaluation."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Model name or path of model to be evaluated."""} ) UpperCamelCase_ : Optional[int] = field(default=UpperCAmelCase_ , metadata={"""help""": """Number of workers used for code evaluation."""} ) UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={"""help""": """The number of human-eval tasks to run. If not included all tasks are evaluated."""} , ) UpperCamelCase_ : Optional[bool] = field( default=UpperCAmelCase_ , metadata={"""help""": """Sample from the language model's output distribution."""} ) UpperCamelCase_ : Optional[float] = field(default=0.2 , metadata={"""help""": """Sampling temperature used for generation."""} ) UpperCamelCase_ : Optional[int] = field(default=256 , metadata={"""help""": """Maximum number of newly generated tokens."""} ) UpperCamelCase_ : Optional[int] = field(default=0 , metadata={"""help""": """Top-k parameter used for generation."""} ) UpperCamelCase_ : Optional[float] = field(default=0.95 , metadata={"""help""": """Top-p parameter used for nucleus sampling."""} ) UpperCamelCase_ : Optional[int] = field(default=10 , metadata={"""help""": """Number of generations to run in parallel."""} ) UpperCamelCase_ : Optional[int] = field( default=200 , metadata={"""help""": """Number of completions to generate for each sample."""} ) UpperCamelCase_ : Optional[int] = field(default=1 , metadata={"""help""": """Random seed used for evaluation."""} ) UpperCamelCase_ : Optional[str] = field( default="""eval_results.json""" , metadata={"""help""": """Random seed used for evaluation."""} ) UpperCamelCase_ : Optional[str] = field( default="""0""" , metadata={"""help""": """Allow `code_eval` to execute Python code on machine"""} ) UpperCamelCase_ : Optional[int] = field( default=-1 , metadata={ """help""": ( """Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive""" """ number corresponds to which GPU device id to run on.""" ) } , ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[int] = field( default=UpperCAmelCase_ , metadata={ """help""": """The number of CPU cores to use for parallel preprocessing. Default uses the maximum available.""" } , ) UpperCamelCase_ : Optional[str] = field( default="""transformersbook/codeparrot""" , metadata={"""help""": """Folder or name of dataset to process."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot-clean""" , metadata={"""help""": """Folder to save processed processed dataset."""} ) UpperCamelCase_ : Optional[int] = field( default=10_0000 , metadata={"""help""": """Number of files to save per JSON output file."""} ) UpperCamelCase_ : Optional[str] = field(default="""content""" , metadata={"""help""": """Column containing text data to process."""} ) UpperCamelCase_ : Optional[float] = field( default=1000 , metadata={"""help""": """Maximum line length in file, otherwise file is filtered."""} ) UpperCamelCase_ : Optional[float] = field( default=100 , metadata={"""help""": """Maximum mean line length in file, otherwise file is filtered."""} ) UpperCamelCase_ : Optional[float] = field( default=0.25 , metadata={"""help""": """Maximum fraction of non-alphanumeric characters, otherwise file is filtered."""} ) UpperCamelCase_ : Optional[float] = field( default=1.5 , metadata={"""help""": """Minimum character token ratio for the file, otherwise file is filtered."""} ) UpperCamelCase_ : Optional[float] = field( default=0.7 , metadata={"""help""": """Probability for filtering config, test and uncommon files."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Name or path to the tokenizer."""} , ) UpperCamelCase_ : Optional[bool] = field( default=UpperCAmelCase_ , metadata={"""help""": """If True, near-duplicate samples are removed."""} ) UpperCamelCase_ : Optional[float] = field( default=0.85 , metadata={"""help""": """Jaccard threshold for near-duplicate samples."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""gpt2""" , metadata={"""help""": """Base tokenizer to build new tokenizer from."""} ) UpperCamelCase_ : Optional[str] = field( default="""transformersbook/codeparrot-train""" , metadata={"""help""": """Dataset to train tokenizer on."""} ) UpperCamelCase_ : Optional[str] = field(default="""content""" , metadata={"""help""": """Column containing text data to process."""} ) UpperCamelCase_ : Optional[int] = field(default=20_0000 , metadata={"""help""": """Number of examples to train tokenizer on."""} ) UpperCamelCase_ : Optional[int] = field( default=3_2768 , metadata={"""help""": """Number of examples to train the tokenizer on."""} ) UpperCamelCase_ : Optional[str] = field(default="""codeparrot""" , metadata={"""help""": """Name of new tokenizer."""} ) UpperCamelCase_ : Optional[bool] = field(default=UpperCAmelCase_ , metadata={"""help""": """Push saved tokenizer to the hub."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Name or path to the tokenizer."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot-clean-train""" , metadata={"""help""": """Name or path to the dataset to pretokenize."""} ) UpperCamelCase_ : Optional[str] = field( default="""tokenized-codeparrot-train""" , metadata={"""help""": """Repo name of the pretokenized data."""} ) UpperCamelCase_ : Optional[int] = field(default=UpperCAmelCase_ , metadata={"""help""": """Number of workers used for code evaluation."""} ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[str] = field( default="""gpt2-large""" , metadata={"""help""": """Configuration to use for model initialization."""} ) UpperCamelCase_ : Optional[str] = field( default="""codeparrot/codeparrot""" , metadata={"""help""": """Tokenizer attached to model."""} ) UpperCamelCase_ : Optional[str] = field(default="""codeparrot""" , metadata={"""help""": """Name of the created model."""} ) UpperCamelCase_ : Optional[bool] = field(default=UpperCAmelCase_ , metadata={"""help""": """Push saved tokenizer to the hub."""} )
334
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
1
'''simple docstring''' import numpy as np import datasets UpperCamelCase = ''' Compute the Mahalanobis Distance Mahalonobis distance is the distance between a point and a distribution. And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance. It was introduced by Prof. P. C. Mahalanobis in 1936 and has been used in various statistical applications ever since [source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/] ''' UpperCamelCase = '''\ @article{de2000mahalanobis, title={The mahalanobis distance}, author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L}, journal={Chemometrics and intelligent laboratory systems}, volume={50}, number={1}, pages={1--18}, year={2000}, publisher={Elsevier} } ''' UpperCamelCase = ''' Args: X: List of datapoints to be compared with the `reference_distribution`. reference_distribution: List of datapoints from the reference distribution we want to compare to. Returns: mahalanobis: The Mahalonobis distance for each datapoint in `X`. Examples: >>> mahalanobis_metric = datasets.load_metric("mahalanobis") >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]]) >>> print(results) {\'mahalanobis\': array([0.5])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def _snake_case ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' , id='''sequence''' ) , id='''X''' ), } ) , ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict ) -> List[Any]: '''simple docstring''' A: int = np.array(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = np.array(SCREAMING_SNAKE_CASE_ ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction A: Dict = X - np.mean(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = np.cov(reference_distribution.T ) try: A: Any = np.linalg.inv(SCREAMING_SNAKE_CASE_ ) except np.linalg.LinAlgError: A: List[Any] = np.linalg.pinv(SCREAMING_SNAKE_CASE_ ) A: Dict = np.dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = np.dot(SCREAMING_SNAKE_CASE_ , X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
334
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
1
'''simple docstring''' import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) UpperCamelCase = '''\ Text data. Second line of data.''' UpperCamelCase = '''file''' @pytest.fixture(scope='''session''' ) def SCREAMING_SNAKE_CASE( __lowercase ) -> str: A: Tuple = tmp_path_factory.mktemp('''data''' ) / (FILE_PATH + '''.zstd''') A: Union[str, Any] = bytes(__lowercase , '''utf-8''' ) with zstd.open(__lowercase , '''wb''' ) as f: f.write(__lowercase ) return path @pytest.fixture def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: with open(os.path.join(tmpfs.local_root_dir , __lowercase ) , '''w''' ) as f: f.write(__lowercase ) return FILE_PATH @pytest.mark.parametrize('''compression_format''' , ['''gzip''', '''xz''', '''zstd'''] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: A: Union[str, Any] = {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path} A: Tuple = input_paths[compression_format] A: Optional[Any] = tmp_path / '''cache''' A: Any = DownloadConfig(cache_dir=__lowercase , extract_compressed_file=__lowercase ) A: Any = cached_path(__lowercase , download_config=__lowercase ) with open(__lowercase ) as f: A: Optional[int] = f.read() with open(__lowercase ) as f: A: int = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize('''default_extracted''' , [True, False] ) @pytest.mark.parametrize('''default_cache_dir''' , [True, False] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: Optional[Any] = '''custom_cache''' A: List[Any] = '''custom_extracted_dir''' A: Optional[int] = tmp_path / '''custom_extracted_path''' if default_extracted: A: str = ('''downloads''' if default_cache_dir else custom_cache_dir, '''extracted''') else: monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_DIR''' , __lowercase ) monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' , str(__lowercase ) ) A: List[str] = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) A: Tuple = xz_file A: str = ( DownloadConfig(extract_compressed_file=__lowercase ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir , extract_compressed_file=__lowercase ) ) A: Optional[Any] = cached_path(__lowercase , download_config=__lowercase ) assert Path(__lowercase ).parent.parts[-2:] == expected def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: # absolute path A: List[Any] = str(Path(__lowercase ).resolve() ) assert cached_path(__lowercase ) == text_file # relative path A: Dict = str(Path(__lowercase ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(__lowercase ) == text_file def SCREAMING_SNAKE_CASE( __lowercase ) -> str: # absolute path A: Any = str(tmp_path.resolve() / '''__missing_file__.txt''' ) with pytest.raises(__lowercase ): cached_path(__lowercase ) # relative path A: List[str] = '''./__missing_file__.txt''' with pytest.raises(__lowercase ): cached_path(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Union[str, Any]: A: Dict = get_from_cache(F"""tmp://{tmpfs_file}""" ) with open(__lowercase ) as f: A: List[Any] = f.read() assert output_file_content == FILE_CONTENT @patch('''datasets.config.HF_DATASETS_OFFLINE''' , __lowercase ) def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: with pytest.raises(__lowercase ): cached_path('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: A: Dict = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(__lowercase ): http_get('''https://huggingface.co''' , temp_file=__lowercase ) with pytest.raises(__lowercase ): http_head('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> int: A: Union[str, Any] = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(__lowercase ): ftp_get('''ftp://huggingface.co''' , temp_file=__lowercase ) with pytest.raises(__lowercase ): ftp_head('''ftp://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(__lowercase ): fsspec_get('''s3://huggingface.co''' , temp_file=__lowercase ) with pytest.raises(__lowercase ): fsspec_head('''s3://huggingface.co''' )
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase = True ) -> int: print(F"""Converting {name}...""" ) with torch.no_grad(): if hidden_sizes == 1_2_8: if name[-1] == "S": A: Tuple = timm.create_model('''levit_128s''' , pretrained=__lowercase ) else: A: Optional[int] = timm.create_model('''levit_128''' , pretrained=__lowercase ) if hidden_sizes == 1_9_2: A: List[str] = timm.create_model('''levit_192''' , pretrained=__lowercase ) if hidden_sizes == 2_5_6: A: Optional[Any] = timm.create_model('''levit_256''' , pretrained=__lowercase ) if hidden_sizes == 3_8_4: A: Union[str, Any] = timm.create_model('''levit_384''' , pretrained=__lowercase ) from_model.eval() A: int = LevitForImageClassificationWithTeacher(__lowercase ).eval() A: List[str] = OrderedDict() A: Optional[Any] = from_model.state_dict() A: List[Any] = list(from_model.state_dict().keys() ) A: Any = list(our_model.state_dict().keys() ) print(len(__lowercase ) , len(__lowercase ) ) for i in range(len(__lowercase ) ): A: str = weights[og_keys[i]] our_model.load_state_dict(__lowercase ) A: List[str] = torch.randn((2, 3, 2_2_4, 2_2_4) ) A: List[Any] = from_model(__lowercase ) A: List[Any] = our_model(__lowercase ).logits assert torch.allclose(__lowercase , __lowercase ), "The model logits don't match the original one." A: Dict = name print(__lowercase ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) A: Dict = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> Dict: A: str = '''imagenet-1k-id2label.json''' A: Optional[Any] = 1_0_0_0 A: Tuple = (1, num_labels) A: List[str] = '''huggingface/label-files''' A: Optional[int] = num_labels A: Union[str, Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Union[str, Any] = {int(__lowercase ): v for k, v in idalabel.items()} A: Optional[Any] = idalabel A: Optional[int] = {v: k for k, v in idalabel.items()} A: Optional[Any] = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Optional[Any] = { '''levit-128S''': 1_2_8, '''levit-128''': 1_2_8, '''levit-192''': 1_9_2, '''levit-256''': 2_5_6, '''levit-384''': 3_8_4, } A: Union[str, Any] = { '''levit-128S''': ImageNetPreTrainedConfig( hidden_sizes=[1_2_8, 2_5_6, 3_8_4] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[1_6, 1_6, 1_6] , drop_path_rate=0 , ), '''levit-128''': ImageNetPreTrainedConfig( hidden_sizes=[1_2_8, 2_5_6, 3_8_4] , num_attention_heads=[4, 8, 1_2] , depths=[4, 4, 4] , key_dim=[1_6, 1_6, 1_6] , drop_path_rate=0 , ), '''levit-192''': ImageNetPreTrainedConfig( hidden_sizes=[1_9_2, 2_8_8, 3_8_4] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[3_2, 3_2, 3_2] , drop_path_rate=0 , ), '''levit-256''': ImageNetPreTrainedConfig( hidden_sizes=[2_5_6, 3_8_4, 5_1_2] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[3_2, 3_2, 3_2] , drop_path_rate=0 , ), '''levit-384''': ImageNetPreTrainedConfig( hidden_sizes=[3_8_4, 5_1_2, 7_6_8] , num_attention_heads=[6, 9, 1_2] , depths=[4, 4, 4] , key_dim=[3_2, 3_2, 3_2] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , __lowercase , names_to_config[model_name] , __lowercase , __lowercase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , __lowercase , __lowercase , __lowercase , __lowercase ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help='''The name of the model you wish to convert, it must be one of the supported Levit* architecture,''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''levit-dump-folder/''', type=Path, required=False, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for data in source_data: for i, el in enumerate(__lowercase ): if len(__lowercase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__lowercase ) ) return data_lists def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for dlist, weight in zip(__lowercase , __lowercase ): A: List[str] = min(__lowercase ) A: Union[str, Any] = max(__lowercase ) A: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A: List[str] = F"""Invalid weight of {weight:f} provided""" raise ValueError(__lowercase ) score_lists.append(__lowercase ) return score_lists def SCREAMING_SNAKE_CASE( __lowercase ) -> list[float]: A: list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__lowercase ): A: str = final_scores[j] + ele return final_scores def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: Any = get_data(__lowercase ) A: str = calculate_each_score(__lowercase , __lowercase ) A: int = generate_final_scores(__lowercase ) # append scores to source data for i, ele in enumerate(__lowercase ): source_data[i].append(__lowercase ) return source_data
334
1
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
1
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : str ) -> None: '''simple docstring''' A: dict[str, TrieNode] = {} # Mapping from char to TrieNode A: Optional[int] = False def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : list[str] ) -> None: '''simple docstring''' for word in words: self.insert(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> None: '''simple docstring''' A: Optional[Any] = self for char in word: if char not in curr.nodes: A: Dict = TrieNode() A: Any = curr.nodes[char] A: Any = True def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str ) -> bool: '''simple docstring''' A: List[Any] = self for char in word: if char not in curr.nodes: return False A: Optional[int] = curr.nodes[char] return curr.is_leaf def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : str ) -> None: '''simple docstring''' def _delete(SCREAMING_SNAKE_CASE_ : TrieNode , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int ) -> bool: if index == len(SCREAMING_SNAKE_CASE_ ): # If word does not exist if not curr.is_leaf: return False A: Tuple = False return len(curr.nodes ) == 0 A: str = word[index] A: str = curr.nodes.get(SCREAMING_SNAKE_CASE_ ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted A: List[Any] = _delete(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , SCREAMING_SNAKE_CASE_ , 0 ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> None: if node.is_leaf: print(__lowercase , end=''' ''' ) for key, value in node.nodes.items(): print_words(__lowercase , word + key ) def SCREAMING_SNAKE_CASE( ) -> bool: A: int = '''banana bananas bandana band apple all beast'''.split() A: List[Any] = TrieNode() root.insert_many(__lowercase ) # print_words(root, "") assert all(root.find(__lowercase ) 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( __lowercase , __lowercase ) -> None: print(str(__lowercase ) , '''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()
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''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 UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' from __future__ import annotations import unittest from transformers import RoFormerConfig, 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 import ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any=13 , SCREAMING_SNAKE_CASE_ : Optional[Any]=7 , SCREAMING_SNAKE_CASE_ : List[str]=True , SCREAMING_SNAKE_CASE_ : Optional[Any]=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : List[Any]=99 , SCREAMING_SNAKE_CASE_ : Any=32 , SCREAMING_SNAKE_CASE_ : Any=2 , SCREAMING_SNAKE_CASE_ : List[Any]=4 , SCREAMING_SNAKE_CASE_ : Any=37 , SCREAMING_SNAKE_CASE_ : List[str]="gelu" , SCREAMING_SNAKE_CASE_ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : str=5_12 , SCREAMING_SNAKE_CASE_ : List[Any]=16 , SCREAMING_SNAKE_CASE_ : Tuple=2 , SCREAMING_SNAKE_CASE_ : List[str]=0.02 , SCREAMING_SNAKE_CASE_ : Tuple=3 , SCREAMING_SNAKE_CASE_ : Tuple=4 , SCREAMING_SNAKE_CASE_ : Tuple=None , ) -> List[str]: '''simple docstring''' A: Dict = parent A: List[Any] = 13 A: int = 7 A: Tuple = True A: Dict = True A: Any = True A: Union[str, Any] = True A: List[Any] = 99 A: List[Any] = 32 A: Optional[int] = 2 A: Optional[Any] = 4 A: List[str] = 37 A: Optional[Any] = '''gelu''' A: Optional[Any] = 0.1 A: str = 0.1 A: List[Any] = 5_12 A: List[str] = 16 A: str = 2 A: Union[str, Any] = 0.02 A: Dict = 3 A: Optional[int] = 4 A: Dict = None def _snake_case ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' A: List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A: List[str] = None if self.use_input_mask: A: Tuple = random_attention_mask([self.batch_size, self.seq_length] ) A: Tuple = None if self.use_token_type_ids: A: Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A: Optional[int] = None A: Any = None A: List[str] = None if self.use_labels: A: Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: str = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A: Dict = ids_tensor([self.batch_size] , self.num_choices ) A: Optional[Any] = RoFormerConfig( 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 , initializer_range=self.initializer_range , return_dict=SCREAMING_SNAKE_CASE_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str ) -> Dict: '''simple docstring''' A: Optional[Any] = TFRoFormerModel(config=SCREAMING_SNAKE_CASE_ ) A: Any = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} A: List[str] = [input_ids, input_mask] A: List[str] = model(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[Any]: '''simple docstring''' A: Optional[Any] = True A: List[str] = TFRoFormerForCausalLM(config=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } A: Optional[Any] = model(SCREAMING_SNAKE_CASE_ )['''logits'''] self.parent.assertListEqual( list(prediction_scores.numpy().shape ) , [self.batch_size, self.seq_length, self.vocab_size] ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int ) -> Tuple: '''simple docstring''' A: str = TFRoFormerForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) A: List[Any] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } A: Optional[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> List[str]: '''simple docstring''' A: Any = self.num_labels A: List[str] = TFRoFormerForSequenceClassification(config=SCREAMING_SNAKE_CASE_ ) A: Dict = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } A: int = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any ) -> int: '''simple docstring''' A: Any = self.num_choices A: Dict = TFRoFormerForMultipleChoice(config=SCREAMING_SNAKE_CASE_ ) A: List[str] = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1 ) , (1, self.num_choices, 1) ) A: Optional[int] = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1 ) , (1, self.num_choices, 1) ) A: Any = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_ , 1 ) , (1, self.num_choices, 1) ) A: Dict = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } A: Optional[int] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.num_labels A: Dict = TFRoFormerForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) A: int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } A: int = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' A: Tuple = TFRoFormerForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } A: List[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _snake_case ( self : str ) -> List[str]: '''simple docstring''' A: List[Any] = self.prepare_config_and_inputs() ( ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ( A ) , ): int = config_and_inputs A: List[str] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) UpperCamelCase_ : Union[str, Any] = ( { """feature-extraction""": TFRoFormerModel, """fill-mask""": TFRoFormerForMaskedLM, """question-answering""": TFRoFormerForQuestionAnswering, """text-classification""": TFRoFormerForSequenceClassification, """text-generation""": TFRoFormerForCausalLM, """token-classification""": TFRoFormerForTokenClassification, """zero-shot""": TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase_ : List[Any] = False UpperCamelCase_ : Optional[int] = False def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' A: Tuple = TFRoFormerModelTester(self ) A: Dict = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def _snake_case ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def _snake_case ( self : List[Any] ) -> List[Any]: '''simple docstring''' A: str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' A: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[int] ) -> List[str]: '''simple docstring''' A: str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Optional[int] ) -> Any: '''simple docstring''' A: str = TFRoFormerModel.from_pretrained('''junnyu/roformer_chinese_base''' ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: str = TFRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base''' ) A: Dict = tf.constant([[0, 1, 2, 3, 4, 5]] ) A: Dict = model(SCREAMING_SNAKE_CASE_ )[0] # TODO Replace vocab size A: List[Any] = 5_00_00 A: List[str] = [1, 6, vocab_size] self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) print(output[:, :3, :3] ) # TODO Replace values below with what was printed above. A: List[str] = tf.constant( [ [ [-0.1205_3341, -1.026_4901, 0.2922_1946], [-1.513_3783, 0.19_7433, 0.1519_0607], [-5.013_5403, -3.90_0256, -0.8403_8764], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : str = 1e-4 def _snake_case ( self : str ) -> List[Any]: '''simple docstring''' A: int = tf.constant([[4, 10]] ) A: List[Any] = TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6 ) A: Optional[int] = emba(input_ids.shape ) A: Optional[Any] = tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]] ) tf.debugging.assert_near(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=self.tolerance ) def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: Any = tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ] ) A: List[Any] = TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12 , embedding_dim=5_12 ) emba([2, 16, 5_12] ) A: Tuple = emba.weight[:3, :5] tf.debugging.assert_near(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=self.tolerance ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : int = 1e-4 def _snake_case ( self : List[Any] ) -> Tuple: '''simple docstring''' A: Union[str, Any] = tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 A: Optional[Any] = -tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa ) , shape=(2, 12, 16, 64) ) / 1_00 A: str = TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64 ) A: Dict = embed_positions([2, 16, 7_68] )[None, None, :, :] A , A: Any = TFRoFormerSelfAttention.apply_rotary_position_embeddings( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ] ) A: List[str] = tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ] ) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , SCREAMING_SNAKE_CASE_ , atol=self.tolerance ) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , SCREAMING_SNAKE_CASE_ , atol=self.tolerance )
334
'''simple docstring''' from __future__ import annotations from typing import Any class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' pass class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> None: '''simple docstring''' A: Any = data A: Node | None = None def __iter__( self : Optional[int] ) -> List[str]: '''simple docstring''' A: List[str] = self A: Dict = [] while node: if node in visited: raise ContainsLoopError visited.append(SCREAMING_SNAKE_CASE_ ) yield node.data A: str = node.next_node @property def _snake_case ( self : List[str] ) -> bool: '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCamelCase = Node(1) UpperCamelCase = Node(2) UpperCamelCase = Node(3) UpperCamelCase = Node(4) print(root_node.has_loop) # False UpperCamelCase = root_node.next_node print(root_node.has_loop) # True UpperCamelCase = Node(5) UpperCamelCase = Node(6) UpperCamelCase = Node(5) UpperCamelCase = Node(6) print(root_node.has_loop) # False UpperCamelCase = Node(1) print(root_node.has_loop) # False
334
1
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> int: A: Tuple = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ('''pre_logits.fc.weight''', '''pooler.dense.weight'''), ('''pre_logits.fc.bias''', '''pooler.dense.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" A: int = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=False ) -> Dict: for i in range(config.num_hidden_layers ): if base_model: A: str = '''''' else: A: Union[str, Any] = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A: Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) A: Any = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A: str = in_proj_weight[ : config.hidden_size, : ] A: List[str] = in_proj_bias[: config.hidden_size] A: List[str] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A: Optional[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A: Union[str, Any] = in_proj_weight[ -config.hidden_size :, : ] A: List[Any] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: Tuple = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Dict: A: int = dct.pop(__lowercase ) A: List[str] = val def SCREAMING_SNAKE_CASE( ) -> str: A: Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A: List[str] = Image.open(requests.get(__lowercase , stream=__lowercase ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Tuple: A: Optional[int] = ViTConfig() A: Union[str, Any] = False # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size if vit_name[-5:] == "in21k": A: Optional[int] = True A: Dict = int(vit_name[-1_2:-1_0] ) A: int = int(vit_name[-9:-6] ) else: A: int = 1_0_0_0 A: Union[str, Any] = '''huggingface/label-files''' A: List[str] = '''imagenet-1k-id2label.json''' A: Optional[Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Any = {int(__lowercase ): v for k, v in idalabel.items()} A: Union[str, Any] = idalabel A: str = {v: k for k, v in idalabel.items()} A: str = int(vit_name[-6:-4] ) A: Optional[int] = int(vit_name[-3:] ) # size of the architecture if "deit" in vit_name: if vit_name[9:].startswith('''tiny''' ): A: Any = 1_9_2 A: List[Any] = 7_6_8 A: List[str] = 1_2 A: Union[str, Any] = 3 elif vit_name[9:].startswith('''small''' ): A: str = 3_8_4 A: int = 1_5_3_6 A: int = 1_2 A: Any = 6 else: pass else: if vit_name[4:].startswith('''small''' ): A: Any = 7_6_8 A: List[Any] = 2_3_0_4 A: Any = 8 A: Tuple = 8 elif vit_name[4:].startswith('''base''' ): pass elif vit_name[4:].startswith('''large''' ): A: List[Any] = 1_0_2_4 A: Any = 4_0_9_6 A: Optional[int] = 2_4 A: Any = 1_6 elif vit_name[4:].startswith('''huge''' ): A: List[str] = 1_2_8_0 A: str = 5_1_2_0 A: Optional[int] = 3_2 A: int = 1_6 # load original model from timm A: List[str] = timm.create_model(__lowercase , pretrained=__lowercase ) timm_model.eval() # load state_dict of original model, remove and rename some keys A: Any = timm_model.state_dict() if base_model: remove_classification_head_(__lowercase ) A: Optional[Any] = create_rename_keys(__lowercase , __lowercase ) for src, dest in rename_keys: rename_key(__lowercase , __lowercase , __lowercase ) read_in_q_k_v(__lowercase , __lowercase , __lowercase ) # load HuggingFace model if vit_name[-5:] == "in21k": A: Optional[int] = ViTModel(__lowercase ).eval() else: A: Any = ViTForImageClassification(__lowercase ).eval() model.load_state_dict(__lowercase ) # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor if "deit" in vit_name: A: int = DeiTImageProcessor(size=config.image_size ) else: A: List[Any] = ViTImageProcessor(size=config.image_size ) A: str = image_processor(images=prepare_img() , return_tensors='''pt''' ) A: Optional[int] = encoding['''pixel_values'''] A: Dict = model(__lowercase ) if base_model: A: Dict = timm_model.forward_features(__lowercase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(__lowercase , outputs.pooler_output , atol=1E-3 ) else: A: Tuple = timm_model(__lowercase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__lowercase , outputs.logits , atol=1E-3 ) Path(__lowercase ).mkdir(exist_ok=__lowercase ) print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowercase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--vit_name''', default='''vit_base_patch16_224''', type=str, help='''Name of the ViT timm 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.''' ) UpperCamelCase = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)
334
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase = 4 ) -> list[list[int]]: A: Tuple = abs(__lowercase ) or 4 return [[1 + x + y * row_size for x in range(__lowercase )] for y in range(__lowercase )] def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(transpose(__lowercase ) ) # OR.. transpose(reverse_column(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(reverse_column(__lowercase ) ) # OR.. reverse_column(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_column(transpose(__lowercase ) ) # OR.. transpose(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Union[str, Any] = [list(__lowercase ) for x in zip(*__lowercase )] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[int] = matrix[::-1] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[Any] = [x[::-1] for x in matrix] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> None: for i in matrix: print(*__lowercase ) if __name__ == "__main__": UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
334
1
'''simple docstring''' import numpy as np import torch import tqdm from ...models.unet_ad import UNetaDModel from ...pipelines import DiffusionPipeline from ...utils import randn_tensor from ...utils.dummy_pt_objects import DDPMScheduler class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : UNetaDModel , SCREAMING_SNAKE_CASE_ : UNetaDModel , SCREAMING_SNAKE_CASE_ : DDPMScheduler , SCREAMING_SNAKE_CASE_ : Dict , ) -> Tuple: '''simple docstring''' super().__init__() A: int = value_function A: int = unet A: Optional[Any] = scheduler A: Union[str, Any] = env A: Union[str, Any] = env.get_dataset() A: Tuple = {} for key in self.data.keys(): try: A: List[str] = self.data[key].mean() except: # noqa: E722 pass A: Tuple = {} for key in self.data.keys(): try: A: List[str] = self.data[key].std() except: # noqa: E722 pass A: Optional[int] = env.observation_space.shape[0] A: Any = env.action_space.shape[0] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Optional[int]: '''simple docstring''' return (x_in - self.means[key]) / self.stds[key] def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' return x_in * self.stds[key] + self.means[key] def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: '''simple docstring''' if type(SCREAMING_SNAKE_CASE_ ) is dict: return {k: self.to_torch(SCREAMING_SNAKE_CASE_ ) for k, v in x_in.items()} elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ): return x_in.to(self.unet.device ) return torch.tensor(SCREAMING_SNAKE_CASE_ , device=self.unet.device ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[Any]: '''simple docstring''' for key, val in cond.items(): A: List[Any] = val.clone() return x_in def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> str: '''simple docstring''' A: str = x.shape[0] A: List[Any] = None for i in tqdm.tqdm(self.scheduler.timesteps ): # create batch of timesteps to pass into model A: int = torch.full((batch_size,) , SCREAMING_SNAKE_CASE_ , device=self.unet.device , dtype=torch.long ) for _ in range(SCREAMING_SNAKE_CASE_ ): with torch.enable_grad(): x.requires_grad_() # permute to match dimension for pre-trained models A: List[str] = self.value_function(x.permute(0 , 2 , 1 ) , SCREAMING_SNAKE_CASE_ ).sample A: Optional[Any] = torch.autograd.grad([y.sum()] , [x] )[0] A: str = self.scheduler._get_variance(SCREAMING_SNAKE_CASE_ ) A: int = torch.exp(0.5 * posterior_variance ) A: Dict = model_std * grad A: List[Any] = 0 A: List[str] = x.detach() A: Dict = x + scale * grad A: Dict = self.reset_xa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.action_dim ) A: Optional[Any] = self.unet(x.permute(0 , 2 , 1 ) , SCREAMING_SNAKE_CASE_ ).sample.permute(0 , 2 , 1 ) # TODO: verify deprecation of this kwarg A: str = self.scheduler.step(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , predict_epsilon=SCREAMING_SNAKE_CASE_ )['''prev_sample'''] # apply conditions to the trajectory (set the initial state) A: List[str] = self.reset_xa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.action_dim ) A: List[str] = self.to_torch(SCREAMING_SNAKE_CASE_ ) return x, y def __call__( self : int , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int=64 , SCREAMING_SNAKE_CASE_ : Optional[Any]=32 , SCREAMING_SNAKE_CASE_ : Any=2 , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 ) -> List[Any]: '''simple docstring''' A: Dict = self.normalize(SCREAMING_SNAKE_CASE_ , '''observations''' ) A: Tuple = obs[None].repeat(SCREAMING_SNAKE_CASE_ , axis=0 ) A: int = {0: self.to_torch(SCREAMING_SNAKE_CASE_ )} A: List[str] = (batch_size, planning_horizon, self.state_dim + self.action_dim) # generate initial noise and apply our conditions (to make the trajectories start at current state) A: Optional[int] = randn_tensor(SCREAMING_SNAKE_CASE_ , device=self.unet.device ) A: str = self.reset_xa(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.action_dim ) A: int = self.to_torch(SCREAMING_SNAKE_CASE_ ) # run the diffusion process A , A: List[str] = self.run_diffusion(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # sort output trajectories by value A: str = y.argsort(0 , descending=SCREAMING_SNAKE_CASE_ ).squeeze() A: List[str] = x[sorted_idx] A: str = sorted_values[:, :, : self.action_dim] A: Any = actions.detach().cpu().numpy() A: List[str] = self.de_normalize(SCREAMING_SNAKE_CASE_ , key='''actions''' ) # select the action with the highest value if y is not None: A: Union[str, Any] = 0 else: # if we didn't run value guiding, select a random action A: List[Any] = np.random.randint(0 , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = denorm_actions[selected_index, 0] return denorm_actions
334
'''simple docstring''' from __future__ import annotations import numpy as np def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
334
1
'''simple docstring''' import re from flax.core.frozen_dict import freeze from flax.traverse_util import flatten_dict, unflatten_dict from jax.experimental import PartitionSpec as P # Sentinels UpperCamelCase = object() # For specifying empty leaf dict `{}` UpperCamelCase = object() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Tuple: A: Optional[int] = tuple((re.compile(x + '''$''' ) for x in qs) ) for i in range(len(__lowercase ) - len(__lowercase ) + 1 ): A: int = [x.match(__lowercase ) for x, y in zip(__lowercase , ks[i:] )] if matches and all(__lowercase ): return True return False def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: def replace(__lowercase , __lowercase ): for rule, replacement in rules: if _match(__lowercase , __lowercase ): return replacement return val return replace def SCREAMING_SNAKE_CASE( ) -> Optional[Any]: return [ # embeddings (("transformer", "wpe", "embedding"), P('''mp''' , __lowercase )), (("transformer", "wte", "embedding"), P('''mp''' , __lowercase )), # atention (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__lowercase , '''mp''' )), (("attention", "out_proj", "kernel"), P('''mp''' , __lowercase )), (("attention", "out_proj", "bias"), None), # mlp (("mlp", "c_fc", "kernel"), P(__lowercase , '''mp''' )), (("mlp", "c_fc", "bias"), P('''mp''' )), (("mlp", "c_proj", "kernel"), P('''mp''' , __lowercase )), (("mlp", "c_proj", "bias"), None), # layer norms ((r"ln_\d+", "bias"), None), ((r"\d+", r"ln_\d+", "scale"), None), (("ln_f", "bias"), None), (("ln_f", "scale"), None), ] def SCREAMING_SNAKE_CASE( __lowercase ) -> List[str]: A: Optional[Any] = _get_partition_rules() A: List[str] = _replacement_rules(__lowercase ) A: Any = {k: _unmatched for k in flatten_dict(__lowercase )} A: Optional[Any] = {k: replace(__lowercase , __lowercase ) for k, v in initd.items()} assert _unmatched not in result.values(), "Incomplete partition spec." return freeze(unflatten_dict(__lowercase ) )
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' import warnings from ..trainer import Trainer from ..utils import logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int]=None , **SCREAMING_SNAKE_CASE_ : Any ) -> Tuple: '''simple docstring''' warnings.warn( '''`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ''' '''instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(args=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
334
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
334
1
'''simple docstring''' import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params UpperCamelCase = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['''memory_attention''', '''encoder_attn'''], ['''attention''', '''attn'''], ['''/''', '''.'''], ['''.LayerNorm.gamma''', '''_layer_norm.weight'''], ['''.LayerNorm.beta''', '''_layer_norm.bias'''], ['''r.layer_''', '''r.layers.'''], ['''output_proj''', '''out_proj'''], ['''ffn.dense_1.''', '''fc2.'''], ['''ffn.dense.''', '''fc1.'''], ['''ffn_layer_norm''', '''final_layer_norm'''], ['''kernel''', '''weight'''], ['''encoder_layer_norm.''', '''encoder.layer_norm.'''], ['''decoder_layer_norm.''', '''decoder.layer_norm.'''], ['''embeddings.weights''', '''shared.weight'''], ] def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[int]: for pegasus_name, hf_name in PATTERNS: A: Dict = k.replace(__lowercase , __lowercase ) return k def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> PegasusForConditionalGeneration: A: int = DEFAULTS.copy() cfg_kwargs.update(__lowercase ) A: Union[str, Any] = PegasusConfig(**__lowercase ) A: Tuple = PegasusForConditionalGeneration(__lowercase ) A: List[Any] = torch_model.model.state_dict() A: List[Any] = {} for k, v in tf_weights.items(): A: Tuple = rename_state_dict_key(__lowercase ) if new_k not in sd: raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: A: Tuple = v.T A: Dict = torch.tensor(__lowercase , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, F"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected A: Dict = torch.zeros_like(mapping['''shared.weight'''][cfg.pad_token_id + 1] ) A: List[str] = mapping['''shared.weight'''] A: Optional[int] = mapping['''shared.weight'''] A: Optional[Any] = {k: torch.zeros_like(__lowercase ) for k, v in sd.items() if k.endswith('''bias''' ) and k not in mapping} mapping.update(**__lowercase ) A , A: str = torch_model.model.load_state_dict(__lowercase , strict=__lowercase ) A: List[str] = [ k for k in missing if k not in ['''encoder.embed_positions.weight''', '''decoder.embed_positions.weight'''] ] assert unexpected_missing == [], F"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], F"""no matches found for the following tf keys {extra}""" return torch_model def SCREAMING_SNAKE_CASE( __lowercase="./ckpt/aeslc/model.ckpt-32000" ) -> Dict: A: List[str] = tf.train.list_variables(__lowercase ) A: int = {} A: Union[str, Any] = ['''Adafactor''', '''global_step'''] for name, shape in tqdm(__lowercase , desc='''converting tf checkpoint to dict''' ): A: Dict = any(pat in name for pat in ignore_name ) if skip_key: continue A: Any = tf.train.load_variable(__lowercase , __lowercase ) A: Optional[int] = array return tf_weights def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: # save tokenizer first A: int = Path(__lowercase ).parent.name A: Any = task_specific_params[F"""summarization_{dataset}"""]['''max_position_embeddings'''] A: Tuple = PegasusTokenizer.from_pretrained('''sshleifer/pegasus''' , model_max_length=__lowercase ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(__lowercase ) # convert model A: Optional[int] = get_tf_weights_as_numpy(__lowercase ) A: str = task_specific_params[F"""summarization_{dataset}"""] if dataset == "large": A: Union[str, Any] = task_specific_params A: Optional[int] = convert_pegasus(__lowercase , __lowercase ) torch_model.save_pretrained(__lowercase ) A: Union[str, Any] = torch_model.state_dict() sd.pop('''model.decoder.embed_positions.weight''' ) sd.pop('''model.encoder.embed_positions.weight''' ) torch.save(__lowercase , Path(__lowercase ) / '''pytorch_model.bin''' ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''tf_ckpt_path''', type=str, help='''passed to tf.train.list_variables''') parser.add_argument('''save_dir''', default=None, type=str, help='''Path to the output PyTorch model.''') UpperCamelCase = parser.parse_args() if args.save_dir is None: UpperCamelCase = Path(args.tf_ckpt_path).parent.name UpperCamelCase = os.path.join('''pegasus''', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
334
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 6_55_36 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : str = "fourier" , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , SCREAMING_SNAKE_CASE_ : Tuple[str] = "UNetMidBlock1D" , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : Tuple[int] = (32, 32, 64) , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[Any] = sample_size # time if time_embedding_type == "fourier": A: Tuple = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=SCREAMING_SNAKE_CASE_ , log=SCREAMING_SNAKE_CASE_ , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ ) A: List[str] = 2 * block_out_channels[0] elif time_embedding_type == "positional": A: str = Timesteps( block_out_channels[0] , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ , downscale_freq_shift=SCREAMING_SNAKE_CASE_ ) A: Any = block_out_channels[0] if use_timestep_embedding: A: Optional[Any] = block_out_channels[0] * 4 A: List[Any] = TimestepEmbedding( in_channels=SCREAMING_SNAKE_CASE_ , time_embed_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , out_dim=block_out_channels[0] , ) A: Optional[Any] = nn.ModuleList([] ) A: str = None A: str = nn.ModuleList([] ) A: Tuple = None # down A: Any = in_channels for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = output_channel A: List[Any] = block_out_channels[i] if i == 0: input_channel += extra_in_channels A: List[Any] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[int] = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid A: Union[str, Any] = get_mid_block( SCREAMING_SNAKE_CASE_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=SCREAMING_SNAKE_CASE_ , add_downsample=SCREAMING_SNAKE_CASE_ , ) # up A: Optional[Any] = list(reversed(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = reversed_block_out_channels[0] if out_block_type is None: A: int = out_channels else: A: Union[str, Any] = block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: List[Any] = output_channel A: int = ( reversed_block_out_channels[i + 1] if i < len(SCREAMING_SNAKE_CASE_ ) - 1 else final_upsample_channels ) A: Optional[int] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[Any] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) A: Any = output_channel # out A: List[str] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) A: Optional[int] = get_out_block( out_block_type=SCREAMING_SNAKE_CASE_ , num_groups_out=SCREAMING_SNAKE_CASE_ , embed_dim=block_out_channels[0] , out_channels=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , fc_dim=block_out_channels[-1] // 4 , ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[UNetaDOutput, Tuple]: '''simple docstring''' A: Any = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: List[str] = timesteps[None].to(sample.device ) A: int = self.time_proj(SCREAMING_SNAKE_CASE_ ) if self.config.use_timestep_embedding: A: List[Any] = self.time_mlp(SCREAMING_SNAKE_CASE_ ) else: A: str = timestep_embed[..., None] A: Union[str, Any] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) A: Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down A: List[str] = () for downsample_block in self.down_blocks: A , A: Optional[int] = downsample_block(hidden_states=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: A: Dict = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): A: List[Any] = down_block_res_samples[-1:] A: List[str] = down_block_res_samples[:-1] A: Optional[int] = upsample_block(SCREAMING_SNAKE_CASE_ , res_hidden_states_tuple=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) # 5. post-process if self.out_block: A: Any = self.out_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 UpperCamelCase = get_tests_dir('''fixtures''') class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : str ) -> Tuple: '''simple docstring''' A: str = mock.Mock() A: Tuple = 5_00 A: Optional[Any] = {} A: Tuple = HTTPError A: int = {} # Download this model to make sure it's in the cache. A: Dict = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=SCREAMING_SNAKE_CASE_ ) as mock_head: A: List[Any] = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # This check we did call the fake head request mock_head.assert_called() def _snake_case ( self : Union[str, Any] ) -> str: '''simple docstring''' A: Dict = WavaVecaFeatureExtractor.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json''' ) @is_staging_test class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @classmethod def _snake_case ( cls : List[str] ) -> Any: '''simple docstring''' A: Any = TOKEN HfFolder.save_token(SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : List[str] ) -> List[str]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-feature-extractor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-feature-extractor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-feature-extractor''' ) except HTTPError: pass def _snake_case ( self : Any ) -> Any: '''simple docstring''' A: Optional[Any] = WavaVecaFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) feature_extractor.push_to_hub('''test-feature-extractor''' , use_auth_token=self._token ) A: Tuple = WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( SCREAMING_SNAKE_CASE_ , repo_id='''test-feature-extractor''' , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) A: int = WavaVecaFeatureExtractor.from_pretrained(f"""{USER}/test-feature-extractor""" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Optional[Any] ) -> List[str]: '''simple docstring''' A: Dict = WavaVecaFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) feature_extractor.push_to_hub('''valid_org/test-feature-extractor''' , use_auth_token=self._token ) A: Tuple = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( SCREAMING_SNAKE_CASE_ , repo_id='''valid_org/test-feature-extractor-org''' , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) A: Tuple = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(SCREAMING_SNAKE_CASE_ , getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' CustomFeatureExtractor.register_for_auto_class() A: Optional[int] = CustomFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) feature_extractor.push_to_hub('''test-dynamic-feature-extractor''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''} , ) A: Tuple = AutoFeatureExtractor.from_pretrained( f"""{USER}/test-dynamic-feature-extractor""" , trust_remote_code=SCREAMING_SNAKE_CASE_ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , '''CustomFeatureExtractor''' )
334
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> None: '''simple docstring''' warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase = { '''configuration_data2vec_audio''': ['''DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecAudioConfig'''], '''configuration_data2vec_text''': [ '''DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecTextConfig''', '''Data2VecTextOnnxConfig''', ], '''configuration_data2vec_vision''': [ '''DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecVisionConfig''', '''Data2VecVisionOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecAudioForAudioFrameClassification''', '''Data2VecAudioForCTC''', '''Data2VecAudioForSequenceClassification''', '''Data2VecAudioForXVector''', '''Data2VecAudioModel''', '''Data2VecAudioPreTrainedModel''', ] UpperCamelCase = [ '''DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecTextForCausalLM''', '''Data2VecTextForMaskedLM''', '''Data2VecTextForMultipleChoice''', '''Data2VecTextForQuestionAnswering''', '''Data2VecTextForSequenceClassification''', '''Data2VecTextForTokenClassification''', '''Data2VecTextModel''', '''Data2VecTextPreTrainedModel''', ] UpperCamelCase = [ '''DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Data2VecVisionForImageClassification''', '''Data2VecVisionForMaskedImageModeling''', '''Data2VecVisionForSemanticSegmentation''', '''Data2VecVisionModel''', '''Data2VecVisionPreTrainedModel''', ] if is_tf_available(): UpperCamelCase = [ '''TFData2VecVisionForImageClassification''', '''TFData2VecVisionForSemanticSegmentation''', '''TFData2VecVisionModel''', '''TFData2VecVisionPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
'''simple docstring''' from collections import deque class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> None: '''simple docstring''' A: Union[str, Any] = process_name # process name A: List[str] = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A: Dict = arrival_time A: Optional[Any] = burst_time # remaining burst time A: Any = 0 # total time of the process wait in ready queue A: Any = 0 # time from arrival time to completion time class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int , ) -> None: '''simple docstring''' A: Dict = number_of_queues # time slice of queues that round robin algorithm applied A: int = time_slices # unfinished process is in this ready_queue A: Tuple = queue # current time A: int = current_time # finished process is in this sequence queue A: deque[Process] = deque() def _snake_case ( self : List[Any] ) -> list[str]: '''simple docstring''' A: str = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Optional[int] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Any = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: List[Any] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): completion_times.append(queue[i].stop_time ) return completion_times def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> list[int]: '''simple docstring''' return [q.burst_time for q in queue] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Process ) -> int: '''simple docstring''' process.waiting_time += self.current_time - process.stop_time return process.waiting_time def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> deque[Process]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of finished process while len(SCREAMING_SNAKE_CASE_ ) != 0: A: Optional[Any] = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A: Any = 0 # set the process's turnaround time because it is finished A: int = self.current_time - cp.arrival_time # set the completion time A: List[str] = self.current_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int ) -> tuple[deque[Process], deque[Process]]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(SCREAMING_SNAKE_CASE_ ) ): A: Dict = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A: Optional[Any] = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(SCREAMING_SNAKE_CASE_ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A: int = 0 # set the finish time A: Union[str, Any] = self.current_time # update the process' turnaround time because it is finished A: Tuple = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def _snake_case ( self : Optional[Any] ) -> deque[Process]: '''simple docstring''' for i in range(self.number_of_queues - 1 ): A , A: Optional[Any] = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) UpperCamelCase = MLFQ(number_of_queues, time_slices, queue, 0) UpperCamelCase = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f'waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print completion times of processes(P1, P2, P3, P4) print( f'completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print total turnaround times of processes(P1, P2, P3, P4) print( f'turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print sequence of finished processes print( f'sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}' )
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tensorflow_text_available, is_torch_available UpperCamelCase = { '''configuration_ernie''': ['''ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieConfig''', '''ErnieOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ErnieForCausalLM''', '''ErnieForMaskedLM''', '''ErnieForMultipleChoice''', '''ErnieForNextSentencePrediction''', '''ErnieForPreTraining''', '''ErnieForQuestionAnswering''', '''ErnieForSequenceClassification''', '''ErnieForTokenClassification''', '''ErnieModel''', '''ErniePreTrainedModel''', ] if TYPE_CHECKING: from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ernie import ( ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST, ErnieForCausalLM, ErnieForMaskedLM, ErnieForMultipleChoice, ErnieForNextSentencePrediction, ErnieForPreTraining, ErnieForQuestionAnswering, ErnieForSequenceClassification, ErnieForTokenClassification, ErnieModel, ErniePreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : list = field(default_factory=UpperCAmelCase_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tensor , SCREAMING_SNAKE_CASE_ : Tensor ) -> int: '''simple docstring''' A: List[str] = len(list(m.modules() ) ) == 1 or isinstance(SCREAMING_SNAKE_CASE_ , nn.Convad ) or isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ) if has_not_submodules: self.traced.append(SCREAMING_SNAKE_CASE_ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tensor ) -> Dict: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(SCREAMING_SNAKE_CASE_ ) [x.remove() for x in self.handles] return self @property def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return list(filter(lambda SCREAMING_SNAKE_CASE_ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 0 UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) def __call__( self : Any , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[Any]: '''simple docstring''' A: Dict = Tracker(self.dest )(SCREAMING_SNAKE_CASE_ ).parametrized A: Tuple = Tracker(self.src )(SCREAMING_SNAKE_CASE_ ).parametrized A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.src_skip , SCREAMING_SNAKE_CASE_ ) ) A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.dest_skip , SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise Exception( f"""Numbers of operations are different. Source module has {len(SCREAMING_SNAKE_CASE_ )} operations while""" f""" destination module has {len(SCREAMING_SNAKE_CASE_ )}.""" ) for dest_m, src_m in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"""Transfered from={src_m} to={dest_m}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase = True ) -> Any: print(F"""Converting {name}...""" ) with torch.no_grad(): A: Union[str, Any] = timm.create_model(__lowercase , pretrained=__lowercase ).eval() A: List[str] = ResNetForImageClassification(__lowercase ).eval() A: int = ModuleTransfer(src=__lowercase , dest=__lowercase ) A: List[str] = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(__lowercase ) assert torch.allclose(from_model(__lowercase ) , our_model(__lowercase ).logits ), "The model logits don't match the original one." A: str = F"""resnet{'-'.join(name.split('resnet' ) )}""" print(__lowercase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add model''' , use_temp_dir=__lowercase , ) # we can use the convnext one A: Any = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add image processor''' , use_temp_dir=__lowercase , ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> List[Any]: A: Union[str, Any] = '''imagenet-1k-id2label.json''' A: Union[str, Any] = 1_0_0_0 A: Optional[int] = (1, num_labels) A: Dict = '''huggingface/label-files''' A: Any = num_labels A: Union[str, Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Optional[int] = {int(__lowercase ): v for k, v in idalabel.items()} A: Optional[int] = idalabel A: List[str] = {v: k for k, v in idalabel.items()} A: str = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Optional[Any] = { '''resnet18''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet26''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet34''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet50''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet101''': ImageNetPreTrainedConfig( depths=[3, 4, 2_3, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet152''': ImageNetPreTrainedConfig( depths=[3, 8, 3_6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), } if model_name: convert_weight_and_push(__lowercase , names_to_config[model_name] , __lowercase , __lowercase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(__lowercase , __lowercase , __lowercase , __lowercase ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,''' ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
1
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''allegro/herbert-base-cased''': '''https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json''' }, '''merges_file''': { '''allegro/herbert-base-cased''': '''https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt''' }, } UpperCamelCase = {'''allegro/herbert-base-cased''': 514} UpperCamelCase = {} class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : str = PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : List[Any] = HerbertTokenizer def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Dict=None , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=None , SCREAMING_SNAKE_CASE_ : List[str]="<s>" , SCREAMING_SNAKE_CASE_ : List[Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : int="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="</s>" , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> List[Any]: '''simple docstring''' super().__init__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Tuple = [self.cls_token_id] A: List[str] = [self.sep_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE_ : bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE_ , token_ids_a=SCREAMING_SNAKE_CASE_ , already_has_special_tokens=SCREAMING_SNAKE_CASE_ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: Dict = [self.sep_token_id] A: Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' A: Optional[int] = self._tokenizer.model.save(SCREAMING_SNAKE_CASE_ , name=SCREAMING_SNAKE_CASE_ ) return tuple(SCREAMING_SNAKE_CASE_ )
334
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
1
'''simple docstring''' from __future__ import annotations import math def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if len(__lowercase ) != 2 or len(a[0] ) != 2 or len(__lowercase ) != 2 or len(b[0] ) != 2: raise Exception('''Matrices are not 2x2''' ) A: Optional[Any] = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[int]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(__lowercase ) ) ] def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[list, list, list, list]: if len(__lowercase ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception('''Odd matrices are not supported!''' ) A: str = len(__lowercase ) A: Any = matrix_length // 2 A: Optional[int] = [[a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase )] A: List[str] = [ [a[i][j] for j in range(__lowercase , __lowercase )] for i in range(__lowercase , __lowercase ) ] A: str = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase )] A: Dict = [[a[i][j] for j in range(__lowercase )] for i in range(__lowercase , __lowercase )] return top_left, top_right, bot_left, bot_right def SCREAMING_SNAKE_CASE( __lowercase ) -> tuple[int, int]: return len(__lowercase ), len(matrix[0] ) def SCREAMING_SNAKE_CASE( __lowercase ) -> None: print('''\n'''.join(str(__lowercase ) for line in matrix ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase ) == (2, 2): return default_matrix_multiplication(__lowercase , __lowercase ) A , A , A , A: str = split_matrix(__lowercase ) A , A , A , A: Dict = split_matrix(__lowercase ) A: int = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: List[str] = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: int = actual_strassen(matrix_addition(__lowercase , __lowercase ) , __lowercase ) A: Any = actual_strassen(__lowercase , matrix_subtraction(__lowercase , __lowercase ) ) A: Optional[Any] = actual_strassen(matrix_addition(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Optional[int] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: Optional[Any] = actual_strassen(matrix_subtraction(__lowercase , __lowercase ) , matrix_addition(__lowercase , __lowercase ) ) A: str = matrix_addition(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) A: Dict = matrix_addition(__lowercase , __lowercase ) A: List[Any] = matrix_addition(__lowercase , __lowercase ) A: Optional[int] = matrix_subtraction(matrix_subtraction(matrix_addition(__lowercase , __lowercase ) , __lowercase ) , __lowercase ) # construct the new matrix from our 4 quadrants A: Union[str, Any] = [] for i in range(len(__lowercase ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(__lowercase ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list: if matrix_dimensions(__lowercase )[1] != matrix_dimensions(__lowercase )[0]: A: List[str] = ( '''Unable to multiply these matrices, please check the dimensions.\n''' F"""Matrix A: {matrixa}\n""" F"""Matrix B: {matrixa}""" ) raise Exception(__lowercase ) A: str = matrix_dimensions(__lowercase ) A: Tuple = matrix_dimensions(__lowercase ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] A: Dict = max(*__lowercase , *__lowercase ) A: str = int(math.pow(2 , math.ceil(math.loga(__lowercase ) ) ) ) A: List[str] = matrixa A: Optional[int] = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) A: Any = actual_strassen(__lowercase , __lowercase ) # Removing the additional zeros for i in range(0 , __lowercase ): if i < dimensiona[0]: for _ in range(dimensiona[1] , __lowercase ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": UpperCamelCase = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] UpperCamelCase = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = len(__lowercase ) for i in range(length - 1 ): A: Dict = i for k in range(i + 1 , __lowercase ): if collection[k] < collection[least]: A: List[str] = k if least != i: A , A: Tuple = (collection[i], collection[least]) return collection if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
334
1
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : list = field(default_factory=UpperCAmelCase_ ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tensor , SCREAMING_SNAKE_CASE_ : Tensor ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = len(list(m.modules() ) ) == 1 or isinstance(SCREAMING_SNAKE_CASE_ , nn.Convad ) or isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ) if has_not_submodules: self.traced.append(SCREAMING_SNAKE_CASE_ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[Any]: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(SCREAMING_SNAKE_CASE_ ) [x.remove() for x in self.handles] return self @property def _snake_case ( self : List[Any] ) -> str: '''simple docstring''' return list(filter(lambda SCREAMING_SNAKE_CASE_ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 1 UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : bool = True def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[int]: '''simple docstring''' A: List[Any] = Tracker(self.dest )(SCREAMING_SNAKE_CASE_ ).parametrized A: Union[str, Any] = Tracker(self.src )(SCREAMING_SNAKE_CASE_ ).parametrized A: Any = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.src_skip , SCREAMING_SNAKE_CASE_ ) ) A: Union[str, Any] = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.dest_skip , SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ) and self.raise_if_mismatch: raise Exception( f"""Numbers of operations are different. Source module has {len(SCREAMING_SNAKE_CASE_ )} operations while""" f""" destination module has {len(SCREAMING_SNAKE_CASE_ )}.""" ) for dest_m, src_m in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"""Transfered from={src_m} to={dest_m}""" ) class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : nn.Module ) -> Any: '''simple docstring''' super().__init__() A: List[Tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(('''conv1''', model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith('''block''' ), f"""Unexpected layer name {k}""" A: int = len(SCREAMING_SNAKE_CASE_ ) + 1 feature_blocks.append((f"""res{block_index}""", v) ) A: int = nn.ModuleDict(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Tensor ) -> str: '''simple docstring''' return get_trunk_forward_outputs( SCREAMING_SNAKE_CASE_ , out_feat_keys=SCREAMING_SNAKE_CASE_ , feature_blocks=self._feature_blocks , ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str ) -> str: '''simple docstring''' A: str = x.split('''-''' ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self : List[str] , SCREAMING_SNAKE_CASE_ : str ) -> Callable[[], Tuple[nn.Module, Dict]]: '''simple docstring''' if x not in self: A: List[str] = self.convert_name_to_timm(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = partial(lambda: (timm.create_model(SCREAMING_SNAKE_CASE_ , pretrained=SCREAMING_SNAKE_CASE_ ).eval(), None) ) else: A: Optional[Any] = super().__getitem__(SCREAMING_SNAKE_CASE_ ) return val class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __getitem__( self : Dict , SCREAMING_SNAKE_CASE_ : str ) -> Callable[[], nn.Module]: '''simple docstring''' if "seer" in x and "in1k" not in x: A: int = RegNetModel else: A: int = RegNetForImageClassification return val def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: for from_key, to_key in keys: A: List[Any] = from_state_dict[from_key].clone() print(F"""Copied key={from_key} to={to_key}""" ) return to_state_dict def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase = True , ) -> Tuple: print(F"""Converting {name}...""" ) with torch.no_grad(): A , A: Dict = from_model_func() A: Tuple = our_model_func(__lowercase ).eval() A: Tuple = ModuleTransfer(src=__lowercase , dest=__lowercase , raise_if_mismatch=__lowercase ) A: str = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(__lowercase ) if from_state_dict is not None: A: int = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: A: Dict = [('''0.clf.0.weight''', '''classifier.1.weight'''), ('''0.clf.0.bias''', '''classifier.1.bias''')] A: int = manually_copy_vissl_head(__lowercase , our_model.state_dict() , __lowercase ) our_model.load_state_dict(__lowercase ) A: Optional[int] = our_model(__lowercase , output_hidden_states=__lowercase ) A: List[Any] = ( our_outputs.logits if isinstance(__lowercase , __lowercase ) else our_outputs.last_hidden_state ) A: List[str] = from_model(__lowercase ) A: Optional[int] = from_output[-1] if type(__lowercase ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: A: Optional[Any] = our_outputs.hidden_states[-1] assert torch.allclose(__lowercase , __lowercase ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message='''Add model''' , use_temp_dir=__lowercase , ) A: List[str] = 2_2_4 if '''seer''' not in name else 3_8_4 # we can use the convnext one A: Union[str, Any] = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' , size=__lowercase ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message='''Add image processor''' , use_temp_dir=__lowercase , ) print(F"""Pushed {name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> str: A: str = '''imagenet-1k-id2label.json''' A: Any = 1_0_0_0 A: Optional[Any] = (1, num_labels) A: Tuple = '''huggingface/label-files''' A: Optional[int] = num_labels A: Any = json.load(open(cached_download(hf_hub_url(__lowercase , __lowercase , repo_type='''dataset''' ) ) , '''r''' ) ) A: Dict = {int(__lowercase ): v for k, v in idalabel.items()} A: List[Any] = idalabel A: Optional[int] = {v: k for k, v in idalabel.items()} A: List[str] = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Dict = { '''regnet-x-002''': ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8] , groups_width=8 , layer_type='''x''' ), '''regnet-x-004''': ImageNetPreTrainedConfig( depths=[1, 2, 7, 1_2] , hidden_sizes=[3_2, 6_4, 1_6_0, 3_8_4] , groups_width=1_6 , layer_type='''x''' ), '''regnet-x-006''': ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[4_8, 9_6, 2_4_0, 5_2_8] , groups_width=2_4 , layer_type='''x''' ), '''regnet-x-008''': ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[6_4, 1_2_8, 2_8_8, 6_7_2] , groups_width=1_6 , layer_type='''x''' ), '''regnet-x-016''': ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 2] , hidden_sizes=[7_2, 1_6_8, 4_0_8, 9_1_2] , groups_width=2_4 , layer_type='''x''' ), '''regnet-x-032''': ImageNetPreTrainedConfig( depths=[2, 6, 1_5, 2] , hidden_sizes=[9_6, 1_9_2, 4_3_2, 1_0_0_8] , groups_width=4_8 , layer_type='''x''' ), '''regnet-x-040''': ImageNetPreTrainedConfig( depths=[2, 5, 1_4, 2] , hidden_sizes=[8_0, 2_4_0, 5_6_0, 1_3_6_0] , groups_width=4_0 , layer_type='''x''' ), '''regnet-x-064''': ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 1] , hidden_sizes=[1_6_8, 3_9_2, 7_8_4, 1_6_2_4] , groups_width=5_6 , layer_type='''x''' ), '''regnet-x-080''': ImageNetPreTrainedConfig( depths=[2, 5, 1_5, 1] , hidden_sizes=[8_0, 2_4_0, 7_2_0, 1_9_2_0] , groups_width=1_2_0 , layer_type='''x''' ), '''regnet-x-120''': ImageNetPreTrainedConfig( depths=[2, 5, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0] , groups_width=1_1_2 , layer_type='''x''' ), '''regnet-x-160''': ImageNetPreTrainedConfig( depths=[2, 6, 1_3, 1] , hidden_sizes=[2_5_6, 5_1_2, 8_9_6, 2_0_4_8] , groups_width=1_2_8 , layer_type='''x''' ), '''regnet-x-320''': ImageNetPreTrainedConfig( depths=[2, 7, 1_3, 1] , hidden_sizes=[3_3_6, 6_7_2, 1_3_4_4, 2_5_2_0] , groups_width=1_6_8 , layer_type='''x''' ), # y variant '''regnet-y-002''': ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8] , groups_width=8 ), '''regnet-y-004''': ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[4_8, 1_0_4, 2_0_8, 4_4_0] , groups_width=8 ), '''regnet-y-006''': ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[4_8, 1_1_2, 2_5_6, 6_0_8] , groups_width=1_6 ), '''regnet-y-008''': ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[6_4, 1_2_8, 3_2_0, 7_6_8] , groups_width=1_6 ), '''regnet-y-016''': ImageNetPreTrainedConfig( depths=[2, 6, 1_7, 2] , hidden_sizes=[4_8, 1_2_0, 3_3_6, 8_8_8] , groups_width=2_4 ), '''regnet-y-032''': ImageNetPreTrainedConfig( depths=[2, 5, 1_3, 1] , hidden_sizes=[7_2, 2_1_6, 5_7_6, 1_5_1_2] , groups_width=2_4 ), '''regnet-y-040''': ImageNetPreTrainedConfig( depths=[2, 6, 1_2, 2] , hidden_sizes=[1_2_8, 1_9_2, 5_1_2, 1_0_8_8] , groups_width=6_4 ), '''regnet-y-064''': ImageNetPreTrainedConfig( depths=[2, 7, 1_4, 2] , hidden_sizes=[1_4_4, 2_8_8, 5_7_6, 1_2_9_6] , groups_width=7_2 ), '''regnet-y-080''': ImageNetPreTrainedConfig( depths=[2, 4, 1_0, 1] , hidden_sizes=[1_6_8, 4_4_8, 8_9_6, 2_0_1_6] , groups_width=5_6 ), '''regnet-y-120''': ImageNetPreTrainedConfig( depths=[2, 5, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0] , groups_width=1_1_2 ), '''regnet-y-160''': ImageNetPreTrainedConfig( depths=[2, 4, 1_1, 1] , hidden_sizes=[2_2_4, 4_4_8, 1_2_3_2, 3_0_2_4] , groups_width=1_1_2 ), '''regnet-y-320''': ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 '''regnet-y-320-seer''': RegNetConfig(depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), '''regnet-y-640-seer''': RegNetConfig(depths=[2, 5, 1_2, 1] , hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0] , groups_width=3_2_8 ), '''regnet-y-1280-seer''': RegNetConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2] , groups_width=2_6_4 ), '''regnet-y-2560-seer''': RegNetConfig( depths=[3, 7, 1_6, 1] , hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8] , groups_width=6_4_0 ), '''regnet-y-10b-seer''': ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0] , groups_width=1_0_1_0 ), # finetuned on imagenet '''regnet-y-320-seer-in1k''': ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2] , groups_width=2_3_2 ), '''regnet-y-640-seer-in1k''': ImageNetPreTrainedConfig( depths=[2, 5, 1_2, 1] , hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0] , groups_width=3_2_8 ), '''regnet-y-1280-seer-in1k''': ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2] , groups_width=2_6_4 ), '''regnet-y-2560-seer-in1k''': ImageNetPreTrainedConfig( depths=[3, 7, 1_6, 1] , hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8] , groups_width=6_4_0 ), '''regnet-y-10b-seer-in1k''': ImageNetPreTrainedConfig( depths=[2, 7, 1_7, 1] , hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0] , groups_width=1_0_1_0 ), } A: Optional[Any] = NameToOurModelFuncMap() A: List[Any] = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(__lowercase , __lowercase ) -> Tuple[nn.Module, Dict]: A: Dict = torch.hub.load_state_dict_from_url(__lowercase , model_dir=str(__lowercase ) , map_location='''cpu''' ) A: List[str] = model_func() # check if we have a head, if yes add it A: Optional[int] = files['''classy_state_dict''']['''base_model''']['''model'''] A: Optional[Any] = model_state_dict['''trunk'''] model.load_state_dict(__lowercase ) return model.eval(), model_state_dict["heads"] # pretrained A: List[Any] = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) A: int = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) A: Union[str, Any] = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) A: Tuple = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch''' , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=2_7 , group_width=1_0_1_0 , w_a=1_7_4_4 , w_a=6_2_0.8_3 , w_m=2.5_2 ) ) ) , ) # IN1K finetuned A: Dict = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) A: Any = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) A: List[Any] = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch''' , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) A: Dict = partial( __lowercase , '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch''' , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=2_7 , group_width=1_0_1_0 , w_a=1_7_4_4 , w_a=6_2_0.8_3 , w_m=2.5_2 ) ) ) , ) if model_name: convert_weight_and_push( __lowercase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , __lowercase , __lowercase , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( __lowercase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , __lowercase , __lowercase , __lowercase , ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported regnet* architecture,''' ''' currently: regnetx-*, regnety-*. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
1
'''simple docstring''' import os import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from huggingface_hub.file_download import http_get from requests.exceptions import HTTPError from transformers import ( AlbertTokenizer, AutoTokenizer, BertTokenizer, BertTokenizerFast, GPTaTokenizerFast, is_tokenizers_available, ) from transformers.testing_utils import TOKEN, USER, is_staging_test, require_tokenizers from transformers.tokenization_utils import Trie sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Dict ) -> Any: '''simple docstring''' A: Any = mock.Mock() A: List[Any] = 5_00 A: Optional[int] = {} A: Dict = HTTPError A: Dict = {} # Download this model to make sure it's in the cache. A: str = BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=SCREAMING_SNAKE_CASE_ ) as mock_head: A: str = BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # This check we did call the fake head request mock_head.assert_called() @require_tokenizers def _snake_case ( self : Optional[Any] ) -> List[str]: '''simple docstring''' A: Tuple = mock.Mock() A: List[str] = 5_00 A: List[str] = {} A: List[str] = HTTPError A: List[str] = {} # Download this model to make sure it's in the cache. A: Tuple = GPTaTokenizerFast.from_pretrained('''gpt2''' ) # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=SCREAMING_SNAKE_CASE_ ) as mock_head: A: Optional[int] = GPTaTokenizerFast.from_pretrained('''gpt2''' ) # This check we did call the fake head request mock_head.assert_called() def _snake_case ( self : Optional[Any] ) -> Any: '''simple docstring''' try: A: Union[str, Any] = tempfile.mktemp() with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: http_get('''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''' , SCREAMING_SNAKE_CASE_ ) A: str = AlbertTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) finally: os.remove(SCREAMING_SNAKE_CASE_ ) # Supporting this legacy load introduced a weird bug where the tokenizer would load local files if they are in # the current folder and have the right name. if os.path.isfile('''tokenizer.json''' ): # We skip the test if the user has a `tokenizer.json` in this folder to avoid deleting it. return try: with open('''tokenizer.json''' , '''wb''' ) as f: http_get('''https://huggingface.co/hf-internal-testing/tiny-random-bert/blob/main/tokenizer.json''' , SCREAMING_SNAKE_CASE_ ) A: Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) # The tiny random BERT has a vocab size of 1024, tiny gpt2 as a vocab size of 1000 self.assertEqual(tokenizer.vocab_size , 10_00 ) # Tokenizer should depend on the remote checkpoint, not the local tokenizer.json file. finally: os.remove('''tokenizer.json''' ) def _snake_case ( self : Tuple ) -> Tuple: '''simple docstring''' A: Any = AlbertTokenizer.from_pretrained('''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''' ) @is_staging_test class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : List[Any] = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""] @classmethod def _snake_case ( cls : Any ) -> str: '''simple docstring''' A: str = TOKEN HfFolder.save_token(SCREAMING_SNAKE_CASE_ ) @classmethod def _snake_case ( cls : List[Any] ) -> List[str]: '''simple docstring''' try: delete_repo(token=cls._token , repo_id='''test-tokenizer''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-tokenizer-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-tokenizer''' ) except HTTPError: pass def _snake_case ( self : Optional[int] ) -> int: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: A: int = os.path.join(SCREAMING_SNAKE_CASE_ , '''vocab.txt''' ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A: int = BertTokenizer(SCREAMING_SNAKE_CASE_ ) tokenizer.push_to_hub('''test-tokenizer''' , use_auth_token=self._token ) A: List[str] = BertTokenizer.from_pretrained(f"""{USER}/test-tokenizer""" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) # Reset repo delete_repo(token=self._token , repo_id='''test-tokenizer''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ , repo_id='''test-tokenizer''' , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) A: Union[str, Any] = BertTokenizer.from_pretrained(f"""{USER}/test-tokenizer""" ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: A: Union[str, Any] = os.path.join(SCREAMING_SNAKE_CASE_ , '''vocab.txt''' ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A: List[str] = BertTokenizer(SCREAMING_SNAKE_CASE_ ) tokenizer.push_to_hub('''valid_org/test-tokenizer-org''' , use_auth_token=self._token ) A: str = BertTokenizer.from_pretrained('''valid_org/test-tokenizer-org''' ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-tokenizer-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained( SCREAMING_SNAKE_CASE_ , repo_id='''valid_org/test-tokenizer-org''' , push_to_hub=SCREAMING_SNAKE_CASE_ , use_auth_token=self._token ) A: List[str] = BertTokenizer.from_pretrained('''valid_org/test-tokenizer-org''' ) self.assertDictEqual(new_tokenizer.vocab , tokenizer.vocab ) @require_tokenizers def _snake_case ( self : Union[str, Any] ) -> Any: '''simple docstring''' CustomTokenizer.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: A: Tuple = os.path.join(SCREAMING_SNAKE_CASE_ , '''vocab.txt''' ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A: Union[str, Any] = CustomTokenizer(SCREAMING_SNAKE_CASE_ ) # No fast custom tokenizer tokenizer.push_to_hub('''test-dynamic-tokenizer''' , use_auth_token=self._token ) A: List[str] = AutoTokenizer.from_pretrained(f"""{USER}/test-dynamic-tokenizer""" , trust_remote_code=SCREAMING_SNAKE_CASE_ ) # Can't make an isinstance check because the new_model.config is from the CustomTokenizer class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , '''CustomTokenizer''' ) # Fast and slow custom tokenizer CustomTokenizerFast.register_for_auto_class() with tempfile.TemporaryDirectory() as tmp_dir: A: Tuple = os.path.join(SCREAMING_SNAKE_CASE_ , '''vocab.txt''' ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A: Optional[int] = BertTokenizerFast.from_pretrained(SCREAMING_SNAKE_CASE_ ) bert_tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = CustomTokenizerFast.from_pretrained(SCREAMING_SNAKE_CASE_ ) tokenizer.push_to_hub('''test-dynamic-tokenizer''' , use_auth_token=self._token ) A: Dict = AutoTokenizer.from_pretrained(f"""{USER}/test-dynamic-tokenizer""" , trust_remote_code=SCREAMING_SNAKE_CASE_ ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , '''CustomTokenizerFast''' ) A: int = AutoTokenizer.from_pretrained( f"""{USER}/test-dynamic-tokenizer""" , use_fast=SCREAMING_SNAKE_CASE_ , trust_remote_code=SCREAMING_SNAKE_CASE_ ) # Can't make an isinstance check because the new_model.config is from the FakeConfig class of a dynamic module self.assertEqual(tokenizer.__class__.__name__ , '''CustomTokenizer''' ) class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Any ) -> str: '''simple docstring''' A: Tuple = Trie() trie.add('''Hello 友達''' ) self.assertEqual(trie.data , {'''H''': {'''e''': {'''l''': {'''l''': {'''o''': {''' ''': {'''友''': {'''達''': {'''''': 1}}}}}}}}} ) trie.add('''Hello''' ) trie.data self.assertEqual(trie.data , {'''H''': {'''e''': {'''l''': {'''l''': {'''o''': {'''''': 1, ''' ''': {'''友''': {'''達''': {'''''': 1}}}}}}}}} ) def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = Trie() self.assertEqual(trie.split('''[CLS] This is a extra_id_100''' ) , ['''[CLS] This is a extra_id_100'''] ) trie.add('''[CLS]''' ) trie.add('''extra_id_1''' ) trie.add('''extra_id_100''' ) self.assertEqual(trie.split('''[CLS] This is a extra_id_100''' ) , ['''[CLS]''', ''' This is a ''', '''extra_id_100'''] ) def _snake_case ( self : Union[str, Any] ) -> str: '''simple docstring''' A: Optional[Any] = Trie() trie.add('''A''' ) self.assertEqual(trie.split('''ABC''' ) , ['''A''', '''BC'''] ) self.assertEqual(trie.split('''BCA''' ) , ['''BC''', '''A'''] ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' A: Any = Trie() trie.add('''TOKEN]''' ) trie.add('''[SPECIAL_TOKEN]''' ) self.assertEqual(trie.split('''This is something [SPECIAL_TOKEN]''' ) , ['''This is something ''', '''[SPECIAL_TOKEN]'''] ) def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' A: Optional[int] = Trie() trie.add('''A''' ) trie.add('''P''' ) trie.add('''[SPECIAL_TOKEN]''' ) self.assertEqual(trie.split('''This is something [SPECIAL_TOKEN]''' ) , ['''This is something ''', '''[SPECIAL_TOKEN]'''] ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' A: Tuple = Trie() trie.add('''AB''' ) trie.add('''B''' ) trie.add('''C''' ) self.assertEqual(trie.split('''ABC''' ) , ['''AB''', '''C'''] ) def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: Optional[int] = Trie() trie.add('''ABC''' ) trie.add('''B''' ) trie.add('''CD''' ) self.assertEqual(trie.split('''ABCD''' ) , ['''ABC''', '''D'''] ) def _snake_case ( self : Any ) -> Any: '''simple docstring''' A: Union[str, Any] = Trie() A: List[str] = trie.cut_text('''ABC''' , [0, 0, 2, 1, 2, 3] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , ['''AB''', '''C'''] )
334
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
1
'''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()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
334
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = DebertaTokenizer UpperCamelCase_ : List[str] = True UpperCamelCase_ : int = DebertaTokenizerFast def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A: Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] A: int = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Union[str, Any] = {'''unk_token''': '''[UNK]'''} A: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: str = 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(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = '''lower newer''' A: str = '''lower newer''' return input_text, output_text def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: str = self.get_tokenizer() A: Any = '''lower newer''' A: Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] A: int = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokens + [tokenizer.unk_token] A: int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' A: str = self.get_tokenizer() A: List[str] = tokenizer('''Hello''' , '''World''' ) A: Union[str, Any] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Any = tokenizer.encode('''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: int = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: A: List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Dict = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: Any = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for seq in encoding['''input_ids''']] # fmt: off A: Any = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on A: Optional[int] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , SCREAMING_SNAKE_CASE_ ) for expected, decoded in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : str UpperCamelCase_ : str = None @staticmethod def _snake_case ( ) -> int: '''simple docstring''' raise NotImplementedError def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> int: '''simple docstring''' raise NotImplementedError def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Tuple ) -> Union[str, Any]: '''simple docstring''' raise NotImplementedError def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.is_available(): raise RuntimeError( f"""You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.""" ) @classmethod def _snake_case ( cls : Optional[int] ) -> Tuple: '''simple docstring''' return f"""`pip install {cls.pip_package or cls.name}`""" class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = """optuna""" @staticmethod def _snake_case ( ) -> Dict: '''simple docstring''' return is_optuna_available() def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' return run_hp_search_optuna(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int ) -> Optional[int]: '''simple docstring''' return default_hp_space_optuna(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : List[Any] = """ray""" UpperCamelCase_ : Any = """'ray[tune]'""" @staticmethod def _snake_case ( ) -> Dict: '''simple docstring''' return is_ray_available() def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[int]: '''simple docstring''' return run_hp_search_ray(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Dict: '''simple docstring''' return default_hp_space_ray(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = """sigopt""" @staticmethod def _snake_case ( ) -> Union[str, Any]: '''simple docstring''' return is_sigopt_available() def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' return run_hp_search_sigopt(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Union[str, Any]: '''simple docstring''' return default_hp_space_sigopt(SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = """wandb""" @staticmethod def _snake_case ( ) -> Optional[Any]: '''simple docstring''' return is_wandb_available() def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Tuple ) -> int: '''simple docstring''' return run_hp_search_wandb(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Tuple ) -> Optional[Any]: '''simple docstring''' return default_hp_space_wandb(SCREAMING_SNAKE_CASE_ ) UpperCamelCase = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def SCREAMING_SNAKE_CASE( ) -> str: A: Any = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(__lowercase ) > 0: A: List[str] = available_backends[0].name if len(__lowercase ) > 1: logger.info( F"""{len(__lowercase )} hyperparameter search backends available. Using {name} as the default.""" ) return name raise RuntimeError( '''No hyperparameter search backend available.\n''' + '''\n'''.join( F""" - To install {backend.name} run {backend.pip_install()}""" for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
334
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
1
'''simple docstring''' from __future__ import annotations from typing import Any class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' pass class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> None: '''simple docstring''' A: Any = data A: Node | None = None def __iter__( self : Optional[int] ) -> List[str]: '''simple docstring''' A: List[str] = self A: Dict = [] while node: if node in visited: raise ContainsLoopError visited.append(SCREAMING_SNAKE_CASE_ ) yield node.data A: str = node.next_node @property def _snake_case ( self : List[str] ) -> bool: '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCamelCase = Node(1) UpperCamelCase = Node(2) UpperCamelCase = Node(3) UpperCamelCase = Node(4) print(root_node.has_loop) # False UpperCamelCase = root_node.next_node print(root_node.has_loop) # True UpperCamelCase = Node(5) UpperCamelCase = Node(6) UpperCamelCase = Node(5) UpperCamelCase = Node(6) print(root_node.has_loop) # False UpperCamelCase = Node(1) print(root_node.has_loop) # False
334
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCamelCase = { '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MegaForCausalLM''', '''MegaForMaskedLM''', '''MegaForMultipleChoice''', '''MegaForQuestionAnswering''', '''MegaForSequenceClassification''', '''MegaForTokenClassification''', '''MegaModel''', '''MegaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''xlm-mlm-en-2048''': '''https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json''', '''xlm-mlm-ende-1024''': '''https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json''', '''xlm-mlm-enfr-1024''': '''https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json''', '''xlm-mlm-enro-1024''': '''https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json''', '''xlm-mlm-tlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json''', '''xlm-mlm-xnli15-1024''': '''https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json''', '''xlm-clm-enfr-1024''': '''https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json''', '''xlm-clm-ende-1024''': '''https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json''', '''xlm-mlm-17-1280''': '''https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json''', '''xlm-mlm-100-1280''': '''https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json''', } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = """xlm""" UpperCamelCase_ : Any = { """hidden_size""": """emb_dim""", """num_attention_heads""": """n_heads""", """num_hidden_layers""": """n_layers""", """n_words""": """vocab_size""", # For backward compatibility } def __init__( self : int , SCREAMING_SNAKE_CASE_ : Optional[Any]=3_01_45 , SCREAMING_SNAKE_CASE_ : Dict=20_48 , SCREAMING_SNAKE_CASE_ : Dict=12 , SCREAMING_SNAKE_CASE_ : Tuple=16 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : str=False , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , SCREAMING_SNAKE_CASE_ : int=1 , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Optional[int]=5_12 , SCREAMING_SNAKE_CASE_ : Dict=20_48**-0.5 , SCREAMING_SNAKE_CASE_ : Optional[int]=1E-12 , SCREAMING_SNAKE_CASE_ : str=0.02 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE_ : str=1 , SCREAMING_SNAKE_CASE_ : Optional[int]=2 , SCREAMING_SNAKE_CASE_ : Tuple=3 , SCREAMING_SNAKE_CASE_ : str=5 , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : int="first" , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Dict=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE_ : str=5 , SCREAMING_SNAKE_CASE_ : List[str]=5 , SCREAMING_SNAKE_CASE_ : Any=0 , SCREAMING_SNAKE_CASE_ : List[str]=0 , SCREAMING_SNAKE_CASE_ : List[str]=2 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0 , **SCREAMING_SNAKE_CASE_ : Optional[Any] , ) -> Tuple: '''simple docstring''' A: int = vocab_size A: Dict = emb_dim A: int = n_layers A: Optional[Any] = n_heads A: List[Any] = dropout A: Union[str, Any] = attention_dropout A: int = gelu_activation A: str = sinusoidal_embeddings A: Union[str, Any] = causal A: Tuple = asm A: Optional[Any] = n_langs A: Any = use_lang_emb A: List[str] = layer_norm_eps A: Tuple = bos_index A: Tuple = eos_index A: Optional[Any] = pad_index A: Optional[Any] = unk_index A: Optional[Any] = mask_index A: List[str] = is_encoder A: Any = max_position_embeddings A: Optional[int] = embed_init_std A: Tuple = init_std A: Optional[Any] = summary_type A: str = summary_use_proj A: str = summary_activation A: Optional[Any] = summary_proj_to_labels A: Tuple = summary_first_dropout A: Any = start_n_top A: Union[str, Any] = end_n_top A: Optional[int] = mask_token_id A: Optional[int] = lang_id if "n_words" in kwargs: A: Optional[int] = kwargs['''n_words'''] super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' @property def _snake_case ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A: Union[str, Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: A: Optional[Any] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
334
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
1
'''simple docstring''' import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import YolosImageProcessor class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[str]=7 , SCREAMING_SNAKE_CASE_ : List[str]=3 , SCREAMING_SNAKE_CASE_ : Optional[Any]=30 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=4_00 , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Dict=None , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : Optional[Any]=[0.5, 0.5, 0.5] , SCREAMING_SNAKE_CASE_ : Dict=[0.5, 0.5, 0.5] , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , SCREAMING_SNAKE_CASE_ : Any=1 / 2_55 , SCREAMING_SNAKE_CASE_ : str=True , ) -> List[str]: '''simple docstring''' A: str = size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 13_33} A: Tuple = parent A: Any = batch_size A: Optional[Any] = num_channels A: int = min_resolution A: List[Any] = max_resolution A: Optional[Any] = do_resize A: List[str] = size A: int = do_normalize A: Dict = image_mean A: List[str] = image_std A: Any = do_rescale A: List[str] = rescale_factor A: Dict = do_pad def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False ) -> Any: '''simple docstring''' if not batched: A: Any = image_inputs[0] if isinstance(SCREAMING_SNAKE_CASE_ , Image.Image ): A , A: Tuple = image.size else: A , A: Optional[Any] = image.shape[1], image.shape[2] if w < h: A: Optional[Any] = int(self.size['''shortest_edge'''] * h / w ) A: Union[str, Any] = self.size['''shortest_edge'''] elif w > h: A: Optional[int] = self.size['''shortest_edge'''] A: Optional[int] = int(self.size['''shortest_edge'''] * w / h ) else: A: int = self.size['''shortest_edge'''] A: Tuple = self.size['''shortest_edge'''] else: A: int = [] for image in image_inputs: A , A: Optional[int] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A: Any = max(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : item[0] )[0] A: int = max(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[int] = YolosImageProcessor if is_vision_available() else None def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' A: int = YolosImageProcessingTester(self ) @property def _snake_case ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def _snake_case ( self : Optional[int] ) -> str: '''simple docstring''' A: Tuple = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''image_mean''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''image_std''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_normalize''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''do_resize''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''size''' ) ) def _snake_case ( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' A: Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18, '''longest_edge''': 13_33} ) self.assertEqual(image_processor.do_pad , SCREAMING_SNAKE_CASE_ ) A: Dict = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=SCREAMING_SNAKE_CASE_ ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42, '''longest_edge''': 84} ) self.assertEqual(image_processor.do_pad , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : str ) -> Union[str, Any]: '''simple docstring''' pass def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' A: Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A: Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , Image.Image ) # Test not batched input A: Dict = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values A , A: Dict = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A , A: Any = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) A: List[Any] = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _snake_case ( self : Optional[Any] ) -> str: '''simple docstring''' A: Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A: int = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , numpify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) # Test not batched input A: Optional[int] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values A , A: List[str] = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A: Any = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values A , A: List[str] = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _snake_case ( self : Dict ) -> str: '''simple docstring''' A: Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A: Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) # Test not batched input A: Union[str, Any] = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values A , A: Union[str, Any] = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched A: str = image_processing(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values A , A: Tuple = self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _snake_case ( self : Any ) -> Tuple: '''simple docstring''' A: Tuple = self.image_processing_class(**self.image_processor_dict ) A: Optional[int] = self.image_processing_class(do_resize=SCREAMING_SNAKE_CASE_ , do_normalize=SCREAMING_SNAKE_CASE_ , do_rescale=SCREAMING_SNAKE_CASE_ ) # create random PyTorch tensors A: Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=SCREAMING_SNAKE_CASE_ , torchify=SCREAMING_SNAKE_CASE_ ) for image in image_inputs: self.assertIsInstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ) # Test whether the method "pad" and calling the image processor return the same tensors A: int = image_processing_a.pad(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) A: Optional[int] = image_processing_a(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) self.assertTrue( torch.allclose(encoded_images_with_method['''pixel_values'''] , encoded_images['''pixel_values'''] , atol=1E-4 ) ) @slow def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' , '''r''' ) as f: A: Optional[Any] = json.loads(f.read() ) A: str = {'''image_id''': 3_97_69, '''annotations''': target} # encode them A: List[str] = YolosImageProcessor.from_pretrained('''hustvl/yolos-small''' ) A: Optional[Any] = image_processing(images=SCREAMING_SNAKE_CASE_ , annotations=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) # verify pixel values A: str = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding['''pixel_values'''].shape , SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) # verify area A: Any = torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , SCREAMING_SNAKE_CASE_ ) ) # verify boxes A: Dict = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , SCREAMING_SNAKE_CASE_ ) A: Tuple = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) ) # verify image_id A: List[str] = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , SCREAMING_SNAKE_CASE_ ) ) # verify is_crowd A: str = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , SCREAMING_SNAKE_CASE_ ) ) # verify class_labels A: Optional[int] = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , SCREAMING_SNAKE_CASE_ ) ) # verify orig_size A: Tuple = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , SCREAMING_SNAKE_CASE_ ) ) # verify size A: Tuple = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , SCREAMING_SNAKE_CASE_ ) ) @slow def _snake_case ( self : str ) -> Optional[int]: '''simple docstring''' A: Optional[int] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' , '''r''' ) as f: A: Optional[int] = json.loads(f.read() ) A: Optional[Any] = {'''file_name''': '''000000039769.png''', '''image_id''': 3_97_69, '''segments_info''': target} A: Any = pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic''' ) # encode them A: Any = YolosImageProcessor(format='''coco_panoptic''' ) A: Union[str, Any] = image_processing(images=SCREAMING_SNAKE_CASE_ , annotations=SCREAMING_SNAKE_CASE_ , masks_path=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ) # verify pixel values A: Tuple = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding['''pixel_values'''].shape , SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) # verify area A: Optional[Any] = torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , SCREAMING_SNAKE_CASE_ ) ) # verify boxes A: int = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , SCREAMING_SNAKE_CASE_ ) A: List[str] = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) ) # verify image_id A: Dict = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , SCREAMING_SNAKE_CASE_ ) ) # verify is_crowd A: int = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , SCREAMING_SNAKE_CASE_ ) ) # verify class_labels A: int = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , SCREAMING_SNAKE_CASE_ ) ) # verify masks A: Tuple = 82_28_73 self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() , SCREAMING_SNAKE_CASE_ ) # verify orig_size A: Tuple = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , SCREAMING_SNAKE_CASE_ ) ) # verify size A: Union[str, Any] = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , SCREAMING_SNAKE_CASE_ ) )
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for data in source_data: for i, el in enumerate(__lowercase ): if len(__lowercase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__lowercase ) ) return data_lists def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for dlist, weight in zip(__lowercase , __lowercase ): A: List[str] = min(__lowercase ) A: Union[str, Any] = max(__lowercase ) A: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A: List[str] = F"""Invalid weight of {weight:f} provided""" raise ValueError(__lowercase ) score_lists.append(__lowercase ) return score_lists def SCREAMING_SNAKE_CASE( __lowercase ) -> list[float]: A: list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__lowercase ): A: str = final_scores[j] + ele return final_scores def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: Any = get_data(__lowercase ) A: str = calculate_each_score(__lowercase , __lowercase ) A: int = generate_final_scores(__lowercase ) # append scores to source data for i, ele in enumerate(__lowercase ): source_data[i].append(__lowercase ) return source_data
334
1
'''simple docstring''' import numpy as np from transformers import BatchFeature from transformers.testing_utils import require_tf, require_torch from .test_feature_extraction_common import FeatureExtractionSavingTestMixin class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = None UpperCamelCase_ : List[str] = None @property def _snake_case ( self : List[str] ) -> Optional[int]: '''simple docstring''' return self.feat_extract_tester.prepare_feat_extract_dict() def _snake_case ( self : Any ) -> Dict: '''simple docstring''' A: str = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''feature_size''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''sampling_rate''' ) ) self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ , '''padding_value''' ) ) def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' A: Union[str, Any] = self.feat_extract_tester.prepare_inputs_for_common() A: int = self.feature_extraction_class(**self.feat_extract_dict ) A: Optional[int] = feat_extract.model_input_names[0] A: str = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) for x, y in zip(SCREAMING_SNAKE_CASE_ , processed_features[input_name] ) ) ) A: Optional[int] = self.feat_extract_tester.prepare_inputs_for_common(equal_length=SCREAMING_SNAKE_CASE_ ) A: Any = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) A: Optional[Any] = processed_features[input_name] if len(batch_features_input.shape ) < 3: A: str = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_torch def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' A: Optional[Any] = self.feat_extract_tester.prepare_inputs_for_common(equal_length=SCREAMING_SNAKE_CASE_ ) A: List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) A: List[Any] = feat_extract.model_input_names[0] A: Any = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) A: Union[str, Any] = processed_features[input_name] if len(batch_features_input.shape ) < 3: A: str = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_tf def _snake_case ( self : str ) -> List[Any]: '''simple docstring''' A: Optional[int] = self.feat_extract_tester.prepare_inputs_for_common(equal_length=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) A: Any = feat_extract.model_input_names[0] A: str = BatchFeature({input_name: speech_inputs} , tensor_type='''tf''' ) A: List[Any] = processed_features[input_name] if len(batch_features_input.shape ) < 3: A: Optional[int] = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any]=False ) -> Tuple: '''simple docstring''' def _inputs_have_equal_length(SCREAMING_SNAKE_CASE_ : Tuple ): A: str = len(input[0] ) for input_slice in input[1:]: if len(SCREAMING_SNAKE_CASE_ ) != length: return False return True def _inputs_are_equal(SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ): if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): return False for input_slice_a, input_slice_a in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if not np.allclose(np.asarray(SCREAMING_SNAKE_CASE_ ) , np.asarray(SCREAMING_SNAKE_CASE_ ) , atol=1E-3 ): return False return True A: Tuple = self.feature_extraction_class(**self.feat_extract_dict ) A: Union[str, Any] = self.feat_extract_tester.prepare_inputs_for_common(numpify=SCREAMING_SNAKE_CASE_ ) A: Dict = feat_extract.model_input_names[0] A: Dict = BatchFeature({input_name: speech_inputs} ) A: Any = self.feat_extract_tester.seq_length_diff A: List[str] = self.feat_extract_tester.max_seq_length + pad_diff A: Union[str, Any] = self.feat_extract_tester.min_seq_length A: Dict = self.feat_extract_tester.batch_size A: str = self.feat_extract_tester.feature_size # test padding for List[int] + numpy A: int = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: List[Any] = input_a[input_name] A: Tuple = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' ) A: Tuple = input_a[input_name] A: Tuple = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[-1] ) ) A: Optional[int] = input_a[input_name] A: Any = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''np''' ) A: List[str] = input_a[input_name] # max_length parameter has to be provided when setting `padding="max_length"` with self.assertRaises(SCREAMING_SNAKE_CASE_ ): feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''max_length''' )[input_name] A: Union[str, Any] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' ) A: int = input_a[input_name] self.assertFalse(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(_inputs_are_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(len(input_a[0] ) == pad_min_length ) self.assertTrue(len(input_a[1] ) == pad_min_length + pad_diff ) self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0] )) ) self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size ) # test padding for `pad_to_multiple_of` for List[int] + numpy A: Dict = feat_extract.pad(SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=10 ) A: Optional[Any] = input_a[input_name] A: Optional[Any] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , pad_to_multiple_of=10 ) A: Optional[int] = input_a[input_name] A: List[str] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , pad_to_multiple_of=10 , max_length=SCREAMING_SNAKE_CASE_ ) A: Tuple = input_a[input_name] A: List[str] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , pad_to_multiple_of=10 , max_length=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' , ) A: Optional[Any] = input_a[input_name] self.assertTrue(all(len(SCREAMING_SNAKE_CASE_ ) % 10 == 0 for x in input_a ) ) self.assertTrue(_inputs_are_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A: Tuple = pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10 self.assertTrue(all(len(SCREAMING_SNAKE_CASE_ ) == expected_mult_pad_length for x in input_a ) ) self.assertEqual(input_a.shape[:2] , (batch_size, expected_mult_pad_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == feature_size ) # Check padding value is correct A: Any = (np.ones(self.feat_extract_tester.feature_size ) * feat_extract.padding_value).sum() self.assertTrue( abs(np.asarray(input_a[0] )[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1E-3 ) self.assertTrue( abs( np.asarray(input_a[1] )[pad_min_length + pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff) ) < 1E-3 ) self.assertTrue( abs( np.asarray(input_a[2] )[pad_min_length + 2 * pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff) ) < 1E-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1E-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length) ) < 1E-3 ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False ) -> List[Any]: '''simple docstring''' def _inputs_have_equal_length(SCREAMING_SNAKE_CASE_ : str ): A: str = len(input[0] ) for input_slice in input[1:]: if len(SCREAMING_SNAKE_CASE_ ) != length: return False return True def _inputs_are_equal(SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Tuple ): if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): return False for input_slice_a, input_slice_a in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if not np.allclose(np.asarray(SCREAMING_SNAKE_CASE_ ) , np.asarray(SCREAMING_SNAKE_CASE_ ) , atol=1E-3 ): return False return True A: List[str] = self.feature_extraction_class(**self.feat_extract_dict ) A: Dict = self.feat_extract_tester.prepare_inputs_for_common(numpify=SCREAMING_SNAKE_CASE_ ) A: List[str] = feat_extract.model_input_names[0] A: int = BatchFeature({input_name: speech_inputs} ) # truncate to smallest A: Optional[Any] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) , truncation=SCREAMING_SNAKE_CASE_ ) A: Tuple = input_a[input_name] A: int = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) ) A: Optional[Any] = input_a[input_name] self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertFalse(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) # truncate to smallest with np A: int = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' , truncation=SCREAMING_SNAKE_CASE_ , ) A: Optional[Any] = input_a[input_name] A: List[str] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' ) A: List[Any] = input_a[input_name] self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(input_a.shape[1] == len(speech_inputs[0] ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) # truncate to middle A: Union[str, Any] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' , ) A: Any = input_a[input_name] A: Any = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=SCREAMING_SNAKE_CASE_ ) A: Tuple = input_a[input_name] A: Dict = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[1] ) , return_tensors='''np''' ) A: str = input_a[input_name] self.assertTrue(input_a.shape[1] == len(speech_inputs[1] ) ) self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(_inputs_are_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertTrue(len(input_a[-1] ) == len(speech_inputs[-1] ) ) # padding has to be max_length when setting `truncation=True` with self.assertRaises(SCREAMING_SNAKE_CASE_ ): feat_extract.pad(SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(SCREAMING_SNAKE_CASE_ ): feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , truncation=SCREAMING_SNAKE_CASE_ )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(SCREAMING_SNAKE_CASE_ ): feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , truncation=SCREAMING_SNAKE_CASE_ )[input_name] # max_length parameter has to be provided when setting `truncation=True` and padding="max_length" with self.assertRaises(SCREAMING_SNAKE_CASE_ ): feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''max_length''' , truncation=SCREAMING_SNAKE_CASE_ )[input_name] # test truncation for `pad_to_multiple_of` for List[int] + numpy A: int = 12 A: Any = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , ) A: List[Any] = input_a[input_name] A: Optional[Any] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , ) A: Optional[int] = input_a[input_name] # retrieve expected_length as multiple of pad_to_multiple_of A: str = len(speech_inputs[0] ) if expected_length % pad_to_multiple_of != 0: A: Tuple = ((len(speech_inputs[0] ) // pad_to_multiple_of) + 1) * pad_to_multiple_of self.assertTrue(len(input_a[0] ) == expected_length ) self.assertTrue(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) self.assertFalse(_inputs_have_equal_length(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Any ) -> List[Any]: '''simple docstring''' self._check_padding(numpify=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' self._check_padding(numpify=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' self._check_truncation(numpify=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> Optional[Any]: '''simple docstring''' self._check_truncation(numpify=SCREAMING_SNAKE_CASE_ ) @require_torch def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: str = self.feature_extraction_class(**self.feat_extract_dict ) A: Any = self.feat_extract_tester.prepare_inputs_for_common() A: Tuple = feat_extract.model_input_names[0] A: str = BatchFeature({input_name: speech_inputs} ) A: Union[str, Any] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''np''' )[input_name] A: List[str] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) @require_tf def _snake_case ( self : List[str] ) -> Any: '''simple docstring''' A: Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) A: Tuple = self.feat_extract_tester.prepare_inputs_for_common() A: int = feat_extract.model_input_names[0] A: Tuple = BatchFeature({input_name: speech_inputs} ) A: Optional[int] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''np''' )[input_name] A: Optional[int] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''tf''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_tf.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def _snake_case ( self : List[Any] ) -> int: '''simple docstring''' A: Union[str, Any] = self.feat_extract_dict A: Optional[Any] = True A: Dict = self.feature_extraction_class(**SCREAMING_SNAKE_CASE_ ) A: List[Any] = self.feat_extract_tester.prepare_inputs_for_common() A: Union[str, Any] = [len(SCREAMING_SNAKE_CASE_ ) for x in speech_inputs] A: str = feat_extract.model_input_names[0] A: str = BatchFeature({input_name: speech_inputs} ) A: List[Any] = feat_extract.pad(SCREAMING_SNAKE_CASE_ , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Tuple: '''simple docstring''' A: Optional[int] = self.feat_extract_dict A: Any = True A: Union[str, Any] = self.feature_extraction_class(**SCREAMING_SNAKE_CASE_ ) A: List[Any] = self.feat_extract_tester.prepare_inputs_for_common() A: Dict = [len(SCREAMING_SNAKE_CASE_ ) for x in speech_inputs] A: Union[str, Any] = feat_extract.model_input_names[0] A: Optional[int] = BatchFeature({input_name: speech_inputs} ) A: Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ) A: List[Any] = feat_extract.pad( SCREAMING_SNAKE_CASE_ , padding='''max_length''' , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , return_tensors='''np''' ) self.assertIn('''attention_mask''' , SCREAMING_SNAKE_CASE_ ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] )
334
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
1
'''simple docstring''' from __future__ import annotations import numpy as np def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''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 UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' from __future__ import annotations from functools import lru_cache from math import ceil UpperCamelCase = 100 UpperCamelCase = set(range(3, NUM_PRIMES, 2)) primes.add(2) UpperCamelCase = 42 for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=1_0_0 ) def SCREAMING_SNAKE_CASE( __lowercase ) -> set[int]: if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} A: set[int] = set() A: int A: int for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def SCREAMING_SNAKE_CASE( __lowercase = 5_0_0_0 ) -> int | None: for number_to_partition in range(1 , __lowercase ): if len(partition(__lowercase ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(f'{solution() = }')
334
'''simple docstring''' from __future__ import annotations from typing import Any class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' pass class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> None: '''simple docstring''' A: Any = data A: Node | None = None def __iter__( self : Optional[int] ) -> List[str]: '''simple docstring''' A: List[str] = self A: Dict = [] while node: if node in visited: raise ContainsLoopError visited.append(SCREAMING_SNAKE_CASE_ ) yield node.data A: str = node.next_node @property def _snake_case ( self : List[str] ) -> bool: '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCamelCase = Node(1) UpperCamelCase = Node(2) UpperCamelCase = Node(3) UpperCamelCase = Node(4) print(root_node.has_loop) # False UpperCamelCase = root_node.next_node print(root_node.has_loop) # True UpperCamelCase = Node(5) UpperCamelCase = Node(6) UpperCamelCase = Node(5) UpperCamelCase = Node(6) print(root_node.has_loop) # False UpperCamelCase = Node(1) print(root_node.has_loop) # False
334
1
'''simple docstring''' import argparse import os # New Code # 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 from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, 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) # # 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 # ######################################################################## UpperCamelCase = 16 UpperCamelCase = 32 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = 1_6 ) -> List[str]: A: Optional[Any] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) A: Any = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__lowercase ): # max_length=None => use the model max length (it's actually the default) A: Optional[int] = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__lowercase , max_length=__lowercase ) 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(): A: List[Any] = datasets.map( __lowercase , batched=__lowercase , 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 A: str = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__lowercase ): # On TPU it's best to pad everything to the same length or training will be very slow. A: List[str] = 1_2_8 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A: Optional[Any] = 1_6 elif accelerator.mixed_precision != "no": A: Union[str, Any] = 8 else: A: Dict = None return tokenizer.pad( __lowercase , padding='''longest''' , max_length=__lowercase , pad_to_multiple_of=__lowercase , return_tensors='''pt''' , ) # Instantiate dataloaders. A: Dict = DataLoader( tokenized_datasets['''train'''] , shuffle=__lowercase , collate_fn=__lowercase , batch_size=__lowercase ) A: Optional[Any] = DataLoader( tokenized_datasets['''validation'''] , shuffle=__lowercase , collate_fn=__lowercase , batch_size=__lowercase ) 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 UpperCamelCase = mocked_dataloaders # noqa: F811 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __lowercase ) == "1": A: Dict = 2 # Initialize accelerator A: Optional[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A: Dict = config['''lr'''] A: Any = int(config['''num_epochs'''] ) A: Optional[Any] = int(config['''seed'''] ) A: Dict = int(config['''batch_size'''] ) A: str = evaluate.load('''glue''' , '''mrpc''' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=__lowercase ) def inner_training_loop(__lowercase ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(__lowercase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A: List[str] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__lowercase ) # 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). A: Optional[Any] = model.to(accelerator.device ) # Instantiate optimizer A: Any = AdamW(params=model.parameters() , lr=__lowercase ) A , A: str = get_dataloaders(__lowercase , __lowercase ) # Instantiate scheduler A: Union[str, Any] = get_linear_schedule_with_warmup( optimizer=__lowercase , num_warmup_steps=1_0_0 , num_training_steps=(len(__lowercase ) * num_epochs) , ) # 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. A , A , A , A , A: List[str] = accelerator.prepare( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) # Now we train the model for epoch in range(__lowercase ): model.train() for step, batch in enumerate(__lowercase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) A: int = model(**__lowercase ) A: Optional[Any] = outputs.loss accelerator.backward(__lowercase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(__lowercase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A: int = model(**__lowercase ) A: List[str] = outputs.logits.argmax(dim=-1 ) A , A: Optional[Any] = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__lowercase , references=__lowercase , ) A: Dict = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , __lowercase ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def SCREAMING_SNAKE_CASE( ) -> Optional[int]: A: int = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__lowercase , default=__lowercase , 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.''' ) A: Tuple = parser.parse_args() A: Dict = {'''lr''': 2E-5, '''num_epochs''': 3, '''seed''': 4_2, '''batch_size''': 1_6} training_function(__lowercase , __lowercase ) if __name__ == "__main__": main()
334
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase = 4 ) -> list[list[int]]: A: Tuple = abs(__lowercase ) or 4 return [[1 + x + y * row_size for x in range(__lowercase )] for y in range(__lowercase )] def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(transpose(__lowercase ) ) # OR.. transpose(reverse_column(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(reverse_column(__lowercase ) ) # OR.. reverse_column(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_column(transpose(__lowercase ) ) # OR.. transpose(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Union[str, Any] = [list(__lowercase ) for x in zip(*__lowercase )] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[int] = matrix[::-1] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[Any] = [x[::-1] for x in matrix] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> None: for i in matrix: print(*__lowercase ) if __name__ == "__main__": UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
334
1
'''simple docstring''' import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : List[Any] ) -> Dict: '''simple docstring''' A: Optional[Any] = inspect.getfile(accelerate.test_utils ) A: int = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) A: Dict = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def _snake_case ( self : str ) -> Dict: '''simple docstring''' A: List[Any] = f""" {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} """.split() A: Any = [sys.executable] + distributed_args execute_subprocess_async(SCREAMING_SNAKE_CASE_ , env=os.environ.copy() )
334
'''simple docstring''' from __future__ import annotations import numpy as np def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> float: if digit_amount > 0: return round(number - int(__lowercase ) , __lowercase ) return number - int(__lowercase ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.3_45, 1)) print(decimal_isolate(35.3_45, 2)) print(decimal_isolate(35.3_45, 3)) print(decimal_isolate(-14.7_89, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.1_23, 1)) print(decimal_isolate(-14.1_23, 2)) print(decimal_isolate(-14.1_23, 3))
334
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
1
'''simple docstring''' import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class lowerCAmelCase_ : '''simple docstring''' @staticmethod def _snake_case ( *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: '''simple docstring''' pass def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. UpperCamelCase = ( '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png''' ) @is_pipeline_test @require_torch @require_vision class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Any = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[str] ) -> List[Any]: '''simple docstring''' A: Optional[Any] = pipeline( '''document-question-answering''' , model=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ ) A: List[str] = INVOICE_URL A: int = list(zip(*apply_tesseract(load_image(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , '''''' ) ) ) A: Union[str, Any] = '''What is the placebo?''' A: Union[str, Any] = [ { '''image''': load_image(SCREAMING_SNAKE_CASE_ ), '''question''': question, }, { '''image''': image, '''question''': question, }, { '''image''': image, '''question''': question, '''word_boxes''': word_boxes, }, ] return dqa_pipeline, examples def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict ) -> int: '''simple docstring''' A: Any = dqa_pipeline(SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual( SCREAMING_SNAKE_CASE_ , [ [ {'''score''': ANY(SCREAMING_SNAKE_CASE_ ), '''answer''': ANY(SCREAMING_SNAKE_CASE_ ), '''start''': ANY(SCREAMING_SNAKE_CASE_ ), '''end''': ANY(SCREAMING_SNAKE_CASE_ )}, {'''score''': ANY(SCREAMING_SNAKE_CASE_ ), '''answer''': ANY(SCREAMING_SNAKE_CASE_ ), '''start''': ANY(SCREAMING_SNAKE_CASE_ ), '''end''': ANY(SCREAMING_SNAKE_CASE_ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: List[Any] = pipeline('''document-question-answering''' , model='''hf-internal-testing/tiny-random-layoutlmv2''' ) A: Union[str, Any] = INVOICE_URL A: Optional[Any] = '''How many cats are there?''' A: List[Any] = [ {'''score''': 0.0001, '''answer''': '''oy 2312/2019''', '''start''': 38, '''end''': 39}, {'''score''': 0.0001, '''answer''': '''oy 2312/2019 DUE''', '''start''': 38, '''end''': 40}, ] A: str = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , SCREAMING_SNAKE_CASE_ ) A: int = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , SCREAMING_SNAKE_CASE_ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably A: str = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' A: Dict = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) # We can optionnally pass directly the words and bounding boxes A: Optional[int] = '''./tests/fixtures/tests_samples/COCO/000000039769.png''' A: Union[str, Any] = [] A: str = [] A: Any = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , words=SCREAMING_SNAKE_CASE_ , boxes=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , ) A: int = INVOICE_URL A: Optional[int] = '''What is the invoice number?''' A: str = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A: Union[str, Any] = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A: int = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ [ {'''score''': 0.9944, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0009, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: Any = pipeline( '''document-question-answering''' , model='''tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa''' , revision='''9977165''' , max_seq_len=50 , ) A: int = INVOICE_URL A: Any = '''What is the invoice number?''' A: List[Any] = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A: str = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A: Union[str, Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ [ {'''score''': 0.9974, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, {'''score''': 0.9948, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : Dict ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Tuple = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=SCREAMING_SNAKE_CASE_ , revision='''3dc6de3''' , ) A: List[str] = INVOICE_URL A: Tuple = '''What is the invoice number?''' A: Union[str, Any] = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) A: Tuple = dqa_pipeline({'''image''': image, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) A: int = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] ] * 2 , ) A: List[str] = list(zip(*apply_tesseract(load_image(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , '''''' ) ) ) # This model should also work if `image` is set to None A: List[Any] = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.4251, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.0819, '''answer''': '''1110212019''', '''start''': 23, '''end''': 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: Tuple = AutoTokenizer.from_pretrained( '''impira/layoutlm-document-qa''' , revision='''3dc6de3''' , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Tuple = pipeline( '''document-question-answering''' , model='''impira/layoutlm-document-qa''' , tokenizer=SCREAMING_SNAKE_CASE_ , revision='''3dc6de3''' , max_seq_len=50 , ) A: Dict = INVOICE_URL A: Dict = '''What is the invoice number?''' A: Dict = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) A: List[Any] = dqa_pipeline( [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] ] * 2 , ) A: Any = list(zip(*apply_tesseract(load_image(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , '''''' ) ) ) # This model should also work if `image` is set to None A: int = dqa_pipeline({'''image''': None, '''word_boxes''': word_boxes, '''question''': question} , top_k=2 ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [ {'''score''': 0.9999, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, {'''score''': 0.9998, '''answer''': '''us-001''', '''start''': 16, '''end''': 16}, ] , ) @slow @require_torch def _snake_case ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' A: Optional[Any] = pipeline( '''document-question-answering''' , model='''naver-clova-ix/donut-base-finetuned-docvqa''' , tokenizer=AutoTokenizer.from_pretrained('''naver-clova-ix/donut-base-finetuned-docvqa''' ) , feature_extractor='''naver-clova-ix/donut-base-finetuned-docvqa''' , ) A: int = INVOICE_URL A: str = '''What is the invoice number?''' A: Optional[Any] = dqa_pipeline(image=SCREAMING_SNAKE_CASE_ , question=SCREAMING_SNAKE_CASE_ , top_k=2 ) self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE_ , decimals=4 ) , [{'''answer''': '''us-001'''}] ) @require_tf @unittest.skip('''Document question answering not implemented in TF''' ) def _snake_case ( self : Dict ) -> Tuple: '''simple docstring''' pass
334
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
334
1
'''simple docstring''' # limitations under the License. from typing import Optional, Tuple, Union import torch from diffusers import DiffusionPipeline, ImagePipelineOutput class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Optional[int]: '''simple docstring''' super().__init__() self.register_modules(unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : Optional[torch.Generator] = None , SCREAMING_SNAKE_CASE_ : int = 50 , SCREAMING_SNAKE_CASE_ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE_ : bool = True , **SCREAMING_SNAKE_CASE_ : str , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' A: Optional[int] = torch.randn( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=SCREAMING_SNAKE_CASE_ , ) A: str = image.to(self.device ) # set step values self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_ ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output A: Any = self.unet(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).sample # 2. predict previous mean of image x_t-1 and add variance depending on eta # eta corresponds to η in paper and should be between [0, 1] # do x_t -> x_t-1 A: List[Any] = self.scheduler.step(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).prev_sample A: List[str] = (image / 2 + 0.5).clamp(0 , 1 ) A: Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": A: Union[str, Any] = self.numpy_to_pil(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (image,), "This is a local test" return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE_ ), "This is a local test"
334
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 6_55_36 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : str = "fourier" , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , SCREAMING_SNAKE_CASE_ : Tuple[str] = "UNetMidBlock1D" , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : Tuple[int] = (32, 32, 64) , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[Any] = sample_size # time if time_embedding_type == "fourier": A: Tuple = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=SCREAMING_SNAKE_CASE_ , log=SCREAMING_SNAKE_CASE_ , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ ) A: List[str] = 2 * block_out_channels[0] elif time_embedding_type == "positional": A: str = Timesteps( block_out_channels[0] , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ , downscale_freq_shift=SCREAMING_SNAKE_CASE_ ) A: Any = block_out_channels[0] if use_timestep_embedding: A: Optional[Any] = block_out_channels[0] * 4 A: List[Any] = TimestepEmbedding( in_channels=SCREAMING_SNAKE_CASE_ , time_embed_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , out_dim=block_out_channels[0] , ) A: Optional[Any] = nn.ModuleList([] ) A: str = None A: str = nn.ModuleList([] ) A: Tuple = None # down A: Any = in_channels for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = output_channel A: List[Any] = block_out_channels[i] if i == 0: input_channel += extra_in_channels A: List[Any] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[int] = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid A: Union[str, Any] = get_mid_block( SCREAMING_SNAKE_CASE_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=SCREAMING_SNAKE_CASE_ , add_downsample=SCREAMING_SNAKE_CASE_ , ) # up A: Optional[Any] = list(reversed(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = reversed_block_out_channels[0] if out_block_type is None: A: int = out_channels else: A: Union[str, Any] = block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: List[Any] = output_channel A: int = ( reversed_block_out_channels[i + 1] if i < len(SCREAMING_SNAKE_CASE_ ) - 1 else final_upsample_channels ) A: Optional[int] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[Any] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) A: Any = output_channel # out A: List[str] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) A: Optional[int] = get_out_block( out_block_type=SCREAMING_SNAKE_CASE_ , num_groups_out=SCREAMING_SNAKE_CASE_ , embed_dim=block_out_channels[0] , out_channels=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , fc_dim=block_out_channels[-1] // 4 , ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[UNetaDOutput, Tuple]: '''simple docstring''' A: Any = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: List[str] = timesteps[None].to(sample.device ) A: int = self.time_proj(SCREAMING_SNAKE_CASE_ ) if self.config.use_timestep_embedding: A: List[Any] = self.time_mlp(SCREAMING_SNAKE_CASE_ ) else: A: str = timestep_embed[..., None] A: Union[str, Any] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) A: Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down A: List[str] = () for downsample_block in self.down_blocks: A , A: Optional[int] = downsample_block(hidden_states=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: A: Dict = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): A: List[Any] = down_block_res_samples[-1:] A: List[str] = down_block_res_samples[:-1] A: Optional[int] = upsample_block(SCREAMING_SNAKE_CASE_ , res_hidden_states_tuple=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) # 5. post-process if self.out_block: A: Any = self.out_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> None: '''simple docstring''' warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
334
'''simple docstring''' from collections import deque class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> None: '''simple docstring''' A: Union[str, Any] = process_name # process name A: List[str] = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A: Dict = arrival_time A: Optional[Any] = burst_time # remaining burst time A: Any = 0 # total time of the process wait in ready queue A: Any = 0 # time from arrival time to completion time class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int , ) -> None: '''simple docstring''' A: Dict = number_of_queues # time slice of queues that round robin algorithm applied A: int = time_slices # unfinished process is in this ready_queue A: Tuple = queue # current time A: int = current_time # finished process is in this sequence queue A: deque[Process] = deque() def _snake_case ( self : List[Any] ) -> list[str]: '''simple docstring''' A: str = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Optional[int] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Any = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: List[Any] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): completion_times.append(queue[i].stop_time ) return completion_times def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> list[int]: '''simple docstring''' return [q.burst_time for q in queue] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Process ) -> int: '''simple docstring''' process.waiting_time += self.current_time - process.stop_time return process.waiting_time def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> deque[Process]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of finished process while len(SCREAMING_SNAKE_CASE_ ) != 0: A: Optional[Any] = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A: Any = 0 # set the process's turnaround time because it is finished A: int = self.current_time - cp.arrival_time # set the completion time A: List[str] = self.current_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int ) -> tuple[deque[Process], deque[Process]]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(SCREAMING_SNAKE_CASE_ ) ): A: Dict = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A: Optional[Any] = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(SCREAMING_SNAKE_CASE_ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A: int = 0 # set the finish time A: Union[str, Any] = self.current_time # update the process' turnaround time because it is finished A: Tuple = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def _snake_case ( self : Optional[Any] ) -> deque[Process]: '''simple docstring''' for i in range(self.number_of_queues - 1 ): A , A: Optional[Any] = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) UpperCamelCase = MLFQ(number_of_queues, time_slices, queue, 0) UpperCamelCase = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f'waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print completion times of processes(P1, P2, P3, P4) print( f'completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print total turnaround times of processes(P1, P2, P3, P4) print( f'turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print sequence of finished processes print( f'sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}' )
334
1
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : list = field(default_factory=UpperCAmelCase_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tensor , SCREAMING_SNAKE_CASE_ : Tensor ) -> int: '''simple docstring''' A: List[str] = len(list(m.modules() ) ) == 1 or isinstance(SCREAMING_SNAKE_CASE_ , nn.Convad ) or isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ) if has_not_submodules: self.traced.append(SCREAMING_SNAKE_CASE_ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tensor ) -> Dict: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(SCREAMING_SNAKE_CASE_ ) [x.remove() for x in self.handles] return self @property def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return list(filter(lambda SCREAMING_SNAKE_CASE_ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 0 UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) def __call__( self : Any , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[Any]: '''simple docstring''' A: Dict = Tracker(self.dest )(SCREAMING_SNAKE_CASE_ ).parametrized A: Tuple = Tracker(self.src )(SCREAMING_SNAKE_CASE_ ).parametrized A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.src_skip , SCREAMING_SNAKE_CASE_ ) ) A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.dest_skip , SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise Exception( f"""Numbers of operations are different. Source module has {len(SCREAMING_SNAKE_CASE_ )} operations while""" f""" destination module has {len(SCREAMING_SNAKE_CASE_ )}.""" ) for dest_m, src_m in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"""Transfered from={src_m} to={dest_m}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase = True ) -> Any: print(F"""Converting {name}...""" ) with torch.no_grad(): A: Union[str, Any] = timm.create_model(__lowercase , pretrained=__lowercase ).eval() A: List[str] = ResNetForImageClassification(__lowercase ).eval() A: int = ModuleTransfer(src=__lowercase , dest=__lowercase ) A: List[str] = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(__lowercase ) assert torch.allclose(from_model(__lowercase ) , our_model(__lowercase ).logits ), "The model logits don't match the original one." A: str = F"""resnet{'-'.join(name.split('resnet' ) )}""" print(__lowercase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add model''' , use_temp_dir=__lowercase , ) # we can use the convnext one A: Any = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add image processor''' , use_temp_dir=__lowercase , ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> List[Any]: A: Union[str, Any] = '''imagenet-1k-id2label.json''' A: Union[str, Any] = 1_0_0_0 A: Optional[int] = (1, num_labels) A: Dict = '''huggingface/label-files''' A: Any = num_labels A: Union[str, Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Optional[int] = {int(__lowercase ): v for k, v in idalabel.items()} A: Optional[int] = idalabel A: List[str] = {v: k for k, v in idalabel.items()} A: str = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Optional[Any] = { '''resnet18''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet26''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet34''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet50''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet101''': ImageNetPreTrainedConfig( depths=[3, 4, 2_3, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet152''': ImageNetPreTrainedConfig( depths=[3, 8, 3_6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), } if model_name: convert_weight_and_push(__lowercase , names_to_config[model_name] , __lowercase , __lowercase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(__lowercase , __lowercase , __lowercase , __lowercase ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,''' ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
1
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase = 1_0_0_0 ) -> int: A: int = -1 A: 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 A: List[str] = (n * n - 2 * a * n) // (2 * n - 2 * a) A: Union[str, Any] = n - a - b if c * c == (a * a + b * b): A: Any = a * b * c if candidate >= product: A: Dict = candidate return product if __name__ == "__main__": print(f'{solution() = }')
334
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
1
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : list ) -> None: '''simple docstring''' A: Any = set_counts A: Optional[int] = max(SCREAMING_SNAKE_CASE_ ) A: int = len(SCREAMING_SNAKE_CASE_ ) A: List[str] = [1] * num_sets A: List[Any] = list(range(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> bool: '''simple docstring''' A: List[str] = self.get_parent(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = self.get_parent(SCREAMING_SNAKE_CASE_ ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] A: Tuple = 0 A: Optional[int] = dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 A: str = self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] A: Optional[int] = 0 A: Optional[int] = src_parent A: Tuple = self.set_counts[src_parent] A: Any = max(self.max_set , SCREAMING_SNAKE_CASE_ ) return True def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' if self.parents[disj_set] == disj_set: return disj_set A: List[Any] = self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
334
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = len(__lowercase ) for i in range(length - 1 ): A: Dict = i for k in range(i + 1 , __lowercase ): if collection[k] < collection[least]: A: List[str] = k if least != i: A , A: Tuple = (collection[i], collection[least]) return collection if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
334
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { '''microsoft/table-transformer-detection''': ( '''https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json''' ), } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = """table-transformer""" UpperCamelCase_ : str = ["""past_key_values"""] UpperCamelCase_ : int = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Any=3 , SCREAMING_SNAKE_CASE_ : Tuple=1_00 , SCREAMING_SNAKE_CASE_ : int=6 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=20_48 , SCREAMING_SNAKE_CASE_ : Tuple=8 , SCREAMING_SNAKE_CASE_ : Tuple=6 , SCREAMING_SNAKE_CASE_ : Tuple=20_48 , SCREAMING_SNAKE_CASE_ : Tuple=8 , SCREAMING_SNAKE_CASE_ : List[Any]=0.0 , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.0 , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : int="relu" , SCREAMING_SNAKE_CASE_ : Optional[Any]=2_56 , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : Any=0.0 , SCREAMING_SNAKE_CASE_ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE_ : List[Any]=0.02 , SCREAMING_SNAKE_CASE_ : List[Any]=1.0 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False , SCREAMING_SNAKE_CASE_ : str="sine" , SCREAMING_SNAKE_CASE_ : int="resnet50" , SCREAMING_SNAKE_CASE_ : List[str]=True , SCREAMING_SNAKE_CASE_ : int=False , SCREAMING_SNAKE_CASE_ : str=1 , SCREAMING_SNAKE_CASE_ : Tuple=5 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : int=1 , SCREAMING_SNAKE_CASE_ : Optional[Any]=1 , SCREAMING_SNAKE_CASE_ : Dict=5 , SCREAMING_SNAKE_CASE_ : Optional[int]=2 , SCREAMING_SNAKE_CASE_ : int=0.1 , **SCREAMING_SNAKE_CASE_ : List[Any] , ) -> Any: '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) A: Optional[Any] = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): A: Tuple = backbone_config.get('''model_type''' ) A: Dict = CONFIG_MAPPING[backbone_model_type] A: Any = config_class.from_dict(SCREAMING_SNAKE_CASE_ ) # set timm attributes to None A , A , A: Optional[Any] = None, None, None A: str = use_timm_backbone A: List[Any] = backbone_config A: Tuple = num_channels A: str = num_queries A: Optional[Any] = d_model A: Dict = encoder_ffn_dim A: Optional[int] = encoder_layers A: Dict = encoder_attention_heads A: List[str] = decoder_ffn_dim A: Tuple = decoder_layers A: Tuple = decoder_attention_heads A: List[Any] = dropout A: List[Any] = attention_dropout A: Tuple = activation_dropout A: Optional[Any] = activation_function A: List[Any] = init_std A: Optional[Any] = init_xavier_std A: Dict = encoder_layerdrop A: List[str] = decoder_layerdrop A: Optional[int] = encoder_layers A: List[str] = auxiliary_loss A: List[str] = position_embedding_type A: List[Any] = backbone A: Union[str, Any] = use_pretrained_backbone A: Any = dilation # Hungarian matcher A: Tuple = class_cost A: Union[str, Any] = bbox_cost A: Dict = giou_cost # Loss coefficients A: Tuple = mask_loss_coefficient A: Optional[Any] = dice_loss_coefficient A: Optional[int] = bbox_loss_coefficient A: Dict = giou_loss_coefficient A: Optional[Any] = eos_coefficient super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) @property def _snake_case ( self : Tuple ) -> int: '''simple docstring''' return self.encoder_attention_heads @property def _snake_case ( self : Dict ) -> int: '''simple docstring''' return self.d_model class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = version.parse("""1.11""" ) @property def _snake_case ( self : int ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _snake_case ( self : Optional[Any] ) -> float: '''simple docstring''' return 1E-5 @property def _snake_case ( self : Any ) -> int: '''simple docstring''' return 12
334
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
1
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : str = field(default="""summarization""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) UpperCamelCase_ : ClassVar[Features] = Features({"""text""": Value("""string""" )} ) UpperCamelCase_ : ClassVar[Features] = Features({"""summary""": Value("""string""" )} ) UpperCamelCase_ : str = "text" UpperCamelCase_ : str = "summary" @property def _snake_case ( self : List[str] ) -> Dict[str, str]: '''simple docstring''' return {self.text_column: "text", self.summary_column: "summary"}
334
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
1
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = DebertaTokenizer UpperCamelCase_ : List[str] = True UpperCamelCase_ : int = DebertaTokenizerFast def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A: Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] A: int = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Union[str, Any] = {'''unk_token''': '''[UNK]'''} A: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: str = 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(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = '''lower newer''' A: str = '''lower newer''' return input_text, output_text def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: str = self.get_tokenizer() A: Any = '''lower newer''' A: Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] A: int = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokens + [tokenizer.unk_token] A: int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' A: str = self.get_tokenizer() A: List[str] = tokenizer('''Hello''' , '''World''' ) A: Union[str, Any] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Any = tokenizer.encode('''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: int = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: A: List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Dict = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: Any = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for seq in encoding['''input_ids''']] # fmt: off A: Any = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on A: Optional[int] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , SCREAMING_SNAKE_CASE_ ) for expected, decoded in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
334
1
'''simple docstring''' from __future__ import annotations from cmath import sqrt def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> tuple[complex, complex]: if a == 0: raise ValueError('''Coefficient \'a\' must not be zero.''' ) A: int = b * b - 4 * a * c A: Optional[int] = (-b + sqrt(_UpperCAmelCase )) / (2 * a) A: List[Any] = (-b - sqrt(_UpperCAmelCase )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def SCREAMING_SNAKE_CASE( ) -> Optional[int]: A: Union[str, Any] = quadratic_roots(a=5 , b=6 , c=1 ) print(F"""The solutions are: {solutiona} and {solutiona}""" ) if __name__ == "__main__": main()
350
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
0
def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: return int((input_a, input_a).count(0 ) == 0 ) def SCREAMING_SNAKE_CASE( ) -> None: assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
351
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = { '''configuration_mgp_str''': ['''MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MgpstrConfig'''], '''processing_mgp_str''': ['''MgpstrProcessor'''], '''tokenization_mgp_str''': ['''MgpstrTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MgpstrModel''', '''MgpstrPreTrainedModel''', '''MgpstrForSceneTextRecognition''', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
352
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
0
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class lowerCAmelCase_ ( A__ ): '''simple docstring''' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Dict: '''simple docstring''' super().__init__() self.register_modules(unet=__snake_case , scheduler=__snake_case ) @torch.no_grad() def __call__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : int = 1_00 , SCREAMING_SNAKE_CASE_ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE_ : Optional[float] = None , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[AudioPipelineOutput, Tuple]: '''simple docstring''' if audio_length_in_s is None: A: Any = self.unet.config.sample_size / self.unet.config.sample_rate A: Any = audio_length_in_s * self.unet.config.sample_rate A: List[Any] = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( f"""{audio_length_in_s} is too small. Make sure it\'s bigger or equal to""" f""" {3 * down_scale_factor / self.unet.config.sample_rate}.""" ) A: int = int(__snake_case ) if sample_size % down_scale_factor != 0: A: int = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( f"""{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled""" f""" by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising""" ''' process.''' ) A: str = int(__snake_case ) A: List[Any] = next(iter(self.unet.parameters() ) ).dtype A: Dict = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(__snake_case , __snake_case ) and len(__snake_case ) != batch_size: raise ValueError( f"""You have passed a list of generators of length {len(__snake_case )}, but requested an effective batch""" f""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) A: Optional[int] = randn_tensor(__snake_case , generator=__snake_case , device=self.device , dtype=__snake_case ) # set step values self.scheduler.set_timesteps(__snake_case , device=audio.device ) A: int = self.scheduler.timesteps.to(__snake_case ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output A: Optional[Any] = self.unet(__snake_case , __snake_case ).sample # 2. compute previous image: x_t -> t_t-1 A: List[Any] = self.scheduler.step(__snake_case , __snake_case , __snake_case ).prev_sample A: Optional[Any] = audio.clamp(-1 , 1 ).float().cpu().numpy() A: Union[str, Any] = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=__snake_case )
353
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
0
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import OwlViTImageProcessor, OwlViTProcessor @require_vision class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' A: int = tempfile.mkdtemp() # fmt: off A: Optional[int] = ['''''', '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>'''] # fmt: on A: Dict = dict(zip(_lowerCamelCase , range(len(_lowerCamelCase ) ) ) ) A: Tuple = ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', ''''''] A: Any = {'''unk_token''': '''<unk>'''} A: List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: int = 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(_lowerCamelCase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_lowerCamelCase ) ) A: Tuple = { '''do_resize''': True, '''size''': 20, '''do_center_crop''': True, '''crop_size''': 18, '''do_normalize''': True, '''image_mean''': [0.4814_5466, 0.457_8275, 0.4082_1073], '''image_std''': [0.2686_2954, 0.2613_0258, 0.2757_7711], } A: Optional[int] = os.path.join(self.tmpdirname , _lowerCamelCase ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_lowerCamelCase , _lowerCamelCase ) def _snake_case ( self : Dict , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: '''simple docstring''' return CLIPTokenizer.from_pretrained(self.tmpdirname , pad_token='''!''' , **_lowerCamelCase ) def _snake_case ( self : Dict , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' return CLIPTokenizerFast.from_pretrained(self.tmpdirname , pad_token='''!''' , **_lowerCamelCase ) def _snake_case ( self : Dict , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' return OwlViTImageProcessor.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def _snake_case ( self : int ) -> int: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def _snake_case ( self : int ) -> List[Any]: '''simple docstring''' A: Tuple = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A: Dict = [Image.fromarray(np.moveaxis(_lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' A: Dict = self.get_tokenizer() A: List[str] = self.get_rust_tokenizer() A: Optional[int] = self.get_image_processor() A: Tuple = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) processor_slow.save_pretrained(self.tmpdirname ) A: str = OwlViTProcessor.from_pretrained(self.tmpdirname , use_fast=_lowerCamelCase ) A: Dict = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) processor_fast.save_pretrained(self.tmpdirname ) A: Optional[int] = OwlViTProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _lowerCamelCase ) self.assertIsInstance(processor_fast.tokenizer , _lowerCamelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _lowerCamelCase ) self.assertIsInstance(processor_fast.image_processor , _lowerCamelCase ) def _snake_case ( self : str ) -> Any: '''simple docstring''' A: Tuple = OwlViTProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A: Tuple = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) A: List[str] = self.get_image_processor(do_normalize=_lowerCamelCase ) A: Dict = OwlViTProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_lowerCamelCase ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _lowerCamelCase ) def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: Tuple = self.get_image_processor() A: Any = self.get_tokenizer() A: Optional[Any] = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A: Optional[int] = self.prepare_image_inputs() A: List[Any] = image_processor(_lowerCamelCase , return_tensors='''np''' ) A: Dict = processor(images=_lowerCamelCase , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def _snake_case ( self : Dict ) -> str: '''simple docstring''' A: str = self.get_image_processor() A: Optional[int] = self.get_tokenizer() A: List[Any] = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A: Union[str, Any] = '''lower newer''' A: Optional[int] = processor(text=_lowerCamelCase , return_tensors='''np''' ) A: int = tokenizer(_lowerCamelCase , return_tensors='''np''' ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key][0].tolist() , encoded_processor[key][0].tolist() ) def _snake_case ( self : Optional[Any] ) -> Dict: '''simple docstring''' A: Tuple = self.get_image_processor() A: Optional[Any] = self.get_tokenizer() A: Optional[Any] = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A: str = '''lower newer''' A: List[Any] = self.prepare_image_inputs() A: Dict = processor(text=_lowerCamelCase , images=_lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_lowerCamelCase ): processor() def _snake_case ( self : List[str] ) -> Dict: '''simple docstring''' A: Tuple = '''google/owlvit-base-patch32''' A: Tuple = OwlViTProcessor.from_pretrained(_lowerCamelCase ) A: int = ['''cat''', '''nasa badge'''] A: int = processor(text=_lowerCamelCase ) A: Optional[Any] = 16 self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''attention_mask'''] ) self.assertEqual(inputs['''input_ids'''].shape , (2, seq_length) ) # test if it raises when no input is passed with pytest.raises(_lowerCamelCase ): processor() def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' A: int = '''google/owlvit-base-patch32''' A: Any = OwlViTProcessor.from_pretrained(_lowerCamelCase ) A: Tuple = [['''cat''', '''nasa badge'''], ['''person''']] A: int = processor(text=_lowerCamelCase ) A: Optional[int] = 16 A: int = len(_lowerCamelCase ) A: str = max([len(_lowerCamelCase ) for texts in input_texts] ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''attention_mask'''] ) self.assertEqual(inputs['''input_ids'''].shape , (batch_size * num_max_text_queries, seq_length) ) # test if it raises when no input is passed with pytest.raises(_lowerCamelCase ): processor() def _snake_case ( self : str ) -> int: '''simple docstring''' A: List[Any] = '''google/owlvit-base-patch32''' A: List[str] = OwlViTProcessor.from_pretrained(_lowerCamelCase ) A: int = ['''cat''', '''nasa badge'''] A: Optional[int] = processor(text=_lowerCamelCase ) A: str = 16 A: int = inputs['''input_ids'''] A: List[str] = [ [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [4_94_06, 68_41, 1_13_01, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''attention_mask'''] ) self.assertEqual(inputs['''input_ids'''].shape , (2, seq_length) ) self.assertListEqual(list(input_ids[0] ) , predicted_ids[0] ) self.assertListEqual(list(input_ids[1] ) , predicted_ids[1] ) def _snake_case ( self : Any ) -> List[str]: '''simple docstring''' A: Dict = self.get_image_processor() A: Tuple = self.get_tokenizer() A: Tuple = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A: int = self.prepare_image_inputs() A: Optional[Any] = self.prepare_image_inputs() A: Optional[int] = processor(images=_lowerCamelCase , query_images=_lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ['''query_pixel_values''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_lowerCamelCase ): processor() def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: Optional[int] = self.get_image_processor() A: Tuple = self.get_tokenizer() A: str = OwlViTProcessor(tokenizer=_lowerCamelCase , image_processor=_lowerCamelCase ) A: Union[str, Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A: Any = processor.batch_decode(_lowerCamelCase ) A: List[str] = tokenizer.batch_decode(_lowerCamelCase ) self.assertListEqual(_lowerCamelCase , _lowerCamelCase )
354
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
0
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase : float , __lowercase : int ) -> int: if digit_amount > 0: return round(number - int(__a ) , __a ) return number - int(__a ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.3_45, 1)) print(decimal_isolate(35.3_45, 2)) print(decimal_isolate(35.3_45, 3)) print(decimal_isolate(-14.7_89, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.1_23, 1)) print(decimal_isolate(-14.1_23, 2)) print(decimal_isolate(-14.1_23, 3))
355
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for data in source_data: for i, el in enumerate(__lowercase ): if len(__lowercase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__lowercase ) ) return data_lists def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for dlist, weight in zip(__lowercase , __lowercase ): A: List[str] = min(__lowercase ) A: Union[str, Any] = max(__lowercase ) A: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A: List[str] = F"""Invalid weight of {weight:f} provided""" raise ValueError(__lowercase ) score_lists.append(__lowercase ) return score_lists def SCREAMING_SNAKE_CASE( __lowercase ) -> list[float]: A: list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__lowercase ): A: str = final_scores[j] + ele return final_scores def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: Any = get_data(__lowercase ) A: str = calculate_each_score(__lowercase , __lowercase ) A: int = generate_final_scores(__lowercase ) # append scores to source data for i, ele in enumerate(__lowercase ): source_data[i].append(__lowercase ) return source_data
334
0
import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self : Any , *SCREAMING_SNAKE_CASE_ : Optional[Any] , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Dict: '''simple docstring''' warnings.warn( '''The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use LayoutLMv2ImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
356
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase = { "configuration_instructblip": [ "INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP", "InstructBlipConfig", "InstructBlipQFormerConfig", "InstructBlipVisionConfig", ], "processing_instructblip": ["InstructBlipProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ "INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST", "InstructBlipQFormerModel", "InstructBlipPreTrainedModel", "InstructBlipForConditionalGeneration", "InstructBlipVisionModel", ] if TYPE_CHECKING: from .configuration_instructblip import ( INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, InstructBlipConfig, InstructBlipQFormerConfig, InstructBlipVisionConfig, ) from .processing_instructblip import InstructBlipProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_instructblip import ( INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST, InstructBlipForConditionalGeneration, InstructBlipPreTrainedModel, InstructBlipQFormerModel, InstructBlipVisionModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
357
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCamelCase = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''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 UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
0
'''simple docstring''' from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def SCREAMING_SNAKE_CASE( __lowercase = "isbn/0140328726" ) -> Optional[Any]: A: Tuple = olid.strip().strip('''/''' ) # Remove leading/trailing whitespace & slashes if new_olid.count('''/''' ) != 1: A: int = F"""{olid} is not a valid Open Library olid""" raise ValueError(lowerCamelCase_ ) return requests.get(F"""https://openlibrary.org/{new_olid}.json""" ).json() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: A: List[str] = { """title""": """Title""", """publish_date""": """Publish date""", """authors""": """Authors""", """number_of_pages""": """Number of pages:""", """first_sentence""": """First sentence""", """isbn_10""": """ISBN (10)""", """isbn_13""": """ISBN (13)""", } A: Optional[int] = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} A: List[Any] = [ get_openlibrary_data(author['''key'''] )["""name"""] for author in data["""Authors"""] ] A: Optional[int] = data["""First sentence"""]["""value"""] for key, value in data.items(): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): A: str = """, """.join(lowerCamelCase_ ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: UpperCamelCase = input('''\nEnter the ISBN code to search (or \'quit\' to stop): ''').strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (10, 13) or not isbn.isdigit(): print(f'Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.') continue print(f'\nSearching Open Library for ISBN: {isbn}...\n') try: UpperCamelCase = summarize_book(get_openlibrary_data(f'isbn/{isbn}')) print('''\n'''.join(f'{key}: {value}' for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(f'Sorry, there are no results for ISBN: {isbn}.')
358
'''simple docstring''' from __future__ import annotations from typing import Any class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' pass class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Any ) -> None: '''simple docstring''' A: Any = data A: Node | None = None def __iter__( self : Optional[int] ) -> List[str]: '''simple docstring''' A: List[str] = self A: Dict = [] while node: if node in visited: raise ContainsLoopError visited.append(SCREAMING_SNAKE_CASE_ ) yield node.data A: str = node.next_node @property def _snake_case ( self : List[str] ) -> bool: '''simple docstring''' try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCamelCase = Node(1) UpperCamelCase = Node(2) UpperCamelCase = Node(3) UpperCamelCase = Node(4) print(root_node.has_loop) # False UpperCamelCase = root_node.next_node print(root_node.has_loop) # True UpperCamelCase = Node(5) UpperCamelCase = Node(6) UpperCamelCase = Node(5) UpperCamelCase = Node(6) print(root_node.has_loop) # False UpperCamelCase = Node(1) print(root_node.has_loop) # False
334
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase = { """configuration_graphormer""": ["""GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GraphormerConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """GraphormerForGraphClassification""", """GraphormerModel""", """GraphormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
359
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE( __lowercase = 4 ) -> list[list[int]]: A: Tuple = abs(__lowercase ) or 4 return [[1 + x + y * row_size for x in range(__lowercase )] for y in range(__lowercase )] def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(transpose(__lowercase ) ) # OR.. transpose(reverse_column(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_row(reverse_column(__lowercase ) ) # OR.. reverse_column(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: return reverse_column(transpose(__lowercase ) ) # OR.. transpose(reverse_row(matrix)) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Union[str, Any] = [list(__lowercase ) for x in zip(*__lowercase )] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[int] = matrix[::-1] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[int]]: A: Optional[Any] = [x[::-1] for x in matrix] return matrix def SCREAMING_SNAKE_CASE( __lowercase ) -> None: for i in matrix: print(*__lowercase ) if __name__ == "__main__": UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 90 counterclockwise:\n''') print_matrix(rotate_aa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 180:\n''') print_matrix(rotate_aaa(matrix)) UpperCamelCase = make_matrix() print('''\norigin:\n''') print_matrix(matrix) print('''\nrotate 270 counterclockwise:\n''') print_matrix(rotate_aaa(matrix))
334
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase = { '''configuration_groupvit''': [ '''GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GroupViTConfig''', '''GroupViTOnnxConfig''', '''GroupViTTextConfig''', '''GroupViTVisionConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GroupViTModel''', '''GroupViTPreTrainedModel''', '''GroupViTTextModel''', '''GroupViTVisionModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFGroupViTModel''', '''TFGroupViTPreTrainedModel''', '''TFGroupViTTextModel''', '''TFGroupViTVisionModel''', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
360
'''simple docstring''' from __future__ import annotations import numpy as np def SCREAMING_SNAKE_CASE( __lowercase ) -> Dict: return np.maximum(0 , __lowercase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
334
0
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCamelCase = { '''configuration_vivit''': ['''VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VivitConfig'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VivitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''VivitModel''', '''VivitPreTrainedModel''', '''VivitForVideoClassification''', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
361
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
0
'''simple docstring''' from __future__ import annotations import unittest import numpy as np from transformers import OPTConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, 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 GPTaTokenizer, TFOPTForCausalLM, TFOPTModel def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=None , __lowercase=None ) -> str: if attention_mask is None: A: List[str] = tf.cast(tf.math.not_equal(lowercase__ , config.pad_token_id ) , tf.inta ) return {"input_ids": input_ids, "attention_mask": attention_mask} @require_tf class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Union[str, Any] = OPTConfig UpperCamelCase_ : int = {} UpperCamelCase_ : Union[str, Any] = """gelu""" def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any]=13 , SCREAMING_SNAKE_CASE_ : str=7 , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : int=False , SCREAMING_SNAKE_CASE_ : Any=99 , SCREAMING_SNAKE_CASE_ : Tuple=16 , SCREAMING_SNAKE_CASE_ : Tuple=2 , SCREAMING_SNAKE_CASE_ : Optional[int]=4 , SCREAMING_SNAKE_CASE_ : List[Any]=4 , SCREAMING_SNAKE_CASE_ : Dict="gelu" , SCREAMING_SNAKE_CASE_ : str=0.1 , SCREAMING_SNAKE_CASE_ : List[Any]=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=20 , SCREAMING_SNAKE_CASE_ : Optional[int]=2 , SCREAMING_SNAKE_CASE_ : str=1 , SCREAMING_SNAKE_CASE_ : str=0 , SCREAMING_SNAKE_CASE_ : List[Any]=16 , SCREAMING_SNAKE_CASE_ : Optional[Any]=16 , ) -> Optional[int]: '''simple docstring''' A: Tuple = parent A: Any = batch_size A: Tuple = seq_length A: str = is_training A: Optional[int] = use_labels A: List[Any] = vocab_size A: Dict = hidden_size A: str = num_hidden_layers A: Optional[int] = num_attention_heads A: Any = intermediate_size A: Dict = hidden_act A: Any = hidden_dropout_prob A: List[str] = attention_probs_dropout_prob A: Optional[Any] = max_position_embeddings A: List[Any] = eos_token_id A: Tuple = pad_token_id A: List[str] = bos_token_id A: Optional[int] = embed_dim A: List[str] = word_embed_proj_dim A: Any = False def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' A: Optional[int] = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) A: Optional[int] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) A: str = tf.concat([input_ids, eos_tensor] , axis=1 ) A: Tuple = self.config_cls( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , embed_dim=self.embed_dim , word_embed_proj_dim=self.word_embed_proj_dim , is_encoder_decoder=SCREAMING_SNAKE_CASE_ , **self.config_updates , ) A: int = prepare_opt_inputs_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return config, inputs_dict def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int ) -> Tuple: '''simple docstring''' A: Optional[Any] = TFOPTModel(config=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = inputs_dict['input_ids'] A: str = input_ids[:1, :] A: Dict = inputs_dict['attention_mask'][:1, :] A: Optional[Any] = 1 # first forward pass A: Any = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ ) A: List[str] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids A: Optional[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) A: Optional[Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and A: List[Any] = tf.concat([input_ids, next_tokens] , axis=-1 ) A: Optional[int] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) A: Optional[Any] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ )[0] A: List[str] = model(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ , past_key_values=SCREAMING_SNAKE_CASE_ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice A: Any = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) A: Optional[int] = output_from_no_past[:, -3:, random_slice_idx] A: List[str] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , rtol=1E-3 ) @require_tf class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : List[str] = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else () UpperCamelCase_ : Optional[int] = (TFOPTForCausalLM,) if is_tf_available() else () UpperCamelCase_ : Union[str, Any] = ( {"""feature-extraction""": TFOPTModel, """text-generation""": TFOPTForCausalLM} if is_tf_available() else {} ) UpperCamelCase_ : int = False UpperCamelCase_ : Union[str, Any] = False UpperCamelCase_ : int = False UpperCamelCase_ : Optional[int] = 10 def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' A: int = TFOPTModelTester(self ) A: Tuple = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' self.config_tester.run_common_tests() def _snake_case ( self : Optional[int] ) -> str: '''simple docstring''' A: Tuple = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> List[Any]: '''simple docstring''' A: Any = self.model_tester.prepare_config_and_inputs_for_common() def _get_word_embedding_weight(SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] ): if hasattr(SCREAMING_SNAKE_CASE_ , '''weight''' ): return embedding_layer.weight else: # Here we build the word embeddings weights if not exists. # And then we retry to get the attribute once built. model.build() if hasattr(SCREAMING_SNAKE_CASE_ , '''weight''' ): return embedding_layer.weight else: return None for model_class in self.all_model_classes: for size in [config.vocab_size - 10, config.vocab_size + 10]: # build the embeddings A: Optional[int] = model_class(config=SCREAMING_SNAKE_CASE_ ) A: int = _get_word_embedding_weight(SCREAMING_SNAKE_CASE_ , model.get_input_embeddings() ) A: Tuple = _get_word_embedding_weight(SCREAMING_SNAKE_CASE_ , model.get_output_embeddings() ) # reshape the embeddings model.resize_token_embeddings(SCREAMING_SNAKE_CASE_ ) A: str = _get_word_embedding_weight(SCREAMING_SNAKE_CASE_ , model.get_input_embeddings() ) A: Any = _get_word_embedding_weight(SCREAMING_SNAKE_CASE_ , model.get_output_embeddings() ) # check that the resized embeddings size matches the desired size. A: Union[str, Any] = size if size is not None else config.vocab_size self.assertEqual(new_input_embeddings.shape[0] , SCREAMING_SNAKE_CASE_ ) # check that weights remain the same after resizing A: int = True for pa, pa in zip(old_input_embeddings.value() , new_input_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: A: Optional[Any] = False self.assertTrue(SCREAMING_SNAKE_CASE_ ) if old_output_embeddings is not None and new_output_embeddings is not None: self.assertEqual(new_output_embeddings.shape[0] , SCREAMING_SNAKE_CASE_ ) A: Dict = True for pa, pa in zip(old_output_embeddings.value() , new_output_embeddings.value() ): if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0: A: Union[str, Any] = False self.assertTrue(SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: return tf.constant(lowercase__ , dtype=tf.intaa ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = 99 def _snake_case ( self : Dict ) -> List[Any]: '''simple docstring''' A: Tuple = tf.ones((4, 1) , dtype=tf.intaa ) * 2 A: Tuple = tf.concat([ids_tensor((4, 6) , self.vocab_size - 3 ) + 3, eos_column_vector] , axis=1 ) A: int = input_ids.shape[0] A: List[Any] = OPTConfig( vocab_size=self.vocab_size , hidden_size=24 , num_hidden_layers=2 , num_attention_heads=2 , ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size @require_sentencepiece @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : Optional[int] ) -> List[Any]: '''simple docstring''' A: Tuple = TFOPTModel.from_pretrained('''facebook/opt-350m''' ) A: List[Any] = _long_tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] ) A: List[str] = tf.not_equal(SCREAMING_SNAKE_CASE_ , model.config.pad_token_id ) with tf.GradientTape(): A: List[str] = model(input_ids=SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ).last_hidden_state A: Optional[Any] = (1, 11, 5_12) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE_ ) A: List[str] = tf.constant( [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]] ) self.assertTrue(np.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=4E-3 ) ) A: List[str] = tf.function(SCREAMING_SNAKE_CASE_ , jit_compile=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = xla_generate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )[0] self.assertTrue(np.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=4E-2 ) ) @require_tf @slow class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Optional[Any] ) -> Tuple: '''simple docstring''' super().setUp() A: List[Any] = 'facebook/opt-350m' def _snake_case ( self : List[str] ) -> Tuple: '''simple docstring''' A: int = TFOPTForCausalLM.from_pretrained(self.path_model ) A: List[Any] = GPTaTokenizer.from_pretrained(self.path_model ) A: List[str] = [ 'Today is a beautiful day and I want to', 'In the city of', 'Paris is the capital of France and', 'Computers and mobile phones have taken', ] # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False A: List[Any] = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''tf''' , padding=SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Optional[int] = tf.math.reduce_mean(model(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) A: Any = tf.constant( [ [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670], [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822], [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703], [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477], ] ) self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) A: Tuple = tf.function(SCREAMING_SNAKE_CASE_ , jit_compile=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tf.math.reduce_mean(xla_generate(inputs.input_ids , attention_mask=inputs.attention_mask )[0] , axis=-1 ) self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) @require_tf @slow class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @property def _snake_case ( self : List[Any] ) -> Tuple: '''simple docstring''' return [ "Today is a beautiful day and I want", "In the city of", "Paris is the capital of France and", "Computers and mobile phones have taken", ] def _snake_case ( self : Dict ) -> int: '''simple docstring''' A: str = 'facebook/opt-125m' A: Dict = [ 'Today is a beautiful day and I want to', 'In the city of New York, the city', 'Paris is the capital of France and the capital', 'Computers and mobile phones have taken over the', ] A: Optional[int] = [] A: Optional[int] = GPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Dict = TFOPTForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) for prompt in self.prompts: A: int = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''tf''' ).input_ids A: int = model.generate(SCREAMING_SNAKE_CASE_ , max_length=10 ) A: Any = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) predicted_outputs += generated_string self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' A: List[Any] = 'facebook/opt-350m' A: int = GPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = TFOPTForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Any = 'left' # use different length sentences to test batching A: Optional[int] = [ 'Hello, my dog is a little', 'Today, I', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''tf''' , padding=SCREAMING_SNAKE_CASE_ ) A: int = inputs['input_ids'] A: Tuple = model.generate(input_ids=SCREAMING_SNAKE_CASE_ , attention_mask=inputs['''attention_mask'''] ) A: Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids A: Union[str, Any] = model.generate(input_ids=SCREAMING_SNAKE_CASE_ ) A: Dict = inputs_non_padded.shape[-1] - tf.math.reduce_sum( tf.cast(inputs['''attention_mask'''][-1] , tf.intaa ) ) A: int = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids A: Union[str, Any] = model.generate(input_ids=SCREAMING_SNAKE_CASE_ , max_length=model.config.max_length - num_paddings ) A: List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = tokenizer.decode(output_padded[0] , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = [ 'Hello, my dog is a little bit of a dork.\nI\'m a little bit', 'Today, I was in the middle of a conversation with a friend about the', ] self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , [non_padded_sentence, padded_sentence] ) def _snake_case ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' A: Tuple = 'facebook/opt-350m' A: List[Any] = [ 'Today is a beautiful day and I want to', 'In the city of San Francisco, the city', 'Paris is the capital of France and the capital', 'Computers and mobile phones have taken over the', ] A: Optional[int] = [] A: Optional[Any] = GPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = TFOPTForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_ ) for prompt in self.prompts: A: List[Any] = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''tf''' ).input_ids A: Optional[Any] = model.generate(SCREAMING_SNAKE_CASE_ , max_length=10 ) A: Dict = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) predicted_outputs += generated_string self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
362
'''simple docstring''' from ..utils import is_flax_available, is_torch_available if is_torch_available(): from .autoencoder_kl import AutoencoderKL from .controlnet import ControlNetModel from .dual_transformer_ad import DualTransformeraDModel from .modeling_utils import ModelMixin from .prior_transformer import PriorTransformer from .ta_film_transformer import TaFilmDecoder from .transformer_ad import TransformeraDModel from .unet_ad import UNetaDModel from .unet_ad import UNetaDModel from .unet_ad_condition import UNetaDConditionModel from .unet_ad_condition import UNetaDConditionModel from .vq_model import VQModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel from .unet_ad_condition_flax import FlaxUNetaDConditionModel from .vae_flax import FlaxAutoencoderKL
334
0
'''simple docstring''' from __future__ import annotations from math import pi def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> dict[str, float]: if (inductance, frequency, reactance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if inductance < 0: raise ValueError('''Inductance cannot be negative''' ) if frequency < 0: raise ValueError('''Frequency cannot be negative''' ) if reactance < 0: raise ValueError('''Inductive reactance cannot be negative''' ) if inductance == 0: return {"inductance": reactance / (2 * pi * frequency)} elif frequency == 0: return {"frequency": reactance / (2 * pi * inductance)} elif reactance == 0: return {"reactance": 2 * pi * frequency * inductance} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
363
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : torch.FloatTensor class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 6_55_36 , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 0 , SCREAMING_SNAKE_CASE_ : str = "fourier" , SCREAMING_SNAKE_CASE_ : bool = True , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 0.0 , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , SCREAMING_SNAKE_CASE_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , SCREAMING_SNAKE_CASE_ : Tuple[str] = "UNetMidBlock1D" , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : Tuple[int] = (32, 32, 64) , SCREAMING_SNAKE_CASE_ : str = None , SCREAMING_SNAKE_CASE_ : int = 8 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : bool = False , ) -> Tuple: '''simple docstring''' super().__init__() A: Optional[Any] = sample_size # time if time_embedding_type == "fourier": A: Tuple = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=SCREAMING_SNAKE_CASE_ , log=SCREAMING_SNAKE_CASE_ , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ ) A: List[str] = 2 * block_out_channels[0] elif time_embedding_type == "positional": A: str = Timesteps( block_out_channels[0] , flip_sin_to_cos=SCREAMING_SNAKE_CASE_ , downscale_freq_shift=SCREAMING_SNAKE_CASE_ ) A: Any = block_out_channels[0] if use_timestep_embedding: A: Optional[Any] = block_out_channels[0] * 4 A: List[Any] = TimestepEmbedding( in_channels=SCREAMING_SNAKE_CASE_ , time_embed_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , out_dim=block_out_channels[0] , ) A: Optional[Any] = nn.ModuleList([] ) A: str = None A: str = nn.ModuleList([] ) A: Tuple = None # down A: Any = in_channels for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: Optional[int] = output_channel A: List[Any] = block_out_channels[i] if i == 0: input_channel += extra_in_channels A: List[Any] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[int] = get_down_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(SCREAMING_SNAKE_CASE_ ) # mid A: Union[str, Any] = get_mid_block( SCREAMING_SNAKE_CASE_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=SCREAMING_SNAKE_CASE_ , add_downsample=SCREAMING_SNAKE_CASE_ , ) # up A: Optional[Any] = list(reversed(SCREAMING_SNAKE_CASE_ ) ) A: List[str] = reversed_block_out_channels[0] if out_block_type is None: A: int = out_channels else: A: Union[str, Any] = block_out_channels[0] for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE_ ): A: List[Any] = output_channel A: int = ( reversed_block_out_channels[i + 1] if i < len(SCREAMING_SNAKE_CASE_ ) - 1 else final_upsample_channels ) A: Optional[int] = i == len(SCREAMING_SNAKE_CASE_ ) - 1 A: Optional[Any] = get_up_block( SCREAMING_SNAKE_CASE_ , num_layers=SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(SCREAMING_SNAKE_CASE_ ) A: Any = output_channel # out A: List[str] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) A: Optional[int] = get_out_block( out_block_type=SCREAMING_SNAKE_CASE_ , num_groups_out=SCREAMING_SNAKE_CASE_ , embed_dim=block_out_channels[0] , out_channels=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ , fc_dim=block_out_channels[-1] // 4 , ) def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : Union[torch.Tensor, float, int] , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[UNetaDOutput, Tuple]: '''simple docstring''' A: Any = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): A: Union[str, Any] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: A: List[str] = timesteps[None].to(sample.device ) A: int = self.time_proj(SCREAMING_SNAKE_CASE_ ) if self.config.use_timestep_embedding: A: List[Any] = self.time_mlp(SCREAMING_SNAKE_CASE_ ) else: A: str = timestep_embed[..., None] A: Union[str, Any] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) A: Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down A: List[str] = () for downsample_block in self.down_blocks: A , A: Optional[int] = downsample_block(hidden_states=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: A: Dict = self.mid_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): A: List[Any] = down_block_res_samples[-1:] A: List[str] = down_block_res_samples[:-1] A: Optional[int] = upsample_block(SCREAMING_SNAKE_CASE_ , res_hidden_states_tuple=SCREAMING_SNAKE_CASE_ , temb=SCREAMING_SNAKE_CASE_ ) # 5. post-process if self.out_block: A: Any = self.out_block(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=SCREAMING_SNAKE_CASE_ )
334
0
'''simple docstring''' import pandas as pd from matplotlib import pyplot as plt from sklearn.linear_model import LinearRegression # Splitting the dataset into the Training set and Test set from sklearn.model_selection import train_test_split # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures # Importing the dataset UpperCamelCase = pd.read_csv( '''https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/''' '''position_salaries.csv''' ) UpperCamelCase = dataset.iloc[:, 1:2].values UpperCamelCase = dataset.iloc[:, 2].values UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = train_test_split(X, y, test_size=0.2, random_state=0) UpperCamelCase = PolynomialFeatures(degree=4) UpperCamelCase = poly_reg.fit_transform(X) UpperCamelCase = LinearRegression() pol_reg.fit(X_poly, y) def SCREAMING_SNAKE_CASE( ) -> List[str]: plt.scatter(a_ , a_ , color='''red''' ) plt.plot(a_ , pol_reg.predict(poly_reg.fit_transform(a_ ) ) , color='''blue''' ) plt.title('''Truth or Bluff (Linear Regression)''' ) plt.xlabel('''Position level''' ) plt.ylabel('''Salary''' ) plt.show() if __name__ == "__main__": viz_polymonial() # Predicting a new result with Polymonial Regression pol_reg.predict(poly_reg.fit_transform([[5.5]])) # output should be 132148.43750003
364
'''simple docstring''' import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Dict ) -> None: '''simple docstring''' warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , SCREAMING_SNAKE_CASE_ , ) super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ )
334
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, PNDMScheduler, StableDiffusionLDMaDPipeline, UNetaDConditionModel, ) from diffusers.utils import nightly, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS enable_full_determinism() class lowerCAmelCase_ ( unittest.TestCase ): UpperCamelCase_ : int = StableDiffusionLDMaDPipeline UpperCamelCase_ : List[Any] = TEXT_TO_IMAGE_PARAMS UpperCamelCase_ : Optional[Any] = TEXT_TO_IMAGE_BATCH_PARAMS UpperCamelCase_ : str = TEXT_TO_IMAGE_IMAGE_PARAMS def _snake_case ( self : Optional[int] ) -> List[str]: '''simple docstring''' torch.manual_seed(0 ) A: Optional[Any] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) A: Optional[Any] = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_a , set_alpha_to_one=_a , ) torch.manual_seed(0 ) A: str = AutoencoderKL( block_out_channels=[32, 64] , in_channels=6 , out_channels=6 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) A: Any = 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=10_00 , ) A: List[str] = CLIPTextModel(_a ) A: List[str] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) A: Tuple = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str]=0 ) -> Optional[Any]: '''simple docstring''' if str(_a ).startswith('''mps''' ): A: Tuple = torch.manual_seed(_a ) else: A: str = torch.Generator(device=_a ).manual_seed(_a ) A: Optional[int] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def _snake_case ( self : Optional[int] ) -> int: '''simple docstring''' A: Any = '''cpu''' # ensure determinism for the device-dependent torch.Generator A: Union[str, Any] = self.get_dummy_components() A: str = StableDiffusionLDMaDPipeline(**_a ) A: int = ldmad_pipe.to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: Dict = self.get_dummy_inputs(_a ) A: Any = ldmad_pipe(**_a ) A: Union[str, Any] = output.rgb, output.depth A: Tuple = rgb[0, -3:, -3:, -1] A: int = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) A: int = np.array( [0.3733_8176, 0.7_0247, 0.7420_3193, 0.5164_3604, 0.5825_6793, 0.6093_2136, 0.418_1095, 0.4835_5877, 0.4653_5262] ) A: Any = np.array([103.4_6727, 85.81_2004, 87.84_9236] ) assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb ).max() < 1E-2 assert np.abs(image_slice_depth.flatten() - expected_slice_depth ).max() < 1E-2 def _snake_case ( self : Any ) -> List[str]: '''simple docstring''' A: Optional[int] = self.get_dummy_components() A: Optional[Any] = StableDiffusionLDMaDPipeline(**_a ) A: int = ldmad_pipe.to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: Union[str, Any] = self.get_dummy_inputs(_a ) A: Union[str, Any] = 3 * [inputs['''prompt''']] # forward A: List[Any] = ldmad_pipe(**_a ) A: str = output.rgb, output.depth A: str = rgb_slice_a[0, -3:, -3:, -1] A: Optional[Any] = depth_slice_a[0, -3:, -1] A: Any = self.get_dummy_inputs(_a ) A: List[Any] = 3 * [inputs.pop('''prompt''' )] A: int = ldmad_pipe.tokenizer( _a , padding='''max_length''' , max_length=ldmad_pipe.tokenizer.model_max_length , truncation=_a , return_tensors='''pt''' , ) A: Tuple = text_inputs['''input_ids'''].to(_a ) A: Union[str, Any] = ldmad_pipe.text_encoder(_a )[0] A: Optional[int] = prompt_embeds # forward A: Dict = ldmad_pipe(**_a ) A: List[str] = output.rgb, output.depth A: Union[str, Any] = rgb_slice_a[0, -3:, -3:, -1] A: int = depth_slice_a[0, -3:, -1] assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten() ).max() < 1E-4 assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten() ).max() < 1E-4 def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: Optional[Any] = '''cpu''' # ensure determinism for the device-dependent torch.Generator A: List[str] = self.get_dummy_components() A: Any = PNDMScheduler(skip_prk_steps=_a ) A: Any = StableDiffusionLDMaDPipeline(**_a ) A: Any = ldmad_pipe.to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: Tuple = self.get_dummy_inputs(_a ) A: int = '''french fries''' A: Tuple = ldmad_pipe(**_a , negative_prompt=_a ) A: Optional[Any] = output.rgb, output.depth A: List[Any] = rgb[0, -3:, -3:, -1] A: Tuple = depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) A: Any = np.array( [0.3_7044, 0.7181_1503, 0.722_3251, 0.4860_3675, 0.563_8391, 0.636_4948, 0.4283_3704, 0.490_1315, 0.4792_6217] ) A: int = np.array([107.8_4738, 84.6_2802, 89.96_2135] ) assert np.abs(rgb_slice.flatten() - expected_slice_rgb ).max() < 1E-2 assert np.abs(depth_slice.flatten() - expected_slice_depth ).max() < 1E-2 @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : List[str]="cpu" , SCREAMING_SNAKE_CASE_ : Optional[int]=torch.floataa , SCREAMING_SNAKE_CASE_ : List[str]=0 ) -> Tuple: '''simple docstring''' A: int = torch.Generator(device=_a ).manual_seed(_a ) A: Optional[int] = np.random.RandomState(_a ).standard_normal((1, 4, 64, 64) ) A: Dict = torch.from_numpy(_a ).to(device=_a , dtype=_a ) A: int = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def _snake_case ( self : int ) -> Optional[Any]: '''simple docstring''' A: Tuple = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ) A: Optional[Any] = ldmad_pipe.to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: int = self.get_inputs(_a ) A: Optional[int] = ldmad_pipe(**_a ) A: Any = output.rgb, output.depth A: Tuple = rgb[0, -3:, -3:, -1].flatten() A: int = rgb[0, -3:, -1].flatten() assert rgb.shape == (1, 5_12, 5_12, 3) assert depth.shape == (1, 5_12, 5_12) A: int = np.array( [0.5380_5465, 0.5670_7305, 0.548_6515, 0.5701_2236, 0.581_4511, 0.5625_3487, 0.5484_3014, 0.5509_2263, 0.645_9706] ) A: Dict = np.array( [0.926_3781, 0.667_8672, 0.548_6515, 0.9220_2145, 0.6783_1135, 0.5625_3487, 0.924_1694, 0.755_1478, 0.645_9706] ) assert np.abs(rgb_slice - expected_slice_rgb ).max() < 3E-3 assert np.abs(depth_slice - expected_slice_depth ).max() < 3E-3 @nightly @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def _snake_case ( self : Tuple ) -> Tuple: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def _snake_case ( self : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int]="cpu" , SCREAMING_SNAKE_CASE_ : str=torch.floataa , SCREAMING_SNAKE_CASE_ : Optional[Any]=0 ) -> Optional[int]: '''simple docstring''' A: Optional[Any] = torch.Generator(device=_a ).manual_seed(_a ) A: Dict = np.random.RandomState(_a ).standard_normal((1, 4, 64, 64) ) A: Optional[Any] = torch.from_numpy(_a ).to(device=_a , dtype=_a ) A: str = { '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 50, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def _snake_case ( self : Optional[int] ) -> Tuple: '''simple docstring''' A: Optional[Any] = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ).to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: Any = self.get_inputs(_a ) A: List[str] = ldmad_pipe(**_a ) A: Any = output.rgb, output.depth A: str = 0.49_5586 A: Union[str, Any] = 0.3379_5515 A: Any = 112.4_8518 A: List[Any] = 98.48_9746 assert np.abs(expected_rgb_mean - rgb.mean() ) < 1E-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1E-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1E-3 assert np.abs(expected_depth_std - depth.std() ) < 1E-3 def _snake_case ( self : Any ) -> int: '''simple docstring''' A: Optional[Any] = StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c''' ).to(_a ) ldmad_pipe.set_progress_bar_config(disable=_a ) A: List[str] = self.get_inputs(_a ) A: List[Any] = ldmad_pipe(**_a ) A: Union[str, Any] = output.rgb, output.depth A: int = 0.419_4127 A: str = 0.3537_5586 A: str = 0.563_8502 A: Tuple = 0.3468_6103 assert rgb.shape == (1, 5_12, 5_12, 3) assert depth.shape == (1, 5_12, 5_12, 1) assert np.abs(expected_rgb_mean - rgb.mean() ) < 1E-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1E-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1E-3 assert np.abs(expected_depth_std - depth.std() ) < 1E-3
365
'''simple docstring''' from collections import deque class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ) -> None: '''simple docstring''' A: Union[str, Any] = process_name # process name A: List[str] = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A: Dict = arrival_time A: Optional[Any] = burst_time # remaining burst time A: Any = 0 # total time of the process wait in ready queue A: Any = 0 # time from arrival time to completion time class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int , ) -> None: '''simple docstring''' A: Dict = number_of_queues # time slice of queues that round robin algorithm applied A: int = time_slices # unfinished process is in this ready_queue A: Tuple = queue # current time A: int = current_time # finished process is in this sequence queue A: deque[Process] = deque() def _snake_case ( self : List[Any] ) -> list[str]: '''simple docstring''' A: str = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Optional[int] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: Any = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : list[Process] ) -> list[int]: '''simple docstring''' A: List[Any] = [] for i in range(len(SCREAMING_SNAKE_CASE_ ) ): completion_times.append(queue[i].stop_time ) return completion_times def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> list[int]: '''simple docstring''' return [q.burst_time for q in queue] def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : Process ) -> int: '''simple docstring''' process.waiting_time += self.current_time - process.stop_time return process.waiting_time def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : deque[Process] ) -> deque[Process]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of finished process while len(SCREAMING_SNAKE_CASE_ ) != 0: A: Optional[Any] = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A: Any = 0 # set the process's turnaround time because it is finished A: int = self.current_time - cp.arrival_time # set the completion time A: List[str] = self.current_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : deque[Process] , SCREAMING_SNAKE_CASE_ : int ) -> tuple[deque[Process], deque[Process]]: '''simple docstring''' A: deque[Process] = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(SCREAMING_SNAKE_CASE_ ) ): A: Dict = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(SCREAMING_SNAKE_CASE_ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A: Optional[Any] = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(SCREAMING_SNAKE_CASE_ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A: int = 0 # set the finish time A: Union[str, Any] = self.current_time # update the process' turnaround time because it is finished A: Tuple = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(SCREAMING_SNAKE_CASE_ ) self.finish_queue.extend(SCREAMING_SNAKE_CASE_ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def _snake_case ( self : Optional[Any] ) -> deque[Process]: '''simple docstring''' for i in range(self.number_of_queues - 1 ): A , A: Optional[Any] = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) UpperCamelCase = Process('''P1''', 0, 53) UpperCamelCase = Process('''P2''', 0, 17) UpperCamelCase = Process('''P3''', 0, 68) UpperCamelCase = Process('''P4''', 0, 24) UpperCamelCase = 3 UpperCamelCase = [17, 25] UpperCamelCase = deque([Pa, Pa, Pa, Pa]) UpperCamelCase = MLFQ(number_of_queues, time_slices, queue, 0) UpperCamelCase = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f'waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print completion times of processes(P1, P2, P3, P4) print( f'completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print total turnaround times of processes(P1, P2, P3, P4) print( f'turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}' ) # print sequence of finished processes print( f'sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}' )
334
0
'''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( __lowercase , __lowercase , __lowercase ) -> int: A: List[str] = RemBertConfig.from_json_file(lowerCAmelCase__ ) print('''Building PyTorch model from configuration: {}'''.format(str(lowerCAmelCase__ ) ) ) A: Optional[int] = 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__": UpperCamelCase = 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.''' ) UpperCamelCase = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
366
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase = logging.get_logger() @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : list = field(default_factory=UpperCAmelCase_ ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Tensor , SCREAMING_SNAKE_CASE_ : Tensor ) -> int: '''simple docstring''' A: List[str] = len(list(m.modules() ) ) == 1 or isinstance(SCREAMING_SNAKE_CASE_ , nn.Convad ) or isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ) if has_not_submodules: self.traced.append(SCREAMING_SNAKE_CASE_ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tensor ) -> Dict: '''simple docstring''' for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(SCREAMING_SNAKE_CASE_ ) [x.remove() for x in self.handles] return self @property def _snake_case ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' return list(filter(lambda SCREAMING_SNAKE_CASE_ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 0 UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) UpperCamelCase_ : List = field(default_factory=UpperCAmelCase_ ) def __call__( self : Any , SCREAMING_SNAKE_CASE_ : Tensor ) -> Optional[Any]: '''simple docstring''' A: Dict = Tracker(self.dest )(SCREAMING_SNAKE_CASE_ ).parametrized A: Tuple = Tracker(self.src )(SCREAMING_SNAKE_CASE_ ).parametrized A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.src_skip , SCREAMING_SNAKE_CASE_ ) ) A: str = list(filter(lambda SCREAMING_SNAKE_CASE_ : type(SCREAMING_SNAKE_CASE_ ) not in self.dest_skip , SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise Exception( f"""Numbers of operations are different. Source module has {len(SCREAMING_SNAKE_CASE_ )} operations while""" f""" destination module has {len(SCREAMING_SNAKE_CASE_ )}.""" ) for dest_m, src_m in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"""Transfered from={src_m} to={dest_m}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase = True ) -> Any: print(F"""Converting {name}...""" ) with torch.no_grad(): A: Union[str, Any] = timm.create_model(__lowercase , pretrained=__lowercase ).eval() A: List[str] = ResNetForImageClassification(__lowercase ).eval() A: int = ModuleTransfer(src=__lowercase , dest=__lowercase ) A: List[str] = torch.randn((1, 3, 2_2_4, 2_2_4) ) module_transfer(__lowercase ) assert torch.allclose(from_model(__lowercase ) , our_model(__lowercase ).logits ), "The model logits don't match the original one." A: str = F"""resnet{'-'.join(name.split('resnet' ) )}""" print(__lowercase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add model''' , use_temp_dir=__lowercase , ) # we can use the convnext one A: Any = AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k''' ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name , commit_message='''Add image processor''' , use_temp_dir=__lowercase , ) print(F"""Pushed {checkpoint_name}""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = None , __lowercase = True ) -> List[Any]: A: Union[str, Any] = '''imagenet-1k-id2label.json''' A: Union[str, Any] = 1_0_0_0 A: Optional[int] = (1, num_labels) A: Dict = '''huggingface/label-files''' A: Any = num_labels A: Union[str, Any] = json.load(open(hf_hub_download(__lowercase , __lowercase , repo_type='''dataset''' ) , '''r''' ) ) A: Optional[int] = {int(__lowercase ): v for k, v in idalabel.items()} A: Optional[int] = idalabel A: List[str] = {v: k for k, v in idalabel.items()} A: str = partial(__lowercase , num_labels=__lowercase , idalabel=__lowercase , labelaid=__lowercase ) A: Optional[Any] = { '''resnet18''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet26''': ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet34''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[6_4, 1_2_8, 2_5_6, 5_1_2] , layer_type='''basic''' ), '''resnet50''': ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet101''': ImageNetPreTrainedConfig( depths=[3, 4, 2_3, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), '''resnet152''': ImageNetPreTrainedConfig( depths=[3, 8, 3_6, 3] , hidden_sizes=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , layer_type='''bottleneck''' ), } if model_name: convert_weight_and_push(__lowercase , names_to_config[model_name] , __lowercase , __lowercase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(__lowercase , __lowercase , __lowercase , __lowercase ) return config, expected_shape if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help=( '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,''' ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=Path, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', default=True, type=bool, required=False, help='''If True, push model and image processor to the hub.''', ) UpperCamelCase = parser.parse_args() UpperCamelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
334
0
'''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( __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = 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}}''' ) A: List[str] = DatasetInfosDict.from_directory(a_ ) assert dataset_infos assert dataset_infos["default"].dataset_size == 4_2 @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=4_2 , ), ] , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Any: A: Tuple = str(a_ ) dataset_info.write_to_directory(a_ ) A: Any = DatasetInfo.from_directory(a_ ) assert dataset_info == reloaded assert os.path.exists(os.path.join(a_ , '''dataset_info.json''' ) ) def SCREAMING_SNAKE_CASE( ) -> Optional[Any]: A: Optional[int] = 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''': 4_2}] , download_checksums={} , download_size=1_3_3_7 , post_processing_size=4_4_2 , dataset_size=1_2_3_4 , size_in_bytes=1_3_3_7 + 4_4_2 + 1_2_3_4 , ) A: Tuple = dataset_info._to_yaml_dict() assert sorted(a_ ) == 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) ) A: List[Any] = yaml.safe_dump(a_ ) A: Optional[int] = yaml.safe_load(a_ ) assert dataset_info_yaml_dict == reloaded def SCREAMING_SNAKE_CASE( ) -> Dict: A: str = DatasetInfo() A: List[str] = 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=4_2 , ) } ), DatasetInfosDict( { '''v1''': DatasetInfo(dataset_size=4_2 ), '''v2''': DatasetInfo(dataset_size=1_3_3_7 ), } ), ] , ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Tuple: A: Union[str, Any] = str(a_ ) dataset_infos_dict.write_to_directory(a_ ) A: Union[str, Any] = DatasetInfosDict.from_directory(a_ ) # the config_name of the dataset_infos_dict take over the attribute for config_name, dataset_info in dataset_infos_dict.items(): A: Optional[int] = 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 A: List[str] = 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(a_ , '''README.md''' ) )
367
'''simple docstring''' from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> str | Literal[False]: A: List[str] = list(__lowercase ) A: Optional[Any] = list(__lowercase ) A: int = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count += 1 A: Optional[Any] = '''_''' if count > 1: return False else: return "".join(__lowercase ) def SCREAMING_SNAKE_CASE( __lowercase ) -> list[str]: A: Any = [] while True: A: Dict = ['''$'''] * len(__lowercase ) A: Union[str, Any] = [] for i in range(len(__lowercase ) ): for j in range(i + 1 , len(__lowercase ) ): A: Any = compare_string(binary[i] , binary[j] ) if k is False: A: Any = '''*''' A: List[Any] = '''*''' temp.append('''X''' ) for i in range(len(__lowercase ) ): if checka[i] == "$": pi.append(binary[i] ) if len(__lowercase ) == 0: return pi A: List[Any] = list(set(__lowercase ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: Optional[int] = [] for minterm in minterms: A: Optional[int] = '''''' for _ in range(__lowercase ): A: List[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(__lowercase ) return temp def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> bool: A: Union[str, Any] = list(__lowercase ) A: Union[str, Any] = list(__lowercase ) A: Optional[int] = 0 for i in range(len(__lowercase ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[str]: A: List[Any] = [] A: Dict = [0] * len(__lowercase ) for i in range(len(chart[0] ) ): A: List[str] = 0 A: str = -1 for j in range(len(__lowercase ) ): if chart[j][i] == 1: count += 1 A: Any = j if count == 1: A: Any = 1 for i in range(len(__lowercase ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(__lowercase ) ): A: Optional[int] = 0 temp.append(prime_implicants[i] ) while True: A: Dict = 0 A: Optional[int] = -1 A: Dict = 0 for i in range(len(__lowercase ) ): A: str = chart[i].count(1 ) if count_n > max_n: A: Tuple = count_n A: Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(__lowercase ) ): A: Any = 0 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[int]]: A: str = [[0 for x in range(len(__lowercase ) )] for x in range(len(__lowercase ) )] for i in range(len(__lowercase ) ): A: Tuple = prime_implicants[i].count('''_''' ) for j in range(len(__lowercase ) ): if is_for_table(prime_implicants[i] , binary[j] , __lowercase ): A: Optional[Any] = 1 return chart def SCREAMING_SNAKE_CASE( ) -> None: A: int = int(input('''Enter the no. of variables\n''' ) ) A: Optional[int] = [ float(__lowercase ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] A: List[str] = decimal_to_binary(__lowercase , __lowercase ) A: str = check(__lowercase ) print('''Prime Implicants are:''' ) print(__lowercase ) A: List[Any] = prime_implicant_chart(__lowercase , __lowercase ) A: Any = selection(__lowercase , __lowercase ) print('''Essential Prime Implicants are:''' ) print(__lowercase ) if __name__ == "__main__": import doctest doctest.testmod() main()
334
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = { 's-JoL/Open-Llama-V1': 'https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json', } class lowerCAmelCase_ ( _UpperCamelCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = 'open-llama' def __init__( self : str , SCREAMING_SNAKE_CASE_ : List[Any]=10_00_00 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=40_96 , SCREAMING_SNAKE_CASE_ : Optional[Any]=1_10_08 , SCREAMING_SNAKE_CASE_ : List[Any]=32 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=32 , SCREAMING_SNAKE_CASE_ : Dict="silu" , SCREAMING_SNAKE_CASE_ : str=20_48 , SCREAMING_SNAKE_CASE_ : Optional[int]=0.02 , SCREAMING_SNAKE_CASE_ : Dict=1E-6 , SCREAMING_SNAKE_CASE_ : List[Any]=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE_ : Optional[Any]=1 , SCREAMING_SNAKE_CASE_ : Any=2 , SCREAMING_SNAKE_CASE_ : Any=False , SCREAMING_SNAKE_CASE_ : List[str]=True , SCREAMING_SNAKE_CASE_ : Any=0.1 , SCREAMING_SNAKE_CASE_ : Tuple=0.1 , SCREAMING_SNAKE_CASE_ : int=True , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : Optional[int]=None , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> Any: '''simple docstring''' A: List[Any] = vocab_size A: Any = max_position_embeddings A: List[str] = hidden_size A: Optional[Any] = intermediate_size A: Optional[Any] = num_hidden_layers A: int = num_attention_heads A: List[Any] = hidden_act A: Any = initializer_range A: Union[str, Any] = rms_norm_eps A: List[str] = use_cache A: List[Any] = kwargs.pop( '''use_memorry_efficient_attention''' , _SCREAMING_SNAKE_CASE ) A: List[Any] = hidden_dropout_prob A: Tuple = attention_dropout_prob A: Tuple = use_stable_embedding A: Any = shared_input_output_embedding A: List[Any] = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , tie_word_embeddings=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def _snake_case ( self : List[Any] ) -> str: '''simple docstring''' if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _SCREAMING_SNAKE_CASE ) or len(self.rope_scaling ) != 2: raise ValueError( '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ''' f"""got {self.rope_scaling}""" ) A: int = self.rope_scaling.get('''type''' , _SCREAMING_SNAKE_CASE ) A: Tuple = self.rope_scaling.get('''factor''' , _SCREAMING_SNAKE_CASE ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f"""`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or rope_scaling_factor <= 1.0: raise ValueError(f"""`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}""" )
368
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: Tuple = len(__lowercase ) for i in range(length - 1 ): A: Dict = i for k in range(i + 1 , __lowercase ): if collection[k] < collection[least]: A: List[str] = k if least != i: A , A: Tuple = (collection[i], collection[least]) return collection if __name__ == "__main__": UpperCamelCase = input('''Enter numbers separated by a comma:\n''').strip() UpperCamelCase = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
334
0
'''simple docstring''' import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[str]: A: Optional[int] = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(a__ ) == len(a__ ), F"""{len(a__ )} != {len(a__ )}""" dest_layers.load_state_dict(layers_to_copy.state_dict() ) UpperCamelCase = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } UpperCamelCase = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: try: A: Tuple = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( F"""no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first""" F""" {n_student}""" ) return list(range(a__ ) ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Dict: if n_student > n_teacher: raise ValueError(F"""Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}""" ) elif n_teacher == n_student: return list(range(a__ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase = "student" , __lowercase = None , __lowercase = None , __lowercase=False , __lowercase=None , __lowercase=None , **__lowercase , ) -> Union[str, Any]: A: Tuple = '''encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher.''' assert (e is not None) or (d is not None), _msg if isinstance(a__ , a__ ): AutoTokenizer.from_pretrained(a__ ).save_pretrained(a__ ) # purely for convenience A: Dict = AutoModelForSeqaSeqLM.from_pretrained(a__ ).eval() else: assert isinstance(a__ , a__ ), F"""teacher must be a model or string got type {type(a__ )}""" A: List[Any] = teacher.config.to_diff_dict() try: A: Optional[Any] = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: A: Optional[Any] = teacher_e if d is None: A: Any = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): A: Optional[int] = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: A: Tuple = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: A: List[Any] = teacher_e if d is None: A: List[str] = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(a__ ) # Copy weights A: Dict = teacher.config_class(**a__ ) A: int = AutoModelForSeqaSeqLM.from_config(a__ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. A: Union[str, Any] = student.load_state_dict(teacher.state_dict() , strict=a__ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save A: List[str] = list(range(a__ ) ), list(range(a__ ) ) logger.info( F"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to""" F""" {save_path}""" ) student.save_pretrained(a__ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: A: List[int] = pick_layers_to_copy(a__ , a__ ) if d_layers_to_copy is None: A: List[int] = pick_layers_to_copy(a__ , a__ ) try: if hasattr( a__ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , a__ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , a__ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , a__ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , a__ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , a__ ) copy_layers(teacher.decoder.block , student.decoder.block , a__ ) logger.info( F"""Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}""" ) A: Tuple = { '''teacher_type''': teacher.config.model_type, '''copied_encoder_layers''': e_layers_to_copy, '''copied_decoder_layers''': d_layers_to_copy, } student.save_pretrained(a__ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
369
'''simple docstring''' class lowerCAmelCase_ : '''simple docstring''' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: '''simple docstring''' A: Tuple = None A: Dict = None A: Optional[int] = graph self._normalize_graph(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: str = len(SCREAMING_SNAKE_CASE_ ) A: Optional[Any] = None def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict ) -> str: '''simple docstring''' if sources is int: A: Union[str, Any] = [sources] if sinks is int: A: Tuple = [sinks] if len(SCREAMING_SNAKE_CASE_ ) == 0 or len(SCREAMING_SNAKE_CASE_ ) == 0: return A: List[str] = sources[0] A: Optional[int] = sinks[0] # make fake vertex if there are more # than one source or sink if len(SCREAMING_SNAKE_CASE_ ) > 1 or len(SCREAMING_SNAKE_CASE_ ) > 1: A: Any = 0 for i in sources: max_input_flow += sum(self.graph[i] ) A: Dict = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: A: Optional[Any] = max_input_flow A: Optional[Any] = 0 A: str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: A: Optional[Any] = max_input_flow A: str = size - 1 def _snake_case ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Tuple: '''simple docstring''' A: Optional[Any] = algorithm(self ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Optional[Any]: '''simple docstring''' A: str = flow_network A: List[str] = flow_network.verticesCount A: Dict = flow_network.sourceIndex A: Any = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that A: str = flow_network.graph A: str = False def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' if not self.executed: self._algorithm() A: str = True def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) # use this to save your result A: Any = -1 def _snake_case ( self : List[str] ) -> Optional[Any]: '''simple docstring''' if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : int , SCREAMING_SNAKE_CASE_ : Any ) -> Optional[int]: '''simple docstring''' super().__init__(SCREAMING_SNAKE_CASE_ ) A: Optional[int] = [[0] * self.verticies_count for i in range(self.verticies_count )] A: Any = [0] * self.verticies_count A: Optional[Any] = [0] * self.verticies_count def _snake_case ( self : str ) -> Optional[Any]: '''simple docstring''' A: Any = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule A: str = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list A: Dict = 0 while i < len(SCREAMING_SNAKE_CASE_ ): A: Any = vertices_list[i] A: str = self.heights[vertex_index] self.process_vertex(SCREAMING_SNAKE_CASE_ ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(SCREAMING_SNAKE_CASE_ ) ) A: Tuple = 0 else: i += 1 A: Tuple = sum(self.preflow[self.source_index] ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[str] ) -> str: '''simple docstring''' while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) self.relabel(SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> int: '''simple docstring''' A: Optional[int] = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> int: '''simple docstring''' A: Optional[Any] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): A: List[Any] = self.heights[to_index] if min_height is not None: A: int = min_height + 1 if __name__ == "__main__": UpperCamelCase = [0] UpperCamelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] UpperCamelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network UpperCamelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate UpperCamelCase = flow_network.find_maximum_flow() print(f'maximum flow is {maximum_flow}')
334
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, OpenAIGPTConfig, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification, OpenAIGPTLMHeadModel, OpenAIGPTModel, ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int=13 , SCREAMING_SNAKE_CASE_ : Tuple=7 , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : List[str]=True , SCREAMING_SNAKE_CASE_ : int=True , SCREAMING_SNAKE_CASE_ : List[str]=99 , SCREAMING_SNAKE_CASE_ : Any=32 , SCREAMING_SNAKE_CASE_ : str=5 , SCREAMING_SNAKE_CASE_ : str=4 , SCREAMING_SNAKE_CASE_ : Dict=37 , SCREAMING_SNAKE_CASE_ : List[Any]="gelu" , SCREAMING_SNAKE_CASE_ : int=0.1 , SCREAMING_SNAKE_CASE_ : Tuple=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=5_12 , SCREAMING_SNAKE_CASE_ : Optional[Any]=16 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.02 , SCREAMING_SNAKE_CASE_ : int=3 , SCREAMING_SNAKE_CASE_ : int=4 , SCREAMING_SNAKE_CASE_ : Optional[int]=None , ) -> str: '''simple docstring''' A: int = parent A: str = batch_size A: str = seq_length A: Any = is_training A: Tuple = use_token_type_ids A: Optional[Any] = use_labels A: Optional[int] = vocab_size A: Any = hidden_size A: Any = num_hidden_layers A: Any = num_attention_heads A: List[Any] = intermediate_size A: Optional[Any] = hidden_act A: List[Any] = hidden_dropout_prob A: List[Any] = attention_probs_dropout_prob A: int = max_position_embeddings A: Optional[int] = type_vocab_size A: int = type_sequence_label_size A: int = initializer_range A: int = num_labels A: Tuple = num_choices A: Tuple = scope A: List[str] = self.vocab_size - 1 def _snake_case ( self : Tuple ) -> Optional[Any]: '''simple docstring''' A: Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A: Optional[int] = None if self.use_token_type_ids: A: Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A: str = None A: Dict = None A: Any = None if self.use_labels: A: str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A: Union[str, Any] = ids_tensor([self.batch_size] , self.num_choices ) A: List[str] = OpenAIGPTConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) A: Dict = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, head_mask, token_type_ids, sequence_labels, token_labels, choice_labels, ) def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] , *SCREAMING_SNAKE_CASE_ : List[str] ) -> List[str]: '''simple docstring''' A: int = OpenAIGPTModel(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A: Any = model(_lowerCAmelCase , token_type_ids=_lowerCAmelCase , head_mask=_lowerCAmelCase ) A: Dict = model(_lowerCAmelCase , token_type_ids=_lowerCAmelCase ) A: List[Any] = model(_lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[Any] , *SCREAMING_SNAKE_CASE_ : Any ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = OpenAIGPTLMHeadModel(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A: Optional[int] = model(_lowerCAmelCase , token_type_ids=_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[Any] , *SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[str]: '''simple docstring''' A: Any = OpenAIGPTDoubleHeadsModel(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A: int = model(_lowerCAmelCase , token_type_ids=_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _snake_case ( self : List[str] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , *SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' A: int = self.num_labels A: str = OpenAIGPTForSequenceClassification(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() A: str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A: Tuple = model(_lowerCAmelCase , token_type_ids=_lowerCAmelCase , labels=_lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _snake_case ( self : Tuple ) -> Any: '''simple docstring''' A: Optional[int] = self.prepare_config_and_inputs() ( A ): Optional[Any] = config_and_inputs A: Dict = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask, } return config, inputs_dict @require_torch class lowerCAmelCase_ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = ( (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification) if is_torch_available() else () ) UpperCamelCase_ : Union[str, Any] = ( (OpenAIGPTLMHeadModel,) if is_torch_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly UpperCamelCase_ : Optional[Any] = ( { "feature-extraction": OpenAIGPTModel, "text-classification": OpenAIGPTForSequenceClassification, "text-generation": OpenAIGPTLMHeadModel, "zero-shot": OpenAIGPTForSequenceClassification, } if is_torch_available() else {} ) def _snake_case ( self : str , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : int ) -> int: '''simple docstring''' if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a # tiny config could not be created. return True return False def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Optional[Any]=False ) -> str: '''simple docstring''' A: List[Any] = super()._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase , return_labels=_lowerCAmelCase ) if return_labels: if model_class.__name__ == "OpenAIGPTDoubleHeadsModel": A: List[str] = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) , dtype=torch.long , device=_lowerCAmelCase , ) A: List[Any] = inputs_dict["""labels"""] A: str = inputs_dict["""labels"""] A: List[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.num_choices) , dtype=torch.long , device=_lowerCAmelCase , ) A: List[str] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_lowerCAmelCase ) return inputs_dict def _snake_case ( self : Any ) -> Optional[int]: '''simple docstring''' A: Tuple = OpenAIGPTModelTester(self ) A: Optional[Any] = ConfigTester(self , config_class=_lowerCAmelCase , n_embd=37 ) def _snake_case ( self : str ) -> Tuple: '''simple docstring''' self.config_tester.run_common_tests() def _snake_case ( self : str ) -> List[Any]: '''simple docstring''' A: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase ) def _snake_case ( self : Tuple ) -> List[str]: '''simple docstring''' A: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase ) def _snake_case ( self : str ) -> List[str]: '''simple docstring''' A: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_double_lm_head_model(*_lowerCAmelCase ) def _snake_case ( self : Union[str, Any] ) -> int: '''simple docstring''' A: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase ) @slow def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A: Optional[Any] = OpenAIGPTModel.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) @require_torch class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A: List[str] = OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' ) model.to(_lowerCAmelCase ) A: List[str] = torch.tensor([[4_81, 47_35, 5_44]] , dtype=torch.long , device=_lowerCAmelCase ) # the president is A: int = [ 4_81, 47_35, 5_44, 2_46, 9_63, 8_70, 7_62, 2_39, 2_44, 4_04_77, 2_44, 2_49, 7_19, 8_81, 4_87, 5_44, 2_40, 2_44, 6_03, 4_81, ] # the president is a very good man. " \n " i\'m sure he is, " said the A: Optional[Any] = model.generate(_lowerCAmelCase , do_sample=_lowerCAmelCase ) self.assertListEqual(output_ids[0].tolist() , _lowerCAmelCase )
370
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging UpperCamelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = ["""input_features""", """attention_mask"""] def __init__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=1_60_00 , SCREAMING_SNAKE_CASE_ : int=80 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0.0 , SCREAMING_SNAKE_CASE_ : Any=True , SCREAMING_SNAKE_CASE_ : Tuple=True , SCREAMING_SNAKE_CASE_ : Union[str, Any]=True , **SCREAMING_SNAKE_CASE_ : List[str] , ) -> List[Any]: '''simple docstring''' super().__init__(feature_size=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , padding_value=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = num_mel_bins A: str = do_ceptral_normalize A: int = normalize_means A: List[Any] = normalize_vars A: Any = True def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : np.ndarray , ) -> np.ndarray: '''simple docstring''' A: Optional[int] = waveform * (2**15) # Kaldi compliance: 16-bit signed integers A: Optional[int] = torch.from_numpy(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) A: List[Any] = ta_kaldi.fbank(SCREAMING_SNAKE_CASE_ , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def _snake_case ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : Optional[bool] = True , SCREAMING_SNAKE_CASE_ : float = 0.0 , ) -> np.ndarray: '''simple docstring''' if normalize_means: A: str = x[:input_length].mean(axis=0 ) A: Dict = np.subtract(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if normalize_vars: A: Tuple = x[:input_length].std(axis=0 ) A: List[Any] = np.divide(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if input_length < x.shape[0]: A: Optional[int] = padding_value # make sure array is in float32 A: Optional[Any] = x.astype(np.floataa ) return x def _snake_case ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[np.ndarray] , SCREAMING_SNAKE_CASE_ : Optional[np.ndarray] = None ) -> List[np.ndarray]: '''simple docstring''' A: int = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] def __call__( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE_ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchFeature: '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) A: Any = isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) A: Optional[Any] = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A: Optional[int] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): A: int = np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A: Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A: Union[str, Any] = [raw_speech] # extract fbank features A: str = [self._extract_fbank_features(SCREAMING_SNAKE_CASE_ ) for waveform in raw_speech] # convert into correct format for padding A: int = BatchFeature({'''input_features''': features} ) A: int = self.pad( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # make sure list is in array format A: List[str] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE_ ): A: Optional[Any] = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.floataa ) for feature in input_features] A: List[Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: A: Dict = [np.asarray(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: A: Dict = ( np.array(SCREAMING_SNAKE_CASE_ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) is not PaddingStrategy.DO_NOT_PAD else None ) A: List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=SCREAMING_SNAKE_CASE_ ) if return_tensors is not None: A: Dict = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_ ) return padded_inputs
334
0
'''simple docstring''' import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def SCREAMING_SNAKE_CASE( ) -> Dict: with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(UpperCAmelCase__ ): requests.request('''GET''' , '''https://huggingface.co''' ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request('''GET''' , '''https://huggingface.co''' , timeout=1.0 ) @pytest.mark.integration def SCREAMING_SNAKE_CASE( ) -> int: with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request('''GET''' , '''https://huggingface.co''' ) def SCREAMING_SNAKE_CASE( ) -> Union[str, Any]: with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(UpperCAmelCase__ ): http_head('''https://huggingface.co''' )
371
'''simple docstring''' import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class lowerCAmelCase_ ( UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ : Optional[Any] = DebertaTokenizer UpperCamelCase_ : List[str] = True UpperCamelCase_ : int = DebertaTokenizerFast def _snake_case ( self : Optional[int] ) -> Dict: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt A: Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] A: int = dict(zip(SCREAMING_SNAKE_CASE_ , range(len(SCREAMING_SNAKE_CASE_ ) ) ) ) A: Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] A: Union[str, Any] = {'''unk_token''': '''[UNK]'''} A: List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) A: str = 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(SCREAMING_SNAKE_CASE_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def _snake_case ( self : int , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Tuple: '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Dict ) -> Union[str, Any]: '''simple docstring''' A: Optional[int] = '''lower newer''' A: str = '''lower newer''' return input_text, output_text def _snake_case ( self : Union[str, Any] ) -> Dict: '''simple docstring''' A: str = self.get_tokenizer() A: Any = '''lower newer''' A: Dict = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] A: int = tokenizer.tokenize(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokens + [tokenizer.unk_token] A: int = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : List[Any] ) -> Any: '''simple docstring''' A: str = self.get_tokenizer() A: List[str] = tokenizer('''Hello''' , '''World''' ) A: Union[str, Any] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , SCREAMING_SNAKE_CASE_ ) @slow def _snake_case ( self : Tuple ) -> Optional[int]: '''simple docstring''' A: Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Any = tokenizer.encode('''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: Dict = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=SCREAMING_SNAKE_CASE_ , add_prefix_space=SCREAMING_SNAKE_CASE_ ) A: List[Any] = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ ) A: int = tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _snake_case ( self : Tuple ) -> Dict: '''simple docstring''' A: int = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: A: List[Any] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) A: Dict = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] A: Dict = tokenizer(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ ) A: Any = [tokenizer.decode(SCREAMING_SNAKE_CASE_ , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) for seq in encoding['''input_ids''']] # fmt: off A: Any = { '''input_ids''': [ [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on A: Optional[int] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , SCREAMING_SNAKE_CASE_ ) for expected, decoded in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
334
0
'''simple docstring''' UpperCamelCase = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} UpperCamelCase = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[Any]: A: Optional[Any] = True A: Optional[int] = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(__a , __a , __a ) order.append(__a ) return order def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = True A: List[Any] = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(__a , __a , __a ) return component def SCREAMING_SNAKE_CASE( __lowercase ) -> List[Any]: A: Optional[int] = len(__a ) * [False] A: List[str] = {vert: [] for vert in range(len(__a ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(__a ) A: Union[str, Any] = [] for i, was_visited in enumerate(__a ): if not was_visited: order += topology_sort(__a , __a , __a ) A: Dict = [] A: Any = len(__a ) * [False] for i in range(len(__a ) ): A: int = order[len(__a ) - i - 1] if not visited[vert]: A: Union[str, Any] = find_components(__a , __a , __a ) components_list.append(__a ) return components_list
350
'''simple docstring''' import requests UpperCamelCase = '''https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=''' def SCREAMING_SNAKE_CASE( __lowercase ) -> None: # fetching a list of articles in json format A: Tuple = requests.get(_NEWS_API + bbc_news_api_key ).json() # each article in the list is a dict for i, article in enumerate(bbc_news_page['''articles'''] , 1 ): print(F"""{i}.) {article['title']}""" ) if __name__ == "__main__": fetch_bbc_news(bbc_news_api_key='''<Your BBC News API key goes here>''')
334
0
import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.testing_utils import require_tensorflow_text, require_tf, slow if is_tf_available(): import tensorflow as tf if is_tensorflow_text_available(): from transformers.models.bert import TFBertTokenizer UpperCamelCase = ['''bert-base-uncased''', '''bert-base-cased'''] UpperCamelCase = '''hf-internal-testing/tiny-bert-tf-only''' if is_tf_available(): class lowerCAmelCase_ ( tf.keras.Model ): '''simple docstring''' def __init__( self : str , SCREAMING_SNAKE_CASE_ : Tuple ) -> Tuple: '''simple docstring''' super().__init__() A: Any = tokenizer A: List[Any] = AutoConfig.from_pretrained(lowercase_ ) A: int = TFAutoModel.from_config(lowercase_ ) def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' A: str = self.tokenizer(lowercase_ ) A: str = self.bert(**lowercase_ ) return out["pooler_output"] @require_tf @require_tensorflow_text class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : int ) -> Optional[int]: '''simple docstring''' super().setUp() A: Any = [ BertTokenizer.from_pretrained(lowercase_ ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2) ] # repeat for when fast_bert_tokenizer=false A: str = [TFBertTokenizer.from_pretrained(lowercase_ ) for checkpoint in TOKENIZER_CHECKPOINTS] + [ TFBertTokenizer.from_pretrained(lowercase_ , use_fast_bert_tokenizer=lowercase_ ) for checkpoint in TOKENIZER_CHECKPOINTS ] assert len(self.tokenizers ) == len(self.tf_tokenizers ) A: Optional[Any] = [ "This is a straightforward English test sentence.", "This one has some weird characters\rto\nsee\r\nif those\u00E9break things.", "Now we're going to add some Chinese: 一 二 三 一二三", "And some much more rare Chinese: 齉 堃 齉堃", "Je vais aussi écrire en français pour tester les accents", "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ", ] A: List[Any] = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def _snake_case ( self : Dict ) -> Dict: '''simple docstring''' for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in (self.test_sentences, self.paired_sentences): A: str = tokenizer(lowercase_ , return_tensors='''tf''' , padding='''longest''' ) A: Tuple = tf_tokenizer(lowercase_ ) for key in python_outputs.keys(): self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) ) self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] , tf.intaa ) == tf_outputs[key] ) ) @slow def _snake_case ( self : List[str] ) -> int: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: A: Optional[int] = tf_tokenizer(self.paired_sentences ) A: Any = tf_tokenizer( text=[sentence[0] for sentence in self.paired_sentences] , text_pair=[sentence[1] for sentence in self.paired_sentences] , ) for key in merged_outputs.keys(): self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] , tf.intaa ) == separated_outputs[key] ) ) @slow def _snake_case ( self : Dict ) -> Dict: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: A: int = tf.function(lowercase_ ) for test_inputs in (self.test_sentences, self.paired_sentences): A: str = tf.constant(lowercase_ ) A: Any = compiled_tokenizer(lowercase_ ) A: Tuple = tf_tokenizer(lowercase_ ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def _snake_case ( self : Dict ) -> Tuple: '''simple docstring''' for tf_tokenizer in self.tf_tokenizers: A: List[Any] = ModelToSave(tokenizer=lowercase_ ) A: Union[str, Any] = tf.convert_to_tensor(self.test_sentences ) A: str = model(lowercase_ ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: A: Optional[int] = Path(lowercase_ ) / "saved.model" model.save(lowercase_ ) A: str = tf.keras.models.load_model(lowercase_ ) A: Dict = loaded_model(lowercase_ ) # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) ) , 1E-5 )
351
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_camembert import CamembertTokenizer else: UpperCamelCase = None UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_file''': { '''camembert-base''': '''https://huggingface.co/camembert-base/resolve/main/tokenizer.json''', }, } UpperCamelCase = { '''camembert-base''': 512, } UpperCamelCase = '''▁''' class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : str = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : int = CamembertTokenizer def __init__( self : Tuple , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : str="<s>" , SCREAMING_SNAKE_CASE_ : Optional[Any]="</s>" , SCREAMING_SNAKE_CASE_ : List[Any]="</s>" , SCREAMING_SNAKE_CASE_ : int="<s>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]="<unk>" , SCREAMING_SNAKE_CASE_ : str="<pad>" , SCREAMING_SNAKE_CASE_ : List[str]="<mask>" , SCREAMING_SNAKE_CASE_ : Union[str, Any]=["<s>NOTUSED", "</s>NOTUSED"] , **SCREAMING_SNAKE_CASE_ : Any , ) -> Any: '''simple docstring''' A: Tuple = AddedToken(SCREAMING_SNAKE_CASE_ , lstrip=SCREAMING_SNAKE_CASE_ , rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else mask_token super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , bos_token=SCREAMING_SNAKE_CASE_ , eos_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , additional_special_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Any = vocab_file A: Any = False if not self.vocab_file else True def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A: List[str] = [self.cls_token_id] A: List[str] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' A: List[str] = [self.sep_token_id] A: 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 _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' 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(SCREAMING_SNAKE_CASE_ ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return A: Dict = os.path.join( SCREAMING_SNAKE_CASE_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE_ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE_ ) return (out_vocab_file,)
334
0
'''simple docstring''' import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class lowerCAmelCase_ : '''simple docstring''' UpperCamelCase_ : Optional[int] = None def _snake_case ( self : Tuple ) -> Tuple: '''simple docstring''' A = self.feature_extraction_class(**self.feat_extract_dict ) A = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , a_ ) def _snake_case ( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A = os.path.join(a_ , '''feat_extract.json''' ) feat_extract_first.to_json_file(a_ ) A = self.feature_extraction_class.from_json_file(a_ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def _snake_case ( self : List[str] ) -> int: '''simple docstring''' A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: A = feat_extract_first.save_pretrained(a_ )[0] check_json_file_has_correct_format(a_ ) A = self.feature_extraction_class.from_pretrained(a_ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def _snake_case ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' A = self.feature_extraction_class() self.assertIsNotNone(a_ )
352
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
0
'''simple docstring''' import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging UpperCamelCase = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE( __lowercase ) -> Tuple: A: int = r'\w+[.]\d+' A: int = re.findall(_A , _A ) for pat in pats: A: Optional[Any] = key.replace(_A , '''_'''.join(pat.split('''.''' ) ) ) return key def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> List[Any]: A: Tuple = pt_tuple_key[:-1] + ('scale',) if ( any('''norm''' in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): A: str = pt_tuple_key[:-1] + ('scale',) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: A: Union[str, Any] = pt_tuple_key[:-1] + ('scale',) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: A: int = pt_tuple_key[:-1] + ('embedding',) return renamed_pt_tuple_key, pt_tensor # conv layer A: Optional[int] = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: A: Any = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer A: Optional[Any] = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight": A: Optional[int] = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight A: Union[str, Any] = pt_tuple_key[:-1] + ('weight',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias A: Tuple = pt_tuple_key[:-1] + ('bias',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase=4_2 ) -> Optional[Any]: A: Optional[Any] = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params A: List[Any] = flax_model.init_weights(PRNGKey(_A ) ) A: List[Any] = flatten_dict(_A ) A: List[Any] = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): A: Union[str, Any] = rename_key(_A ) A: List[Any] = tuple(renamed_pt_key.split('''.''' ) ) # Correctly rename weight parameters A: Tuple = rename_key_and_reshape_tensor(_A , _A , _A ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """ F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) # also add unexpected weight so that warning is thrown A: Dict = jnp.asarray(_A ) return unflatten_dict(_A )
353
'''simple docstring''' import argparse import torch from transformers import ( EncodecConfig, EncodecFeatureExtractor, EncodecModel, logging, ) # checkpoints downloaded from: # https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th # https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin # https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th logging.set_verbosity_info() UpperCamelCase = logging.get_logger('''transformers.models.encodec''') UpperCamelCase = { '''quantizer.vq.layers.*._codebook.inited''': '''quantizer.layers.*.codebook.inited''', '''quantizer.vq.layers.*._codebook.cluster_size''': '''quantizer.layers.*.codebook.cluster_size''', '''quantizer.vq.layers.*._codebook.embed''': '''quantizer.layers.*.codebook.embed''', '''quantizer.vq.layers.*._codebook.embed_avg''': '''quantizer.layers.*.codebook.embed_avg''', } UpperCamelCase = { '''encoder.model.0.conv.conv''': '''encoder.layers.0.conv''', '''encoder.model.1.block.1.conv.conv''': '''encoder.layers.1.block.1.conv''', '''encoder.model.1.block.3.conv.conv''': '''encoder.layers.1.block.3.conv''', '''encoder.model.1.shortcut.conv.conv''': '''encoder.layers.1.shortcut.conv''', '''encoder.model.3.conv.conv''': '''encoder.layers.3.conv''', '''encoder.model.4.block.1.conv.conv''': '''encoder.layers.4.block.1.conv''', '''encoder.model.4.block.3.conv.conv''': '''encoder.layers.4.block.3.conv''', '''encoder.model.4.shortcut.conv.conv''': '''encoder.layers.4.shortcut.conv''', '''encoder.model.6.conv.conv''': '''encoder.layers.6.conv''', '''encoder.model.7.block.1.conv.conv''': '''encoder.layers.7.block.1.conv''', '''encoder.model.7.block.3.conv.conv''': '''encoder.layers.7.block.3.conv''', '''encoder.model.7.shortcut.conv.conv''': '''encoder.layers.7.shortcut.conv''', '''encoder.model.9.conv.conv''': '''encoder.layers.9.conv''', '''encoder.model.10.block.1.conv.conv''': '''encoder.layers.10.block.1.conv''', '''encoder.model.10.block.3.conv.conv''': '''encoder.layers.10.block.3.conv''', '''encoder.model.10.shortcut.conv.conv''': '''encoder.layers.10.shortcut.conv''', '''encoder.model.12.conv.conv''': '''encoder.layers.12.conv''', '''encoder.model.13.lstm''': '''encoder.layers.13.lstm''', '''encoder.model.15.conv.conv''': '''encoder.layers.15.conv''', } UpperCamelCase = { '''encoder.model.0.conv.norm''': '''encoder.layers.0.norm''', '''encoder.model.1.block.1.conv.norm''': '''encoder.layers.1.block.1.norm''', '''encoder.model.1.block.3.conv.norm''': '''encoder.layers.1.block.3.norm''', '''encoder.model.1.shortcut.conv.norm''': '''encoder.layers.1.shortcut.norm''', '''encoder.model.3.conv.norm''': '''encoder.layers.3.norm''', '''encoder.model.4.block.1.conv.norm''': '''encoder.layers.4.block.1.norm''', '''encoder.model.4.block.3.conv.norm''': '''encoder.layers.4.block.3.norm''', '''encoder.model.4.shortcut.conv.norm''': '''encoder.layers.4.shortcut.norm''', '''encoder.model.6.conv.norm''': '''encoder.layers.6.norm''', '''encoder.model.7.block.1.conv.norm''': '''encoder.layers.7.block.1.norm''', '''encoder.model.7.block.3.conv.norm''': '''encoder.layers.7.block.3.norm''', '''encoder.model.7.shortcut.conv.norm''': '''encoder.layers.7.shortcut.norm''', '''encoder.model.9.conv.norm''': '''encoder.layers.9.norm''', '''encoder.model.10.block.1.conv.norm''': '''encoder.layers.10.block.1.norm''', '''encoder.model.10.block.3.conv.norm''': '''encoder.layers.10.block.3.norm''', '''encoder.model.10.shortcut.conv.norm''': '''encoder.layers.10.shortcut.norm''', '''encoder.model.12.conv.norm''': '''encoder.layers.12.norm''', '''encoder.model.15.conv.norm''': '''encoder.layers.15.norm''', } UpperCamelCase = { '''decoder.model.0.conv.conv''': '''decoder.layers.0.conv''', '''decoder.model.1.lstm''': '''decoder.layers.1.lstm''', '''decoder.model.3.convtr.convtr''': '''decoder.layers.3.conv''', '''decoder.model.4.block.1.conv.conv''': '''decoder.layers.4.block.1.conv''', '''decoder.model.4.block.3.conv.conv''': '''decoder.layers.4.block.3.conv''', '''decoder.model.4.shortcut.conv.conv''': '''decoder.layers.4.shortcut.conv''', '''decoder.model.6.convtr.convtr''': '''decoder.layers.6.conv''', '''decoder.model.7.block.1.conv.conv''': '''decoder.layers.7.block.1.conv''', '''decoder.model.7.block.3.conv.conv''': '''decoder.layers.7.block.3.conv''', '''decoder.model.7.shortcut.conv.conv''': '''decoder.layers.7.shortcut.conv''', '''decoder.model.9.convtr.convtr''': '''decoder.layers.9.conv''', '''decoder.model.10.block.1.conv.conv''': '''decoder.layers.10.block.1.conv''', '''decoder.model.10.block.3.conv.conv''': '''decoder.layers.10.block.3.conv''', '''decoder.model.10.shortcut.conv.conv''': '''decoder.layers.10.shortcut.conv''', '''decoder.model.12.convtr.convtr''': '''decoder.layers.12.conv''', '''decoder.model.13.block.1.conv.conv''': '''decoder.layers.13.block.1.conv''', '''decoder.model.13.block.3.conv.conv''': '''decoder.layers.13.block.3.conv''', '''decoder.model.13.shortcut.conv.conv''': '''decoder.layers.13.shortcut.conv''', '''decoder.model.15.conv.conv''': '''decoder.layers.15.conv''', } UpperCamelCase = { '''decoder.model.0.conv.norm''': '''decoder.layers.0.norm''', '''decoder.model.3.convtr.norm''': '''decoder.layers.3.norm''', '''decoder.model.4.block.1.conv.norm''': '''decoder.layers.4.block.1.norm''', '''decoder.model.4.block.3.conv.norm''': '''decoder.layers.4.block.3.norm''', '''decoder.model.4.shortcut.conv.norm''': '''decoder.layers.4.shortcut.norm''', '''decoder.model.6.convtr.norm''': '''decoder.layers.6.norm''', '''decoder.model.7.block.1.conv.norm''': '''decoder.layers.7.block.1.norm''', '''decoder.model.7.block.3.conv.norm''': '''decoder.layers.7.block.3.norm''', '''decoder.model.7.shortcut.conv.norm''': '''decoder.layers.7.shortcut.norm''', '''decoder.model.9.convtr.norm''': '''decoder.layers.9.norm''', '''decoder.model.10.block.1.conv.norm''': '''decoder.layers.10.block.1.norm''', '''decoder.model.10.block.3.conv.norm''': '''decoder.layers.10.block.3.norm''', '''decoder.model.10.shortcut.conv.norm''': '''decoder.layers.10.shortcut.norm''', '''decoder.model.12.convtr.norm''': '''decoder.layers.12.norm''', '''decoder.model.13.block.1.conv.norm''': '''decoder.layers.13.block.1.norm''', '''decoder.model.13.block.3.conv.norm''': '''decoder.layers.13.block.3.norm''', '''decoder.model.13.shortcut.conv.norm''': '''decoder.layers.13.shortcut.norm''', '''decoder.model.15.conv.norm''': '''decoder.layers.15.norm''', } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_DECODER, } UpperCamelCase = { **MAPPING_QUANTIZER, **MAPPING_ENCODER, **MAPPING_ENCODER_48K, **MAPPING_DECODER, **MAPPING_DECODER_48K, } UpperCamelCase = [] UpperCamelCase = [] def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Dict: for attribute in key.split('''.''' ): A: Union[str, Any] = getattr(__lowercase , __lowercase ) if weight_type is not None: A: Tuple = getattr(__lowercase , __lowercase ).shape else: A: str = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": A: Dict = value elif weight_type == "weight_g": A: Tuple = value elif weight_type == "weight_v": A: Any = value elif weight_type == "bias": A: str = value elif weight_type == "running_mean": A: List[Any] = value elif weight_type == "running_var": A: Dict = value elif weight_type == "num_batches_tracked": A: List[str] = value elif weight_type == "weight_ih_l0": A: Dict = value elif weight_type == "weight_hh_l0": A: Optional[int] = value elif weight_type == "bias_ih_l0": A: List[Any] = value elif weight_type == "bias_hh_l0": A: str = value elif weight_type == "weight_ih_l1": A: Optional[int] = value elif weight_type == "weight_hh_l1": A: int = value elif weight_type == "bias_ih_l1": A: Optional[Any] = value elif weight_type == "bias_hh_l1": A: str = value else: A: Optional[int] = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: for key in ignore_keys: if key.endswith('''.*''' ): if name.startswith(key[:-1] ): return True elif ".*." in key: A , A: Any = key.split('''.*.''' ) if prefix in name and suffix in name: return True elif key in name: return True return False def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Tuple: A: Any = [] if model_name == "encodec_24khz" or "encodec_32khz": A: List[str] = MAPPING_24K elif model_name == "encodec_48khz": A: List[Any] = MAPPING_48K else: raise ValueError(F"""Unsupported model: {model_name}""" ) for name, value in orig_dict.items(): if should_ignore(__lowercase , __lowercase ): logger.info(F"""{name} was ignored""" ) continue A: Optional[int] = False for key, mapped_key in MAPPING.items(): if "*" in key: A , A: Optional[int] = key.split('''.*.''' ) if prefix in name and suffix in name: A: str = suffix if key in name: # HACK otherwise .embed gets initialized with .embed_avg too if key.endswith('''embed''' ) and name.endswith('''embed_avg''' ): continue A: Optional[Any] = True if "*" in mapped_key: A: Any = name.split(__lowercase )[0].split('''.''' )[-2] A: Tuple = mapped_key.replace('''*''' , __lowercase ) if "weight_g" in name: A: str = '''weight_g''' elif "weight_v" in name: A: List[Any] = '''weight_v''' elif "weight_ih_l0" in name: A: Dict = '''weight_ih_l0''' elif "weight_hh_l0" in name: A: int = '''weight_hh_l0''' elif "bias_ih_l0" in name: A: Union[str, Any] = '''bias_ih_l0''' elif "bias_hh_l0" in name: A: Tuple = '''bias_hh_l0''' elif "weight_ih_l1" in name: A: int = '''weight_ih_l1''' elif "weight_hh_l1" in name: A: Optional[Any] = '''weight_hh_l1''' elif "bias_ih_l1" in name: A: Dict = '''bias_ih_l1''' elif "bias_hh_l1" in name: A: str = '''bias_hh_l1''' elif "bias" in name: A: Union[str, Any] = '''bias''' elif "weight" in name: A: Dict = '''weight''' elif "running_mean" in name: A: Tuple = '''running_mean''' elif "running_var" in name: A: Any = '''running_var''' elif "num_batches_tracked" in name: A: str = '''num_batches_tracked''' else: A: Tuple = None set_recursively(__lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) continue if not is_used: unused_weights.append(__lowercase ) logger.warning(F"""Unused weights: {unused_weights}""" ) @torch.no_grad() def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase=None , __lowercase=None , ) -> Dict: if config_path is not None: A: Tuple = EncodecConfig.from_pretrained(__lowercase ) else: A: Union[str, Any] = EncodecConfig() if model_name == "encodec_24khz": pass # config is already correct elif model_name == "encodec_32khz": A: Union[str, Any] = [8, 5, 4, 4] A: Dict = [2.2] A: List[Any] = 6_4 A: Optional[Any] = 3_2_0_0_0 A: List[Any] = 2_0_4_8 A: Optional[Any] = False A: int = False A: Union[str, Any] = False elif model_name == "encodec_48khz": A: Optional[int] = [8, 5, 4, 2] A: List[Any] = [3.0, 6.0, 1_2.0, 2_4.0] A: List[Any] = 4_8_0_0_0 A: int = 2 A: List[Any] = False A: Any = '''time_group_norm''' A: Optional[Any] = True A: Any = 1.0 A: Any = 0.0_1 else: raise ValueError(F"""Unknown model name: {model_name}""" ) A: str = EncodecModel(__lowercase ) A: Optional[Any] = EncodecFeatureExtractor( feature_size=config.audio_channels , sampling_rate=config.sampling_rate , chunk_length_s=config.chunk_length_s , overlap=config.overlap , ) feature_extractor.save_pretrained(__lowercase ) A: Union[str, Any] = torch.load(__lowercase ) if "best_state" in original_checkpoint: # we might have a training state saved, in which case discard the yaml results and just retain the weights A: Optional[int] = original_checkpoint['''best_state'''] recursively_load_weights(__lowercase , __lowercase , __lowercase ) model.save_pretrained(__lowercase ) if repo_id: print('''Pushing to the hub...''' ) feature_extractor.push_to_hub(__lowercase ) model.push_to_hub(__lowercase ) if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() parser.add_argument( '''--model''', default='''encodec_24khz''', type=str, help='''The model to convert. Should be one of \'encodec_24khz\', \'encodec_32khz\', \'encodec_48khz\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to original checkpoint''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) UpperCamelCase = parser.parse_args() convert_checkpoint( args.model, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
334
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase = { '''configuration_roberta''': ['''ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RobertaConfig''', '''RobertaOnnxConfig'''], '''tokenization_roberta''': ['''RobertaTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''RobertaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RobertaForCausalLM''', '''RobertaForMaskedLM''', '''RobertaForMultipleChoice''', '''RobertaForQuestionAnswering''', '''RobertaForSequenceClassification''', '''RobertaForTokenClassification''', '''RobertaModel''', '''RobertaPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRobertaForCausalLM''', '''TFRobertaForMaskedLM''', '''TFRobertaForMultipleChoice''', '''TFRobertaForQuestionAnswering''', '''TFRobertaForSequenceClassification''', '''TFRobertaForTokenClassification''', '''TFRobertaMainLayer''', '''TFRobertaModel''', '''TFRobertaPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''FlaxRobertaForCausalLM''', '''FlaxRobertaForMaskedLM''', '''FlaxRobertaForMultipleChoice''', '''FlaxRobertaForQuestionAnswering''', '''FlaxRobertaForSequenceClassification''', '''FlaxRobertaForTokenClassification''', '''FlaxRobertaModel''', '''FlaxRobertaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
354
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) UpperCamelCase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
334
0
'''simple docstring''' import numpy as np import qiskit def SCREAMING_SNAKE_CASE( __lowercase : int = 8 , __lowercase : int | None = None ) -> str: A: Dict = np.random.default_rng(seed=snake_case_ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. A: str = 6 * key_len # Measurement basis for Alice's qubits. A: Union[str, Any] = rng.integers(2 , size=snake_case_ ) # The set of states Alice will prepare. A: int = rng.integers(2 , size=snake_case_ ) # Measurement basis for Bob's qubits. A: str = rng.integers(2 , size=snake_case_ ) # Quantum Circuit to simulate BB84 A: str = qiskit.QuantumCircuit(snake_case_ , name='''BB84''' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(snake_case_ ): if alice_state[index] == 1: bbaa_circ.x(snake_case_ ) if alice_basis[index] == 1: bbaa_circ.h(snake_case_ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(snake_case_ ): if bob_basis[index] == 1: bbaa_circ.h(snake_case_ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. A: List[Any] = qiskit.Aer.get_backend('''aer_simulator''' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. A: Union[str, Any] = qiskit.execute(snake_case_ , snake_case_ , shots=1 , seed_simulator=snake_case_ ) # Returns the result of measurement. A: List[Any] = job.result().get_counts(snake_case_ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. A: Optional[int] = """""".join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( snake_case_ , snake_case_ , snake_case_ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. A: Union[str, Any] = gen_key[:key_len] if len(snake_case_ ) >= key_len else gen_key.ljust(snake_case_ , '''0''' ) return key if __name__ == "__main__": print(f'The generated key is : {bbaa(8, seed=0)}') from doctest import testmod testmod()
355
'''simple docstring''' def SCREAMING_SNAKE_CASE( __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for data in source_data: for i, el in enumerate(__lowercase ): if len(__lowercase ) < i + 1: data_lists.append([] ) data_lists[i].append(float(__lowercase ) ) return data_lists def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: list[list[float]] = [] for dlist, weight in zip(__lowercase , __lowercase ): A: List[str] = min(__lowercase ) A: Union[str, Any] = max(__lowercase ) A: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: A: List[str] = F"""Invalid weight of {weight:f} provided""" raise ValueError(__lowercase ) score_lists.append(__lowercase ) return score_lists def SCREAMING_SNAKE_CASE( __lowercase ) -> list[float]: A: list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(__lowercase ): A: str = final_scores[j] + ele return final_scores def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> list[list[float]]: A: Any = get_data(__lowercase ) A: str = calculate_each_score(__lowercase , __lowercase ) A: int = generate_final_scores(__lowercase ) # append scores to source data for i, ele in enumerate(__lowercase ): source_data[i].append(__lowercase ) return source_data
334
0
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowerCAmelCase_ ( __UpperCamelCase , __UpperCamelCase ): '''simple docstring''' UpperCamelCase_ : Optional[int] = 1 @register_to_config def __init__( self : str , SCREAMING_SNAKE_CASE_ : int = 10_00 , SCREAMING_SNAKE_CASE_ : Optional[Union[np.ndarray, List[float]]] = None ) -> int: '''simple docstring''' self.set_timesteps(SCREAMING_SNAKE_CASE_ ) # standard deviation of the initial noise distribution A: Tuple = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. A: int = 4 # running values A: List[Any] = [] def _snake_case ( self : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Union[str, torch.device] = None ) -> Any: '''simple docstring''' A: Dict = num_inference_steps A: Tuple = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] A: Dict = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: A: Any = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: A: List[Any] = torch.sin(steps * math.pi / 2 ) ** 2 A: Optional[Any] = (1.0 - self.betas**2) ** 0.5 A: List[str] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] A: Tuple = timesteps.to(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = [] def _snake_case ( self : Any , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , SCREAMING_SNAKE_CASE_ : bool = True , ) -> Union[str, Any]: '''simple docstring''' if self.num_inference_steps is None: raise ValueError( '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler''' ) A: Dict = (self.timesteps == timestep).nonzero().item() A: Optional[Any] = timestep_index + 1 A: Optional[int] = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(SCREAMING_SNAKE_CASE_ ) if len(self.ets ) == 1: A: Optional[Any] = self.ets[-1] elif len(self.ets ) == 2: A: Union[str, Any] = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: A: List[str] = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: A: Optional[Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) A: Union[str, Any] = self._get_prev_sample(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : torch.FloatTensor , *SCREAMING_SNAKE_CASE_ : Union[str, Any] , **SCREAMING_SNAKE_CASE_ : Dict ) -> Dict: '''simple docstring''' return sample def _snake_case ( self : Tuple , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' A: Dict = self.alphas[timestep_index] A: int = self.betas[timestep_index] A: Tuple = self.alphas[prev_timestep_index] A: int = self.betas[prev_timestep_index] A: Optional[int] = (sample - sigma * ets) / max(SCREAMING_SNAKE_CASE_ , 1E-8 ) A: Optional[int] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : Tuple ) -> List[Any]: '''simple docstring''' return self.config.num_train_timesteps
356
'''simple docstring''' import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': 512, '''facebook/dpr-ctx_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': 512, '''facebook/dpr-question_encoder-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': 512, '''facebook/dpr-reader-multiset-base''': 512, } UpperCamelCase = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } UpperCamelCase = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Union[str, Any] = VOCAB_FILES_NAMES UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Optional[Any] = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = DPRContextEncoderTokenizer class lowerCAmelCase_ ( UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Dict = VOCAB_FILES_NAMES UpperCamelCase_ : List[str] = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : List[Any] = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Tuple = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Optional[int] = DPRQuestionEncoderTokenizer UpperCamelCase = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) UpperCamelCase = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) UpperCamelCase = R''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ : '''simple docstring''' def __call__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Optional[str] = None , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Union[bool, str] = False , SCREAMING_SNAKE_CASE_ : Optional[int] = None , SCREAMING_SNAKE_CASE_ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) elif titles is None or texts is None: A: Union[str, Any] = titles if texts is None else texts return super().__call__( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) A: Union[str, Any] = titles if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [titles] A: Optional[Any] = texts if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [texts] A: str = len(SCREAMING_SNAKE_CASE_ ) A: List[Any] = questions if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else [questions] * n_passages assert len(SCREAMING_SNAKE_CASE_ ) == len( SCREAMING_SNAKE_CASE_ ), f"""There should be as many titles than texts but got {len(SCREAMING_SNAKE_CASE_ )} titles and {len(SCREAMING_SNAKE_CASE_ )} texts.""" A: Union[str, Any] = super().__call__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: Dict = super().__call__(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ )['''input_ids'''] A: str = { '''input_ids''': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ] } if return_attention_mask is not False: A: Union[str, Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) A: Optional[Any] = attention_mask return self.pad(SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , return_tensors=SCREAMING_SNAKE_CASE_ ) def _snake_case ( self : int , SCREAMING_SNAKE_CASE_ : BatchEncoding , SCREAMING_SNAKE_CASE_ : DPRReaderOutput , SCREAMING_SNAKE_CASE_ : int = 16 , SCREAMING_SNAKE_CASE_ : int = 64 , SCREAMING_SNAKE_CASE_ : int = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Any = reader_input['''input_ids'''] A , A , A: str = reader_output[:3] A: str = len(SCREAMING_SNAKE_CASE_ ) A: Union[str, Any] = sorted(range(SCREAMING_SNAKE_CASE_ ) , reverse=SCREAMING_SNAKE_CASE_ , key=relevance_logits.__getitem__ ) A: List[DPRReaderOutput] = [] for doc_id in sorted_docs: A: List[str] = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence A: Dict = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: A: Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: A: int = len(SCREAMING_SNAKE_CASE_ ) A: Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=SCREAMING_SNAKE_CASE_ , top_spans=SCREAMING_SNAKE_CASE_ , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=SCREAMING_SNAKE_CASE_ , start_index=SCREAMING_SNAKE_CASE_ , end_index=SCREAMING_SNAKE_CASE_ , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(SCREAMING_SNAKE_CASE_ ) >= num_spans: break return nbest_spans_predictions[:num_spans] def _snake_case ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : List[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , ) -> List[DPRSpanPrediction]: '''simple docstring''' A: Union[str, Any] = [] for start_index, start_score in enumerate(SCREAMING_SNAKE_CASE_ ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) A: Any = sorted(SCREAMING_SNAKE_CASE_ , key=lambda SCREAMING_SNAKE_CASE_ : x[1] , reverse=SCREAMING_SNAKE_CASE_ ) A: Dict = [] for (start_index, end_index), score in scores: assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]""" A: int = end_index - start_index + 1 assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(SCREAMING_SNAKE_CASE_ ) == top_spans: break return chosen_span_intervals @add_end_docstrings(UpperCAmelCase_ ) class lowerCAmelCase_ ( UpperCAmelCase_ , UpperCAmelCase_ ): '''simple docstring''' UpperCamelCase_ : Tuple = VOCAB_FILES_NAMES UpperCamelCase_ : List[Any] = READER_PRETRAINED_VOCAB_FILES_MAP UpperCamelCase_ : Union[str, Any] = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase_ : Dict = READER_PRETRAINED_INIT_CONFIGURATION UpperCamelCase_ : Any = ["""input_ids""", """attention_mask"""] UpperCamelCase_ : Optional[Any] = DPRReaderTokenizer
334
0